blob: cfe5d1b16ab447709c2ad0682b404a02da8921a7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000017#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000019#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000025#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// C99 6.7: Declarations.
30//===----------------------------------------------------------------------===//
31
32/// ParseTypeName
33/// type-name: [C99 6.7.6]
34/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000035///
36/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000037TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000038 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000039 AccessSpecifier AS,
40 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000041 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000042 if (DSC == DSC_normal)
43 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000044
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000046 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000047 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000048 if (OwnedType)
49 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000050
Reid Spencer5f016e22007-07-11 17:01:13 +000051 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000052 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000054 if (Range)
55 *Range = DeclaratorInfo.getSourceRange();
56
Chris Lattnereaaebc72009-04-25 08:06:05 +000057 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000058 return true;
59
Douglas Gregor23c94db2010-07-02 17:43:08 +000060 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000061}
62
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000063
64/// isAttributeLateParsed - Return true if the attribute has arguments that
65/// require late parsing.
66static bool isAttributeLateParsed(const IdentifierInfo &II) {
67 return llvm::StringSwitch<bool>(II.getName())
68#include "clang/Parse/AttrLateParsed.inc"
69 .Default(false);
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
Bill Wendlingad017fa2012-12-20 19:22:21 +0000145 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000146 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000147 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148 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 {
Michael Han6880f492012-10-03 01:56:22 +0000158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000159 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000162 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000163 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 }
166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000168 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000169 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
170 SkipUntil(tok::r_paren, false);
171 }
John McCall7f040a92010-12-24 02:08:15 +0000172 if (endLoc)
173 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000177
Michael Han6880f492012-10-03 01:56:22 +0000178/// Parse the arguments to a parameterized GNU attribute or
179/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000180void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
181 SourceLocation AttrNameLoc,
182 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000183 SourceLocation *EndLoc,
184 IdentifierInfo *ScopeName,
185 SourceLocation ScopeLoc,
186 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000187
188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
189
190 // Availability attributes have their own grammar.
191 if (AttrName->isStr("availability")) {
192 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
195 // Thread safety attributes fit into the FIXME case above, so we
196 // just parse the arguments as a list of expressions
197 if (IsThreadSafetyAttribute(AttrName->getName())) {
198 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
199 return;
200 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000201 // Type safety attributes have their own grammar.
202 if (AttrName->isStr("type_tag_for_datatype")) {
203 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000206
207 ConsumeParen(); // ignore the left paren loc for now
208
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000209 IdentifierInfo *ParmName = 0;
210 SourceLocation ParmLoc;
211 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000212
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000213 switch (Tok.getKind()) {
214 case tok::kw_char:
215 case tok::kw_wchar_t:
216 case tok::kw_char16_t:
217 case tok::kw_char32_t:
218 case tok::kw_bool:
219 case tok::kw_short:
220 case tok::kw_int:
221 case tok::kw_long:
222 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000223 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000224 case tok::kw_signed:
225 case tok::kw_unsigned:
226 case tok::kw_float:
227 case tok::kw_double:
228 case tok::kw_void:
229 case tok::kw_typeof:
230 // __attribute__(( vec_type_hint(char) ))
231 // FIXME: Don't just discard the builtin type token.
232 ConsumeToken();
233 BuiltinType = true;
234 break;
235
236 case tok::identifier:
237 ParmName = Tok.getIdentifierInfo();
238 ParmLoc = ConsumeToken();
239 break;
240
241 default:
242 break;
243 }
244
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000245 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000246
247 if (!BuiltinType &&
248 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
249 // Eat the comma.
250 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000252
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000253 // Parse the non-empty comma-separated list of expressions.
254 while (1) {
255 ExprResult ArgExpr(ParseAssignmentExpression());
256 if (ArgExpr.isInvalid()) {
257 SkipUntil(tok::r_paren);
258 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000259 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000260 ArgExprs.push_back(ArgExpr.release());
261 if (Tok.isNot(tok::comma))
262 break;
263 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000266 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
267 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
268 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000269 while (Tok.is(tok::identifier)) {
270 ConsumeToken();
271 if (Tok.is(tok::greater))
272 break;
273 if (Tok.is(tok::comma)) {
274 ConsumeToken();
275 continue;
276 }
277 }
278 if (Tok.isNot(tok::greater))
279 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000280 SkipUntil(tok::r_paren, false, true); // skip until ')'
281 }
282 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000283
284 SourceLocation RParen = Tok.getLocation();
285 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000286 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000287 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000288 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000289 ScopeName, ScopeLoc, ParmName, ParmLoc,
290 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000291 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000292 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000293 }
294}
295
Chad Rosier8decdee2012-06-26 22:30:43 +0000296/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000297/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000298void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000299 SourceLocation AttrNameLoc,
300 ParsedAttributes &Attrs)
301{
302 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000303 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000304 AttrName->getNameStart(), tok::r_paren))
305 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000306
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000307 ExprResult ArgExpr(ParseConstantExpression());
308 if (ArgExpr.isInvalid()) {
309 T.skipToEnd();
310 return;
311 }
312 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000313 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000314 &ExprList, 1, AttributeList::AS_Declspec);
315
316 T.consumeClose();
317}
318
Chad Rosier8decdee2012-06-26 22:30:43 +0000319/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000320/// arguments.
321bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
322 return llvm::StringSwitch<bool>(Ident->getName())
323 .Case("dllimport", true)
324 .Case("dllexport", true)
325 .Case("noreturn", true)
326 .Case("nothrow", true)
327 .Case("noinline", true)
328 .Case("naked", true)
329 .Case("appdomain", true)
330 .Case("process", true)
331 .Case("jitintrinsic", true)
332 .Case("noalias", true)
333 .Case("restrict", true)
334 .Case("novtable", true)
335 .Case("selectany", true)
336 .Case("thread", true)
337 .Default(false);
338}
339
Chad Rosier8decdee2012-06-26 22:30:43 +0000340/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000341/// parameters). Will return false if we properly handled the declspec, or
342/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000343void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000344 SourceLocation Loc,
345 ParsedAttributes &Attrs) {
346 // Try to handle the easy case first -- these declspecs all take a single
347 // parameter as their argument.
348 if (llvm::StringSwitch<bool>(Ident->getName())
349 .Case("uuid", true)
350 .Case("align", true)
351 .Case("allocate", true)
352 .Default(false)) {
353 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
354 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000355 // The deprecated declspec has an optional single argument, so we will
356 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000357 // not.
358 if (Tok.getKind() == tok::l_paren)
359 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
360 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000361 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000362 AttributeList::AS_Declspec);
363 } else if (Ident->getName() == "property") {
364 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000365 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000366 // must be named get or put.
367 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000368 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000369 // property expression.
370 //
371 // FIXME: we should deal with __declspec(property) at some point because it
372 // is used in the platform SDK headers for the Parallel Patterns Library
373 // and ATL.
374 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000376 Ident->getNameStart(), tok::r_paren))
377 return;
378 T.skipToEnd();
379 } else {
380 // We don't recognize this as a valid declspec, but instead of creating the
381 // attribute and allowing sema to warn about it, we will warn here instead.
382 // This is because some attributes have multiple spellings, but we need to
383 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000384 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000385 // both locations.
386 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
387
388 // If there's an open paren, we should eat the open and close parens under
389 // the assumption that this unknown declspec has parameters.
390 BalancedDelimiterTracker T(*this, tok::l_paren);
391 if (!T.consumeOpen())
392 T.skipToEnd();
393 }
394}
395
Eli Friedmana23b4852009-06-08 07:21:15 +0000396/// [MS] decl-specifier:
397/// __declspec ( extended-decl-modifier-seq )
398///
399/// [MS] extended-decl-modifier-seq:
400/// extended-decl-modifier[opt]
401/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000402void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000403 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000404
Steve Narofff59e17e2008-12-24 20:59:21 +0000405 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000407 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000408 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000409 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000410
Chad Rosier8decdee2012-06-26 22:30:43 +0000411 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412 // you can specify multiple attributes per declspec.
413 while (Tok.getKind() != tok::r_paren) {
414 // We expect either a well-known identifier or a generic string. Anything
415 // else is a malformed declspec.
416 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000417 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000418 Tok.getKind() != tok::kw_restrict) {
419 Diag(Tok, diag::err_ms_declspec_type);
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000422 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000423
424 IdentifierInfo *AttrName;
425 SourceLocation AttrNameLoc;
426 if (IsString) {
427 SmallString<8> StrBuffer;
428 bool Invalid = false;
429 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
430 if (Invalid) {
431 T.skipToEnd();
432 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000433 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000434 AttrName = PP.getIdentifierInfo(Str);
435 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000436 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000437 AttrName = Tok.getIdentifierInfo();
438 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000439 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000440
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000441 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000442 // If we have a generic string, we will allow it because there is no
443 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000444 // (for instance, SAL declspecs in older versions of MSVC).
445 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000446 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000447 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000448 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000449 0, 0, AttributeList::AS_Declspec);
450 else
451 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000452 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000453 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000454}
455
John McCall7f040a92010-12-24 02:08:15 +0000456void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000457 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000458 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000459 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000460 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000461 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000462 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);
Eli Friedman290eeb02009-06-08 23:27:34 +0000466 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000467}
468
John McCall7f040a92010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000470 // Treat these like attributes
471 while (Tok.is(tok::kw___pascal)) {
472 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
473 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000475 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000476 }
John McCall7f040a92010-12-24 02:08:15 +0000477}
478
Peter Collingbournef315fa82011-02-14 01:42:53 +0000479void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
480 // Treat these like attributes
481 while (Tok.is(tok::kw___kernel)) {
482 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000483 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
484 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000485 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
490 SourceLocation Loc = Tok.getLocation();
491 switch(Tok.getKind()) {
492 // OpenCL qualifiers:
493 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000494 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000496 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000498 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000499
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000502 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000505
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000511
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000517
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000520 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000523
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000529
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000532 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 break;
535 default: break;
536 }
537}
538
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539/// \brief Parse a version number.
540///
541/// version:
542/// simple-integer
543/// simple-integer ',' simple-integer
544/// simple-integer ',' simple-integer ',' simple-integer
545VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
546 Range = Tok.getLocation();
547
548 if (!Tok.is(tok::numeric_constant)) {
549 Diag(Tok, diag::err_expected_version);
550 SkipUntil(tok::comma, tok::r_paren, true, true, true);
551 return VersionTuple();
552 }
553
554 // Parse the major (and possibly minor and subminor) versions, which
555 // are stored in the numeric constant. We utilize a quirk of the
556 // lexer, which is that it handles something like 1.2.3 as a single
557 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000558 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559 Buffer.resize(Tok.getLength()+1);
560 const char *ThisTokBegin = &Buffer[0];
561
562 // Get the spelling of the token, which eliminates trigraphs, etc.
563 bool Invalid = false;
564 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
565 if (Invalid)
566 return VersionTuple();
567
568 // Parse the major version.
569 unsigned AfterMajor = 0;
570 unsigned Major = 0;
571 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
572 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
573 ++AfterMajor;
574 }
575
576 if (AfterMajor == 0) {
577 Diag(Tok, diag::err_expected_version);
578 SkipUntil(tok::comma, tok::r_paren, true, true, true);
579 return VersionTuple();
580 }
581
582 if (AfterMajor == ActualLength) {
583 ConsumeToken();
584
585 // We only had a single version component.
586 if (Major == 0) {
587 Diag(Tok, diag::err_zero_version);
588 return VersionTuple();
589 }
590
591 return VersionTuple(Major);
592 }
593
594 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
595 Diag(Tok, diag::err_expected_version);
596 SkipUntil(tok::comma, tok::r_paren, true, true, true);
597 return VersionTuple();
598 }
599
600 // Parse the minor version.
601 unsigned AfterMinor = AfterMajor + 1;
602 unsigned Minor = 0;
603 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
604 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
605 ++AfterMinor;
606 }
607
608 if (AfterMinor == ActualLength) {
609 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000610
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000611 // We had major.minor.
612 if (Major == 0 && Minor == 0) {
613 Diag(Tok, diag::err_zero_version);
614 return VersionTuple();
615 }
616
Chad Rosier8decdee2012-06-26 22:30:43 +0000617 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000618 }
619
620 // If what follows is not a '.', we have a problem.
621 if (ThisTokBegin[AfterMinor] != '.') {
622 Diag(Tok, diag::err_expected_version);
623 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000624 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000625 }
626
627 // Parse the subminor version.
628 unsigned AfterSubminor = AfterMinor + 1;
629 unsigned Subminor = 0;
630 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
631 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
632 ++AfterSubminor;
633 }
634
635 if (AfterSubminor != ActualLength) {
636 Diag(Tok, diag::err_expected_version);
637 SkipUntil(tok::comma, tok::r_paren, true, true, true);
638 return VersionTuple();
639 }
640 ConsumeToken();
641 return VersionTuple(Major, Minor, Subminor);
642}
643
644/// \brief Parse the contents of the "availability" attribute.
645///
646/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000647/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000648///
649/// platform:
650/// identifier
651///
652/// version-arg-list:
653/// version-arg
654/// version-arg ',' version-arg-list
655///
656/// version-arg:
657/// 'introduced' '=' version
658/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000659/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000660/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000661/// opt-message:
662/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000663void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
664 SourceLocation AvailabilityLoc,
665 ParsedAttributes &attrs,
666 SourceLocation *endLoc) {
667 SourceLocation PlatformLoc;
668 IdentifierInfo *Platform = 0;
669
670 enum { Introduced, Deprecated, Obsoleted, Unknown };
671 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000672 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000673
674 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 BalancedDelimiterTracker T(*this, tok::l_paren);
676 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 Diag(Tok, diag::err_expected_lparen);
678 return;
679 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000680
681 // Parse the platform name,
682 if (Tok.isNot(tok::identifier)) {
683 Diag(Tok, diag::err_availability_expected_platform);
684 SkipUntil(tok::r_paren);
685 return;
686 }
687 Platform = Tok.getIdentifierInfo();
688 PlatformLoc = ConsumeToken();
689
690 // Parse the ',' following the platform name.
691 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
692 return;
693
694 // If we haven't grabbed the pointers for the identifiers
695 // "introduced", "deprecated", and "obsoleted", do so now.
696 if (!Ident_introduced) {
697 Ident_introduced = PP.getIdentifierInfo("introduced");
698 Ident_deprecated = PP.getIdentifierInfo("deprecated");
699 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000700 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000701 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702 }
703
704 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706 do {
707 if (Tok.isNot(tok::identifier)) {
708 Diag(Tok, diag::err_availability_expected_change);
709 SkipUntil(tok::r_paren);
710 return;
711 }
712 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
713 SourceLocation KeywordLoc = ConsumeToken();
714
Douglas Gregorb53e4172011-03-26 03:35:55 +0000715 if (Keyword == Ident_unavailable) {
716 if (UnavailableLoc.isValid()) {
717 Diag(KeywordLoc, diag::err_availability_redundant)
718 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000719 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000720 UnavailableLoc = KeywordLoc;
721
722 if (Tok.isNot(tok::comma))
723 break;
724
725 ConsumeToken();
726 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000727 }
728
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000729 if (Tok.isNot(tok::equal)) {
730 Diag(Tok, diag::err_expected_equal_after)
731 << Keyword;
732 SkipUntil(tok::r_paren);
733 return;
734 }
735 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000736 if (Keyword == Ident_message) {
737 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000738 Diag(Tok, diag::err_expected_string_literal)
739 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000740 SkipUntil(tok::r_paren);
741 return;
742 }
743 MessageExpr = ParseStringLiteralExpression();
744 break;
745 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000746
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000747 SourceRange VersionRange;
748 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000749
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 if (Version.empty()) {
751 SkipUntil(tok::r_paren);
752 return;
753 }
754
755 unsigned Index;
756 if (Keyword == Ident_introduced)
757 Index = Introduced;
758 else if (Keyword == Ident_deprecated)
759 Index = Deprecated;
760 else if (Keyword == Ident_obsoleted)
761 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000762 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000763 Index = Unknown;
764
765 if (Index < Unknown) {
766 if (!Changes[Index].KeywordLoc.isInvalid()) {
767 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000768 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000769 << SourceRange(Changes[Index].KeywordLoc,
770 Changes[Index].VersionRange.getEnd());
771 }
772
773 Changes[Index].KeywordLoc = KeywordLoc;
774 Changes[Index].Version = Version;
775 Changes[Index].VersionRange = VersionRange;
776 } else {
777 Diag(KeywordLoc, diag::err_availability_unknown_change)
778 << Keyword << VersionRange;
779 }
780
781 if (Tok.isNot(tok::comma))
782 break;
783
784 ConsumeToken();
785 } while (true);
786
787 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000789 return;
790
791 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000792 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000793
Douglas Gregorb53e4172011-03-26 03:35:55 +0000794 // The 'unavailable' availability cannot be combined with any other
795 // availability changes. Make sure that hasn't happened.
796 if (UnavailableLoc.isValid()) {
797 bool Complained = false;
798 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
799 if (Changes[Index].KeywordLoc.isValid()) {
800 if (!Complained) {
801 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
802 << SourceRange(Changes[Index].KeywordLoc,
803 Changes[Index].VersionRange.getEnd());
804 Complained = true;
805 }
806
807 // Clear out the availability.
808 Changes[Index] = AvailabilityChange();
809 }
810 }
811 }
812
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000813 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000814 attrs.addNew(&Availability,
815 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000816 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000817 Platform, PlatformLoc,
818 Changes[Introduced],
819 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000820 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000821 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000822 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000823}
824
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000825
Bill Wendlingad017fa2012-12-20 19:22:21 +0000826// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
828
829void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
830
831void Parser::LateParsedClass::ParseLexedAttributes() {
832 Self->ParseLexedAttributes(*Class);
833}
834
835void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000836 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837}
838
839/// Wrapper class which calls ParseLexedAttribute, after setting up the
840/// scope appropriately.
841void Parser::ParseLexedAttributes(ParsingClass &Class) {
842 // Deal with templates
843 // FIXME: Test cases to make sure this does the right thing for templates.
844 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
845 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
846 HasTemplateScope);
847 if (HasTemplateScope)
848 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
849
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000850 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000851 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000852 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000853 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
854 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
855
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000856 // Enter the scope of nested classes
857 if (!AlreadyHasClassScope)
858 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
859 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000860 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000861 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
862 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
863 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000864 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000865
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000866 if (!AlreadyHasClassScope)
867 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
868 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000869}
870
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000871
872/// \brief Parse all attributes in LAs, and attach them to Decl D.
873void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
874 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000875 assert(LAs.parseSoon() &&
876 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000878 if (D)
879 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000880 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000881 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882 }
883 LAs.clear();
884}
885
886
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887/// \brief Finish parsing an attribute for which parsing was delayed.
888/// This will be called at the end of parsing a class declaration
889/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000890/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000891/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000892void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
893 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894 // Save the current token position.
895 SourceLocation OrigLoc = Tok.getLocation();
896
897 // Append the current token at the end of the new token stream so that it
898 // doesn't get lost.
899 LA.Toks.push_back(Tok);
900 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
901 // Consume the previously pushed token.
902 ConsumeAnyToken();
903
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000904 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000905 // FIXME: Do not warn on C++11 attributes, once we start supporting
906 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000907 Diag(Tok, diag::warn_attribute_on_function_definition)
908 << LA.AttrName.getName();
909 }
910
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000911 ParsedAttributes Attrs(AttrFactory);
912 SourceLocation endLoc;
913
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000914 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000915 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000916 NamedDecl *ND = dyn_cast<NamedDecl>(D);
917 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000918
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000919 // Allow 'this' within late-parsed attributes.
920 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
921 /*TypeQuals=*/0,
922 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000923
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000924 if (LA.Decls.size() == 1) {
925 // If the Decl is templatized, add template parameters to scope.
926 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
927 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
928 if (HasTemplateScope)
929 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000930
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000931 // If the Decl is on a function, add function parameters to the scope.
932 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
933 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
934 if (HasFunScope)
935 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000936
Michael Han6880f492012-10-03 01:56:22 +0000937 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000938 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000939
940 if (HasFunScope) {
941 Actions.ActOnExitFunctionContext();
942 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
943 }
944 if (HasTemplateScope) {
945 TempScope.Exit();
946 }
947 } else {
948 // If there are multiple decls, then the decl cannot be within the
949 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000950 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000951 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000952 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000953 } else {
954 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000955 }
956
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000957 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
958 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
959 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000960
961 if (Tok.getLocation() != OrigLoc) {
962 // Due to a parsing error, we either went over the cached tokens or
963 // there are still cached tokens left, so we skip the leftover tokens.
964 // Since this is an uncommon situation that should be avoided, use the
965 // expensive isBeforeInTranslationUnit call.
966 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
967 OrigLoc))
968 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000969 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000970 }
971}
972
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000973/// \brief Wrapper around a case statement checking if AttrName is
974/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000975bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976 return llvm::StringSwitch<bool>(AttrName)
977 .Case("guarded_by", true)
978 .Case("guarded_var", true)
979 .Case("pt_guarded_by", true)
980 .Case("pt_guarded_var", true)
981 .Case("lockable", true)
982 .Case("scoped_lockable", true)
983 .Case("no_thread_safety_analysis", true)
984 .Case("acquired_after", true)
985 .Case("acquired_before", true)
986 .Case("exclusive_lock_function", true)
987 .Case("shared_lock_function", true)
988 .Case("exclusive_trylock_function", true)
989 .Case("shared_trylock_function", true)
990 .Case("unlock_function", true)
991 .Case("lock_returned", true)
992 .Case("locks_excluded", true)
993 .Case("exclusive_locks_required", true)
994 .Case("shared_locks_required", true)
995 .Default(false);
996}
997
998/// \brief Parse the contents of thread safety attributes. These
999/// should always be parsed as an expression list.
1000///
1001/// We need to special case the parsing due to the fact that if the first token
1002/// of the first argument is an identifier, the main parse loop will store
1003/// that token as a "parameter" and the rest of
1004/// the arguments will be added to a list of "arguments". However,
1005/// subsequent tokens in the first argument are lost. We instead parse each
1006/// argument as an expression and add all arguments to the list of "arguments".
1007/// In future, we will take advantage of this special case to also
1008/// deal with some argument scoping issues here (for example, referring to a
1009/// function parameter in the attribute on that function).
1010void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1011 SourceLocation AttrNameLoc,
1012 ParsedAttributes &Attrs,
1013 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001014 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001015
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001016 BalancedDelimiterTracker T(*this, tok::l_paren);
1017 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001018
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001019 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001021
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001022 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001023 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001024 ExprResult ArgExpr(ParseAssignmentExpression());
1025 if (ArgExpr.isInvalid()) {
1026 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001027 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 break;
1029 } else {
1030 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001031 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001032 if (Tok.isNot(tok::comma))
1033 break;
1034 ConsumeToken(); // Eat the comma, move to the next argument
1035 }
1036 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001037 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001038 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001039 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001040 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001041 if (EndLoc)
1042 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001043}
1044
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001045void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1046 SourceLocation AttrNameLoc,
1047 ParsedAttributes &Attrs,
1048 SourceLocation *EndLoc) {
1049 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1050
1051 BalancedDelimiterTracker T(*this, tok::l_paren);
1052 T.consumeOpen();
1053
1054 if (Tok.isNot(tok::identifier)) {
1055 Diag(Tok, diag::err_expected_ident);
1056 T.skipToEnd();
1057 return;
1058 }
1059 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1060 SourceLocation ArgumentKindLoc = ConsumeToken();
1061
1062 if (Tok.isNot(tok::comma)) {
1063 Diag(Tok, diag::err_expected_comma);
1064 T.skipToEnd();
1065 return;
1066 }
1067 ConsumeToken();
1068
1069 SourceRange MatchingCTypeRange;
1070 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1071 if (MatchingCType.isInvalid()) {
1072 T.skipToEnd();
1073 return;
1074 }
1075
1076 bool LayoutCompatible = false;
1077 bool MustBeNull = false;
1078 while (Tok.is(tok::comma)) {
1079 ConsumeToken();
1080 if (Tok.isNot(tok::identifier)) {
1081 Diag(Tok, diag::err_expected_ident);
1082 T.skipToEnd();
1083 return;
1084 }
1085 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1086 if (Flag->isStr("layout_compatible"))
1087 LayoutCompatible = true;
1088 else if (Flag->isStr("must_be_null"))
1089 MustBeNull = true;
1090 else {
1091 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1092 T.skipToEnd();
1093 return;
1094 }
1095 ConsumeToken(); // consume flag
1096 }
1097
1098 if (!T.consumeClose()) {
1099 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1100 ArgumentKind, ArgumentKindLoc,
1101 MatchingCType.release(), LayoutCompatible,
1102 MustBeNull, AttributeList::AS_GNU);
1103 }
1104
1105 if (EndLoc)
1106 *EndLoc = T.getCloseLocation();
1107}
1108
Richard Smith6ee326a2012-04-10 01:32:12 +00001109/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1110/// of a C++11 attribute-specifier in a location where an attribute is not
1111/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1112/// situation.
1113///
1114/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1115/// this doesn't appear to actually be an attribute-specifier, and the caller
1116/// should try to parse it.
1117bool Parser::DiagnoseProhibitedCXX11Attribute() {
1118 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1119
1120 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1121 case CAK_NotAttributeSpecifier:
1122 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1123 return false;
1124
1125 case CAK_InvalidAttributeSpecifier:
1126 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1127 return false;
1128
1129 case CAK_AttributeSpecifier:
1130 // Parse and discard the attributes.
1131 SourceLocation BeginLoc = ConsumeBracket();
1132 ConsumeBracket();
1133 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1134 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1135 SourceLocation EndLoc = ConsumeBracket();
1136 Diag(BeginLoc, diag::err_attributes_not_allowed)
1137 << SourceRange(BeginLoc, EndLoc);
1138 return true;
1139 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001140 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001141}
1142
John McCall7f040a92010-12-24 02:08:15 +00001143void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1144 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1145 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001146}
1147
Michael Hanf64231e2012-11-06 19:34:54 +00001148void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1149 AttributeList *AttrList = attrs.getList();
1150 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001151 if (AttrList->isCXX11Attribute()) {
Michael Hanf64231e2012-11-06 19:34:54 +00001152 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1153 << AttrList->getName();
1154 AttrList->setInvalid();
1155 }
1156 AttrList = AttrList->getNext();
1157 }
1158}
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160/// ParseDeclaration - Parse a full 'declaration', which consists of
1161/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001162/// 'Context' should be a Declarator::TheContext value. This returns the
1163/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001164///
1165/// declaration: [C99 6.7]
1166/// block-declaration ->
1167/// simple-declaration
1168/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001169/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001170/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001171/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001172/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001173/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001174/// others... [FIXME]
1175///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001176Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1177 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001178 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001179 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001180 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001181 // Must temporarily exit the objective-c container scope for
1182 // parsing c none objective-c decls.
1183 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001184
John McCalld226f652010-08-21 09:40:31 +00001185 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001186 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001187 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001188 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001189 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001190 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001191 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001192 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001193 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001194 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001195 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001196 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001197 SourceLocation InlineLoc = ConsumeToken();
1198 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1199 break;
1200 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001201 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001202 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001203 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001204 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001205 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001206 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001207 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001208 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001209 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001210 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001211 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001212 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001213 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001214 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001215 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001216 default:
John McCall7f040a92010-12-24 02:08:15 +00001217 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001218 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001219
Chris Lattner682bf922009-03-29 16:50:03 +00001220 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001221 // single decl, convert it now. Alias declarations can also declare a type;
1222 // include that too if it is present.
1223 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001224}
1225
1226/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1227/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001228/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1229/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001230///[C90/C++]init-declarator-list ';' [TODO]
1231/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001232///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001233/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001234/// attribute-specifier-seq[opt] type-specifier-seq declarator
1235///
Chris Lattnercd147752009-03-29 17:27:48 +00001236/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001237/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001238///
1239/// If FRI is non-null, we might be parsing a for-range-declaration instead
1240/// of a simple-declaration. If we find that we are, we also parse the
1241/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001242Parser::DeclGroupPtrTy
1243Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1244 SourceLocation &DeclEnd,
1245 ParsedAttributesWithRange &attrs,
1246 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001248 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001249 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001250
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001251 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001252 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001253
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1255 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001256 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001257 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001258 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001259 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001260 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001261 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001262 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001264
1265 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001266}
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Richard Smith0706df42011-10-19 21:33:05 +00001268/// Returns true if this might be the start of a declarator, or a common typo
1269/// for a declarator.
1270bool Parser::MightBeDeclarator(unsigned Context) {
1271 switch (Tok.getKind()) {
1272 case tok::annot_cxxscope:
1273 case tok::annot_template_id:
1274 case tok::caret:
1275 case tok::code_completion:
1276 case tok::coloncolon:
1277 case tok::ellipsis:
1278 case tok::kw___attribute:
1279 case tok::kw_operator:
1280 case tok::l_paren:
1281 case tok::star:
1282 return true;
1283
1284 case tok::amp:
1285 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001286 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001287
Richard Smith1c94c162012-01-09 22:31:44 +00001288 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001289 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001290 NextToken().is(tok::l_square);
1291
1292 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001293 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001294
Richard Smith0706df42011-10-19 21:33:05 +00001295 case tok::identifier:
1296 switch (NextToken().getKind()) {
1297 case tok::code_completion:
1298 case tok::coloncolon:
1299 case tok::comma:
1300 case tok::equal:
1301 case tok::equalequal: // Might be a typo for '='.
1302 case tok::kw_alignas:
1303 case tok::kw_asm:
1304 case tok::kw___attribute:
1305 case tok::l_brace:
1306 case tok::l_paren:
1307 case tok::l_square:
1308 case tok::less:
1309 case tok::r_brace:
1310 case tok::r_paren:
1311 case tok::r_square:
1312 case tok::semi:
1313 return true;
1314
1315 case tok::colon:
1316 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001317 // and in block scope it's probably a label. Inside a class definition,
1318 // this is a bit-field.
1319 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001320 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001321
1322 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001323 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001324
1325 default:
1326 return false;
1327 }
1328
1329 default:
1330 return false;
1331 }
1332}
1333
Richard Smith994d73f2012-04-11 20:59:20 +00001334/// Skip until we reach something which seems like a sensible place to pick
1335/// up parsing after a malformed declaration. This will sometimes stop sooner
1336/// than SkipUntil(tok::r_brace) would, but will never stop later.
1337void Parser::SkipMalformedDecl() {
1338 while (true) {
1339 switch (Tok.getKind()) {
1340 case tok::l_brace:
1341 // Skip until matching }, then stop. We've probably skipped over
1342 // a malformed class or function definition or similar.
1343 ConsumeBrace();
1344 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1345 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1346 // This declaration isn't over yet. Keep skipping.
1347 continue;
1348 }
1349 if (Tok.is(tok::semi))
1350 ConsumeToken();
1351 return;
1352
1353 case tok::l_square:
1354 ConsumeBracket();
1355 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1356 continue;
1357
1358 case tok::l_paren:
1359 ConsumeParen();
1360 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1361 continue;
1362
1363 case tok::r_brace:
1364 return;
1365
1366 case tok::semi:
1367 ConsumeToken();
1368 return;
1369
1370 case tok::kw_inline:
1371 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001372 // a good place to pick back up parsing, except in an Objective-C
1373 // @interface context.
1374 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1375 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001376 return;
1377 break;
1378
1379 case tok::kw_namespace:
1380 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001381 // place to pick back up parsing, except in an Objective-C
1382 // @interface context.
1383 if (Tok.isAtStartOfLine() &&
1384 (!ParsingInObjCContainer || CurParsedObjCImpl))
1385 return;
1386 break;
1387
1388 case tok::at:
1389 // @end is very much like } in Objective-C contexts.
1390 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1391 ParsingInObjCContainer)
1392 return;
1393 break;
1394
1395 case tok::minus:
1396 case tok::plus:
1397 // - and + probably start new method declarations in Objective-C contexts.
1398 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001399 return;
1400 break;
1401
1402 case tok::eof:
1403 return;
1404
1405 default:
1406 break;
1407 }
1408
1409 ConsumeAnyToken();
1410 }
1411}
1412
John McCalld8ac0572009-11-03 19:26:08 +00001413/// ParseDeclGroup - Having concluded that this is either a function
1414/// definition or a group of object declarations, actually parse the
1415/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001416Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1417 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001418 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001419 SourceLocation *DeclEnd,
1420 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001421 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001422 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001423 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001424
John McCalld8ac0572009-11-03 19:26:08 +00001425 // Bail out if the first declarator didn't seem well-formed.
1426 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001427 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001428 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001431 // Save late-parsed attributes for now; they need to be parsed in the
1432 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001433 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1434 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001435 if (D.isFunctionDeclarator())
1436 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1437
Chris Lattnerc82daef2010-07-11 22:24:20 +00001438 // Check to see if we have a function *definition* which must have a body.
1439 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1440 // Look at the next token to make sure that this isn't a function
1441 // declaration. We have to check this because __attribute__ might be the
1442 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001443 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001444
Chris Lattner004659a2010-07-11 22:42:07 +00001445 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001446 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1447 Diag(Tok, diag::err_function_declared_typedef);
1448
1449 // Recover by treating the 'typedef' as spurious.
1450 DS.ClearStorageClassSpecs();
1451 }
1452
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001453 Decl *TheDecl =
1454 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001455 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001456 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001457
Chris Lattner004659a2010-07-11 22:42:07 +00001458 if (isDeclarationSpecifier()) {
1459 // If there is an invalid declaration specifier right after the function
1460 // prototype, then we must be in a missing semicolon case where this isn't
1461 // actually a body. Just fall through into the code that handles it as a
1462 // prototype, and let the top-level code handle the erroneous declspec
1463 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001464 } else {
1465 Diag(Tok, diag::err_expected_fn_body);
1466 SkipUntil(tok::semi);
1467 return DeclGroupPtrTy();
1468 }
1469 }
1470
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001471 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001472 return DeclGroupPtrTy();
1473
1474 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1475 // must parse and analyze the for-range-initializer before the declaration is
1476 // analyzed.
1477 if (FRI && Tok.is(tok::colon)) {
1478 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001479 if (Tok.is(tok::l_brace))
1480 FRI->RangeExpr = ParseBraceInitializer();
1481 else
1482 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001483 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1484 Actions.ActOnCXXForRangeDecl(ThisDecl);
1485 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001486 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001487 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1488 }
1489
Chris Lattner5f9e2722011-07-23 10:55:15 +00001490 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001491 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001492 if (LateParsedAttrs.size() > 0)
1493 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001494 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001495 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001496 DeclsInGroup.push_back(FirstDecl);
1497
Richard Smith0706df42011-10-19 21:33:05 +00001498 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001499
John McCalld8ac0572009-11-03 19:26:08 +00001500 // If we don't have a comma, it is either the end of the list (a ';') or an
1501 // error, bail out.
1502 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001503 SourceLocation CommaLoc = ConsumeToken();
1504
1505 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1506 // This comma was followed by a line-break and something which can't be
1507 // the start of a declarator. The comma was probably a typo for a
1508 // semicolon.
1509 Diag(CommaLoc, diag::err_expected_semi_declaration)
1510 << FixItHint::CreateReplacement(CommaLoc, ";");
1511 ExpectSemi = false;
1512 break;
1513 }
John McCalld8ac0572009-11-03 19:26:08 +00001514
1515 // Parse the next declarator.
1516 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001517 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001518
1519 // Accept attributes in an init-declarator. In the first declarator in a
1520 // declaration, these would be part of the declspec. In subsequent
1521 // declarators, they become part of the declarator itself, so that they
1522 // don't apply to declarators after *this* one. Examples:
1523 // short __attribute__((common)) var; -> declspec
1524 // short var __attribute__((common)); -> declarator
1525 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001526 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001527
1528 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001529 if (!D.isInvalidType()) {
1530 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1531 D.complete(ThisDecl);
1532 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001533 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001534 }
John McCalld8ac0572009-11-03 19:26:08 +00001535 }
1536
1537 if (DeclEnd)
1538 *DeclEnd = Tok.getLocation();
1539
Richard Smith0706df42011-10-19 21:33:05 +00001540 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001541 ExpectAndConsumeSemi(Context == Declarator::FileContext
1542 ? diag::err_invalid_token_after_toplevel_declarator
1543 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001544 // Okay, there was no semicolon and one was expected. If we see a
1545 // declaration specifier, just assume it was missing and continue parsing.
1546 // Otherwise things are very confused and we skip to recover.
1547 if (!isDeclarationSpecifier()) {
1548 SkipUntil(tok::r_brace, true, true);
1549 if (Tok.is(tok::semi))
1550 ConsumeToken();
1551 }
John McCalld8ac0572009-11-03 19:26:08 +00001552 }
1553
Douglas Gregor23c94db2010-07-02 17:43:08 +00001554 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001555 DeclsInGroup.data(),
1556 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001557}
1558
Richard Smithad762fc2011-04-14 22:09:26 +00001559/// Parse an optional simple-asm-expr and attributes, and attach them to a
1560/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001561bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001562 // If a simple-asm-expr is present, parse it.
1563 if (Tok.is(tok::kw_asm)) {
1564 SourceLocation Loc;
1565 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1566 if (AsmLabel.isInvalid()) {
1567 SkipUntil(tok::semi, true, true);
1568 return true;
1569 }
1570
1571 D.setAsmLabel(AsmLabel.release());
1572 D.SetRangeEnd(Loc);
1573 }
1574
1575 MaybeParseGNUAttributes(D);
1576 return false;
1577}
1578
Douglas Gregor1426e532009-05-12 21:31:51 +00001579/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1580/// declarator'. This method parses the remainder of the declaration
1581/// (including any attributes or initializer, among other things) and
1582/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001583///
Reid Spencer5f016e22007-07-11 17:01:13 +00001584/// init-declarator: [C99 6.7]
1585/// declarator
1586/// declarator '=' initializer
1587/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1588/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001589/// [C++] declarator initializer[opt]
1590///
1591/// [C++] initializer:
1592/// [C++] '=' initializer-clause
1593/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001594/// [C++0x] '=' 'default' [TODO]
1595/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001596/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001597///
1598/// According to the standard grammar, =default and =delete are function
1599/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001600///
John McCalld226f652010-08-21 09:40:31 +00001601Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001602 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001603 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001604 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Richard Smithad762fc2011-04-14 22:09:26 +00001606 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1607}
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Richard Smithad762fc2011-04-14 22:09:26 +00001609Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1610 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001611 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001612 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001613 switch (TemplateInfo.Kind) {
1614 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001615 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001616 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001617
Douglas Gregord5a423b2009-09-25 18:43:00 +00001618 case ParsedTemplateInfo::Template:
1619 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001620 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001621 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001622 D);
1623 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001624
Douglas Gregord5a423b2009-09-25 18:43:00 +00001625 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001626 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001627 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001628 TemplateInfo.ExternLoc,
1629 TemplateInfo.TemplateLoc,
1630 D);
1631 if (ThisRes.isInvalid()) {
1632 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001633 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001634 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001635
Douglas Gregord5a423b2009-09-25 18:43:00 +00001636 ThisDecl = ThisRes.get();
1637 break;
1638 }
1639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Richard Smith34b41d92011-02-20 03:19:35 +00001641 bool TypeContainsAuto =
1642 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1643
Douglas Gregor1426e532009-05-12 21:31:51 +00001644 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001645 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001646 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001647 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001648 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001649 if (D.isFunctionDeclarator())
1650 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1651 << 1 /* delete */;
1652 else
1653 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001654 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001655 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001656 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1657 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001658 else
1659 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001660 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001661 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001662 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001663 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001664 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001665
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001666 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001667 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001668 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001669 cutOffParsing();
1670 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001671 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001672
John McCall60d7b3a2010-08-24 06:29:42 +00001673 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001674
David Blaikie4e4d0842012-03-11 07:00:24 +00001675 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001676 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001677 ExitScope();
1678 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001679
Douglas Gregor1426e532009-05-12 21:31:51 +00001680 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001681 SkipUntil(tok::comma, true, true);
1682 Actions.ActOnInitializerError(ThisDecl);
1683 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001684 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1685 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001686 }
1687 } else if (Tok.is(tok::l_paren)) {
1688 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001689 BalancedDelimiterTracker T(*this, tok::l_paren);
1690 T.consumeOpen();
1691
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001692 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001693 CommaLocsTy CommaLocs;
1694
David Blaikie4e4d0842012-03-11 07:00:24 +00001695 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001696 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001697 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001698 }
1699
Douglas Gregor1426e532009-05-12 21:31:51 +00001700 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001701 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001702 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001703
David Blaikie4e4d0842012-03-11 07:00:24 +00001704 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001705 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001706 ExitScope();
1707 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001708 } else {
1709 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001710 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001711
1712 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1713 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001714
David Blaikie4e4d0842012-03-11 07:00:24 +00001715 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001716 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001717 ExitScope();
1718 }
1719
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001720 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1721 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001722 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001723 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1724 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001725 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001726 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001727 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001728 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001729 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1730
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001731 if (D.getCXXScopeSpec().isSet()) {
1732 EnterScope(0);
1733 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1734 }
1735
1736 ExprResult Init(ParseBraceInitializer());
1737
1738 if (D.getCXXScopeSpec().isSet()) {
1739 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1740 ExitScope();
1741 }
1742
1743 if (Init.isInvalid()) {
1744 Actions.ActOnInitializerError(ThisDecl);
1745 } else
1746 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1747 /*DirectInit=*/true, TypeContainsAuto);
1748
Douglas Gregor1426e532009-05-12 21:31:51 +00001749 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001750 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001751 }
1752
Richard Smith483b9f32011-02-21 20:05:19 +00001753 Actions.FinalizeDeclaration(ThisDecl);
1754
Douglas Gregor1426e532009-05-12 21:31:51 +00001755 return ThisDecl;
1756}
1757
Reid Spencer5f016e22007-07-11 17:01:13 +00001758/// ParseSpecifierQualifierList
1759/// specifier-qualifier-list:
1760/// type-specifier specifier-qualifier-list[opt]
1761/// type-qualifier specifier-qualifier-list[opt]
1762/// [GNU] attributes specifier-qualifier-list[opt]
1763///
Richard Smith69730c12012-03-12 07:56:15 +00001764void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1765 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1767 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001768 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001769 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 // Validate declspec for type-name.
1772 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001773 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1774 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001775 Diag(Tok, diag::err_expected_type);
1776 DS.SetTypeSpecError();
1777 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1778 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001780 if (!DS.hasTypeSpecifier())
1781 DS.SetTypeSpecError();
1782 }
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 // Issue diagnostic and remove storage class if present.
1785 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1786 if (DS.getStorageClassSpecLoc().isValid())
1787 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1788 else
1789 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1790 DS.ClearStorageClassSpecs();
1791 }
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 // Issue diagnostic and remove function specfier if present.
1794 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001795 if (DS.isInlineSpecified())
1796 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1797 if (DS.isVirtualSpecified())
1798 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1799 if (DS.isExplicitSpecified())
1800 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 DS.ClearFunctionSpecs();
1802 }
Richard Smith69730c12012-03-12 07:56:15 +00001803
1804 // Issue diagnostic and remove constexpr specfier if present.
1805 if (DS.isConstexprSpecified()) {
1806 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1807 DS.ClearConstexprSpec();
1808 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001809}
1810
Chris Lattnerc199ab32009-04-12 20:42:31 +00001811/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1812/// specified token is valid after the identifier in a declarator which
1813/// immediately follows the declspec. For example, these things are valid:
1814///
1815/// int x [ 4]; // direct-declarator
1816/// int x ( int y); // direct-declarator
1817/// int(int x ) // direct-declarator
1818/// int x ; // simple-declaration
1819/// int x = 17; // init-declarator-list
1820/// int x , y; // init-declarator-list
1821/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001822/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001823/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001824///
1825/// This is not, because 'x' does not immediately follow the declspec (though
1826/// ')' happens to be valid anyway).
1827/// int (x)
1828///
1829static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1830 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1831 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001832 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001833}
1834
Chris Lattnere40c2952009-04-14 21:34:55 +00001835
1836/// ParseImplicitInt - This method is called when we have an non-typename
1837/// identifier in a declspec (which normally terminates the decl spec) when
1838/// the declspec has no type specifier. In this case, the declspec is either
1839/// malformed or is "implicit int" (in K&R and C89).
1840///
1841/// This method handles diagnosing this prettily and returns false if the
1842/// declspec is done being processed. If it recovers and thinks there may be
1843/// other pieces of declspec after it, it returns true.
1844///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001845bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001846 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001847 AccessSpecifier AS, DeclSpecContext DSC,
1848 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001849 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Chris Lattnere40c2952009-04-14 21:34:55 +00001851 SourceLocation Loc = Tok.getLocation();
1852 // If we see an identifier that is not a type name, we normally would
1853 // parse it as the identifer being declared. However, when a typename
1854 // is typo'd or the definition is not included, this will incorrectly
1855 // parse the typename as the identifier name and fall over misparsing
1856 // later parts of the diagnostic.
1857 //
1858 // As such, we try to do some look-ahead in cases where this would
1859 // otherwise be an "implicit-int" case to see if this is invalid. For
1860 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1861 // an identifier with implicit int, we'd get a parse error because the
1862 // next token is obviously invalid for a type. Parse these as a case
1863 // with an invalid type specifier.
1864 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Chris Lattnere40c2952009-04-14 21:34:55 +00001866 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001867 // error, do lookahead to try to do better recovery. This never applies
1868 // within a type specifier. Outside of C++, we allow this even if the
1869 // language doesn't "officially" support implicit int -- we support
1870 // implicit int as an extension in C99 and C11. Allegedly, MS also
1871 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001872 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001873 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001874 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001875 // If this token is valid for implicit int, e.g. "static x = 4", then
1876 // we just avoid eating the identifier, so it will be parsed as the
1877 // identifier in the declarator.
1878 return false;
1879 }
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Richard Smith827adaf2012-05-15 21:01:51 +00001881 if (getLangOpts().CPlusPlus &&
1882 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1883 // Don't require a type specifier if we have the 'auto' storage class
1884 // specifier in C++98 -- we'll promote it to a type specifier.
1885 return false;
1886 }
1887
Chris Lattnere40c2952009-04-14 21:34:55 +00001888 // Otherwise, if we don't consume this token, we are going to emit an
1889 // error anyway. Try to recover from various common problems. Check
1890 // to see if this was a reference to a tag name without a tag specified.
1891 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001892 //
1893 // C++ doesn't need this, and isTagName doesn't take SS.
1894 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001895 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001896 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Douglas Gregor23c94db2010-07-02 17:43:08 +00001898 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001899 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001900 case DeclSpec::TST_enum:
1901 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1902 case DeclSpec::TST_union:
1903 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1904 case DeclSpec::TST_struct:
1905 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001906 case DeclSpec::TST_interface:
1907 TagName="__interface"; FixitTagName = "__interface ";
1908 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001909 case DeclSpec::TST_class:
1910 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Chris Lattnerf4382f52009-04-14 22:17:06 +00001913 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001914 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1915 LookupResult R(Actions, TokenName, SourceLocation(),
1916 Sema::LookupOrdinaryName);
1917
Chris Lattnerf4382f52009-04-14 22:17:06 +00001918 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001919 << TokenName << TagName << getLangOpts().CPlusPlus
1920 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1921
1922 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1923 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1924 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001925 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001926 << TokenName << TagName;
1927 }
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Chris Lattnerf4382f52009-04-14 22:17:06 +00001929 // Parse this as a tag as if the missing tag were present.
1930 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001931 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001932 else
Richard Smith69730c12012-03-12 07:56:15 +00001933 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001934 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001935 return true;
1936 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Richard Smith8f0a7e72012-05-15 21:29:55 +00001939 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001940 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001941 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1942 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001943 // Look ahead to the next token to try to figure out what this declaration
1944 // was supposed to be.
1945 switch (NextToken().getKind()) {
1946 case tok::comma:
1947 case tok::equal:
1948 case tok::kw_asm:
1949 case tok::l_brace:
1950 case tok::l_square:
1951 case tok::semi:
1952 // This looks like a variable declaration. The type is probably missing.
1953 // We're done parsing decl-specifiers.
1954 return false;
1955
1956 case tok::l_paren: {
1957 // static x(4); // 'x' is not a type
1958 // x(int n); // 'x' is not a type
1959 // x (*p)[]; // 'x' is a type
1960 //
1961 // Since we're in an error case (or the rare 'implicit int in C++' MS
1962 // extension), we can afford to perform a tentative parse to determine
1963 // which case we're in.
1964 TentativeParsingAction PA(*this);
1965 ConsumeToken();
1966 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1967 PA.Revert();
1968 if (TPR == TPResult::False())
1969 return false;
1970 // The identifier is followed by a parenthesized declarator.
1971 // It's supposed to be a type.
1972 break;
1973 }
1974
1975 default:
1976 // This is probably supposed to be a type. This includes cases like:
1977 // int f(itn);
1978 // struct S { unsinged : 4; };
1979 break;
1980 }
1981 }
1982
Chad Rosier8decdee2012-06-26 22:30:43 +00001983 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001984 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001985 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001986 IdentifierInfo *II = Tok.getIdentifierInfo();
1987 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001988 // The action emitted a diagnostic, so we don't have to.
1989 if (T) {
1990 // The action has suggested that the type T could be used. Set that as
1991 // the type in the declaration specifiers, consume the would-be type
1992 // name token, and we're done.
1993 const char *PrevSpec;
1994 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001995 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001996 DS.SetRangeEnd(Tok.getLocation());
1997 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001998 // There may be other declaration specifiers after this.
1999 return true;
2000 } else if (II != Tok.getIdentifierInfo()) {
2001 // If no type was suggested, the correction is to a keyword
2002 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002003 // There may be other declaration specifiers after this.
2004 return true;
2005 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002006
Douglas Gregora786fdb2009-10-13 23:27:22 +00002007 // Fall through; the action had no suggestion for us.
2008 } else {
2009 // The action did not emit a diagnostic, so emit one now.
2010 SourceRange R;
2011 if (SS) R = SS->getRange();
2012 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2013 }
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Douglas Gregora786fdb2009-10-13 23:27:22 +00002015 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002016 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002017 DS.SetRangeEnd(Tok.getLocation());
2018 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Chris Lattnere40c2952009-04-14 21:34:55 +00002020 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2021 // avoid rippling error messages on subsequent uses of the same type,
2022 // could be useful if #include was forgotten.
2023 return false;
2024}
2025
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002026/// \brief Determine the declaration specifier context from the declarator
2027/// context.
2028///
2029/// \param Context the declarator context, which is one of the
2030/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002031Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002032Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2033 if (Context == Declarator::MemberContext)
2034 return DSC_class;
2035 if (Context == Declarator::FileContext)
2036 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002037 if (Context == Declarator::TrailingReturnContext)
2038 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002039 return DSC_normal;
2040}
2041
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002042/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2043///
2044/// FIXME: Simply returns an alignof() expression if the argument is a
2045/// type. Ideally, the type should be propagated directly into Sema.
2046///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002047/// [C11] type-id
2048/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002049/// [C++0x] type-id ...[opt]
2050/// [C++0x] assignment-expression ...[opt]
2051ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2052 SourceLocation &EllipsisLoc) {
2053 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002054 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002055 SourceLocation TypeLoc = Tok.getLocation();
2056 ParsedType Ty = ParseTypeName().get();
2057 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002058 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2059 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002060 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002061 ER = ParseConstantExpression();
2062
Richard Smith80ad52f2013-01-02 11:42:31 +00002063 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002064 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002065
2066 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002067}
2068
2069/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2070/// attribute to Attrs.
2071///
2072/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002073/// [C11] '_Alignas' '(' type-id ')'
2074/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002075/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2076/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002077void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2078 SourceLocation *endLoc) {
2079 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2080 "Not an alignment-specifier!");
2081
2082 SourceLocation KWLoc = Tok.getLocation();
2083 ConsumeToken();
2084
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002085 BalancedDelimiterTracker T(*this, tok::l_paren);
2086 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002087 return;
2088
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002089 SourceLocation EllipsisLoc;
2090 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002091 if (ArgExpr.isInvalid()) {
2092 SkipUntil(tok::r_paren);
2093 return;
2094 }
2095
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002096 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002097 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002098 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002099
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002100 // FIXME: Handle pack-expansions here.
2101 if (EllipsisLoc.isValid()) {
2102 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2103 return;
2104 }
2105
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002106 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002107 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002108 // FIXME: This should not be GNU, but we since the attribute used is
2109 // based on the spelling, and there is no true spelling for
2110 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002111 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002112 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002113 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002114}
2115
Reid Spencer5f016e22007-07-11 17:01:13 +00002116/// ParseDeclarationSpecifiers
2117/// declaration-specifiers: [C99 6.7]
2118/// storage-class-specifier declaration-specifiers[opt]
2119/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002120/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002121/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002122/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002123/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002124///
2125/// storage-class-specifier: [C99 6.7.1]
2126/// 'typedef'
2127/// 'extern'
2128/// 'static'
2129/// 'auto'
2130/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002131/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002132/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002133/// function-specifier: [C99 6.7.4]
2134/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002135/// [C++] 'virtual'
2136/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002137/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002138/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002139/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002142void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002143 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002144 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002145 DeclSpecContext DSContext,
2146 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002147 if (DS.getSourceRange().isInvalid()) {
2148 DS.SetRangeStart(Tok.getLocation());
2149 DS.SetRangeEnd(Tok.getLocation());
2150 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002151
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002152 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002153 bool AttrsLastTime = false;
2154 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002156 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002158 unsigned DiagID = 0;
2159
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002161
Reid Spencer5f016e22007-07-11 17:01:13 +00002162 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002163 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002164 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002165 if (!AttrsLastTime)
2166 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002167 else {
2168 // Reject C++11 attributes that appertain to decl specifiers as
2169 // we don't support any C++11 attributes that appertain to decl
2170 // specifiers. This also conforms to what g++ 4.8 is doing.
2171 ProhibitCXX11Attributes(attrs);
2172
Sean Hunt2edf0a22012-06-23 05:07:58 +00002173 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002174 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002175
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 // If this is not a declaration specifier token, we're done reading decl
2177 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002178 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Sean Hunt2edf0a22012-06-23 05:07:58 +00002181 case tok::l_square:
2182 case tok::kw_alignas:
2183 if (!isCXX11AttributeSpecifier())
2184 goto DoneWithDeclSpec;
2185
2186 ProhibitAttributes(attrs);
2187 // FIXME: It would be good to recover by accepting the attributes,
2188 // but attempting to do that now would cause serious
2189 // madness in terms of diagnostics.
2190 attrs.clear();
2191 attrs.Range = SourceRange();
2192
2193 ParseCXX11Attributes(attrs);
2194 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002195 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002196
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002197 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002198 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002199 if (DS.hasTypeSpecifier()) {
2200 bool AllowNonIdentifiers
2201 = (getCurScope()->getFlags() & (Scope::ControlScope |
2202 Scope::BlockScope |
2203 Scope::TemplateParamScope |
2204 Scope::FunctionPrototypeScope |
2205 Scope::AtCatchScope)) == 0;
2206 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002207 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002208 (DSContext == DSC_class && DS.isFriendSpecified());
2209
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002210 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002211 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002212 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002213 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002214 }
2215
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002216 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2217 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2218 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002219 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002220 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002221 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002222 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002223 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002224 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002225
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002226 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002227 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002228 }
2229
Chris Lattner5e02c472009-01-05 00:07:25 +00002230 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002231 // C++ scope specifier. Annotate and loop, or bail out on error.
2232 if (TryAnnotateCXXScopeToken(true)) {
2233 if (!DS.hasTypeSpecifier())
2234 DS.SetTypeSpecError();
2235 goto DoneWithDeclSpec;
2236 }
John McCall2e0a7152010-03-01 18:20:46 +00002237 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2238 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002239 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002240
2241 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002242 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002243 goto DoneWithDeclSpec;
2244
John McCallaa87d332009-12-12 11:40:51 +00002245 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002246 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2247 Tok.getAnnotationRange(),
2248 SS);
John McCallaa87d332009-12-12 11:40:51 +00002249
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002250 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002251 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002252 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002253 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002254 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002255 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002256
2257 // C++ [class.qual]p2:
2258 // In a lookup in which the constructor is an acceptable lookup
2259 // result and the nested-name-specifier nominates a class C:
2260 //
2261 // - if the name specified after the
2262 // nested-name-specifier, when looked up in C, is the
2263 // injected-class-name of C (Clause 9), or
2264 //
2265 // - if the name specified after the nested-name-specifier
2266 // is the same as the identifier or the
2267 // simple-template-id's template-name in the last
2268 // component of the nested-name-specifier,
2269 //
2270 // the name is instead considered to name the constructor of
2271 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002272 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002273 // Thus, if the template-name is actually the constructor
2274 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002275 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002276 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002277 if ((DSContext == DSC_top_level ||
2278 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2279 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002280 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002281 if (isConstructorDeclarator()) {
2282 // The user meant this to be an out-of-line constructor
2283 // definition, but template arguments are not allowed
2284 // there. Just allow this as a constructor; we'll
2285 // complain about it later.
2286 goto DoneWithDeclSpec;
2287 }
2288
2289 // The user meant this to name a type, but it actually names
2290 // a constructor with some extraneous template
2291 // arguments. Complain, then parse it as a type as the user
2292 // intended.
2293 Diag(TemplateId->TemplateNameLoc,
2294 diag::err_out_of_line_template_id_names_constructor)
2295 << TemplateId->Name;
2296 }
2297
John McCallaa87d332009-12-12 11:40:51 +00002298 DS.getTypeSpecScope() = SS;
2299 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002300 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002301 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002302 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002303 continue;
2304 }
2305
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002306 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002307 DS.getTypeSpecScope() = SS;
2308 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002309 if (Tok.getAnnotationValue()) {
2310 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002311 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002312 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002313 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002314 if (isInvalid)
2315 break;
John McCallb3d87482010-08-24 05:47:05 +00002316 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002317 else
2318 DS.SetTypeSpecError();
2319 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2320 ConsumeToken(); // The typename
2321 }
2322
Douglas Gregor9135c722009-03-25 15:40:00 +00002323 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002324 goto DoneWithDeclSpec;
2325
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002326 // If we're in a context where the identifier could be a class name,
2327 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002328 if ((DSContext == DSC_top_level ||
2329 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002330 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002331 &SS)) {
2332 if (isConstructorDeclarator())
2333 goto DoneWithDeclSpec;
2334
2335 // As noted in C++ [class.qual]p2 (cited above), when the name
2336 // of the class is qualified in a context where it could name
2337 // a constructor, its a constructor name. However, we've
2338 // looked at the declarator, and the user probably meant this
2339 // to be a type. Complain that it isn't supposed to be treated
2340 // as a type, then proceed to parse it as a type.
2341 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2342 << Next.getIdentifierInfo();
2343 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002344
John McCallb3d87482010-08-24 05:47:05 +00002345 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2346 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002347 getCurScope(), &SS,
2348 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002349 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002350 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002351
Chris Lattnerf4382f52009-04-14 22:17:06 +00002352 // If the referenced identifier is not a type, then this declspec is
2353 // erroneous: We already checked about that it has no type specifier, and
2354 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002355 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002356 if (TypeRep == 0) {
2357 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002358 ParsedAttributesWithRange Attrs(AttrFactory);
2359 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2360 if (!Attrs.empty()) {
2361 AttrsLastTime = true;
2362 attrs.takeAllFrom(Attrs);
2363 }
2364 continue;
2365 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002366 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002367 }
Mike Stump1eb44332009-09-09 15:08:12 +00002368
John McCallaa87d332009-12-12 11:40:51 +00002369 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002370 ConsumeToken(); // The C++ scope.
2371
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002373 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002374 if (isInvalid)
2375 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002377 DS.SetRangeEnd(Tok.getLocation());
2378 ConsumeToken(); // The typename.
2379
2380 continue;
2381 }
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002384 if (Tok.getAnnotationValue()) {
2385 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002387 DiagID, T);
2388 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002389 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002390
Chris Lattner5c5db552010-04-05 18:18:31 +00002391 if (isInvalid)
2392 break;
2393
Chris Lattner80d0c892009-01-21 19:48:37 +00002394 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2395 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Chris Lattner80d0c892009-01-21 19:48:37 +00002397 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2398 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002399 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002400 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002401 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002402
Chris Lattner80d0c892009-01-21 19:48:37 +00002403 continue;
2404 }
Mike Stump1eb44332009-09-09 15:08:12 +00002405
Douglas Gregorbfad9152011-04-28 15:48:45 +00002406 case tok::kw___is_signed:
2407 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2408 // typically treats it as a trait. If we see __is_signed as it appears
2409 // in libstdc++, e.g.,
2410 //
2411 // static const bool __is_signed;
2412 //
2413 // then treat __is_signed as an identifier rather than as a keyword.
2414 if (DS.getTypeSpecType() == TST_bool &&
2415 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2416 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2417 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2418 Tok.setKind(tok::identifier);
2419 }
2420
2421 // We're done with the declaration-specifiers.
2422 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002423
Chris Lattner3bd934a2008-07-26 01:18:38 +00002424 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002425 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002426 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002427 // In C++, check to see if this is a scope specifier like foo::bar::, if
2428 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002429 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002430 if (TryAnnotateCXXScopeToken(true)) {
2431 if (!DS.hasTypeSpecifier())
2432 DS.SetTypeSpecError();
2433 goto DoneWithDeclSpec;
2434 }
2435 if (!Tok.is(tok::identifier))
2436 continue;
2437 }
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Chris Lattner3bd934a2008-07-26 01:18:38 +00002439 // This identifier can only be a typedef name if we haven't already seen
2440 // a type-specifier. Without this check we misparse:
2441 // typedef int X; struct Y { short X; }; as 'short int'.
2442 if (DS.hasTypeSpecifier())
2443 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002444
John Thompson82287d12010-02-05 00:12:22 +00002445 // Check for need to substitute AltiVec keyword tokens.
2446 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2447 break;
2448
Richard Smithf63eee72012-05-09 18:56:43 +00002449 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2450 // allow the use of a typedef name as a type specifier.
2451 if (DS.isTypeAltiVecVector())
2452 goto DoneWithDeclSpec;
2453
John McCallb3d87482010-08-24 05:47:05 +00002454 ParsedType TypeRep =
2455 Actions.getTypeName(*Tok.getIdentifierInfo(),
2456 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002457
Chris Lattnerc199ab32009-04-12 20:42:31 +00002458 // If this is not a typedef name, don't parse it as part of the declspec,
2459 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002460 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002461 ParsedAttributesWithRange Attrs(AttrFactory);
2462 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2463 if (!Attrs.empty()) {
2464 AttrsLastTime = true;
2465 attrs.takeAllFrom(Attrs);
2466 }
2467 continue;
2468 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002469 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002470 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002471
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002472 // If we're in a context where the identifier could be a class name,
2473 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002474 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002475 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002476 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002477 goto DoneWithDeclSpec;
2478
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002480 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002481 if (isInvalid)
2482 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Chris Lattner3bd934a2008-07-26 01:18:38 +00002484 DS.SetRangeEnd(Tok.getLocation());
2485 ConsumeToken(); // The identifier
2486
2487 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2488 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002489 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002490 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002491 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002492
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002493 // Need to support trailing type qualifiers (e.g. "id<p> const").
2494 // If a type specifier follows, it will be diagnosed elsewhere.
2495 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002496 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002497
2498 // type-name
2499 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002500 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002501 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002502 // This template-id does not refer to a type name, so we're
2503 // done with the type-specifiers.
2504 goto DoneWithDeclSpec;
2505 }
2506
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002507 // If we're in a context where the template-id could be a
2508 // constructor name or specialization, check whether this is a
2509 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002510 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002511 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002512 isConstructorDeclarator())
2513 goto DoneWithDeclSpec;
2514
Douglas Gregor39a8de12009-02-25 19:37:18 +00002515 // Turn the template-id annotation token into a type annotation
2516 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002517 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002518 continue;
2519 }
2520
Reid Spencer5f016e22007-07-11 17:01:13 +00002521 // GNU attributes support.
2522 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002523 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002525
2526 // Microsoft declspec support.
2527 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002528 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002529 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Steve Naroff239f0732008-12-25 14:16:32 +00002531 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002532 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002533 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002534 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002535 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002536 // FIXME: This does not work correctly if it is set to be a declspec
2537 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002538 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002539 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002540 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002541 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002542
2543 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002544 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002545 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002546 case tok::kw___cdecl:
2547 case tok::kw___stdcall:
2548 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002549 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002550 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002551 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002552 continue;
2553
Dawn Perchik52fc3142010-09-03 01:29:35 +00002554 // Borland single token adornments.
2555 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002556 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002557 continue;
2558
Peter Collingbournef315fa82011-02-14 01:42:53 +00002559 // OpenCL single token adornments.
2560 case tok::kw___kernel:
2561 ParseOpenCLAttributes(DS.getAttributes());
2562 continue;
2563
Reid Spencer5f016e22007-07-11 17:01:13 +00002564 // storage-class-specifier
2565 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002566 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2567 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 break;
2569 case tok::kw_extern:
2570 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002571 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002572 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2573 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002575 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002576 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2577 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002578 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 case tok::kw_static:
2580 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002581 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002582 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2583 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002584 break;
2585 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002586 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002587 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002588 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2589 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002590 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002591 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002592 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002593 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2595 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002596 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002597 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2598 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 break;
2600 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002601 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2602 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002603 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002604 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002605 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2606 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002607 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002609 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Reid Spencer5f016e22007-07-11 17:01:13 +00002612 // function-specifier
2613 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002614 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002615 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002616 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002617 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002618 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002619 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002620 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002621 break;
Richard Smithde03c152013-01-17 22:16:11 +00002622 case tok::kw__Noreturn:
2623 if (!getLangOpts().C11)
2624 Diag(Loc, diag::ext_c11_noreturn);
2625 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2626 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002627
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002628 // alignment-specifier
2629 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002630 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002631 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002632 ParseAlignmentSpecifier(DS.getAttributes());
2633 continue;
2634
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002635 // friend
2636 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002637 if (DSContext == DSC_class)
2638 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2639 else {
2640 PrevSpec = ""; // not actually used by the diagnostic
2641 DiagID = diag::err_friend_invalid_in_context;
2642 isInvalid = true;
2643 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002644 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Douglas Gregor8d267c52011-09-09 02:06:17 +00002646 // Modules
2647 case tok::kw___module_private__:
2648 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2649 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002650
Sebastian Redl2ac67232009-11-05 15:47:02 +00002651 // constexpr
2652 case tok::kw_constexpr:
2653 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2654 break;
2655
Chris Lattner80d0c892009-01-21 19:48:37 +00002656 // type-specifier
2657 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002658 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2659 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002660 break;
2661 case tok::kw_long:
2662 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002663 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2664 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002665 else
John McCallfec54012009-08-03 20:12:06 +00002666 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2667 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002668 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002669 case tok::kw___int64:
2670 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2671 DiagID);
2672 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002673 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002674 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2675 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002676 break;
2677 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002678 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2679 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002680 break;
2681 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002682 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2683 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002684 break;
2685 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002686 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2687 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002688 break;
2689 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2691 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002692 break;
2693 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002694 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2695 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002696 break;
2697 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002698 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2699 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002700 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002701 case tok::kw___int128:
2702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2703 DiagID);
2704 break;
2705 case tok::kw_half:
2706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2707 DiagID);
2708 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002709 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2711 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002712 break;
2713 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2715 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002716 break;
2717 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2719 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002720 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002721 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2723 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002724 break;
2725 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2727 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002728 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 case tok::kw_bool:
2730 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002731 if (Tok.is(tok::kw_bool) &&
2732 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2733 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2734 PrevSpec = ""; // Not used by the diagnostic.
2735 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002736 // For better error recovery.
2737 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002738 isInvalid = true;
2739 } else {
2740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2741 DiagID);
2742 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002743 break;
2744 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2746 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002747 break;
2748 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2750 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002751 break;
2752 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2754 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002755 break;
John Thompson82287d12010-02-05 00:12:22 +00002756 case tok::kw___vector:
2757 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2758 break;
2759 case tok::kw___pixel:
2760 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2761 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002762 case tok::kw_image1d_t:
2763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2764 PrevSpec, DiagID);
2765 break;
2766 case tok::kw_image1d_array_t:
2767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2768 PrevSpec, DiagID);
2769 break;
2770 case tok::kw_image1d_buffer_t:
2771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2772 PrevSpec, DiagID);
2773 break;
2774 case tok::kw_image2d_t:
2775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2776 PrevSpec, DiagID);
2777 break;
2778 case tok::kw_image2d_array_t:
2779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2780 PrevSpec, DiagID);
2781 break;
2782 case tok::kw_image3d_t:
2783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2784 PrevSpec, DiagID);
2785 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002786 case tok::kw_event_t:
2787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2788 PrevSpec, DiagID);
2789 break;
John McCalla5fc4722011-04-09 22:50:59 +00002790 case tok::kw___unknown_anytype:
2791 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2792 PrevSpec, DiagID);
2793 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002794
2795 // class-specifier:
2796 case tok::kw_class:
2797 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002798 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002799 case tok::kw_union: {
2800 tok::TokenKind Kind = Tok.getKind();
2801 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002802
2803 // These are attributes following class specifiers.
2804 // To produce better diagnostic, we parse them when
2805 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002806 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002807 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002808 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002809
2810 // If there are attributes following class specifier,
2811 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002812 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002813 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002814 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002815 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002816 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002817 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002818
2819 // enum-specifier:
2820 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002821 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002822 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002823 continue;
2824
2825 // cv-qualifier:
2826 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002827 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002828 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002829 break;
2830 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002831 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002832 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002833 break;
2834 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002835 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002836 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002837 break;
2838
Douglas Gregord57959a2009-03-27 23:10:48 +00002839 // C++ typename-specifier:
2840 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002841 if (TryAnnotateTypeOrScopeToken()) {
2842 DS.SetTypeSpecError();
2843 goto DoneWithDeclSpec;
2844 }
2845 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002846 continue;
2847 break;
2848
Chris Lattner80d0c892009-01-21 19:48:37 +00002849 // GNU typeof support.
2850 case tok::kw_typeof:
2851 ParseTypeofSpecifier(DS);
2852 continue;
2853
David Blaikie42d6d0c2011-12-04 05:04:18 +00002854 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002855 ParseDecltypeSpecifier(DS);
2856 continue;
2857
Sean Huntdb5d44b2011-05-19 05:37:45 +00002858 case tok::kw___underlying_type:
2859 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002860 continue;
2861
2862 case tok::kw__Atomic:
2863 ParseAtomicSpecifier(DS);
2864 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002865
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002866 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002867 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002868 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002869 goto DoneWithDeclSpec;
2870 case tok::kw___private:
2871 case tok::kw___global:
2872 case tok::kw___local:
2873 case tok::kw___constant:
2874 case tok::kw___read_only:
2875 case tok::kw___write_only:
2876 case tok::kw___read_write:
2877 ParseOpenCLQualifiers(DS);
2878 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002879
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002880 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002881 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002882 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2883 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002884 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002885 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Douglas Gregor46f936e2010-11-19 17:10:50 +00002887 if (!ParseObjCProtocolQualifiers(DS))
2888 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2889 << FixItHint::CreateInsertion(Loc, "id")
2890 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002891
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002892 // Need to support trailing type qualifiers (e.g. "id<p> const").
2893 // If a type specifier follows, it will be diagnosed elsewhere.
2894 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002895 }
John McCallfec54012009-08-03 20:12:06 +00002896 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002897 if (isInvalid) {
2898 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002899 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002900
Douglas Gregorae2fb142010-08-23 14:34:43 +00002901 if (DiagID == diag::ext_duplicate_declspec)
2902 Diag(Tok, DiagID)
2903 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2904 else
2905 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002906 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002907
Chris Lattner81c018d2008-03-13 06:29:04 +00002908 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002909 if (DiagID != diag::err_bool_redeclaration)
2910 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002911
2912 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 }
2914}
Douglas Gregoradcac882008-12-01 23:54:00 +00002915
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002916/// ParseStructDeclaration - Parse a struct declaration without the terminating
2917/// semicolon.
2918///
Reid Spencer5f016e22007-07-11 17:01:13 +00002919/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002920/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002921/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002922/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002923/// struct-declarator-list:
2924/// struct-declarator
2925/// struct-declarator-list ',' struct-declarator
2926/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2927/// struct-declarator:
2928/// declarator
2929/// [GNU] declarator attributes[opt]
2930/// declarator[opt] ':' constant-expression
2931/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2932///
Chris Lattnere1359422008-04-10 06:46:29 +00002933void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002934ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002935
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002936 if (Tok.is(tok::kw___extension__)) {
2937 // __extension__ silences extension warnings in the subexpression.
2938 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002939 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002940 return ParseStructDeclaration(DS, Fields);
2941 }
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Steve Naroff28a7ca82007-08-20 22:28:22 +00002943 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002944 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002946 // If there are no declarators, this is a free-standing declaration
2947 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002948 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002949 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2950 DS);
2951 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002952 return;
2953 }
2954
2955 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002956 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002957 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002958 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002959 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002960 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002961
Bill Wendlingad017fa2012-12-20 19:22:21 +00002962 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002963 if (!FirstDeclarator)
2964 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Steve Naroff28a7ca82007-08-20 22:28:22 +00002966 /// struct-declarator: declarator
2967 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002968 if (Tok.isNot(tok::colon)) {
2969 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2970 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002971 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002972 }
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Chris Lattner04d66662007-10-09 17:33:22 +00002974 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002975 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002976 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002977 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002978 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002979 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002980 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002981 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002982
Steve Naroff28a7ca82007-08-20 22:28:22 +00002983 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002984 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002985
John McCallbdd563e2009-11-03 02:38:08 +00002986 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002987 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002988
Steve Naroff28a7ca82007-08-20 22:28:22 +00002989 // If we don't have a comma, it is either the end of the list (a ';')
2990 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002991 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002992 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002993
Steve Naroff28a7ca82007-08-20 22:28:22 +00002994 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002995 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002996
John McCallbdd563e2009-11-03 02:38:08 +00002997 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002998 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002999}
3000
3001/// ParseStructUnionBody
3002/// struct-contents:
3003/// struct-declaration-list
3004/// [EXT] empty
3005/// [GNU] "struct-declaration-list" without terminatoring ';'
3006/// struct-declaration-list:
3007/// struct-declaration
3008/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003009/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003010///
Reid Spencer5f016e22007-07-11 17:01:13 +00003011void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003012 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003013 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3014 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003016 BalancedDelimiterTracker T(*this, tok::l_brace);
3017 if (T.consumeOpen())
3018 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003020 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003021 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003022
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3024 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003025 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003026 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3027 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3028 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003029
Chris Lattner5f9e2722011-07-23 10:55:15 +00003030 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003031
Reid Spencer5f016e22007-07-11 17:01:13 +00003032 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003033 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003034 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003035
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003037 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003038 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003039 continue;
3040 }
Chris Lattnere1359422008-04-10 06:46:29 +00003041
John McCallbdd563e2009-11-03 02:38:08 +00003042 if (!Tok.is(tok::at)) {
3043 struct CFieldCallback : FieldCallback {
3044 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003045 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003046 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003047
John McCalld226f652010-08-21 09:40:31 +00003048 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003049 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003050 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3051
Eli Friedmandcdff462012-08-08 23:53:27 +00003052 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003053 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003054 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003055 FD.D.getDeclSpec().getSourceRange().getBegin(),
3056 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003057 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003058 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003059 }
John McCallbdd563e2009-11-03 02:38:08 +00003060 } Callback(*this, TagDecl, FieldDecls);
3061
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003062 // Parse all the comma separated declarators.
3063 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003064 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003065 } else { // Handle @defs
3066 ConsumeToken();
3067 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3068 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003069 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003070 continue;
3071 }
3072 ConsumeToken();
3073 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3074 if (!Tok.is(tok::identifier)) {
3075 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003076 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003077 continue;
3078 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003079 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003080 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003081 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003082 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3083 ConsumeToken();
3084 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003085 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003086
Chris Lattner04d66662007-10-09 17:33:22 +00003087 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003088 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003089 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003090 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 break;
3092 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003093 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3094 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003096 // If we stopped at a ';', eat it.
3097 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003098 }
3099 }
Mike Stump1eb44332009-09-09 15:08:12 +00003100
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003101 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003102
John McCall0b7e6782011-03-24 11:26:52 +00003103 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003105 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003106
Douglas Gregor23c94db2010-07-02 17:43:08 +00003107 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003108 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003109 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003110 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003111 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003112 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3113 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003114}
3115
Reid Spencer5f016e22007-07-11 17:01:13 +00003116/// ParseEnumSpecifier
3117/// enum-specifier: [C99 6.7.2.2]
3118/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003119///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003120/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3121/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003122/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3123/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003124/// 'enum' identifier
3125/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003126///
Richard Smith1af83c42012-03-23 03:33:32 +00003127/// [C++11] enum-head '{' enumerator-list[opt] '}'
3128/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003129///
Richard Smith1af83c42012-03-23 03:33:32 +00003130/// enum-head: [C++11]
3131/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3132/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3133/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003134///
Richard Smith1af83c42012-03-23 03:33:32 +00003135/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003136/// 'enum'
3137/// 'enum' 'class'
3138/// 'enum' 'struct'
3139///
Richard Smith1af83c42012-03-23 03:33:32 +00003140/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003141/// ':' type-specifier-seq
3142///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003143/// [C++] elaborated-type-specifier:
3144/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3145///
Chris Lattner4c97d762009-04-12 21:49:30 +00003146void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003147 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003148 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003150 if (Tok.is(tok::code_completion)) {
3151 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003152 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003153 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003154 }
John McCall57c13002011-07-06 05:58:41 +00003155
Sean Hunt2edf0a22012-06-23 05:07:58 +00003156 // If attributes exist after tag, parse them.
3157 ParsedAttributesWithRange attrs(AttrFactory);
3158 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003159 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003160
3161 // If declspecs exist after tag, parse them.
3162 while (Tok.is(tok::kw___declspec))
3163 ParseMicrosoftDeclSpec(attrs);
3164
Richard Smithbdad7a22012-01-10 01:33:14 +00003165 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003166 bool IsScopedUsingClassTag = false;
3167
John McCall1e12b3d2012-06-23 22:30:04 +00003168 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003169 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003170 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003171 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003172 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003173 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003174
Bill Wendlingad017fa2012-12-20 19:22:21 +00003175 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003176 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003177 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003178
3179 // They are allowed afterwards, though.
3180 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003181 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003182 while (Tok.is(tok::kw___declspec))
3183 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003184 }
Richard Smith1af83c42012-03-23 03:33:32 +00003185
John McCall13489672012-05-07 06:16:58 +00003186 // C++11 [temp.explicit]p12:
3187 // The usual access controls do not apply to names used to specify
3188 // explicit instantiations.
3189 // We extend this to also cover explicit specializations. Note that
3190 // we don't suppress if this turns out to be an elaborated type
3191 // specifier.
3192 bool shouldDelayDiagsInTag =
3193 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3194 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3195 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003196
Richard Smith7796eb52012-03-12 08:56:40 +00003197 // Enum definitions should not be parsed in a trailing-return-type.
3198 bool AllowDeclaration = DSC != DSC_trailing;
3199
3200 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003201 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003202 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003203
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003204 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003205 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003206 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3207 // if a fixed underlying type is allowed.
3208 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003209
3210 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003211 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003212 return;
3213
3214 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003215 Diag(Tok, diag::err_expected_ident);
3216 if (Tok.isNot(tok::l_brace)) {
3217 // Has no name and is not a definition.
3218 // Skip the rest of this declarator, up until the comma or semicolon.
3219 SkipUntil(tok::comma, true);
3220 return;
3221 }
3222 }
3223 }
Mike Stump1eb44332009-09-09 15:08:12 +00003224
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003225 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003226 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003227 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003228 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003230 // Skip the rest of this declarator, up until the comma or semicolon.
3231 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003232 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003233 }
Mike Stump1eb44332009-09-09 15:08:12 +00003234
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003235 // If an identifier is present, consume and remember it.
3236 IdentifierInfo *Name = 0;
3237 SourceLocation NameLoc;
3238 if (Tok.is(tok::identifier)) {
3239 Name = Tok.getIdentifierInfo();
3240 NameLoc = ConsumeToken();
3241 }
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Richard Smithbdad7a22012-01-10 01:33:14 +00003243 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003244 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3245 // declaration of a scoped enumeration.
3246 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003247 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003248 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003249 }
3250
John McCall13489672012-05-07 06:16:58 +00003251 // Okay, end the suppression area. We'll decide whether to emit the
3252 // diagnostics in a second.
3253 if (shouldDelayDiagsInTag)
3254 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003255
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003256 TypeResult BaseType;
3257
Douglas Gregora61b3e72010-12-01 17:42:47 +00003258 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003259 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003260 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003261 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003262 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003263 // If we're in class scope, this can either be an enum declaration with
3264 // an underlying type, or a declaration of a bitfield member. We try to
3265 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003266 // (integer literal, sizeof); if it's still ambiguous, we then consider
3267 // anything that's a simple-type-specifier followed by '(' as an
3268 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003269 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003270 EnterExpressionEvaluationContext Unevaluated(Actions,
3271 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003272 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003273 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003274 // bit-field. This is the common case.
3275 if (TPR == TPResult::True())
3276 PossibleBitfield = true;
3277 // If the next token starts a type-specifier-seq, it may be either a
3278 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003279 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003280 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003281 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003282 GetLookAheadToken(2).getKind() == tok::semi) {
3283 // Consume the ':'.
3284 ConsumeToken();
3285 } else {
3286 // We have the start of a type-specifier-seq, so we have to perform
3287 // tentative parsing to determine whether we have an expression or a
3288 // type.
3289 TentativeParsingAction TPA(*this);
3290
3291 // Consume the ':'.
3292 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003293
3294 // If we see a type specifier followed by an open-brace, we have an
3295 // ambiguity between an underlying type and a C++11 braced
3296 // function-style cast. Resolve this by always treating it as an
3297 // underlying type.
3298 // FIXME: The standard is not entirely clear on how to disambiguate in
3299 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003300 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003301 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003302 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003303 // We'll parse this as a bitfield later.
3304 PossibleBitfield = true;
3305 TPA.Revert();
3306 } else {
3307 // We have a type-specifier-seq.
3308 TPA.Commit();
3309 }
3310 }
3311 } else {
3312 // Consume the ':'.
3313 ConsumeToken();
3314 }
3315
3316 if (!PossibleBitfield) {
3317 SourceRange Range;
3318 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003319
Richard Smith80ad52f2013-01-02 11:42:31 +00003320 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003321 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003322 } else if (!getLangOpts().ObjC2) {
3323 if (getLangOpts().CPlusPlus)
3324 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3325 else
3326 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3327 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003328 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003329 }
3330
Richard Smithbdad7a22012-01-10 01:33:14 +00003331 // There are four options here. If we have 'friend enum foo;' then this is a
3332 // friend declaration, and cannot have an accompanying definition. If we have
3333 // 'enum foo;', then this is a forward declaration. If we have
3334 // 'enum foo {...' then this is a definition. Otherwise we have something
3335 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003336 //
3337 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3338 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3339 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3340 //
John McCallf312b1e2010-08-26 23:41:50 +00003341 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003342 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003343 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003344 } else if (Tok.is(tok::l_brace)) {
3345 if (DS.isFriendSpecified()) {
3346 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3347 << SourceRange(DS.getFriendSpecLoc());
3348 ConsumeBrace();
3349 SkipUntil(tok::r_brace);
3350 TUK = Sema::TUK_Friend;
3351 } else {
3352 TUK = Sema::TUK_Definition;
3353 }
Richard Smithc9f35172012-06-25 21:37:02 +00003354 } else if (DSC != DSC_type_specifier &&
3355 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003356 (Tok.isAtStartOfLine() &&
3357 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003358 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3359 if (Tok.isNot(tok::semi)) {
3360 // A semicolon was missing after this declaration. Diagnose and recover.
3361 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3362 "enum");
3363 PP.EnterToken(Tok);
3364 Tok.setKind(tok::semi);
3365 }
John McCall13489672012-05-07 06:16:58 +00003366 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003367 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003368 }
3369
3370 // If this is an elaborated type specifier, and we delayed
3371 // diagnostics before, just merge them into the current pool.
3372 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3373 diagsFromTag.redelay();
3374 }
Richard Smith1af83c42012-03-23 03:33:32 +00003375
3376 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003377 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003378 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003379 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003380 // Skip the rest of this declarator, up until the comma or semicolon.
3381 Diag(Tok, diag::err_enum_template);
3382 SkipUntil(tok::comma, true);
3383 return;
3384 }
3385
3386 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3387 // Enumerations can't be explicitly instantiated.
3388 DS.SetTypeSpecError();
3389 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3390 return;
3391 }
3392
3393 assert(TemplateInfo.TemplateParams && "no template parameters");
3394 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3395 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003396 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003397
Sean Hunt2edf0a22012-06-23 05:07:58 +00003398 if (TUK == Sema::TUK_Reference)
3399 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003400
Douglas Gregorb9075602011-02-22 02:55:24 +00003401 if (!Name && TUK != Sema::TUK_Definition) {
3402 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003403
Douglas Gregorb9075602011-02-22 02:55:24 +00003404 // Skip the rest of this declarator, up until the comma or semicolon.
3405 SkipUntil(tok::comma, true);
3406 return;
3407 }
Richard Smith1af83c42012-03-23 03:33:32 +00003408
Douglas Gregor402abb52009-05-28 23:31:59 +00003409 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003410 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003411 const char *PrevSpec = 0;
3412 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003413 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003414 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003415 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003416 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003417 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003418
Douglas Gregor48c89f42010-04-24 16:38:41 +00003419 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003420 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003421 // dependent tag.
3422 if (!Name) {
3423 DS.SetTypeSpecError();
3424 Diag(Tok, diag::err_expected_type_name_after_typename);
3425 return;
3426 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003427
Douglas Gregor23c94db2010-07-02 17:43:08 +00003428 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003429 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003430 NameLoc);
3431 if (Type.isInvalid()) {
3432 DS.SetTypeSpecError();
3433 return;
3434 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003435
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003436 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3437 NameLoc.isValid() ? NameLoc : StartLoc,
3438 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003439 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003440
Douglas Gregor48c89f42010-04-24 16:38:41 +00003441 return;
3442 }
Mike Stump1eb44332009-09-09 15:08:12 +00003443
John McCalld226f652010-08-21 09:40:31 +00003444 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003445 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003446 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003447 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003448 ConsumeBrace();
3449 SkipUntil(tok::r_brace);
3450 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003451
Douglas Gregor48c89f42010-04-24 16:38:41 +00003452 DS.SetTypeSpecError();
3453 return;
3454 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003455
Richard Smithc9f35172012-06-25 21:37:02 +00003456 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003457 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003458
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003459 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3460 NameLoc.isValid() ? NameLoc : StartLoc,
3461 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003462 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003463}
3464
3465/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3466/// enumerator-list:
3467/// enumerator
3468/// enumerator-list ',' enumerator
3469/// enumerator:
3470/// enumeration-constant
3471/// enumeration-constant '=' constant-expression
3472/// enumeration-constant:
3473/// identifier
3474///
John McCalld226f652010-08-21 09:40:31 +00003475void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003476 // Enter the scope of the enum body and start the definition.
3477 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003478 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003479
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003480 BalancedDelimiterTracker T(*this, tok::l_brace);
3481 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Chris Lattner7946dd32007-08-27 17:24:30 +00003483 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003484 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003485 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003486
Chris Lattner5f9e2722011-07-23 10:55:15 +00003487 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003488
John McCalld226f652010-08-21 09:40:31 +00003489 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Reid Spencer5f016e22007-07-11 17:01:13 +00003491 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003492 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003493 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3494 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003495
John McCall5b629aa2010-10-22 23:36:17 +00003496 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003497 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003498 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003499 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003500 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003501
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003503 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003504 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003505
Chris Lattner04d66662007-10-09 17:33:22 +00003506 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003507 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003508 AssignedVal = ParseConstantExpression();
3509 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003511 }
Mike Stump1eb44332009-09-09 15:08:12 +00003512
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003514 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3515 LastEnumConstDecl,
3516 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003517 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003518 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003519 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003520
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 EnumConstantDecls.push_back(EnumConstDecl);
3522 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003523
Douglas Gregor751f6922010-09-07 14:51:08 +00003524 if (Tok.is(tok::identifier)) {
3525 // We're missing a comma between enumerators.
3526 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003527 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003528 << FixItHint::CreateInsertion(Loc, ", ");
3529 continue;
3530 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003531
Chris Lattner04d66662007-10-09 17:33:22 +00003532 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003533 break;
3534 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003535
Richard Smith7fe62082011-10-15 05:09:34 +00003536 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003537 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003538 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3539 diag::ext_enumerator_list_comma_cxx :
3540 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003541 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003542 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003543 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3544 << FixItHint::CreateRemoval(CommaLoc);
3545 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003546 }
Mike Stump1eb44332009-09-09 15:08:12 +00003547
Reid Spencer5f016e22007-07-11 17:01:13 +00003548 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003549 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003550
Reid Spencer5f016e22007-07-11 17:01:13 +00003551 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003552 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003553 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003554
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003555 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3556 EnumDecl, EnumConstantDecls.data(),
3557 EnumConstantDecls.size(), getCurScope(),
3558 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003559
Douglas Gregor72de6672009-01-08 20:45:30 +00003560 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003561 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3562 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003563
3564 // The next token must be valid after an enum definition. If not, a ';'
3565 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003566 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3567 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003568 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3569 // Push this token back into the preprocessor and change our current token
3570 // to ';' so that the rest of the code recovers as though there were an
3571 // ';' after the definition.
3572 PP.EnterToken(Tok);
3573 Tok.setKind(tok::semi);
3574 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003575}
3576
3577/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003578/// start of a type-qualifier-list.
3579bool Parser::isTypeQualifier() const {
3580 switch (Tok.getKind()) {
3581 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003582
3583 // type-qualifier only in OpenCL
3584 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003585 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003586
Steve Naroff5f8aa692008-02-11 23:15:56 +00003587 // type-qualifier
3588 case tok::kw_const:
3589 case tok::kw_volatile:
3590 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003591 case tok::kw___private:
3592 case tok::kw___local:
3593 case tok::kw___global:
3594 case tok::kw___constant:
3595 case tok::kw___read_only:
3596 case tok::kw___read_write:
3597 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003598 return true;
3599 }
3600}
3601
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003602/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3603/// is definitely a type-specifier. Return false if it isn't part of a type
3604/// specifier or if we're not sure.
3605bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3606 switch (Tok.getKind()) {
3607 default: return false;
3608 // type-specifiers
3609 case tok::kw_short:
3610 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003611 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003612 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003613 case tok::kw_signed:
3614 case tok::kw_unsigned:
3615 case tok::kw__Complex:
3616 case tok::kw__Imaginary:
3617 case tok::kw_void:
3618 case tok::kw_char:
3619 case tok::kw_wchar_t:
3620 case tok::kw_char16_t:
3621 case tok::kw_char32_t:
3622 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003623 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003624 case tok::kw_float:
3625 case tok::kw_double:
3626 case tok::kw_bool:
3627 case tok::kw__Bool:
3628 case tok::kw__Decimal32:
3629 case tok::kw__Decimal64:
3630 case tok::kw__Decimal128:
3631 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003632
Guy Benyeib13621d2012-12-18 14:38:23 +00003633 // OpenCL specific types:
3634 case tok::kw_image1d_t:
3635 case tok::kw_image1d_array_t:
3636 case tok::kw_image1d_buffer_t:
3637 case tok::kw_image2d_t:
3638 case tok::kw_image2d_array_t:
3639 case tok::kw_image3d_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003640 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003641
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003642 // struct-or-union-specifier (C99) or class-specifier (C++)
3643 case tok::kw_class:
3644 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003645 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003646 case tok::kw_union:
3647 // enum-specifier
3648 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003649
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003650 // typedef-name
3651 case tok::annot_typename:
3652 return true;
3653 }
3654}
3655
Steve Naroff5f8aa692008-02-11 23:15:56 +00003656/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003657/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003658bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003659 switch (Tok.getKind()) {
3660 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003661
Chris Lattner166a8fc2009-01-04 23:41:41 +00003662 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003663 if (TryAltiVecVectorToken())
3664 return true;
3665 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003666 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003667 // Annotate typenames and C++ scope specifiers. If we get one, just
3668 // recurse to handle whatever we get.
3669 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003670 return true;
3671 if (Tok.is(tok::identifier))
3672 return false;
3673 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003674
Chris Lattner166a8fc2009-01-04 23:41:41 +00003675 case tok::coloncolon: // ::foo::bar
3676 if (NextToken().is(tok::kw_new) || // ::new
3677 NextToken().is(tok::kw_delete)) // ::delete
3678 return false;
3679
Chris Lattner166a8fc2009-01-04 23:41:41 +00003680 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003681 return true;
3682 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003683
Reid Spencer5f016e22007-07-11 17:01:13 +00003684 // GNU attributes support.
3685 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003686 // GNU typeof support.
3687 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003688
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 // type-specifiers
3690 case tok::kw_short:
3691 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003692 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003693 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003694 case tok::kw_signed:
3695 case tok::kw_unsigned:
3696 case tok::kw__Complex:
3697 case tok::kw__Imaginary:
3698 case tok::kw_void:
3699 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003700 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003701 case tok::kw_char16_t:
3702 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003703 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003704 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003705 case tok::kw_float:
3706 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003707 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 case tok::kw__Bool:
3709 case tok::kw__Decimal32:
3710 case tok::kw__Decimal64:
3711 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003712 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003713
Guy Benyeib13621d2012-12-18 14:38:23 +00003714 // OpenCL specific types:
3715 case tok::kw_image1d_t:
3716 case tok::kw_image1d_array_t:
3717 case tok::kw_image1d_buffer_t:
3718 case tok::kw_image2d_t:
3719 case tok::kw_image2d_array_t:
3720 case tok::kw_image3d_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003721 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003722
Chris Lattner99dc9142008-04-13 18:59:07 +00003723 // struct-or-union-specifier (C99) or class-specifier (C++)
3724 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003726 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003727 case tok::kw_union:
3728 // enum-specifier
3729 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003730
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 // type-qualifier
3732 case tok::kw_const:
3733 case tok::kw_volatile:
3734 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003735
John McCallb8a8de32012-11-14 00:49:39 +00003736 // Debugger support.
3737 case tok::kw___unknown_anytype:
3738
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003739 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003740 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003741 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003742
Chris Lattner7c186be2008-10-20 00:25:30 +00003743 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3744 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003745 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Steve Naroff239f0732008-12-25 14:16:32 +00003747 case tok::kw___cdecl:
3748 case tok::kw___stdcall:
3749 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003750 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003751 case tok::kw___w64:
3752 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003753 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003754 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003755 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003756
3757 case tok::kw___private:
3758 case tok::kw___local:
3759 case tok::kw___global:
3760 case tok::kw___constant:
3761 case tok::kw___read_only:
3762 case tok::kw___read_write:
3763 case tok::kw___write_only:
3764
Eli Friedman290eeb02009-06-08 23:27:34 +00003765 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003766
3767 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003768 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003769
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003770 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003771 case tok::kw__Atomic:
3772 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003773 }
3774}
3775
3776/// isDeclarationSpecifier() - Return true if the current token is part of a
3777/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003778///
3779/// \param DisambiguatingWithExpression True to indicate that the purpose of
3780/// this check is to disambiguate between an expression and a declaration.
3781bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003782 switch (Tok.getKind()) {
3783 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003784
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003785 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003786 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003787
Chris Lattner166a8fc2009-01-04 23:41:41 +00003788 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003789 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003790 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003791 return false;
John Thompson82287d12010-02-05 00:12:22 +00003792 if (TryAltiVecVectorToken())
3793 return true;
3794 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003795 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003796 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003797 // Annotate typenames and C++ scope specifiers. If we get one, just
3798 // recurse to handle whatever we get.
3799 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003800 return true;
3801 if (Tok.is(tok::identifier))
3802 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003803
Douglas Gregor9497a732010-09-16 01:51:54 +00003804 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003805 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003806 // expression is permitted, then this is probably a class message send
3807 // missing the initial '['. In this case, we won't consider this to be
3808 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003809 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003810 isStartOfObjCClassMessageMissingOpenBracket())
3811 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003812
John McCall9ba61662010-02-26 08:45:28 +00003813 return isDeclarationSpecifier();
3814
Chris Lattner166a8fc2009-01-04 23:41:41 +00003815 case tok::coloncolon: // ::foo::bar
3816 if (NextToken().is(tok::kw_new) || // ::new
3817 NextToken().is(tok::kw_delete)) // ::delete
3818 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003819
Chris Lattner166a8fc2009-01-04 23:41:41 +00003820 // Annotate typenames and C++ scope specifiers. If we get one, just
3821 // recurse to handle whatever we get.
3822 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003823 return true;
3824 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003825
Reid Spencer5f016e22007-07-11 17:01:13 +00003826 // storage-class-specifier
3827 case tok::kw_typedef:
3828 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003829 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003830 case tok::kw_static:
3831 case tok::kw_auto:
3832 case tok::kw_register:
3833 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003834
Douglas Gregor8d267c52011-09-09 02:06:17 +00003835 // Modules
3836 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003837
John McCallb8a8de32012-11-14 00:49:39 +00003838 // Debugger support
3839 case tok::kw___unknown_anytype:
3840
Reid Spencer5f016e22007-07-11 17:01:13 +00003841 // type-specifiers
3842 case tok::kw_short:
3843 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003844 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003845 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003846 case tok::kw_signed:
3847 case tok::kw_unsigned:
3848 case tok::kw__Complex:
3849 case tok::kw__Imaginary:
3850 case tok::kw_void:
3851 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003852 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003853 case tok::kw_char16_t:
3854 case tok::kw_char32_t:
3855
Reid Spencer5f016e22007-07-11 17:01:13 +00003856 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003857 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003858 case tok::kw_float:
3859 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003860 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003861 case tok::kw__Bool:
3862 case tok::kw__Decimal32:
3863 case tok::kw__Decimal64:
3864 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003865 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003866
Guy Benyeib13621d2012-12-18 14:38:23 +00003867 // OpenCL specific types:
3868 case tok::kw_image1d_t:
3869 case tok::kw_image1d_array_t:
3870 case tok::kw_image1d_buffer_t:
3871 case tok::kw_image2d_t:
3872 case tok::kw_image2d_array_t:
3873 case tok::kw_image3d_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003874 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003875
Chris Lattner99dc9142008-04-13 18:59:07 +00003876 // struct-or-union-specifier (C99) or class-specifier (C++)
3877 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003878 case tok::kw_struct:
3879 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003880 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003881 // enum-specifier
3882 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003883
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 // type-qualifier
3885 case tok::kw_const:
3886 case tok::kw_volatile:
3887 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003888
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 // function-specifier
3890 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003891 case tok::kw_virtual:
3892 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003893 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003894
Richard Smith53aec2a2012-10-25 00:00:53 +00003895 // friend keyword.
3896 case tok::kw_friend:
3897
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003898 // static_assert-declaration
3899 case tok::kw__Static_assert:
3900
Chris Lattner1ef08762007-08-09 17:01:07 +00003901 // GNU typeof support.
3902 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003903
Chris Lattner1ef08762007-08-09 17:01:07 +00003904 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003905 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003906
Richard Smith53aec2a2012-10-25 00:00:53 +00003907 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003908 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003909 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003910
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003911 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003912 case tok::kw__Atomic:
3913 return true;
3914
Chris Lattnerf3948c42008-07-26 03:38:44 +00003915 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3916 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003917 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003918
Douglas Gregord9d75e52011-04-27 05:41:15 +00003919 // typedef-name
3920 case tok::annot_typename:
3921 return !DisambiguatingWithExpression ||
3922 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003923
Steve Naroff47f52092009-01-06 19:34:12 +00003924 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003925 case tok::kw___cdecl:
3926 case tok::kw___stdcall:
3927 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003928 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003929 case tok::kw___w64:
3930 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003931 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003932 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003933 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003934 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003935
3936 case tok::kw___private:
3937 case tok::kw___local:
3938 case tok::kw___global:
3939 case tok::kw___constant:
3940 case tok::kw___read_only:
3941 case tok::kw___read_write:
3942 case tok::kw___write_only:
3943
Eli Friedman290eeb02009-06-08 23:27:34 +00003944 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003945 }
3946}
3947
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003948bool Parser::isConstructorDeclarator() {
3949 TentativeParsingAction TPA(*this);
3950
3951 // Parse the C++ scope specifier.
3952 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003953 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003954 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003955 TPA.Revert();
3956 return false;
3957 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003958
3959 // Parse the constructor name.
3960 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3961 // We already know that we have a constructor name; just consume
3962 // the token.
3963 ConsumeToken();
3964 } else {
3965 TPA.Revert();
3966 return false;
3967 }
3968
Richard Smith22592862012-03-27 23:05:05 +00003969 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003970 if (Tok.isNot(tok::l_paren)) {
3971 TPA.Revert();
3972 return false;
3973 }
3974 ConsumeParen();
3975
Richard Smith22592862012-03-27 23:05:05 +00003976 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3977 // that we have a constructor.
3978 if (Tok.is(tok::r_paren) ||
3979 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003980 TPA.Revert();
3981 return true;
3982 }
3983
3984 // If we need to, enter the specified scope.
3985 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003986 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003987 DeclScopeObj.EnterDeclaratorScope();
3988
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003989 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003990 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003991 MaybeParseMicrosoftAttributes(Attrs);
3992
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003993 // Check whether the next token(s) are part of a declaration
3994 // specifier, in which case we have the start of a parameter and,
3995 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003996 bool IsConstructor = false;
3997 if (isDeclarationSpecifier())
3998 IsConstructor = true;
3999 else if (Tok.is(tok::identifier) ||
4000 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4001 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4002 // This might be a parenthesized member name, but is more likely to
4003 // be a constructor declaration with an invalid argument type. Keep
4004 // looking.
4005 if (Tok.is(tok::annot_cxxscope))
4006 ConsumeToken();
4007 ConsumeToken();
4008
4009 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004010 // which must have one of the following syntactic forms (see the
4011 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004012 switch (Tok.getKind()) {
4013 case tok::l_paren:
4014 // C(X ( int));
4015 case tok::l_square:
4016 // C(X [ 5]);
4017 // C(X [ [attribute]]);
4018 case tok::coloncolon:
4019 // C(X :: Y);
4020 // C(X :: *p);
4021 case tok::r_paren:
4022 // C(X )
4023 // Assume this isn't a constructor, rather than assuming it's a
4024 // constructor with an unnamed parameter of an ill-formed type.
4025 break;
4026
4027 default:
4028 IsConstructor = true;
4029 break;
4030 }
4031 }
4032
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004033 TPA.Revert();
4034 return IsConstructor;
4035}
Reid Spencer5f016e22007-07-11 17:01:13 +00004036
4037/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004038/// type-qualifier-list: [C99 6.7.5]
4039/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004040/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004041/// [ only if VendorAttributesAllowed=true ]
4042/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004043/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004044/// [ only if VendorAttributesAllowed=true ]
4045/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004046/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004047/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004048///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004049void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4050 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004051 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004052 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004053 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004054 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004055 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004056 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004057 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004058
4059 SourceLocation EndLoc;
4060
Reid Spencer5f016e22007-07-11 17:01:13 +00004061 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004062 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004063 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004064 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004065 SourceLocation Loc = Tok.getLocation();
4066
4067 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004068 case tok::code_completion:
4069 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004070 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004071
Reid Spencer5f016e22007-07-11 17:01:13 +00004072 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004073 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004074 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004075 break;
4076 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004077 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004078 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004079 break;
4080 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004082 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004083 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004084
4085 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004086 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004087 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004088 goto DoneWithTypeQuals;
4089 case tok::kw___private:
4090 case tok::kw___global:
4091 case tok::kw___local:
4092 case tok::kw___constant:
4093 case tok::kw___read_only:
4094 case tok::kw___write_only:
4095 case tok::kw___read_write:
4096 ParseOpenCLQualifiers(DS);
4097 break;
4098
Eli Friedman290eeb02009-06-08 23:27:34 +00004099 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004100 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004101 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004102 case tok::kw___cdecl:
4103 case tok::kw___stdcall:
4104 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004105 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004106 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004107 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004108 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004109 continue;
4110 }
4111 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004112 case tok::kw___pascal:
4113 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004114 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004115 continue;
4116 }
4117 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004118 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004119 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004120 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004121 continue; // do *not* consume the next token!
4122 }
4123 // otherwise, FALL THROUGH!
4124 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004125 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004126 // If this is not a type-qualifier token, we're done reading type
4127 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004128 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004129 if (EndLoc.isValid())
4130 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004131 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004132 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004133
Reid Spencer5f016e22007-07-11 17:01:13 +00004134 // If the specifier combination wasn't legal, issue a diagnostic.
4135 if (isInvalid) {
4136 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004137 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004138 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004139 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 }
4141}
4142
4143
4144/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4145///
4146void Parser::ParseDeclarator(Declarator &D) {
4147 /// This implements the 'declarator' production in the C grammar, then checks
4148 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004149 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004150}
4151
Richard Smith9988f282012-03-29 01:16:42 +00004152static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4153 if (Kind == tok::star || Kind == tok::caret)
4154 return true;
4155
4156 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4157 if (!Lang.CPlusPlus)
4158 return false;
4159
4160 return Kind == tok::amp || Kind == tok::ampamp;
4161}
4162
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004163/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4164/// is parsed by the function passed to it. Pass null, and the direct-declarator
4165/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004166/// ptr-operator production.
4167///
Richard Smith0706df42011-10-19 21:33:05 +00004168/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004169/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4170/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004171///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004172/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4173/// [C] pointer[opt] direct-declarator
4174/// [C++] direct-declarator
4175/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004176///
4177/// pointer: [C99 6.7.5]
4178/// '*' type-qualifier-list[opt]
4179/// '*' type-qualifier-list[opt] pointer
4180///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004181/// ptr-operator:
4182/// '*' cv-qualifier-seq[opt]
4183/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004184/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004185/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004186/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004187/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004188void Parser::ParseDeclaratorInternal(Declarator &D,
4189 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004190 if (Diags.hasAllExtensionsSilenced())
4191 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004192
Sebastian Redlf30208a2009-01-24 21:16:55 +00004193 // C++ member pointers start with a '::' or a nested-name.
4194 // Member pointers get special handling, since there's no place for the
4195 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004196 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004197 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4198 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004199 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4200 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004201 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004202 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004203
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004204 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004205 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004206 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004207 if (D.mayHaveIdentifier())
4208 D.getCXXScopeSpec() = SS;
4209 else
4210 AnnotateScopeToken(SS, true);
4211
Sebastian Redlf30208a2009-01-24 21:16:55 +00004212 if (DirectDeclParser)
4213 (this->*DirectDeclParser)(D);
4214 return;
4215 }
4216
4217 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004218 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004219 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004220 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004221 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004222
4223 // Recurse to parse whatever is left.
4224 ParseDeclaratorInternal(D, DirectDeclParser);
4225
4226 // Sema will have to catch (syntactically invalid) pointers into global
4227 // scope. It has to catch pointers into namespace scope anyway.
4228 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004229 Loc),
4230 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004231 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004232 return;
4233 }
4234 }
4235
4236 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004237 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004238 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004239 if (DirectDeclParser)
4240 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004241 return;
4242 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004243
Sebastian Redl05532f22009-03-15 22:02:01 +00004244 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4245 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004246 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004247 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004248
Chris Lattner9af55002009-03-27 04:18:06 +00004249 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004250 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004251 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004252
Richard Smith6ee326a2012-04-10 01:32:12 +00004253 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004254 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004255 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004256
Reid Spencer5f016e22007-07-11 17:01:13 +00004257 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004258 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004259 if (Kind == tok::star)
4260 // Remember that we parsed a pointer type, and remember the type-quals.
4261 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004262 DS.getConstSpecLoc(),
4263 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004264 DS.getRestrictSpecLoc()),
4265 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004266 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004267 else
4268 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004269 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004270 Loc),
4271 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004272 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004273 } else {
4274 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004275 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004276
Sebastian Redl743de1f2009-03-23 00:00:23 +00004277 // Complain about rvalue references in C++03, but then go on and build
4278 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004279 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004280 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004281 diag::warn_cxx98_compat_rvalue_reference :
4282 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004283
Richard Smith6ee326a2012-04-10 01:32:12 +00004284 // GNU-style and C++11 attributes are allowed here, as is restrict.
4285 ParseTypeQualifierListOpt(DS);
4286 D.ExtendWithDeclSpec(DS);
4287
Reid Spencer5f016e22007-07-11 17:01:13 +00004288 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4289 // cv-qualifiers are introduced through the use of a typedef or of a
4290 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004291 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4292 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4293 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004294 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004295 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4296 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004297 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004298 }
4299
4300 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004301 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004302
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004303 if (D.getNumTypeObjects() > 0) {
4304 // C++ [dcl.ref]p4: There shall be no references to references.
4305 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4306 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004307 if (const IdentifierInfo *II = D.getIdentifier())
4308 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4309 << II;
4310 else
4311 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4312 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004313
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004314 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004315 // can go ahead and build the (technically ill-formed)
4316 // declarator: reference collapsing will take care of it.
4317 }
4318 }
4319
Reid Spencer5f016e22007-07-11 17:01:13 +00004320 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004321 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004322 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004323 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004324 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004325 }
4326}
4327
Richard Smith9988f282012-03-29 01:16:42 +00004328static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4329 SourceLocation EllipsisLoc) {
4330 if (EllipsisLoc.isValid()) {
4331 FixItHint Insertion;
4332 if (!D.getEllipsisLoc().isValid()) {
4333 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4334 D.setEllipsisLoc(EllipsisLoc);
4335 }
4336 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4337 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4338 }
4339}
4340
Reid Spencer5f016e22007-07-11 17:01:13 +00004341/// ParseDirectDeclarator
4342/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004343/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004344/// '(' declarator ')'
4345/// [GNU] '(' attributes declarator ')'
4346/// [C90] direct-declarator '[' constant-expression[opt] ']'
4347/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4348/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4349/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4350/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004351/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4352/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004353/// direct-declarator '(' parameter-type-list ')'
4354/// direct-declarator '(' identifier-list[opt] ')'
4355/// [GNU] direct-declarator '(' parameter-forward-declarations
4356/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004357/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4358/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004359/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4360/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4361/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004362/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004363/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004364///
4365/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004366/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004367/// '::'[opt] nested-name-specifier[opt] type-name
4368///
4369/// id-expression: [C++ 5.1]
4370/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004371/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004372///
4373/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004374/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004375/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004376/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004377/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004378/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004379///
Richard Smith5d8388c2012-03-27 01:42:32 +00004380/// Note, any additional constructs added here may need corresponding changes
4381/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004382void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004383 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004384
David Blaikie4e4d0842012-03-11 07:00:24 +00004385 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004386 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004387 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004388 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4389 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004390 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004391 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004392 }
4393
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004394 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004395 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004396 // Change the declaration context for name lookup, until this function
4397 // is exited (and the declarator has been parsed).
4398 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004399 }
4400
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004401 // C++0x [dcl.fct]p14:
4402 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004403 // of a parameter-declaration-clause without a preceding comma. In
4404 // this case, the ellipsis is parsed as part of the
4405 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004406 // parameter pack that has not been expanded; otherwise, it is parsed
4407 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004408 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004409 !((D.getContext() == Declarator::PrototypeContext ||
4410 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004411 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004412 !Actions.containsUnexpandedParameterPacks(D))) {
4413 SourceLocation EllipsisLoc = ConsumeToken();
4414 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4415 // The ellipsis was put in the wrong place. Recover, and explain to
4416 // the user what they should have done.
4417 ParseDeclarator(D);
4418 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4419 return;
4420 } else
4421 D.setEllipsisLoc(EllipsisLoc);
4422
4423 // The ellipsis can't be followed by a parenthesized declarator. We
4424 // check for that in ParseParenDeclarator, after we have disambiguated
4425 // the l_paren token.
4426 }
4427
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004428 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4429 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4430 // We found something that indicates the start of an unqualified-id.
4431 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004432 bool AllowConstructorName;
4433 if (D.getDeclSpec().hasTypeSpecifier())
4434 AllowConstructorName = false;
4435 else if (D.getCXXScopeSpec().isSet())
4436 AllowConstructorName =
4437 (D.getContext() == Declarator::FileContext ||
4438 (D.getContext() == Declarator::MemberContext &&
4439 D.getDeclSpec().isFriendSpecified()));
4440 else
4441 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4442
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004443 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004444 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4445 /*EnteringContext=*/true,
4446 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004447 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004448 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004449 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004450 D.getName()) ||
4451 // Once we're past the identifier, if the scope was bad, mark the
4452 // whole declarator bad.
4453 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004454 D.SetIdentifier(0, Tok.getLocation());
4455 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004456 } else {
4457 // Parsed the unqualified-id; update range information and move along.
4458 if (D.getSourceRange().getBegin().isInvalid())
4459 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4460 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004461 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004462 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004463 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004464 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004465 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004466 "There's a C++-specific check for tok::identifier above");
4467 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4468 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4469 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004470 goto PastIdentifier;
4471 }
Richard Smith9988f282012-03-29 01:16:42 +00004472
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004473 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004474 // direct-declarator: '(' declarator ')'
4475 // direct-declarator: '(' attributes declarator ')'
4476 // Example: 'char (*X)' or 'int (*XX)(void)'
4477 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004478
4479 // If the declarator was parenthesized, we entered the declarator
4480 // scope when parsing the parenthesized declarator, then exited
4481 // the scope already. Re-enter the scope, if we need to.
4482 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004483 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004484 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004485 if (!D.isInvalidType() &&
4486 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004487 // Change the declaration context for name lookup, until this function
4488 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004489 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004490 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004491 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004492 // This could be something simple like "int" (in which case the declarator
4493 // portion is empty), if an abstract-declarator is allowed.
4494 D.SetIdentifier(0, Tok.getLocation());
4495 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004496 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004497 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004498 if (D.getContext() == Declarator::MemberContext)
4499 Diag(Tok, diag::err_expected_member_name_or_semi)
4500 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004501 else if (getLangOpts().CPlusPlus)
4502 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004503 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004504 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004505 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004506 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004507 }
Mike Stump1eb44332009-09-09 15:08:12 +00004508
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004509 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004510 assert(D.isPastIdentifier() &&
4511 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004512
Richard Smith6ee326a2012-04-10 01:32:12 +00004513 // Don't parse attributes unless we have parsed an unparenthesized name.
4514 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004515 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004516
Reid Spencer5f016e22007-07-11 17:01:13 +00004517 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004518 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004519 // Enter function-declaration scope, limiting any declarators to the
4520 // function prototype scope, including parameter declarators.
4521 ParseScope PrototypeScope(this,
4522 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004523 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4524 // In such a case, check if we actually have a function declarator; if it
4525 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004526 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004527 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4528 // The name of the declarator, if any, is tentatively declared within
4529 // a possible direct initializer.
4530 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4531 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4532 TentativelyDeclaredIdentifiers.pop_back();
4533 if (!IsFunctionDecl)
4534 break;
4535 }
John McCall0b7e6782011-03-24 11:26:52 +00004536 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004537 BalancedDelimiterTracker T(*this, tok::l_paren);
4538 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004539 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004540 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004541 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004542 ParseBracketDeclarator(D);
4543 } else {
4544 break;
4545 }
4546 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004547}
Reid Spencer5f016e22007-07-11 17:01:13 +00004548
Chris Lattneref4715c2008-04-06 05:45:57 +00004549/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4550/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004551/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004552/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4553///
4554/// direct-declarator:
4555/// '(' declarator ')'
4556/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004557/// direct-declarator '(' parameter-type-list ')'
4558/// direct-declarator '(' identifier-list[opt] ')'
4559/// [GNU] direct-declarator '(' parameter-forward-declarations
4560/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004561///
4562void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004563 BalancedDelimiterTracker T(*this, tok::l_paren);
4564 T.consumeOpen();
4565
Chris Lattneref4715c2008-04-06 05:45:57 +00004566 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004567
Chris Lattner7399ee02008-10-20 02:05:46 +00004568 // Eat any attributes before we look at whether this is a grouping or function
4569 // declarator paren. If this is a grouping paren, the attribute applies to
4570 // the type being built up, for example:
4571 // int (__attribute__(()) *x)(long y)
4572 // If this ends up not being a grouping paren, the attribute applies to the
4573 // first argument, for example:
4574 // int (__attribute__(()) int x)
4575 // In either case, we need to eat any attributes to be able to determine what
4576 // sort of paren this is.
4577 //
John McCall0b7e6782011-03-24 11:26:52 +00004578 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004579 bool RequiresArg = false;
4580 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004581 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004582
Chris Lattner7399ee02008-10-20 02:05:46 +00004583 // We require that the argument list (if this is a non-grouping paren) be
4584 // present even if the attribute list was empty.
4585 RequiresArg = true;
4586 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004587
Steve Naroff239f0732008-12-25 14:16:32 +00004588 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004589 ParseMicrosoftTypeAttributes(attrs);
4590
Dawn Perchik52fc3142010-09-03 01:29:35 +00004591 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004592 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004593 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004594
Chris Lattneref4715c2008-04-06 05:45:57 +00004595 // If we haven't past the identifier yet (or where the identifier would be
4596 // stored, if this is an abstract declarator), then this is probably just
4597 // grouping parens. However, if this could be an abstract-declarator, then
4598 // this could also be the start of function arguments (consider 'void()').
4599 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004600
Chris Lattneref4715c2008-04-06 05:45:57 +00004601 if (!D.mayOmitIdentifier()) {
4602 // If this can't be an abstract-declarator, this *must* be a grouping
4603 // paren, because we haven't seen the identifier yet.
4604 isGrouping = true;
4605 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004606 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4607 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004608 isDeclarationSpecifier() || // 'int(int)' is a function.
4609 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004610 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4611 // considered to be a type, not a K&R identifier-list.
4612 isGrouping = false;
4613 } else {
4614 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4615 isGrouping = true;
4616 }
Mike Stump1eb44332009-09-09 15:08:12 +00004617
Chris Lattneref4715c2008-04-06 05:45:57 +00004618 // If this is a grouping paren, handle:
4619 // direct-declarator: '(' declarator ')'
4620 // direct-declarator: '(' attributes declarator ')'
4621 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004622 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4623 D.setEllipsisLoc(SourceLocation());
4624
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004625 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004626 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004627 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004628 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004629 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004630 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004631 T.getCloseLocation()),
4632 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004633
4634 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004635
4636 // An ellipsis cannot be placed outside parentheses.
4637 if (EllipsisLoc.isValid())
4638 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4639
Chris Lattneref4715c2008-04-06 05:45:57 +00004640 return;
4641 }
Mike Stump1eb44332009-09-09 15:08:12 +00004642
Chris Lattneref4715c2008-04-06 05:45:57 +00004643 // Okay, if this wasn't a grouping paren, it must be the start of a function
4644 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004645 // identifier (and remember where it would have been), then call into
4646 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004647 D.SetIdentifier(0, Tok.getLocation());
4648
David Blaikie42d6d0c2011-12-04 05:04:18 +00004649 // Enter function-declaration scope, limiting any declarators to the
4650 // function prototype scope, including parameter declarators.
4651 ParseScope PrototypeScope(this,
4652 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004653 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004654 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004655}
4656
4657/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4658/// declarator D up to a paren, which indicates that we are parsing function
4659/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004660///
Richard Smith6ee326a2012-04-10 01:32:12 +00004661/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4662/// immediately after the open paren - they should be considered to be the
4663/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004664///
Richard Smith6ee326a2012-04-10 01:32:12 +00004665/// If RequiresArg is true, then the first argument of the function is required
4666/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004667///
Richard Smith6ee326a2012-04-10 01:32:12 +00004668/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4669/// (C++11) ref-qualifier[opt], exception-specification[opt],
4670/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4671///
4672/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004673/// dynamic-exception-specification
4674/// noexcept-specification
4675///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004676void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004677 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004678 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004679 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004680 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004681 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004682 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004683 // lparen is already consumed!
4684 assert(D.isPastIdentifier() && "Should not call before identifier!");
4685
4686 // This should be true when the function has typed arguments.
4687 // Otherwise, it is treated as a K&R-style function.
4688 bool HasProto = false;
4689 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004690 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004691 // Remember where we see an ellipsis, if any.
4692 SourceLocation EllipsisLoc;
4693
4694 DeclSpec DS(AttrFactory);
4695 bool RefQualifierIsLValueRef = true;
4696 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004697 SourceLocation ConstQualifierLoc;
4698 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004699 ExceptionSpecificationType ESpecType = EST_None;
4700 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004701 SmallVector<ParsedType, 2> DynamicExceptions;
4702 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004703 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004704 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004705 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004706
James Molloy16f1f712012-02-29 10:24:19 +00004707 Actions.ActOnStartFunctionDeclarator();
4708
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004709 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4710 EndLoc is the end location for the function declarator.
4711 They differ for trailing return types. */
4712 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004713 SourceLocation LParenLoc, RParenLoc;
4714 LParenLoc = Tracker.getOpenLocation();
4715 StartLoc = LParenLoc;
4716
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004717 if (isFunctionDeclaratorIdentifierList()) {
4718 if (RequiresArg)
4719 Diag(Tok, diag::err_argument_required_after_attribute);
4720
4721 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4722
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004723 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004724 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004725 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004726 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004727 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004728 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004729 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004730 else if (RequiresArg)
4731 Diag(Tok, diag::err_argument_required_after_attribute);
4732
David Blaikie4e4d0842012-03-11 07:00:24 +00004733 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004734
4735 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004736 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004737 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004738 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004739 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004740
David Blaikie4e4d0842012-03-11 07:00:24 +00004741 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004742 // FIXME: Accept these components in any order, and produce fixits to
4743 // correct the order if the user gets it wrong. Ideally we should deal
4744 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004745
4746 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004747 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4748 if (!DS.getSourceRange().getEnd().isInvalid()) {
4749 EndLoc = DS.getSourceRange().getEnd();
4750 ConstQualifierLoc = DS.getConstSpecLoc();
4751 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4752 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004753
4754 // Parse ref-qualifier[opt].
4755 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004756 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004757 diag::warn_cxx98_compat_ref_qualifier :
4758 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004759
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004760 RefQualifierIsLValueRef = Tok.is(tok::amp);
4761 RefQualifierLoc = ConsumeToken();
4762 EndLoc = RefQualifierLoc;
4763 }
4764
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004765 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004766 // If a declaration declares a member function or member function
4767 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004768 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004769 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004770 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004771 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004772 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004773 (D.getContext() == Declarator::MemberContext ||
4774 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004775 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004776 Actions.CurContext->isRecord()));
4777 Sema::CXXThisScopeRAII ThisScope(Actions,
4778 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004779 DS.getTypeQualifiers() |
4780 (D.getDeclSpec().isConstexprSpecified()
4781 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004782 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004783
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004784 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004785 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004786 DynamicExceptions,
4787 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004788 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004789 if (ESpecType != EST_None)
4790 EndLoc = ESpecRange.getEnd();
4791
Richard Smith6ee326a2012-04-10 01:32:12 +00004792 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4793 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004794 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004795
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004796 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004797 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004798 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004799 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004800 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4801 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004802 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004803 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004804 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004805 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004806 }
4807 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004808 }
4809
4810 // Remember that we parsed a function type, and remember the attributes.
4811 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004812 IsAmbiguous,
4813 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004814 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004815 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004816 DS.getTypeQualifiers(),
4817 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004818 RefQualifierLoc, ConstQualifierLoc,
4819 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004820 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004821 ESpecType, ESpecRange.getBegin(),
4822 DynamicExceptions.data(),
4823 DynamicExceptionRanges.data(),
4824 DynamicExceptions.size(),
4825 NoexceptExpr.isUsable() ?
4826 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004827 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004828 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004829 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004830
4831 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004832}
4833
4834/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4835/// identifier list form for a K&R-style function: void foo(a,b,c)
4836///
4837/// Note that identifier-lists are only allowed for normal declarators, not for
4838/// abstract-declarators.
4839bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004840 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004841 && Tok.is(tok::identifier)
4842 && !TryAltiVecVectorToken()
4843 // K&R identifier lists can't have typedefs as identifiers, per C99
4844 // 6.7.5.3p11.
4845 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4846 // Identifier lists follow a really simple grammar: the identifiers can
4847 // be followed *only* by a ", identifier" or ")". However, K&R
4848 // identifier lists are really rare in the brave new modern world, and
4849 // it is very common for someone to typo a type in a non-K&R style
4850 // list. If we are presented with something like: "void foo(intptr x,
4851 // float y)", we don't want to start parsing the function declarator as
4852 // though it is a K&R style declarator just because intptr is an
4853 // invalid type.
4854 //
4855 // To handle this, we check to see if the token after the first
4856 // identifier is a "," or ")". Only then do we parse it as an
4857 // identifier list.
4858 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4859}
4860
4861/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4862/// we found a K&R-style identifier list instead of a typed parameter list.
4863///
4864/// After returning, ParamInfo will hold the parsed parameters.
4865///
4866/// identifier-list: [C99 6.7.5]
4867/// identifier
4868/// identifier-list ',' identifier
4869///
4870void Parser::ParseFunctionDeclaratorIdentifierList(
4871 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004872 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004873 // If there was no identifier specified for the declarator, either we are in
4874 // an abstract-declarator, or we are in a parameter declarator which was found
4875 // to be abstract. In abstract-declarators, identifier lists are not valid:
4876 // diagnose this.
4877 if (!D.getIdentifier())
4878 Diag(Tok, diag::ext_ident_list_in_param);
4879
4880 // Maintain an efficient lookup of params we have seen so far.
4881 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4882
4883 while (1) {
4884 // If this isn't an identifier, report the error and skip until ')'.
4885 if (Tok.isNot(tok::identifier)) {
4886 Diag(Tok, diag::err_expected_ident);
4887 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4888 // Forget we parsed anything.
4889 ParamInfo.clear();
4890 return;
4891 }
4892
4893 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4894
4895 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4896 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4897 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4898
4899 // Verify that the argument identifier has not already been mentioned.
4900 if (!ParamsSoFar.insert(ParmII)) {
4901 Diag(Tok, diag::err_param_redefinition) << ParmII;
4902 } else {
4903 // Remember this identifier in ParamInfo.
4904 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4905 Tok.getLocation(),
4906 0));
4907 }
4908
4909 // Eat the identifier.
4910 ConsumeToken();
4911
4912 // The list continues if we see a comma.
4913 if (Tok.isNot(tok::comma))
4914 break;
4915 ConsumeToken();
4916 }
4917}
4918
4919/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4920/// after the opening parenthesis. This function will not parse a K&R-style
4921/// identifier list.
4922///
Richard Smith6ce48a72012-04-11 04:01:28 +00004923/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4924/// caller parsed those arguments immediately after the open paren - they should
4925/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004926///
4927/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4928/// be the location of the ellipsis, if any was parsed.
4929///
Reid Spencer5f016e22007-07-11 17:01:13 +00004930/// parameter-type-list: [C99 6.7.5]
4931/// parameter-list
4932/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004933/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004934///
4935/// parameter-list: [C99 6.7.5]
4936/// parameter-declaration
4937/// parameter-list ',' parameter-declaration
4938///
4939/// parameter-declaration: [C99 6.7.5]
4940/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004941/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004942/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004943/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004944/// declaration-specifiers abstract-declarator[opt]
4945/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004946/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004947/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004948/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004949///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004950void Parser::ParseParameterDeclarationClause(
4951 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004952 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004953 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004954 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004955
Chris Lattnerf97409f2008-04-06 06:57:35 +00004956 while (1) {
4957 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004958 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4959 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004960 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004961 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004962 }
Mike Stump1eb44332009-09-09 15:08:12 +00004963
Chris Lattnerf97409f2008-04-06 06:57:35 +00004964 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004965 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004966 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004967
Richard Smith6ce48a72012-04-11 04:01:28 +00004968 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004969 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00004970
John McCall7f040a92010-12-24 02:08:15 +00004971 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00004972 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00004973
4974 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004975
4976 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004977 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004978 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004979 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4980 // too much hassle.
4981 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004982
Chris Lattnere64c5492009-02-27 18:38:20 +00004983 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004984
Chris Lattnerf97409f2008-04-06 06:57:35 +00004985 // Parse the declarator. This is "PrototypeContext", because we must
4986 // accept either 'declarator' or 'abstract-declarator' here.
4987 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4988 ParseDeclarator(ParmDecl);
4989
4990 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004991 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004992
Chris Lattnerf97409f2008-04-06 06:57:35 +00004993 // Remember this parsed parameter in ParamInfo.
4994 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004995
Douglas Gregor72b505b2008-12-16 21:30:33 +00004996 // DefArgToks is used when the parsing of default arguments needs
4997 // to be delayed.
4998 CachedTokens *DefArgToks = 0;
4999
Chris Lattnerf97409f2008-04-06 06:57:35 +00005000 // If no parameter was specified, verify that *something* was specified,
5001 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005002 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5003 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005004 // Completely missing, emit error.
5005 Diag(DSStart, diag::err_missing_param);
5006 } else {
5007 // Otherwise, we have something. Add it and let semantic analysis try
5008 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005009
Chris Lattnerf97409f2008-04-06 06:57:35 +00005010 // Inform the actions module about the parameter declarator, so it gets
5011 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005012 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005013
5014 // Parse the default argument, if any. We parse the default
5015 // arguments in all dialects; the semantic analysis in
5016 // ActOnParamDefaultArgument will reject the default argument in
5017 // C.
5018 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005019 SourceLocation EqualLoc = Tok.getLocation();
5020
Chris Lattner04421082008-04-08 04:40:51 +00005021 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005022 if (D.getContext() == Declarator::MemberContext) {
5023 // If we're inside a class definition, cache the tokens
5024 // corresponding to the default argument. We'll actually parse
5025 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005026 // FIXME: Can we use a smart pointer for Toks?
5027 DefArgToks = new CachedTokens;
5028
Mike Stump1eb44332009-09-09 15:08:12 +00005029 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005030 /*StopAtSemi=*/true,
5031 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005032 delete DefArgToks;
5033 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005034 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005035 } else {
5036 // Mark the end of the default argument so that we know when to
5037 // stop when we parse it later on.
5038 Token DefArgEnd;
5039 DefArgEnd.startToken();
5040 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5041 DefArgEnd.setLocation(Tok.getLocation());
5042 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005043 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005044 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005045 }
Chris Lattner04421082008-04-08 04:40:51 +00005046 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005047 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005048 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005049
Chad Rosier8decdee2012-06-26 22:30:43 +00005050 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005051 // used.
5052 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005053 Sema::PotentiallyEvaluatedIfUsed,
5054 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005055
Sebastian Redl84407ba2012-03-14 15:54:00 +00005056 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005057 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005058 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005059 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005060 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005061 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005062 if (DefArgResult.isInvalid()) {
5063 Actions.ActOnParamDefaultArgumentError(Param);
5064 SkipUntil(tok::comma, tok::r_paren, true, true);
5065 } else {
5066 // Inform the actions module about the default argument
5067 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005068 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005069 }
Chris Lattner04421082008-04-08 04:40:51 +00005070 }
5071 }
Mike Stump1eb44332009-09-09 15:08:12 +00005072
5073 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5074 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005075 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005076 }
5077
5078 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005079 if (Tok.isNot(tok::comma)) {
5080 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005081 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005082
David Blaikie4e4d0842012-03-11 07:00:24 +00005083 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005084 // We have ellipsis without a preceding ',', which is ill-formed
5085 // in C. Complain and provide the fix.
5086 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005087 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005088 }
5089 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005090
Douglas Gregored5d6512009-09-22 21:41:40 +00005091 break;
5092 }
Mike Stump1eb44332009-09-09 15:08:12 +00005093
Chris Lattnerf97409f2008-04-06 06:57:35 +00005094 // Consume the comma.
5095 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005096 }
Mike Stump1eb44332009-09-09 15:08:12 +00005097
Chris Lattner66d28652008-04-06 06:34:08 +00005098}
Chris Lattneref4715c2008-04-06 05:45:57 +00005099
Reid Spencer5f016e22007-07-11 17:01:13 +00005100/// [C90] direct-declarator '[' constant-expression[opt] ']'
5101/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5102/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5103/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5104/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005105/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5106/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005107void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005108 if (CheckProhibitedCXX11Attribute())
5109 return;
5110
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005111 BalancedDelimiterTracker T(*this, tok::l_square);
5112 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005113
Chris Lattner378c7e42008-12-18 07:27:21 +00005114 // C array syntax has many features, but by-far the most common is [] and [4].
5115 // This code does a fast path to handle some of the most obvious cases.
5116 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005117 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005118 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005119 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005120
Chris Lattner378c7e42008-12-18 07:27:21 +00005121 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005122 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005123 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005124 T.getOpenLocation(),
5125 T.getCloseLocation()),
5126 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005127 return;
5128 } else if (Tok.getKind() == tok::numeric_constant &&
5129 GetLookAheadToken(1).is(tok::r_square)) {
5130 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005131 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005132 ConsumeToken();
5133
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005134 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005135 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005136 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005137
Chris Lattner378c7e42008-12-18 07:27:21 +00005138 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005139 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005140 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005141 T.getOpenLocation(),
5142 T.getCloseLocation()),
5143 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005144 return;
5145 }
Mike Stump1eb44332009-09-09 15:08:12 +00005146
Reid Spencer5f016e22007-07-11 17:01:13 +00005147 // If valid, this location is the position where we read the 'static' keyword.
5148 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005149 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005150 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005151
Reid Spencer5f016e22007-07-11 17:01:13 +00005152 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005153 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005154 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005155 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005156
Reid Spencer5f016e22007-07-11 17:01:13 +00005157 // If we haven't already read 'static', check to see if there is one after the
5158 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005159 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005160 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005161
Reid Spencer5f016e22007-07-11 17:01:13 +00005162 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5163 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005164 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005165
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005166 // Handle the case where we have '[*]' as the array size. However, a leading
5167 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005168 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005169 // infrequent, use of lookahead is not costly here.
5170 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005171 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005172
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005173 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005174 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005175 StaticLoc = SourceLocation(); // Drop the static.
5176 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005177 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005178 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005179 // Note, in C89, this production uses the constant-expr production instead
5180 // of assignment-expr. The only difference is that assignment-expr allows
5181 // things like '=' and '*='. Sema rejects these in C89 mode because they
5182 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005183
Douglas Gregore0762c92009-06-19 23:52:42 +00005184 // Parse the constant-expression or assignment-expression now (depending
5185 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005186 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005187 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005188 } else {
5189 EnterExpressionEvaluationContext Unevaluated(Actions,
5190 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005191 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005192 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005193 }
Mike Stump1eb44332009-09-09 15:08:12 +00005194
Reid Spencer5f016e22007-07-11 17:01:13 +00005195 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005196 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005197 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005198 // If the expression was invalid, skip it.
5199 SkipUntil(tok::r_square);
5200 return;
5201 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005202
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005203 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005204
John McCall0b7e6782011-03-24 11:26:52 +00005205 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005206 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005207
Chris Lattner378c7e42008-12-18 07:27:21 +00005208 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005209 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005210 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005211 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005212 T.getOpenLocation(),
5213 T.getCloseLocation()),
5214 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005215}
5216
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005217/// [GNU] typeof-specifier:
5218/// typeof ( expressions )
5219/// typeof ( type-name )
5220/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005221///
5222void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005223 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005224 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005225 SourceLocation StartLoc = ConsumeToken();
5226
John McCallcfb708c2010-01-13 20:03:27 +00005227 const bool hasParens = Tok.is(tok::l_paren);
5228
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005229 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5230 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005231
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005232 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005233 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005234 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005235 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5236 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005237 if (hasParens)
5238 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005239
5240 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005241 // FIXME: Not accurate, the range gets one token more than it should.
5242 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005243 else
5244 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005245
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005246 if (isCastExpr) {
5247 if (!CastTy) {
5248 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005249 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005250 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005251
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005252 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005253 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005254 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5255 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005256 DiagID, CastTy))
5257 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005258 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005259 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005260
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005261 // If we get here, the operand to the typeof was an expresion.
5262 if (Operand.isInvalid()) {
5263 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005264 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005265 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005266
Eli Friedman71b8fb52012-01-21 01:01:51 +00005267 // We might need to transform the operand if it is potentially evaluated.
5268 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5269 if (Operand.isInvalid()) {
5270 DS.SetTypeSpecError();
5271 return;
5272 }
5273
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005274 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005275 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005276 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5277 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005278 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005279 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005280}
Chris Lattner1b492422010-02-28 18:33:55 +00005281
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005282/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005283/// _Atomic ( type-name )
5284///
5285void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5286 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5287
5288 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005289 BalancedDelimiterTracker T(*this, tok::l_paren);
5290 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005291 SkipUntil(tok::r_paren);
5292 return;
5293 }
5294
5295 TypeResult Result = ParseTypeName();
5296 if (Result.isInvalid()) {
5297 SkipUntil(tok::r_paren);
5298 return;
5299 }
5300
5301 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005302 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005303
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005304 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005305 return;
5306
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005307 DS.setTypeofParensRange(T.getRange());
5308 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005309
5310 const char *PrevSpec = 0;
5311 unsigned DiagID;
5312 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5313 DiagID, Result.release()))
5314 Diag(StartLoc, DiagID) << PrevSpec;
5315}
5316
Chris Lattner1b492422010-02-28 18:33:55 +00005317
5318/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5319/// from TryAltiVecVectorToken.
5320bool Parser::TryAltiVecVectorTokenOutOfLine() {
5321 Token Next = NextToken();
5322 switch (Next.getKind()) {
5323 default: return false;
5324 case tok::kw_short:
5325 case tok::kw_long:
5326 case tok::kw_signed:
5327 case tok::kw_unsigned:
5328 case tok::kw_void:
5329 case tok::kw_char:
5330 case tok::kw_int:
5331 case tok::kw_float:
5332 case tok::kw_double:
5333 case tok::kw_bool:
5334 case tok::kw___pixel:
5335 Tok.setKind(tok::kw___vector);
5336 return true;
5337 case tok::identifier:
5338 if (Next.getIdentifierInfo() == Ident_pixel) {
5339 Tok.setKind(tok::kw___vector);
5340 return true;
5341 }
5342 return false;
5343 }
5344}
5345
5346bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5347 const char *&PrevSpec, unsigned &DiagID,
5348 bool &isInvalid) {
5349 if (Tok.getIdentifierInfo() == Ident_vector) {
5350 Token Next = NextToken();
5351 switch (Next.getKind()) {
5352 case tok::kw_short:
5353 case tok::kw_long:
5354 case tok::kw_signed:
5355 case tok::kw_unsigned:
5356 case tok::kw_void:
5357 case tok::kw_char:
5358 case tok::kw_int:
5359 case tok::kw_float:
5360 case tok::kw_double:
5361 case tok::kw_bool:
5362 case tok::kw___pixel:
5363 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5364 return true;
5365 case tok::identifier:
5366 if (Next.getIdentifierInfo() == Ident_pixel) {
5367 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5368 return true;
5369 }
5370 break;
5371 default:
5372 break;
5373 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005374 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005375 DS.isTypeAltiVecVector()) {
5376 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5377 return true;
5378 }
5379 return false;
5380}