blob: 14fd581ddcb11f561604df0832f0305767867a4b [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,
Richard Smith5cd532c2013-01-29 01:24:26 +0000465 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
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,
Richard Smith5cd532c2013-01-29 01:24:26 +0000475 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
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)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000482 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000483 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000484 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
485 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000490 // FIXME: The mapping from attribute spelling to semantics should be
491 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000492 SourceLocation Loc = Tok.getLocation();
493 switch(Tok.getKind()) {
494 // OpenCL qualifiers:
495 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000496 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000498 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000501
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000502 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000507
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000513
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000519
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000520 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000522 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000525
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000526 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000527 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000528 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000529 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000531
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000532 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000533 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000535 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000536 break;
537 default: break;
538 }
539}
540
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000541/// \brief Parse a version number.
542///
543/// version:
544/// simple-integer
545/// simple-integer ',' simple-integer
546/// simple-integer ',' simple-integer ',' simple-integer
547VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
548 Range = Tok.getLocation();
549
550 if (!Tok.is(tok::numeric_constant)) {
551 Diag(Tok, diag::err_expected_version);
552 SkipUntil(tok::comma, tok::r_paren, true, true, true);
553 return VersionTuple();
554 }
555
556 // Parse the major (and possibly minor and subminor) versions, which
557 // are stored in the numeric constant. We utilize a quirk of the
558 // lexer, which is that it handles something like 1.2.3 as a single
559 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000560 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000561 Buffer.resize(Tok.getLength()+1);
562 const char *ThisTokBegin = &Buffer[0];
563
564 // Get the spelling of the token, which eliminates trigraphs, etc.
565 bool Invalid = false;
566 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
567 if (Invalid)
568 return VersionTuple();
569
570 // Parse the major version.
571 unsigned AfterMajor = 0;
572 unsigned Major = 0;
573 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
574 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
575 ++AfterMajor;
576 }
577
578 if (AfterMajor == 0) {
579 Diag(Tok, diag::err_expected_version);
580 SkipUntil(tok::comma, tok::r_paren, true, true, true);
581 return VersionTuple();
582 }
583
584 if (AfterMajor == ActualLength) {
585 ConsumeToken();
586
587 // We only had a single version component.
588 if (Major == 0) {
589 Diag(Tok, diag::err_zero_version);
590 return VersionTuple();
591 }
592
593 return VersionTuple(Major);
594 }
595
596 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
597 Diag(Tok, diag::err_expected_version);
598 SkipUntil(tok::comma, tok::r_paren, true, true, true);
599 return VersionTuple();
600 }
601
602 // Parse the minor version.
603 unsigned AfterMinor = AfterMajor + 1;
604 unsigned Minor = 0;
605 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
606 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
607 ++AfterMinor;
608 }
609
610 if (AfterMinor == ActualLength) {
611 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000612
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000613 // We had major.minor.
614 if (Major == 0 && Minor == 0) {
615 Diag(Tok, diag::err_zero_version);
616 return VersionTuple();
617 }
618
Chad Rosier8decdee2012-06-26 22:30:43 +0000619 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000620 }
621
622 // If what follows is not a '.', we have a problem.
623 if (ThisTokBegin[AfterMinor] != '.') {
624 Diag(Tok, diag::err_expected_version);
625 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000626 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000627 }
628
629 // Parse the subminor version.
630 unsigned AfterSubminor = AfterMinor + 1;
631 unsigned Subminor = 0;
632 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
633 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
634 ++AfterSubminor;
635 }
636
637 if (AfterSubminor != ActualLength) {
638 Diag(Tok, diag::err_expected_version);
639 SkipUntil(tok::comma, tok::r_paren, true, true, true);
640 return VersionTuple();
641 }
642 ConsumeToken();
643 return VersionTuple(Major, Minor, Subminor);
644}
645
646/// \brief Parse the contents of the "availability" attribute.
647///
648/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000649/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000650///
651/// platform:
652/// identifier
653///
654/// version-arg-list:
655/// version-arg
656/// version-arg ',' version-arg-list
657///
658/// version-arg:
659/// 'introduced' '=' version
660/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000661/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000662/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000663/// opt-message:
664/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000665void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
666 SourceLocation AvailabilityLoc,
667 ParsedAttributes &attrs,
668 SourceLocation *endLoc) {
669 SourceLocation PlatformLoc;
670 IdentifierInfo *Platform = 0;
671
672 enum { Introduced, Deprecated, Obsoleted, Unknown };
673 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000674 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000675
676 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000677 BalancedDelimiterTracker T(*this, tok::l_paren);
678 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000679 Diag(Tok, diag::err_expected_lparen);
680 return;
681 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000682
683 // Parse the platform name,
684 if (Tok.isNot(tok::identifier)) {
685 Diag(Tok, diag::err_availability_expected_platform);
686 SkipUntil(tok::r_paren);
687 return;
688 }
689 Platform = Tok.getIdentifierInfo();
690 PlatformLoc = ConsumeToken();
691
692 // Parse the ',' following the platform name.
693 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
694 return;
695
696 // If we haven't grabbed the pointers for the identifiers
697 // "introduced", "deprecated", and "obsoleted", do so now.
698 if (!Ident_introduced) {
699 Ident_introduced = PP.getIdentifierInfo("introduced");
700 Ident_deprecated = PP.getIdentifierInfo("deprecated");
701 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000702 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000703 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000704 }
705
706 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000707 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000708 do {
709 if (Tok.isNot(tok::identifier)) {
710 Diag(Tok, diag::err_availability_expected_change);
711 SkipUntil(tok::r_paren);
712 return;
713 }
714 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
715 SourceLocation KeywordLoc = ConsumeToken();
716
Douglas Gregorb53e4172011-03-26 03:35:55 +0000717 if (Keyword == Ident_unavailable) {
718 if (UnavailableLoc.isValid()) {
719 Diag(KeywordLoc, diag::err_availability_redundant)
720 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000721 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000722 UnavailableLoc = KeywordLoc;
723
724 if (Tok.isNot(tok::comma))
725 break;
726
727 ConsumeToken();
728 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000729 }
730
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000731 if (Tok.isNot(tok::equal)) {
732 Diag(Tok, diag::err_expected_equal_after)
733 << Keyword;
734 SkipUntil(tok::r_paren);
735 return;
736 }
737 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000738 if (Keyword == Ident_message) {
739 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000740 Diag(Tok, diag::err_expected_string_literal)
741 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000742 SkipUntil(tok::r_paren);
743 return;
744 }
745 MessageExpr = ParseStringLiteralExpression();
746 break;
747 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000748
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000749 SourceRange VersionRange;
750 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000751
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000752 if (Version.empty()) {
753 SkipUntil(tok::r_paren);
754 return;
755 }
756
757 unsigned Index;
758 if (Keyword == Ident_introduced)
759 Index = Introduced;
760 else if (Keyword == Ident_deprecated)
761 Index = Deprecated;
762 else if (Keyword == Ident_obsoleted)
763 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000764 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000765 Index = Unknown;
766
767 if (Index < Unknown) {
768 if (!Changes[Index].KeywordLoc.isInvalid()) {
769 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000770 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000771 << SourceRange(Changes[Index].KeywordLoc,
772 Changes[Index].VersionRange.getEnd());
773 }
774
775 Changes[Index].KeywordLoc = KeywordLoc;
776 Changes[Index].Version = Version;
777 Changes[Index].VersionRange = VersionRange;
778 } else {
779 Diag(KeywordLoc, diag::err_availability_unknown_change)
780 << Keyword << VersionRange;
781 }
782
783 if (Tok.isNot(tok::comma))
784 break;
785
786 ConsumeToken();
787 } while (true);
788
789 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000790 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000791 return;
792
793 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000794 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000795
Douglas Gregorb53e4172011-03-26 03:35:55 +0000796 // The 'unavailable' availability cannot be combined with any other
797 // availability changes. Make sure that hasn't happened.
798 if (UnavailableLoc.isValid()) {
799 bool Complained = false;
800 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
801 if (Changes[Index].KeywordLoc.isValid()) {
802 if (!Complained) {
803 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
804 << SourceRange(Changes[Index].KeywordLoc,
805 Changes[Index].VersionRange.getEnd());
806 Complained = true;
807 }
808
809 // Clear out the availability.
810 Changes[Index] = AvailabilityChange();
811 }
812 }
813 }
814
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000815 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000816 attrs.addNew(&Availability,
817 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000818 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000819 Platform, PlatformLoc,
820 Changes[Introduced],
821 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000822 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000823 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000824 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000825}
826
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827
Bill Wendlingad017fa2012-12-20 19:22:21 +0000828// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000829// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
830
831void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
832
833void Parser::LateParsedClass::ParseLexedAttributes() {
834 Self->ParseLexedAttributes(*Class);
835}
836
837void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000838 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000839}
840
841/// Wrapper class which calls ParseLexedAttribute, after setting up the
842/// scope appropriately.
843void Parser::ParseLexedAttributes(ParsingClass &Class) {
844 // Deal with templates
845 // FIXME: Test cases to make sure this does the right thing for templates.
846 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
847 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
848 HasTemplateScope);
849 if (HasTemplateScope)
850 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
851
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000852 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000853 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000854 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000855 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
856 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
857
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000858 // Enter the scope of nested classes
859 if (!AlreadyHasClassScope)
860 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000862 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000863 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
864 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
865 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000866 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000867
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000868 if (!AlreadyHasClassScope)
869 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
870 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000871}
872
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000873
874/// \brief Parse all attributes in LAs, and attach them to Decl D.
875void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
876 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000877 assert(LAs.parseSoon() &&
878 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000879 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000880 if (D)
881 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000883 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000884 }
885 LAs.clear();
886}
887
888
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000889/// \brief Finish parsing an attribute for which parsing was delayed.
890/// This will be called at the end of parsing a class declaration
891/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000892/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000894void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
895 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000896 // Save the current token position.
897 SourceLocation OrigLoc = Tok.getLocation();
898
899 // Append the current token at the end of the new token stream so that it
900 // doesn't get lost.
901 LA.Toks.push_back(Tok);
902 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
903 // Consume the previously pushed token.
904 ConsumeAnyToken();
905
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000906 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000907 // FIXME: Do not warn on C++11 attributes, once we start supporting
908 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000909 Diag(Tok, diag::warn_attribute_on_function_definition)
910 << LA.AttrName.getName();
911 }
912
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000913 ParsedAttributes Attrs(AttrFactory);
914 SourceLocation endLoc;
915
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000916 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000917 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000918 NamedDecl *ND = dyn_cast<NamedDecl>(D);
919 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000920
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000921 // Allow 'this' within late-parsed attributes.
922 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
923 /*TypeQuals=*/0,
924 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000925
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000926 if (LA.Decls.size() == 1) {
927 // If the Decl is templatized, add template parameters to scope.
928 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
929 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
930 if (HasTemplateScope)
931 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000932
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000933 // If the Decl is on a function, add function parameters to the scope.
934 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
935 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
936 if (HasFunScope)
937 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000938
Michael Han6880f492012-10-03 01:56:22 +0000939 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000940 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000941
942 if (HasFunScope) {
943 Actions.ActOnExitFunctionContext();
944 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
945 }
946 if (HasTemplateScope) {
947 TempScope.Exit();
948 }
949 } else {
950 // If there are multiple decls, then the decl cannot be within the
951 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000952 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000953 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000954 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000955 } else {
956 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000957 }
958
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000959 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
960 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
961 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000962
963 if (Tok.getLocation() != OrigLoc) {
964 // Due to a parsing error, we either went over the cached tokens or
965 // there are still cached tokens left, so we skip the leftover tokens.
966 // Since this is an uncommon situation that should be avoided, use the
967 // expensive isBeforeInTranslationUnit call.
968 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
969 OrigLoc))
970 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000971 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000972 }
973}
974
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000975/// \brief Wrapper around a case statement checking if AttrName is
976/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000977bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000978 return llvm::StringSwitch<bool>(AttrName)
979 .Case("guarded_by", true)
980 .Case("guarded_var", true)
981 .Case("pt_guarded_by", true)
982 .Case("pt_guarded_var", true)
983 .Case("lockable", true)
984 .Case("scoped_lockable", true)
985 .Case("no_thread_safety_analysis", true)
986 .Case("acquired_after", true)
987 .Case("acquired_before", true)
988 .Case("exclusive_lock_function", true)
989 .Case("shared_lock_function", true)
990 .Case("exclusive_trylock_function", true)
991 .Case("shared_trylock_function", true)
992 .Case("unlock_function", true)
993 .Case("lock_returned", true)
994 .Case("locks_excluded", true)
995 .Case("exclusive_locks_required", true)
996 .Case("shared_locks_required", true)
997 .Default(false);
998}
999
1000/// \brief Parse the contents of thread safety attributes. These
1001/// should always be parsed as an expression list.
1002///
1003/// We need to special case the parsing due to the fact that if the first token
1004/// of the first argument is an identifier, the main parse loop will store
1005/// that token as a "parameter" and the rest of
1006/// the arguments will be added to a list of "arguments". However,
1007/// subsequent tokens in the first argument are lost. We instead parse each
1008/// argument as an expression and add all arguments to the list of "arguments".
1009/// In future, we will take advantage of this special case to also
1010/// deal with some argument scoping issues here (for example, referring to a
1011/// function parameter in the attribute on that function).
1012void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1013 SourceLocation AttrNameLoc,
1014 ParsedAttributes &Attrs,
1015 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001016 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001017
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001018 BalancedDelimiterTracker T(*this, tok::l_paren);
1019 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001020
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001021 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001022 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001023
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001024 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001025 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001026 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001027 ExprResult ArgExpr(ParseAssignmentExpression());
1028 if (ArgExpr.isInvalid()) {
1029 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001030 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001031 break;
1032 } else {
1033 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001034 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001035 if (Tok.isNot(tok::comma))
1036 break;
1037 ConsumeToken(); // Eat the comma, move to the next argument
1038 }
1039 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001040 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001041 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001042 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001043 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001044 if (EndLoc)
1045 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001046}
1047
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001048void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1049 SourceLocation AttrNameLoc,
1050 ParsedAttributes &Attrs,
1051 SourceLocation *EndLoc) {
1052 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1053
1054 BalancedDelimiterTracker T(*this, tok::l_paren);
1055 T.consumeOpen();
1056
1057 if (Tok.isNot(tok::identifier)) {
1058 Diag(Tok, diag::err_expected_ident);
1059 T.skipToEnd();
1060 return;
1061 }
1062 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1063 SourceLocation ArgumentKindLoc = ConsumeToken();
1064
1065 if (Tok.isNot(tok::comma)) {
1066 Diag(Tok, diag::err_expected_comma);
1067 T.skipToEnd();
1068 return;
1069 }
1070 ConsumeToken();
1071
1072 SourceRange MatchingCTypeRange;
1073 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1074 if (MatchingCType.isInvalid()) {
1075 T.skipToEnd();
1076 return;
1077 }
1078
1079 bool LayoutCompatible = false;
1080 bool MustBeNull = false;
1081 while (Tok.is(tok::comma)) {
1082 ConsumeToken();
1083 if (Tok.isNot(tok::identifier)) {
1084 Diag(Tok, diag::err_expected_ident);
1085 T.skipToEnd();
1086 return;
1087 }
1088 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1089 if (Flag->isStr("layout_compatible"))
1090 LayoutCompatible = true;
1091 else if (Flag->isStr("must_be_null"))
1092 MustBeNull = true;
1093 else {
1094 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1095 T.skipToEnd();
1096 return;
1097 }
1098 ConsumeToken(); // consume flag
1099 }
1100
1101 if (!T.consumeClose()) {
1102 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1103 ArgumentKind, ArgumentKindLoc,
1104 MatchingCType.release(), LayoutCompatible,
1105 MustBeNull, AttributeList::AS_GNU);
1106 }
1107
1108 if (EndLoc)
1109 *EndLoc = T.getCloseLocation();
1110}
1111
Richard Smith6ee326a2012-04-10 01:32:12 +00001112/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1113/// of a C++11 attribute-specifier in a location where an attribute is not
1114/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1115/// situation.
1116///
1117/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1118/// this doesn't appear to actually be an attribute-specifier, and the caller
1119/// should try to parse it.
1120bool Parser::DiagnoseProhibitedCXX11Attribute() {
1121 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1122
1123 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1124 case CAK_NotAttributeSpecifier:
1125 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1126 return false;
1127
1128 case CAK_InvalidAttributeSpecifier:
1129 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1130 return false;
1131
1132 case CAK_AttributeSpecifier:
1133 // Parse and discard the attributes.
1134 SourceLocation BeginLoc = ConsumeBracket();
1135 ConsumeBracket();
1136 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1137 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1138 SourceLocation EndLoc = ConsumeBracket();
1139 Diag(BeginLoc, diag::err_attributes_not_allowed)
1140 << SourceRange(BeginLoc, EndLoc);
1141 return true;
1142 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001143 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001144}
1145
John McCall7f040a92010-12-24 02:08:15 +00001146void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1147 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1148 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001149}
1150
Michael Hanf64231e2012-11-06 19:34:54 +00001151void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1152 AttributeList *AttrList = attrs.getList();
1153 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001154 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001155 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001156 << AttrList->getName();
1157 AttrList->setInvalid();
1158 }
1159 AttrList = AttrList->getNext();
1160 }
1161}
1162
Reid Spencer5f016e22007-07-11 17:01:13 +00001163/// ParseDeclaration - Parse a full 'declaration', which consists of
1164/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001165/// 'Context' should be a Declarator::TheContext value. This returns the
1166/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001167///
1168/// declaration: [C99 6.7]
1169/// block-declaration ->
1170/// simple-declaration
1171/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001172/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001173/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001174/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001175/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001176/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001177/// others... [FIXME]
1178///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001179Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1180 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001181 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001182 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001183 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001184 // Must temporarily exit the objective-c container scope for
1185 // parsing c none objective-c decls.
1186 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001187
John McCalld226f652010-08-21 09:40:31 +00001188 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001189 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001190 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001191 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001192 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001193 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001194 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001195 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001196 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001197 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001198 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001199 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001200 SourceLocation InlineLoc = ConsumeToken();
1201 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1202 break;
1203 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001204 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001205 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001206 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001207 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001208 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001209 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001210 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001211 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001212 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001213 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001214 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001215 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001216 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001217 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001218 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001219 default:
John McCall7f040a92010-12-24 02:08:15 +00001220 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001221 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001222
Chris Lattner682bf922009-03-29 16:50:03 +00001223 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001224 // single decl, convert it now. Alias declarations can also declare a type;
1225 // include that too if it is present.
1226 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001227}
1228
1229/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1230/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001231/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1232/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001233///[C90/C++]init-declarator-list ';' [TODO]
1234/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001235///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001236/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001237/// attribute-specifier-seq[opt] type-specifier-seq declarator
1238///
Chris Lattnercd147752009-03-29 17:27:48 +00001239/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001240/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001241///
1242/// If FRI is non-null, we might be parsing a for-range-declaration instead
1243/// of a simple-declaration. If we find that we are, we also parse the
1244/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001245Parser::DeclGroupPtrTy
1246Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1247 SourceLocation &DeclEnd,
1248 ParsedAttributesWithRange &attrs,
1249 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001251 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001252 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001253
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001254 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001255 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001256
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1258 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001259 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001260 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001261 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001262 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001263 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001264 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001265 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001267
1268 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001269}
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Richard Smith0706df42011-10-19 21:33:05 +00001271/// Returns true if this might be the start of a declarator, or a common typo
1272/// for a declarator.
1273bool Parser::MightBeDeclarator(unsigned Context) {
1274 switch (Tok.getKind()) {
1275 case tok::annot_cxxscope:
1276 case tok::annot_template_id:
1277 case tok::caret:
1278 case tok::code_completion:
1279 case tok::coloncolon:
1280 case tok::ellipsis:
1281 case tok::kw___attribute:
1282 case tok::kw_operator:
1283 case tok::l_paren:
1284 case tok::star:
1285 return true;
1286
1287 case tok::amp:
1288 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001289 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001290
Richard Smith1c94c162012-01-09 22:31:44 +00001291 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001292 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001293 NextToken().is(tok::l_square);
1294
1295 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001296 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001297
Richard Smith0706df42011-10-19 21:33:05 +00001298 case tok::identifier:
1299 switch (NextToken().getKind()) {
1300 case tok::code_completion:
1301 case tok::coloncolon:
1302 case tok::comma:
1303 case tok::equal:
1304 case tok::equalequal: // Might be a typo for '='.
1305 case tok::kw_alignas:
1306 case tok::kw_asm:
1307 case tok::kw___attribute:
1308 case tok::l_brace:
1309 case tok::l_paren:
1310 case tok::l_square:
1311 case tok::less:
1312 case tok::r_brace:
1313 case tok::r_paren:
1314 case tok::r_square:
1315 case tok::semi:
1316 return true;
1317
1318 case tok::colon:
1319 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001320 // and in block scope it's probably a label. Inside a class definition,
1321 // this is a bit-field.
1322 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001323 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001324
1325 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001326 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001327
1328 default:
1329 return false;
1330 }
1331
1332 default:
1333 return false;
1334 }
1335}
1336
Richard Smith994d73f2012-04-11 20:59:20 +00001337/// Skip until we reach something which seems like a sensible place to pick
1338/// up parsing after a malformed declaration. This will sometimes stop sooner
1339/// than SkipUntil(tok::r_brace) would, but will never stop later.
1340void Parser::SkipMalformedDecl() {
1341 while (true) {
1342 switch (Tok.getKind()) {
1343 case tok::l_brace:
1344 // Skip until matching }, then stop. We've probably skipped over
1345 // a malformed class or function definition or similar.
1346 ConsumeBrace();
1347 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1348 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1349 // This declaration isn't over yet. Keep skipping.
1350 continue;
1351 }
1352 if (Tok.is(tok::semi))
1353 ConsumeToken();
1354 return;
1355
1356 case tok::l_square:
1357 ConsumeBracket();
1358 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1359 continue;
1360
1361 case tok::l_paren:
1362 ConsumeParen();
1363 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1364 continue;
1365
1366 case tok::r_brace:
1367 return;
1368
1369 case tok::semi:
1370 ConsumeToken();
1371 return;
1372
1373 case tok::kw_inline:
1374 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001375 // a good place to pick back up parsing, except in an Objective-C
1376 // @interface context.
1377 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1378 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001379 return;
1380 break;
1381
1382 case tok::kw_namespace:
1383 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001384 // place to pick back up parsing, except in an Objective-C
1385 // @interface context.
1386 if (Tok.isAtStartOfLine() &&
1387 (!ParsingInObjCContainer || CurParsedObjCImpl))
1388 return;
1389 break;
1390
1391 case tok::at:
1392 // @end is very much like } in Objective-C contexts.
1393 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1394 ParsingInObjCContainer)
1395 return;
1396 break;
1397
1398 case tok::minus:
1399 case tok::plus:
1400 // - and + probably start new method declarations in Objective-C contexts.
1401 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001402 return;
1403 break;
1404
1405 case tok::eof:
1406 return;
1407
1408 default:
1409 break;
1410 }
1411
1412 ConsumeAnyToken();
1413 }
1414}
1415
John McCalld8ac0572009-11-03 19:26:08 +00001416/// ParseDeclGroup - Having concluded that this is either a function
1417/// definition or a group of object declarations, actually parse the
1418/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001419Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1420 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001421 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001422 SourceLocation *DeclEnd,
1423 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001424 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001425 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001426 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001427
John McCalld8ac0572009-11-03 19:26:08 +00001428 // Bail out if the first declarator didn't seem well-formed.
1429 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001430 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001431 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001434 // Save late-parsed attributes for now; they need to be parsed in the
1435 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001436 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1437 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001438 if (D.isFunctionDeclarator())
1439 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1440
Chris Lattnerc82daef2010-07-11 22:24:20 +00001441 // Check to see if we have a function *definition* which must have a body.
1442 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1443 // Look at the next token to make sure that this isn't a function
1444 // declaration. We have to check this because __attribute__ might be the
1445 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001446 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001447
Chris Lattner004659a2010-07-11 22:42:07 +00001448 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001449 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1450 Diag(Tok, diag::err_function_declared_typedef);
1451
1452 // Recover by treating the 'typedef' as spurious.
1453 DS.ClearStorageClassSpecs();
1454 }
1455
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001456 Decl *TheDecl =
1457 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001458 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001459 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001460
Chris Lattner004659a2010-07-11 22:42:07 +00001461 if (isDeclarationSpecifier()) {
1462 // If there is an invalid declaration specifier right after the function
1463 // prototype, then we must be in a missing semicolon case where this isn't
1464 // actually a body. Just fall through into the code that handles it as a
1465 // prototype, and let the top-level code handle the erroneous declspec
1466 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001467 } else {
1468 Diag(Tok, diag::err_expected_fn_body);
1469 SkipUntil(tok::semi);
1470 return DeclGroupPtrTy();
1471 }
1472 }
1473
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001474 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001475 return DeclGroupPtrTy();
1476
1477 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1478 // must parse and analyze the for-range-initializer before the declaration is
1479 // analyzed.
1480 if (FRI && Tok.is(tok::colon)) {
1481 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001482 if (Tok.is(tok::l_brace))
1483 FRI->RangeExpr = ParseBraceInitializer();
1484 else
1485 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001486 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1487 Actions.ActOnCXXForRangeDecl(ThisDecl);
1488 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001489 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001490 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1491 }
1492
Chris Lattner5f9e2722011-07-23 10:55:15 +00001493 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001494 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001495 if (LateParsedAttrs.size() > 0)
1496 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001497 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001498 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001499 DeclsInGroup.push_back(FirstDecl);
1500
Richard Smith0706df42011-10-19 21:33:05 +00001501 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001502
John McCalld8ac0572009-11-03 19:26:08 +00001503 // If we don't have a comma, it is either the end of the list (a ';') or an
1504 // error, bail out.
1505 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001506 SourceLocation CommaLoc = ConsumeToken();
1507
1508 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1509 // This comma was followed by a line-break and something which can't be
1510 // the start of a declarator. The comma was probably a typo for a
1511 // semicolon.
1512 Diag(CommaLoc, diag::err_expected_semi_declaration)
1513 << FixItHint::CreateReplacement(CommaLoc, ";");
1514 ExpectSemi = false;
1515 break;
1516 }
John McCalld8ac0572009-11-03 19:26:08 +00001517
1518 // Parse the next declarator.
1519 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001520 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001521
1522 // Accept attributes in an init-declarator. In the first declarator in a
1523 // declaration, these would be part of the declspec. In subsequent
1524 // declarators, they become part of the declarator itself, so that they
1525 // don't apply to declarators after *this* one. Examples:
1526 // short __attribute__((common)) var; -> declspec
1527 // short var __attribute__((common)); -> declarator
1528 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001529 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001530
1531 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001532 if (!D.isInvalidType()) {
1533 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1534 D.complete(ThisDecl);
1535 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001536 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001537 }
John McCalld8ac0572009-11-03 19:26:08 +00001538 }
1539
1540 if (DeclEnd)
1541 *DeclEnd = Tok.getLocation();
1542
Richard Smith0706df42011-10-19 21:33:05 +00001543 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001544 ExpectAndConsumeSemi(Context == Declarator::FileContext
1545 ? diag::err_invalid_token_after_toplevel_declarator
1546 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001547 // Okay, there was no semicolon and one was expected. If we see a
1548 // declaration specifier, just assume it was missing and continue parsing.
1549 // Otherwise things are very confused and we skip to recover.
1550 if (!isDeclarationSpecifier()) {
1551 SkipUntil(tok::r_brace, true, true);
1552 if (Tok.is(tok::semi))
1553 ConsumeToken();
1554 }
John McCalld8ac0572009-11-03 19:26:08 +00001555 }
1556
Douglas Gregor23c94db2010-07-02 17:43:08 +00001557 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001558 DeclsInGroup.data(),
1559 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001560}
1561
Richard Smithad762fc2011-04-14 22:09:26 +00001562/// Parse an optional simple-asm-expr and attributes, and attach them to a
1563/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001564bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001565 // If a simple-asm-expr is present, parse it.
1566 if (Tok.is(tok::kw_asm)) {
1567 SourceLocation Loc;
1568 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1569 if (AsmLabel.isInvalid()) {
1570 SkipUntil(tok::semi, true, true);
1571 return true;
1572 }
1573
1574 D.setAsmLabel(AsmLabel.release());
1575 D.SetRangeEnd(Loc);
1576 }
1577
1578 MaybeParseGNUAttributes(D);
1579 return false;
1580}
1581
Douglas Gregor1426e532009-05-12 21:31:51 +00001582/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1583/// declarator'. This method parses the remainder of the declaration
1584/// (including any attributes or initializer, among other things) and
1585/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001586///
Reid Spencer5f016e22007-07-11 17:01:13 +00001587/// init-declarator: [C99 6.7]
1588/// declarator
1589/// declarator '=' initializer
1590/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1591/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001592/// [C++] declarator initializer[opt]
1593///
1594/// [C++] initializer:
1595/// [C++] '=' initializer-clause
1596/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001597/// [C++0x] '=' 'default' [TODO]
1598/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001599/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001600///
1601/// According to the standard grammar, =default and =delete are function
1602/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001603///
John McCalld226f652010-08-21 09:40:31 +00001604Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001605 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001606 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001607 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Richard Smithad762fc2011-04-14 22:09:26 +00001609 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1610}
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Richard Smithad762fc2011-04-14 22:09:26 +00001612Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1613 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001614 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001615 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001616 switch (TemplateInfo.Kind) {
1617 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001619 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001620
Douglas Gregord5a423b2009-09-25 18:43:00 +00001621 case ParsedTemplateInfo::Template:
1622 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001623 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001624 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001625 D);
1626 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001627
Douglas Gregord5a423b2009-09-25 18:43:00 +00001628 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001629 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001630 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001631 TemplateInfo.ExternLoc,
1632 TemplateInfo.TemplateLoc,
1633 D);
1634 if (ThisRes.isInvalid()) {
1635 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001636 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001637 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001638
Douglas Gregord5a423b2009-09-25 18:43:00 +00001639 ThisDecl = ThisRes.get();
1640 break;
1641 }
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Richard Smith34b41d92011-02-20 03:19:35 +00001644 bool TypeContainsAuto =
1645 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1646
Douglas Gregor1426e532009-05-12 21:31:51 +00001647 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001648 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001649 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001650 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001651 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001652 if (D.isFunctionDeclarator())
1653 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1654 << 1 /* delete */;
1655 else
1656 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001657 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001658 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001659 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1660 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001661 else
1662 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001663 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001664 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001665 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001666 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001667 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001668
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001669 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001670 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001671 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001672 cutOffParsing();
1673 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001674 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001675
John McCall60d7b3a2010-08-24 06:29:42 +00001676 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001677
David Blaikie4e4d0842012-03-11 07:00:24 +00001678 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001679 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001680 ExitScope();
1681 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001682
Douglas Gregor1426e532009-05-12 21:31:51 +00001683 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001684 SkipUntil(tok::comma, true, true);
1685 Actions.ActOnInitializerError(ThisDecl);
1686 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001687 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1688 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001689 }
1690 } else if (Tok.is(tok::l_paren)) {
1691 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001692 BalancedDelimiterTracker T(*this, tok::l_paren);
1693 T.consumeOpen();
1694
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001695 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001696 CommaLocsTy CommaLocs;
1697
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001699 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001700 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001701 }
1702
Douglas Gregor1426e532009-05-12 21:31:51 +00001703 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001704 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001705 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001706
David Blaikie4e4d0842012-03-11 07:00:24 +00001707 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001708 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001709 ExitScope();
1710 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001711 } else {
1712 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001713 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001714
1715 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1716 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001717
David Blaikie4e4d0842012-03-11 07:00:24 +00001718 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001719 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001720 ExitScope();
1721 }
1722
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001723 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1724 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001725 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001726 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1727 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001728 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001729 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001730 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001731 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001732 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1733
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001734 if (D.getCXXScopeSpec().isSet()) {
1735 EnterScope(0);
1736 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1737 }
1738
1739 ExprResult Init(ParseBraceInitializer());
1740
1741 if (D.getCXXScopeSpec().isSet()) {
1742 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1743 ExitScope();
1744 }
1745
1746 if (Init.isInvalid()) {
1747 Actions.ActOnInitializerError(ThisDecl);
1748 } else
1749 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1750 /*DirectInit=*/true, TypeContainsAuto);
1751
Douglas Gregor1426e532009-05-12 21:31:51 +00001752 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001753 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001754 }
1755
Richard Smith483b9f32011-02-21 20:05:19 +00001756 Actions.FinalizeDeclaration(ThisDecl);
1757
Douglas Gregor1426e532009-05-12 21:31:51 +00001758 return ThisDecl;
1759}
1760
Reid Spencer5f016e22007-07-11 17:01:13 +00001761/// ParseSpecifierQualifierList
1762/// specifier-qualifier-list:
1763/// type-specifier specifier-qualifier-list[opt]
1764/// type-qualifier specifier-qualifier-list[opt]
1765/// [GNU] attributes specifier-qualifier-list[opt]
1766///
Richard Smith69730c12012-03-12 07:56:15 +00001767void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1768 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1770 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001771 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001772 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 // Validate declspec for type-name.
1775 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001776 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1777 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001778 Diag(Tok, diag::err_expected_type);
1779 DS.SetTypeSpecError();
1780 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1781 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001783 if (!DS.hasTypeSpecifier())
1784 DS.SetTypeSpecError();
1785 }
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 // Issue diagnostic and remove storage class if present.
1788 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1789 if (DS.getStorageClassSpecLoc().isValid())
1790 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1791 else
1792 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1793 DS.ClearStorageClassSpecs();
1794 }
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 // Issue diagnostic and remove function specfier if present.
1797 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001798 if (DS.isInlineSpecified())
1799 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1800 if (DS.isVirtualSpecified())
1801 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1802 if (DS.isExplicitSpecified())
1803 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001804 DS.ClearFunctionSpecs();
1805 }
Richard Smith69730c12012-03-12 07:56:15 +00001806
1807 // Issue diagnostic and remove constexpr specfier if present.
1808 if (DS.isConstexprSpecified()) {
1809 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1810 DS.ClearConstexprSpec();
1811 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001812}
1813
Chris Lattnerc199ab32009-04-12 20:42:31 +00001814/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1815/// specified token is valid after the identifier in a declarator which
1816/// immediately follows the declspec. For example, these things are valid:
1817///
1818/// int x [ 4]; // direct-declarator
1819/// int x ( int y); // direct-declarator
1820/// int(int x ) // direct-declarator
1821/// int x ; // simple-declaration
1822/// int x = 17; // init-declarator-list
1823/// int x , y; // init-declarator-list
1824/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001825/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001826/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001827///
1828/// This is not, because 'x' does not immediately follow the declspec (though
1829/// ')' happens to be valid anyway).
1830/// int (x)
1831///
1832static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1833 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1834 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001835 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001836}
1837
Chris Lattnere40c2952009-04-14 21:34:55 +00001838
1839/// ParseImplicitInt - This method is called when we have an non-typename
1840/// identifier in a declspec (which normally terminates the decl spec) when
1841/// the declspec has no type specifier. In this case, the declspec is either
1842/// malformed or is "implicit int" (in K&R and C89).
1843///
1844/// This method handles diagnosing this prettily and returns false if the
1845/// declspec is done being processed. If it recovers and thinks there may be
1846/// other pieces of declspec after it, it returns true.
1847///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001848bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001849 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001850 AccessSpecifier AS, DeclSpecContext DSC,
1851 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001852 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Chris Lattnere40c2952009-04-14 21:34:55 +00001854 SourceLocation Loc = Tok.getLocation();
1855 // If we see an identifier that is not a type name, we normally would
1856 // parse it as the identifer being declared. However, when a typename
1857 // is typo'd or the definition is not included, this will incorrectly
1858 // parse the typename as the identifier name and fall over misparsing
1859 // later parts of the diagnostic.
1860 //
1861 // As such, we try to do some look-ahead in cases where this would
1862 // otherwise be an "implicit-int" case to see if this is invalid. For
1863 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1864 // an identifier with implicit int, we'd get a parse error because the
1865 // next token is obviously invalid for a type. Parse these as a case
1866 // with an invalid type specifier.
1867 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Chris Lattnere40c2952009-04-14 21:34:55 +00001869 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001870 // error, do lookahead to try to do better recovery. This never applies
1871 // within a type specifier. Outside of C++, we allow this even if the
1872 // language doesn't "officially" support implicit int -- we support
1873 // implicit int as an extension in C99 and C11. Allegedly, MS also
1874 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001875 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001876 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001877 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001878 // If this token is valid for implicit int, e.g. "static x = 4", then
1879 // we just avoid eating the identifier, so it will be parsed as the
1880 // identifier in the declarator.
1881 return false;
1882 }
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Richard Smith827adaf2012-05-15 21:01:51 +00001884 if (getLangOpts().CPlusPlus &&
1885 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1886 // Don't require a type specifier if we have the 'auto' storage class
1887 // specifier in C++98 -- we'll promote it to a type specifier.
1888 return false;
1889 }
1890
Chris Lattnere40c2952009-04-14 21:34:55 +00001891 // Otherwise, if we don't consume this token, we are going to emit an
1892 // error anyway. Try to recover from various common problems. Check
1893 // to see if this was a reference to a tag name without a tag specified.
1894 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001895 //
1896 // C++ doesn't need this, and isTagName doesn't take SS.
1897 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001898 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001899 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Douglas Gregor23c94db2010-07-02 17:43:08 +00001901 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001902 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001903 case DeclSpec::TST_enum:
1904 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1905 case DeclSpec::TST_union:
1906 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1907 case DeclSpec::TST_struct:
1908 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001909 case DeclSpec::TST_interface:
1910 TagName="__interface"; FixitTagName = "__interface ";
1911 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001912 case DeclSpec::TST_class:
1913 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001914 }
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Chris Lattnerf4382f52009-04-14 22:17:06 +00001916 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001917 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1918 LookupResult R(Actions, TokenName, SourceLocation(),
1919 Sema::LookupOrdinaryName);
1920
Chris Lattnerf4382f52009-04-14 22:17:06 +00001921 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001922 << TokenName << TagName << getLangOpts().CPlusPlus
1923 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1924
1925 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1926 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1927 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001928 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001929 << TokenName << TagName;
1930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Chris Lattnerf4382f52009-04-14 22:17:06 +00001932 // Parse this as a tag as if the missing tag were present.
1933 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001934 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001935 else
Richard Smith69730c12012-03-12 07:56:15 +00001936 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001937 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001938 return true;
1939 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001940 }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Richard Smith8f0a7e72012-05-15 21:29:55 +00001942 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001943 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001944 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1945 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001946 // Look ahead to the next token to try to figure out what this declaration
1947 // was supposed to be.
1948 switch (NextToken().getKind()) {
1949 case tok::comma:
1950 case tok::equal:
1951 case tok::kw_asm:
1952 case tok::l_brace:
1953 case tok::l_square:
1954 case tok::semi:
1955 // This looks like a variable declaration. The type is probably missing.
1956 // We're done parsing decl-specifiers.
1957 return false;
1958
1959 case tok::l_paren: {
1960 // static x(4); // 'x' is not a type
1961 // x(int n); // 'x' is not a type
1962 // x (*p)[]; // 'x' is a type
1963 //
1964 // Since we're in an error case (or the rare 'implicit int in C++' MS
1965 // extension), we can afford to perform a tentative parse to determine
1966 // which case we're in.
1967 TentativeParsingAction PA(*this);
1968 ConsumeToken();
1969 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1970 PA.Revert();
1971 if (TPR == TPResult::False())
1972 return false;
1973 // The identifier is followed by a parenthesized declarator.
1974 // It's supposed to be a type.
1975 break;
1976 }
1977
1978 default:
1979 // This is probably supposed to be a type. This includes cases like:
1980 // int f(itn);
1981 // struct S { unsinged : 4; };
1982 break;
1983 }
1984 }
1985
Chad Rosier8decdee2012-06-26 22:30:43 +00001986 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001987 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001988 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001989 IdentifierInfo *II = Tok.getIdentifierInfo();
1990 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001991 // The action emitted a diagnostic, so we don't have to.
1992 if (T) {
1993 // The action has suggested that the type T could be used. Set that as
1994 // the type in the declaration specifiers, consume the would-be type
1995 // name token, and we're done.
1996 const char *PrevSpec;
1997 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001998 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001999 DS.SetRangeEnd(Tok.getLocation());
2000 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002001 // There may be other declaration specifiers after this.
2002 return true;
2003 } else if (II != Tok.getIdentifierInfo()) {
2004 // If no type was suggested, the correction is to a keyword
2005 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002006 // There may be other declaration specifiers after this.
2007 return true;
2008 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002009
Douglas Gregora786fdb2009-10-13 23:27:22 +00002010 // Fall through; the action had no suggestion for us.
2011 } else {
2012 // The action did not emit a diagnostic, so emit one now.
2013 SourceRange R;
2014 if (SS) R = SS->getRange();
2015 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Douglas Gregora786fdb2009-10-13 23:27:22 +00002018 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002019 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002020 DS.SetRangeEnd(Tok.getLocation());
2021 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Chris Lattnere40c2952009-04-14 21:34:55 +00002023 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2024 // avoid rippling error messages on subsequent uses of the same type,
2025 // could be useful if #include was forgotten.
2026 return false;
2027}
2028
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002029/// \brief Determine the declaration specifier context from the declarator
2030/// context.
2031///
2032/// \param Context the declarator context, which is one of the
2033/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002034Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002035Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2036 if (Context == Declarator::MemberContext)
2037 return DSC_class;
2038 if (Context == Declarator::FileContext)
2039 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002040 if (Context == Declarator::TrailingReturnContext)
2041 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002042 return DSC_normal;
2043}
2044
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002045/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2046///
2047/// FIXME: Simply returns an alignof() expression if the argument is a
2048/// type. Ideally, the type should be propagated directly into Sema.
2049///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002050/// [C11] type-id
2051/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002052/// [C++0x] type-id ...[opt]
2053/// [C++0x] assignment-expression ...[opt]
2054ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2055 SourceLocation &EllipsisLoc) {
2056 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002057 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002058 SourceLocation TypeLoc = Tok.getLocation();
2059 ParsedType Ty = ParseTypeName().get();
2060 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002061 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2062 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002063 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002064 ER = ParseConstantExpression();
2065
Richard Smith80ad52f2013-01-02 11:42:31 +00002066 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002067 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002068
2069 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002070}
2071
2072/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2073/// attribute to Attrs.
2074///
2075/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002076/// [C11] '_Alignas' '(' type-id ')'
2077/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002078/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2079/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002080void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2081 SourceLocation *endLoc) {
2082 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2083 "Not an alignment-specifier!");
2084
Richard Smith33f04a22013-01-29 01:48:07 +00002085 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2086 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002087
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002088 BalancedDelimiterTracker T(*this, tok::l_paren);
2089 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002090 return;
2091
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002092 SourceLocation EllipsisLoc;
2093 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002094 if (ArgExpr.isInvalid()) {
2095 SkipUntil(tok::r_paren);
2096 return;
2097 }
2098
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002099 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002100 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002101 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002102
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002103 // FIXME: Handle pack-expansions here.
2104 if (EllipsisLoc.isValid()) {
2105 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2106 return;
2107 }
2108
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002109 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002110 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002111 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2112 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002113}
2114
Reid Spencer5f016e22007-07-11 17:01:13 +00002115/// ParseDeclarationSpecifiers
2116/// declaration-specifiers: [C99 6.7]
2117/// storage-class-specifier declaration-specifiers[opt]
2118/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002119/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002120/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002121/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002122/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002123///
2124/// storage-class-specifier: [C99 6.7.1]
2125/// 'typedef'
2126/// 'extern'
2127/// 'static'
2128/// 'auto'
2129/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002130/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002131/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002132/// function-specifier: [C99 6.7.4]
2133/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002134/// [C++] 'virtual'
2135/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002136/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002137/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002138/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002139
Reid Spencer5f016e22007-07-11 17:01:13 +00002140///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002141void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002142 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002143 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002144 DeclSpecContext DSContext,
2145 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002146 if (DS.getSourceRange().isInvalid()) {
2147 DS.SetRangeStart(Tok.getLocation());
2148 DS.SetRangeEnd(Tok.getLocation());
2149 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002150
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002151 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002152 bool AttrsLastTime = false;
2153 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002155 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002157 unsigned DiagID = 0;
2158
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002160
Reid Spencer5f016e22007-07-11 17:01:13 +00002161 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002162 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002163 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002164 if (!AttrsLastTime)
2165 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002166 else {
2167 // Reject C++11 attributes that appertain to decl specifiers as
2168 // we don't support any C++11 attributes that appertain to decl
2169 // specifiers. This also conforms to what g++ 4.8 is doing.
2170 ProhibitCXX11Attributes(attrs);
2171
Sean Hunt2edf0a22012-06-23 05:07:58 +00002172 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002173 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002174
Reid Spencer5f016e22007-07-11 17:01:13 +00002175 // If this is not a declaration specifier token, we're done reading decl
2176 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002177 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Sean Hunt2edf0a22012-06-23 05:07:58 +00002180 case tok::l_square:
2181 case tok::kw_alignas:
2182 if (!isCXX11AttributeSpecifier())
2183 goto DoneWithDeclSpec;
2184
2185 ProhibitAttributes(attrs);
2186 // FIXME: It would be good to recover by accepting the attributes,
2187 // but attempting to do that now would cause serious
2188 // madness in terms of diagnostics.
2189 attrs.clear();
2190 attrs.Range = SourceRange();
2191
2192 ParseCXX11Attributes(attrs);
2193 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002194 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002195
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002196 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002197 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002198 if (DS.hasTypeSpecifier()) {
2199 bool AllowNonIdentifiers
2200 = (getCurScope()->getFlags() & (Scope::ControlScope |
2201 Scope::BlockScope |
2202 Scope::TemplateParamScope |
2203 Scope::FunctionPrototypeScope |
2204 Scope::AtCatchScope)) == 0;
2205 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002206 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002207 (DSContext == DSC_class && DS.isFriendSpecified());
2208
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002209 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002210 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002211 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002212 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002213 }
2214
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002215 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2216 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2217 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002218 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002219 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002220 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002221 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002222 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002223 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002224
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002225 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002226 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002227 }
2228
Chris Lattner5e02c472009-01-05 00:07:25 +00002229 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002230 // C++ scope specifier. Annotate and loop, or bail out on error.
2231 if (TryAnnotateCXXScopeToken(true)) {
2232 if (!DS.hasTypeSpecifier())
2233 DS.SetTypeSpecError();
2234 goto DoneWithDeclSpec;
2235 }
John McCall2e0a7152010-03-01 18:20:46 +00002236 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2237 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002238 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002239
2240 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002241 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002242 goto DoneWithDeclSpec;
2243
John McCallaa87d332009-12-12 11:40:51 +00002244 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002245 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2246 Tok.getAnnotationRange(),
2247 SS);
John McCallaa87d332009-12-12 11:40:51 +00002248
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002249 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002250 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002251 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002252 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002253 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002254 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002255
2256 // C++ [class.qual]p2:
2257 // In a lookup in which the constructor is an acceptable lookup
2258 // result and the nested-name-specifier nominates a class C:
2259 //
2260 // - if the name specified after the
2261 // nested-name-specifier, when looked up in C, is the
2262 // injected-class-name of C (Clause 9), or
2263 //
2264 // - if the name specified after the nested-name-specifier
2265 // is the same as the identifier or the
2266 // simple-template-id's template-name in the last
2267 // component of the nested-name-specifier,
2268 //
2269 // the name is instead considered to name the constructor of
2270 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002271 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002272 // Thus, if the template-name is actually the constructor
2273 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002274 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002275 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002276 if ((DSContext == DSC_top_level ||
2277 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2278 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002279 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002280 if (isConstructorDeclarator()) {
2281 // The user meant this to be an out-of-line constructor
2282 // definition, but template arguments are not allowed
2283 // there. Just allow this as a constructor; we'll
2284 // complain about it later.
2285 goto DoneWithDeclSpec;
2286 }
2287
2288 // The user meant this to name a type, but it actually names
2289 // a constructor with some extraneous template
2290 // arguments. Complain, then parse it as a type as the user
2291 // intended.
2292 Diag(TemplateId->TemplateNameLoc,
2293 diag::err_out_of_line_template_id_names_constructor)
2294 << TemplateId->Name;
2295 }
2296
John McCallaa87d332009-12-12 11:40:51 +00002297 DS.getTypeSpecScope() = SS;
2298 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002299 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002300 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002301 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002302 continue;
2303 }
2304
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002305 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002306 DS.getTypeSpecScope() = SS;
2307 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002308 if (Tok.getAnnotationValue()) {
2309 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002311 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002312 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002313 if (isInvalid)
2314 break;
John McCallb3d87482010-08-24 05:47:05 +00002315 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002316 else
2317 DS.SetTypeSpecError();
2318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2319 ConsumeToken(); // The typename
2320 }
2321
Douglas Gregor9135c722009-03-25 15:40:00 +00002322 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002323 goto DoneWithDeclSpec;
2324
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002325 // If we're in a context where the identifier could be a class name,
2326 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002327 if ((DSContext == DSC_top_level ||
2328 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002329 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002330 &SS)) {
2331 if (isConstructorDeclarator())
2332 goto DoneWithDeclSpec;
2333
2334 // As noted in C++ [class.qual]p2 (cited above), when the name
2335 // of the class is qualified in a context where it could name
2336 // a constructor, its a constructor name. However, we've
2337 // looked at the declarator, and the user probably meant this
2338 // to be a type. Complain that it isn't supposed to be treated
2339 // as a type, then proceed to parse it as a type.
2340 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2341 << Next.getIdentifierInfo();
2342 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002343
John McCallb3d87482010-08-24 05:47:05 +00002344 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2345 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002346 getCurScope(), &SS,
2347 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002348 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002349 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002350
Chris Lattnerf4382f52009-04-14 22:17:06 +00002351 // If the referenced identifier is not a type, then this declspec is
2352 // erroneous: We already checked about that it has no type specifier, and
2353 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002354 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002355 if (TypeRep == 0) {
2356 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002357 ParsedAttributesWithRange Attrs(AttrFactory);
2358 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2359 if (!Attrs.empty()) {
2360 AttrsLastTime = true;
2361 attrs.takeAllFrom(Attrs);
2362 }
2363 continue;
2364 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002365 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002366 }
Mike Stump1eb44332009-09-09 15:08:12 +00002367
John McCallaa87d332009-12-12 11:40:51 +00002368 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002369 ConsumeToken(); // The C++ scope.
2370
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002372 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002373 if (isInvalid)
2374 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002376 DS.SetRangeEnd(Tok.getLocation());
2377 ConsumeToken(); // The typename.
2378
2379 continue;
2380 }
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002383 if (Tok.getAnnotationValue()) {
2384 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002386 DiagID, T);
2387 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002388 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002389
Chris Lattner5c5db552010-04-05 18:18:31 +00002390 if (isInvalid)
2391 break;
2392
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2394 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Chris Lattner80d0c892009-01-21 19:48:37 +00002396 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2397 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002398 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002399 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002400 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002401
Chris Lattner80d0c892009-01-21 19:48:37 +00002402 continue;
2403 }
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Douglas Gregorbfad9152011-04-28 15:48:45 +00002405 case tok::kw___is_signed:
2406 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2407 // typically treats it as a trait. If we see __is_signed as it appears
2408 // in libstdc++, e.g.,
2409 //
2410 // static const bool __is_signed;
2411 //
2412 // then treat __is_signed as an identifier rather than as a keyword.
2413 if (DS.getTypeSpecType() == TST_bool &&
2414 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2415 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2416 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2417 Tok.setKind(tok::identifier);
2418 }
2419
2420 // We're done with the declaration-specifiers.
2421 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002422
Chris Lattner3bd934a2008-07-26 01:18:38 +00002423 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002424 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002425 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002426 // In C++, check to see if this is a scope specifier like foo::bar::, if
2427 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002428 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002429 if (TryAnnotateCXXScopeToken(true)) {
2430 if (!DS.hasTypeSpecifier())
2431 DS.SetTypeSpecError();
2432 goto DoneWithDeclSpec;
2433 }
2434 if (!Tok.is(tok::identifier))
2435 continue;
2436 }
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Chris Lattner3bd934a2008-07-26 01:18:38 +00002438 // This identifier can only be a typedef name if we haven't already seen
2439 // a type-specifier. Without this check we misparse:
2440 // typedef int X; struct Y { short X; }; as 'short int'.
2441 if (DS.hasTypeSpecifier())
2442 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002443
John Thompson82287d12010-02-05 00:12:22 +00002444 // Check for need to substitute AltiVec keyword tokens.
2445 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2446 break;
2447
Richard Smithf63eee72012-05-09 18:56:43 +00002448 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2449 // allow the use of a typedef name as a type specifier.
2450 if (DS.isTypeAltiVecVector())
2451 goto DoneWithDeclSpec;
2452
John McCallb3d87482010-08-24 05:47:05 +00002453 ParsedType TypeRep =
2454 Actions.getTypeName(*Tok.getIdentifierInfo(),
2455 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002456
Chris Lattnerc199ab32009-04-12 20:42:31 +00002457 // If this is not a typedef name, don't parse it as part of the declspec,
2458 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002459 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002460 ParsedAttributesWithRange Attrs(AttrFactory);
2461 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2462 if (!Attrs.empty()) {
2463 AttrsLastTime = true;
2464 attrs.takeAllFrom(Attrs);
2465 }
2466 continue;
2467 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002468 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002469 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002470
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002471 // If we're in a context where the identifier could be a class name,
2472 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002473 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002474 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002475 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002476 goto DoneWithDeclSpec;
2477
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002479 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002480 if (isInvalid)
2481 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Chris Lattner3bd934a2008-07-26 01:18:38 +00002483 DS.SetRangeEnd(Tok.getLocation());
2484 ConsumeToken(); // The identifier
2485
2486 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2487 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002488 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002489 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002490 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002491
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002492 // Need to support trailing type qualifiers (e.g. "id<p> const").
2493 // If a type specifier follows, it will be diagnosed elsewhere.
2494 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002495 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002496
2497 // type-name
2498 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002499 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002500 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002501 // This template-id does not refer to a type name, so we're
2502 // done with the type-specifiers.
2503 goto DoneWithDeclSpec;
2504 }
2505
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002506 // If we're in a context where the template-id could be a
2507 // constructor name or specialization, check whether this is a
2508 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002509 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002510 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002511 isConstructorDeclarator())
2512 goto DoneWithDeclSpec;
2513
Douglas Gregor39a8de12009-02-25 19:37:18 +00002514 // Turn the template-id annotation token into a type annotation
2515 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002516 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002517 continue;
2518 }
2519
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 // GNU attributes support.
2521 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002522 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002523 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002524
2525 // Microsoft declspec support.
2526 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002527 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002528 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Steve Naroff239f0732008-12-25 14:16:32 +00002530 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002531 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002532 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002533 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002534 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002535 // FIXME: This does not work correctly if it is set to be a declspec
2536 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002537 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002538 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002539 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002540 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002541
2542 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002543 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002544 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002545 case tok::kw___cdecl:
2546 case tok::kw___stdcall:
2547 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002548 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002549 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002550 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002551 continue;
2552
Dawn Perchik52fc3142010-09-03 01:29:35 +00002553 // Borland single token adornments.
2554 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002555 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002556 continue;
2557
Peter Collingbournef315fa82011-02-14 01:42:53 +00002558 // OpenCL single token adornments.
2559 case tok::kw___kernel:
2560 ParseOpenCLAttributes(DS.getAttributes());
2561 continue;
2562
Reid Spencer5f016e22007-07-11 17:01:13 +00002563 // storage-class-specifier
2564 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2566 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 break;
2568 case tok::kw_extern:
2569 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002570 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002571 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2572 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002573 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002574 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002575 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2576 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002577 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002578 case tok::kw_static:
2579 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002580 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002581 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2582 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002583 break;
2584 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002585 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002586 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002587 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2588 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002589 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002590 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002591 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002592 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2594 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002595 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002596 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2597 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 break;
2599 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002600 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2601 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002602 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002603 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002604 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2605 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002606 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002607 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002608 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002609 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Reid Spencer5f016e22007-07-11 17:01:13 +00002611 // function-specifier
2612 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002613 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002614 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002615 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002616 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002617 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002618 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002619 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002620 break;
Richard Smithde03c152013-01-17 22:16:11 +00002621 case tok::kw__Noreturn:
2622 if (!getLangOpts().C11)
2623 Diag(Loc, diag::ext_c11_noreturn);
2624 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2625 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002626
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002627 // alignment-specifier
2628 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002629 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002630 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002631 ParseAlignmentSpecifier(DS.getAttributes());
2632 continue;
2633
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002634 // friend
2635 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002636 if (DSContext == DSC_class)
2637 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2638 else {
2639 PrevSpec = ""; // not actually used by the diagnostic
2640 DiagID = diag::err_friend_invalid_in_context;
2641 isInvalid = true;
2642 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002643 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Douglas Gregor8d267c52011-09-09 02:06:17 +00002645 // Modules
2646 case tok::kw___module_private__:
2647 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2648 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002649
Sebastian Redl2ac67232009-11-05 15:47:02 +00002650 // constexpr
2651 case tok::kw_constexpr:
2652 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2653 break;
2654
Chris Lattner80d0c892009-01-21 19:48:37 +00002655 // type-specifier
2656 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2658 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002659 break;
2660 case tok::kw_long:
2661 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002662 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2663 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002664 else
John McCallfec54012009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2666 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002667 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002668 case tok::kw___int64:
2669 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2670 DiagID);
2671 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002672 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2674 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002675 break;
2676 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2678 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002679 break;
2680 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2682 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002683 break;
2684 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002685 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2686 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002687 break;
2688 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2690 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002691 break;
2692 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2694 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002695 break;
2696 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2698 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002699 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002700 case tok::kw___int128:
2701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2702 DiagID);
2703 break;
2704 case tok::kw_half:
2705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2706 DiagID);
2707 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002708 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2710 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002711 break;
2712 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2714 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002715 break;
2716 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2718 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002719 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002720 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2722 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002723 break;
2724 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2726 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002727 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002728 case tok::kw_bool:
2729 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002730 if (Tok.is(tok::kw_bool) &&
2731 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2732 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2733 PrevSpec = ""; // Not used by the diagnostic.
2734 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002735 // For better error recovery.
2736 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002737 isInvalid = true;
2738 } else {
2739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2740 DiagID);
2741 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002742 break;
2743 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2745 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002746 break;
2747 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2749 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002750 break;
2751 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2753 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002754 break;
John Thompson82287d12010-02-05 00:12:22 +00002755 case tok::kw___vector:
2756 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2757 break;
2758 case tok::kw___pixel:
2759 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2760 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002761 case tok::kw_image1d_t:
2762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2763 PrevSpec, DiagID);
2764 break;
2765 case tok::kw_image1d_array_t:
2766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2767 PrevSpec, DiagID);
2768 break;
2769 case tok::kw_image1d_buffer_t:
2770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2771 PrevSpec, DiagID);
2772 break;
2773 case tok::kw_image2d_t:
2774 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2775 PrevSpec, DiagID);
2776 break;
2777 case tok::kw_image2d_array_t:
2778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2779 PrevSpec, DiagID);
2780 break;
2781 case tok::kw_image3d_t:
2782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2783 PrevSpec, DiagID);
2784 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002785 case tok::kw_sampler_t:
2786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2787 PrevSpec, DiagID);
2788 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002789 case tok::kw_event_t:
2790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2791 PrevSpec, DiagID);
2792 break;
John McCalla5fc4722011-04-09 22:50:59 +00002793 case tok::kw___unknown_anytype:
2794 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2795 PrevSpec, DiagID);
2796 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002797
2798 // class-specifier:
2799 case tok::kw_class:
2800 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002801 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002802 case tok::kw_union: {
2803 tok::TokenKind Kind = Tok.getKind();
2804 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002805
2806 // These are attributes following class specifiers.
2807 // To produce better diagnostic, we parse them when
2808 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002809 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002810 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002811 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002812
2813 // If there are attributes following class specifier,
2814 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002815 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002816 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002817 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002818 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002819 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002820 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002821
2822 // enum-specifier:
2823 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002824 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002825 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002826 continue;
2827
2828 // cv-qualifier:
2829 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002830 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002831 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002832 break;
2833 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002834 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002835 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002836 break;
2837 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002838 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002839 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002840 break;
2841
Douglas Gregord57959a2009-03-27 23:10:48 +00002842 // C++ typename-specifier:
2843 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002844 if (TryAnnotateTypeOrScopeToken()) {
2845 DS.SetTypeSpecError();
2846 goto DoneWithDeclSpec;
2847 }
2848 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002849 continue;
2850 break;
2851
Chris Lattner80d0c892009-01-21 19:48:37 +00002852 // GNU typeof support.
2853 case tok::kw_typeof:
2854 ParseTypeofSpecifier(DS);
2855 continue;
2856
David Blaikie42d6d0c2011-12-04 05:04:18 +00002857 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002858 ParseDecltypeSpecifier(DS);
2859 continue;
2860
Sean Huntdb5d44b2011-05-19 05:37:45 +00002861 case tok::kw___underlying_type:
2862 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002863 continue;
2864
2865 case tok::kw__Atomic:
2866 ParseAtomicSpecifier(DS);
2867 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002868
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002869 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002870 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002871 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002872 goto DoneWithDeclSpec;
2873 case tok::kw___private:
2874 case tok::kw___global:
2875 case tok::kw___local:
2876 case tok::kw___constant:
2877 case tok::kw___read_only:
2878 case tok::kw___write_only:
2879 case tok::kw___read_write:
2880 ParseOpenCLQualifiers(DS);
2881 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002882
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002883 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002884 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002885 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2886 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002887 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002888 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Douglas Gregor46f936e2010-11-19 17:10:50 +00002890 if (!ParseObjCProtocolQualifiers(DS))
2891 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2892 << FixItHint::CreateInsertion(Loc, "id")
2893 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002894
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002895 // Need to support trailing type qualifiers (e.g. "id<p> const").
2896 // If a type specifier follows, it will be diagnosed elsewhere.
2897 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002898 }
John McCallfec54012009-08-03 20:12:06 +00002899 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002900 if (isInvalid) {
2901 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002902 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002903
Douglas Gregorae2fb142010-08-23 14:34:43 +00002904 if (DiagID == diag::ext_duplicate_declspec)
2905 Diag(Tok, DiagID)
2906 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2907 else
2908 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002910
Chris Lattner81c018d2008-03-13 06:29:04 +00002911 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002912 if (DiagID != diag::err_bool_redeclaration)
2913 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002914
2915 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 }
2917}
Douglas Gregoradcac882008-12-01 23:54:00 +00002918
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002919/// ParseStructDeclaration - Parse a struct declaration without the terminating
2920/// semicolon.
2921///
Reid Spencer5f016e22007-07-11 17:01:13 +00002922/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002923/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002924/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002925/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002926/// struct-declarator-list:
2927/// struct-declarator
2928/// struct-declarator-list ',' struct-declarator
2929/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2930/// struct-declarator:
2931/// declarator
2932/// [GNU] declarator attributes[opt]
2933/// declarator[opt] ':' constant-expression
2934/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2935///
Chris Lattnere1359422008-04-10 06:46:29 +00002936void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002937ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002938
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002939 if (Tok.is(tok::kw___extension__)) {
2940 // __extension__ silences extension warnings in the subexpression.
2941 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002942 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002943 return ParseStructDeclaration(DS, Fields);
2944 }
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Steve Naroff28a7ca82007-08-20 22:28:22 +00002946 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002947 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002949 // If there are no declarators, this is a free-standing declaration
2950 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002951 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002952 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2953 DS);
2954 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002955 return;
2956 }
2957
2958 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002959 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002960 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002961 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002962 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002963 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002964
Bill Wendlingad017fa2012-12-20 19:22:21 +00002965 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002966 if (!FirstDeclarator)
2967 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002968
Steve Naroff28a7ca82007-08-20 22:28:22 +00002969 /// struct-declarator: declarator
2970 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002971 if (Tok.isNot(tok::colon)) {
2972 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2973 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002974 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002975 }
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Chris Lattner04d66662007-10-09 17:33:22 +00002977 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002978 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002979 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002980 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002981 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002982 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002983 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002984 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002985
Steve Naroff28a7ca82007-08-20 22:28:22 +00002986 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002987 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002988
John McCallbdd563e2009-11-03 02:38:08 +00002989 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002990 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002991
Steve Naroff28a7ca82007-08-20 22:28:22 +00002992 // If we don't have a comma, it is either the end of the list (a ';')
2993 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002994 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002995 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002996
Steve Naroff28a7ca82007-08-20 22:28:22 +00002997 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002998 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002999
John McCallbdd563e2009-11-03 02:38:08 +00003000 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003001 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002}
3003
3004/// ParseStructUnionBody
3005/// struct-contents:
3006/// struct-declaration-list
3007/// [EXT] empty
3008/// [GNU] "struct-declaration-list" without terminatoring ';'
3009/// struct-declaration-list:
3010/// struct-declaration
3011/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003012/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003013///
Reid Spencer5f016e22007-07-11 17:01:13 +00003014void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003015 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003016 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3017 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003019 BalancedDelimiterTracker T(*this, tok::l_brace);
3020 if (T.consumeOpen())
3021 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003023 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003024 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003025
Reid Spencer5f016e22007-07-11 17:01:13 +00003026 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3027 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003028 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003029 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3030 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3031 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003032
Chris Lattner5f9e2722011-07-23 10:55:15 +00003033 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003034
Reid Spencer5f016e22007-07-11 17:01:13 +00003035 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003036 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003037 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Reid Spencer5f016e22007-07-11 17:01:13 +00003039 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003040 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003041 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 continue;
3043 }
Chris Lattnere1359422008-04-10 06:46:29 +00003044
John McCallbdd563e2009-11-03 02:38:08 +00003045 if (!Tok.is(tok::at)) {
3046 struct CFieldCallback : FieldCallback {
3047 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003048 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003049 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003050
John McCalld226f652010-08-21 09:40:31 +00003051 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003052 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003053 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3054
Eli Friedmandcdff462012-08-08 23:53:27 +00003055 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003056 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003057 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003058 FD.D.getDeclSpec().getSourceRange().getBegin(),
3059 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003060 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003061 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003062 }
John McCallbdd563e2009-11-03 02:38:08 +00003063 } Callback(*this, TagDecl, FieldDecls);
3064
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003065 // Parse all the comma separated declarators.
3066 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003067 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003068 } else { // Handle @defs
3069 ConsumeToken();
3070 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3071 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003072 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003073 continue;
3074 }
3075 ConsumeToken();
3076 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3077 if (!Tok.is(tok::identifier)) {
3078 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003079 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003080 continue;
3081 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003082 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003083 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003084 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003085 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3086 ConsumeToken();
3087 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003088 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003089
Chris Lattner04d66662007-10-09 17:33:22 +00003090 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003092 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003093 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003094 break;
3095 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003096 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3097 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003098 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003099 // If we stopped at a ';', eat it.
3100 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003101 }
3102 }
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003104 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003105
John McCall0b7e6782011-03-24 11:26:52 +00003106 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003107 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003108 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003109
Douglas Gregor23c94db2010-07-02 17:43:08 +00003110 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003111 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003112 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003113 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003114 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003115 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3116 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003117}
3118
Reid Spencer5f016e22007-07-11 17:01:13 +00003119/// ParseEnumSpecifier
3120/// enum-specifier: [C99 6.7.2.2]
3121/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003122///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003123/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3124/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003125/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3126/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003127/// 'enum' identifier
3128/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003129///
Richard Smith1af83c42012-03-23 03:33:32 +00003130/// [C++11] enum-head '{' enumerator-list[opt] '}'
3131/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003132///
Richard Smith1af83c42012-03-23 03:33:32 +00003133/// enum-head: [C++11]
3134/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3135/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3136/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003137///
Richard Smith1af83c42012-03-23 03:33:32 +00003138/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003139/// 'enum'
3140/// 'enum' 'class'
3141/// 'enum' 'struct'
3142///
Richard Smith1af83c42012-03-23 03:33:32 +00003143/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003144/// ':' type-specifier-seq
3145///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003146/// [C++] elaborated-type-specifier:
3147/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3148///
Chris Lattner4c97d762009-04-12 21:49:30 +00003149void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003150 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003151 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003153 if (Tok.is(tok::code_completion)) {
3154 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003155 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003156 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003157 }
John McCall57c13002011-07-06 05:58:41 +00003158
Sean Hunt2edf0a22012-06-23 05:07:58 +00003159 // If attributes exist after tag, parse them.
3160 ParsedAttributesWithRange attrs(AttrFactory);
3161 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003162 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003163
3164 // If declspecs exist after tag, parse them.
3165 while (Tok.is(tok::kw___declspec))
3166 ParseMicrosoftDeclSpec(attrs);
3167
Richard Smithbdad7a22012-01-10 01:33:14 +00003168 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003169 bool IsScopedUsingClassTag = false;
3170
John McCall1e12b3d2012-06-23 22:30:04 +00003171 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003172 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003173 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003174 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003175 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003176 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003177
Bill Wendlingad017fa2012-12-20 19:22:21 +00003178 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003179 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003180 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003181
3182 // They are allowed afterwards, though.
3183 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003184 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003185 while (Tok.is(tok::kw___declspec))
3186 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003187 }
Richard Smith1af83c42012-03-23 03:33:32 +00003188
John McCall13489672012-05-07 06:16:58 +00003189 // C++11 [temp.explicit]p12:
3190 // The usual access controls do not apply to names used to specify
3191 // explicit instantiations.
3192 // We extend this to also cover explicit specializations. Note that
3193 // we don't suppress if this turns out to be an elaborated type
3194 // specifier.
3195 bool shouldDelayDiagsInTag =
3196 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3197 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3198 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003199
Richard Smith7796eb52012-03-12 08:56:40 +00003200 // Enum definitions should not be parsed in a trailing-return-type.
3201 bool AllowDeclaration = DSC != DSC_trailing;
3202
3203 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003204 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003205 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003206
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003207 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003208 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003209 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3210 // if a fixed underlying type is allowed.
3211 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003212
3213 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003214 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003215 return;
3216
3217 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003218 Diag(Tok, diag::err_expected_ident);
3219 if (Tok.isNot(tok::l_brace)) {
3220 // Has no name and is not a definition.
3221 // Skip the rest of this declarator, up until the comma or semicolon.
3222 SkipUntil(tok::comma, true);
3223 return;
3224 }
3225 }
3226 }
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003228 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003229 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003230 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003231 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003233 // Skip the rest of this declarator, up until the comma or semicolon.
3234 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003236 }
Mike Stump1eb44332009-09-09 15:08:12 +00003237
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003238 // If an identifier is present, consume and remember it.
3239 IdentifierInfo *Name = 0;
3240 SourceLocation NameLoc;
3241 if (Tok.is(tok::identifier)) {
3242 Name = Tok.getIdentifierInfo();
3243 NameLoc = ConsumeToken();
3244 }
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Richard Smithbdad7a22012-01-10 01:33:14 +00003246 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003247 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3248 // declaration of a scoped enumeration.
3249 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003250 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003251 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003252 }
3253
John McCall13489672012-05-07 06:16:58 +00003254 // Okay, end the suppression area. We'll decide whether to emit the
3255 // diagnostics in a second.
3256 if (shouldDelayDiagsInTag)
3257 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003258
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003259 TypeResult BaseType;
3260
Douglas Gregora61b3e72010-12-01 17:42:47 +00003261 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003262 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003263 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003264 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003265 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003266 // If we're in class scope, this can either be an enum declaration with
3267 // an underlying type, or a declaration of a bitfield member. We try to
3268 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003269 // (integer literal, sizeof); if it's still ambiguous, we then consider
3270 // anything that's a simple-type-specifier followed by '(' as an
3271 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003272 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003273 EnterExpressionEvaluationContext Unevaluated(Actions,
3274 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003275 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003276 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003277 // bit-field. This is the common case.
3278 if (TPR == TPResult::True())
3279 PossibleBitfield = true;
3280 // If the next token starts a type-specifier-seq, it may be either a
3281 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003282 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003283 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003284 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003285 GetLookAheadToken(2).getKind() == tok::semi) {
3286 // Consume the ':'.
3287 ConsumeToken();
3288 } else {
3289 // We have the start of a type-specifier-seq, so we have to perform
3290 // tentative parsing to determine whether we have an expression or a
3291 // type.
3292 TentativeParsingAction TPA(*this);
3293
3294 // Consume the ':'.
3295 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003296
3297 // If we see a type specifier followed by an open-brace, we have an
3298 // ambiguity between an underlying type and a C++11 braced
3299 // function-style cast. Resolve this by always treating it as an
3300 // underlying type.
3301 // FIXME: The standard is not entirely clear on how to disambiguate in
3302 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003303 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003304 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003305 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003306 // We'll parse this as a bitfield later.
3307 PossibleBitfield = true;
3308 TPA.Revert();
3309 } else {
3310 // We have a type-specifier-seq.
3311 TPA.Commit();
3312 }
3313 }
3314 } else {
3315 // Consume the ':'.
3316 ConsumeToken();
3317 }
3318
3319 if (!PossibleBitfield) {
3320 SourceRange Range;
3321 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003322
Richard Smith80ad52f2013-01-02 11:42:31 +00003323 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003324 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003325 } else if (!getLangOpts().ObjC2) {
3326 if (getLangOpts().CPlusPlus)
3327 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3328 else
3329 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3330 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003331 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003332 }
3333
Richard Smithbdad7a22012-01-10 01:33:14 +00003334 // There are four options here. If we have 'friend enum foo;' then this is a
3335 // friend declaration, and cannot have an accompanying definition. If we have
3336 // 'enum foo;', then this is a forward declaration. If we have
3337 // 'enum foo {...' then this is a definition. Otherwise we have something
3338 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003339 //
3340 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3341 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3342 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3343 //
John McCallf312b1e2010-08-26 23:41:50 +00003344 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003345 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003346 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003347 } else if (Tok.is(tok::l_brace)) {
3348 if (DS.isFriendSpecified()) {
3349 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3350 << SourceRange(DS.getFriendSpecLoc());
3351 ConsumeBrace();
3352 SkipUntil(tok::r_brace);
3353 TUK = Sema::TUK_Friend;
3354 } else {
3355 TUK = Sema::TUK_Definition;
3356 }
Richard Smithc9f35172012-06-25 21:37:02 +00003357 } else if (DSC != DSC_type_specifier &&
3358 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003359 (Tok.isAtStartOfLine() &&
3360 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003361 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3362 if (Tok.isNot(tok::semi)) {
3363 // A semicolon was missing after this declaration. Diagnose and recover.
3364 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3365 "enum");
3366 PP.EnterToken(Tok);
3367 Tok.setKind(tok::semi);
3368 }
John McCall13489672012-05-07 06:16:58 +00003369 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003370 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003371 }
3372
3373 // If this is an elaborated type specifier, and we delayed
3374 // diagnostics before, just merge them into the current pool.
3375 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3376 diagsFromTag.redelay();
3377 }
Richard Smith1af83c42012-03-23 03:33:32 +00003378
3379 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003380 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003381 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003382 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003383 // Skip the rest of this declarator, up until the comma or semicolon.
3384 Diag(Tok, diag::err_enum_template);
3385 SkipUntil(tok::comma, true);
3386 return;
3387 }
3388
3389 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3390 // Enumerations can't be explicitly instantiated.
3391 DS.SetTypeSpecError();
3392 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3393 return;
3394 }
3395
3396 assert(TemplateInfo.TemplateParams && "no template parameters");
3397 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3398 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003399 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003400
Sean Hunt2edf0a22012-06-23 05:07:58 +00003401 if (TUK == Sema::TUK_Reference)
3402 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003403
Douglas Gregorb9075602011-02-22 02:55:24 +00003404 if (!Name && TUK != Sema::TUK_Definition) {
3405 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003406
Douglas Gregorb9075602011-02-22 02:55:24 +00003407 // Skip the rest of this declarator, up until the comma or semicolon.
3408 SkipUntil(tok::comma, true);
3409 return;
3410 }
Richard Smith1af83c42012-03-23 03:33:32 +00003411
Douglas Gregor402abb52009-05-28 23:31:59 +00003412 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003413 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003414 const char *PrevSpec = 0;
3415 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003416 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003417 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003418 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003419 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003420 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003421
Douglas Gregor48c89f42010-04-24 16:38:41 +00003422 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003423 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003424 // dependent tag.
3425 if (!Name) {
3426 DS.SetTypeSpecError();
3427 Diag(Tok, diag::err_expected_type_name_after_typename);
3428 return;
3429 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003430
Douglas Gregor23c94db2010-07-02 17:43:08 +00003431 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003432 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003433 NameLoc);
3434 if (Type.isInvalid()) {
3435 DS.SetTypeSpecError();
3436 return;
3437 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003438
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003439 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3440 NameLoc.isValid() ? NameLoc : StartLoc,
3441 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003442 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003443
Douglas Gregor48c89f42010-04-24 16:38:41 +00003444 return;
3445 }
Mike Stump1eb44332009-09-09 15:08:12 +00003446
John McCalld226f652010-08-21 09:40:31 +00003447 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003448 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003449 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003450 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003451 ConsumeBrace();
3452 SkipUntil(tok::r_brace);
3453 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003454
Douglas Gregor48c89f42010-04-24 16:38:41 +00003455 DS.SetTypeSpecError();
3456 return;
3457 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003458
Richard Smithc9f35172012-06-25 21:37:02 +00003459 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003460 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003461
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003462 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3463 NameLoc.isValid() ? NameLoc : StartLoc,
3464 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003465 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003466}
3467
3468/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3469/// enumerator-list:
3470/// enumerator
3471/// enumerator-list ',' enumerator
3472/// enumerator:
3473/// enumeration-constant
3474/// enumeration-constant '=' constant-expression
3475/// enumeration-constant:
3476/// identifier
3477///
John McCalld226f652010-08-21 09:40:31 +00003478void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003479 // Enter the scope of the enum body and start the definition.
3480 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003481 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003482
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003483 BalancedDelimiterTracker T(*this, tok::l_brace);
3484 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003485
Chris Lattner7946dd32007-08-27 17:24:30 +00003486 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003487 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003488 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Chris Lattner5f9e2722011-07-23 10:55:15 +00003490 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003491
John McCalld226f652010-08-21 09:40:31 +00003492 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Reid Spencer5f016e22007-07-11 17:01:13 +00003494 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003495 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003496 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3497 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003498
John McCall5b629aa2010-10-22 23:36:17 +00003499 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003500 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003501 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003502 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003503 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003504
Reid Spencer5f016e22007-07-11 17:01:13 +00003505 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003506 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003507 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003508
Chris Lattner04d66662007-10-09 17:33:22 +00003509 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003511 AssignedVal = ParseConstantExpression();
3512 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003514 }
Mike Stump1eb44332009-09-09 15:08:12 +00003515
Reid Spencer5f016e22007-07-11 17:01:13 +00003516 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003517 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3518 LastEnumConstDecl,
3519 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003520 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003521 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003522 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003523
Reid Spencer5f016e22007-07-11 17:01:13 +00003524 EnumConstantDecls.push_back(EnumConstDecl);
3525 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003526
Douglas Gregor751f6922010-09-07 14:51:08 +00003527 if (Tok.is(tok::identifier)) {
3528 // We're missing a comma between enumerators.
3529 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003530 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003531 << FixItHint::CreateInsertion(Loc, ", ");
3532 continue;
3533 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003534
Chris Lattner04d66662007-10-09 17:33:22 +00003535 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003536 break;
3537 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003538
Richard Smith7fe62082011-10-15 05:09:34 +00003539 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003540 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003541 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3542 diag::ext_enumerator_list_comma_cxx :
3543 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003544 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003545 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003546 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3547 << FixItHint::CreateRemoval(CommaLoc);
3548 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003549 }
Mike Stump1eb44332009-09-09 15:08:12 +00003550
Reid Spencer5f016e22007-07-11 17:01:13 +00003551 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003552 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003553
Reid Spencer5f016e22007-07-11 17:01:13 +00003554 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003555 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003556 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003557
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003558 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3559 EnumDecl, EnumConstantDecls.data(),
3560 EnumConstantDecls.size(), getCurScope(),
3561 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003562
Douglas Gregor72de6672009-01-08 20:45:30 +00003563 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003564 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3565 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003566
3567 // The next token must be valid after an enum definition. If not, a ';'
3568 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003569 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3570 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003571 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3572 // Push this token back into the preprocessor and change our current token
3573 // to ';' so that the rest of the code recovers as though there were an
3574 // ';' after the definition.
3575 PP.EnterToken(Tok);
3576 Tok.setKind(tok::semi);
3577 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003578}
3579
3580/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003581/// start of a type-qualifier-list.
3582bool Parser::isTypeQualifier() const {
3583 switch (Tok.getKind()) {
3584 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003585
3586 // type-qualifier only in OpenCL
3587 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003588 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003589
Steve Naroff5f8aa692008-02-11 23:15:56 +00003590 // type-qualifier
3591 case tok::kw_const:
3592 case tok::kw_volatile:
3593 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003594 case tok::kw___private:
3595 case tok::kw___local:
3596 case tok::kw___global:
3597 case tok::kw___constant:
3598 case tok::kw___read_only:
3599 case tok::kw___read_write:
3600 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003601 return true;
3602 }
3603}
3604
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003605/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3606/// is definitely a type-specifier. Return false if it isn't part of a type
3607/// specifier or if we're not sure.
3608bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3609 switch (Tok.getKind()) {
3610 default: return false;
3611 // type-specifiers
3612 case tok::kw_short:
3613 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003614 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003615 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003616 case tok::kw_signed:
3617 case tok::kw_unsigned:
3618 case tok::kw__Complex:
3619 case tok::kw__Imaginary:
3620 case tok::kw_void:
3621 case tok::kw_char:
3622 case tok::kw_wchar_t:
3623 case tok::kw_char16_t:
3624 case tok::kw_char32_t:
3625 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003626 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003627 case tok::kw_float:
3628 case tok::kw_double:
3629 case tok::kw_bool:
3630 case tok::kw__Bool:
3631 case tok::kw__Decimal32:
3632 case tok::kw__Decimal64:
3633 case tok::kw__Decimal128:
3634 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003635
Guy Benyeib13621d2012-12-18 14:38:23 +00003636 // OpenCL specific types:
3637 case tok::kw_image1d_t:
3638 case tok::kw_image1d_array_t:
3639 case tok::kw_image1d_buffer_t:
3640 case tok::kw_image2d_t:
3641 case tok::kw_image2d_array_t:
3642 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003643 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003644 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003645
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003646 // struct-or-union-specifier (C99) or class-specifier (C++)
3647 case tok::kw_class:
3648 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003649 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003650 case tok::kw_union:
3651 // enum-specifier
3652 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003653
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003654 // typedef-name
3655 case tok::annot_typename:
3656 return true;
3657 }
3658}
3659
Steve Naroff5f8aa692008-02-11 23:15:56 +00003660/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003661/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003662bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 switch (Tok.getKind()) {
3664 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003665
Chris Lattner166a8fc2009-01-04 23:41:41 +00003666 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003667 if (TryAltiVecVectorToken())
3668 return true;
3669 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003670 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003671 // Annotate typenames and C++ scope specifiers. If we get one, just
3672 // recurse to handle whatever we get.
3673 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003674 return true;
3675 if (Tok.is(tok::identifier))
3676 return false;
3677 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003678
Chris Lattner166a8fc2009-01-04 23:41:41 +00003679 case tok::coloncolon: // ::foo::bar
3680 if (NextToken().is(tok::kw_new) || // ::new
3681 NextToken().is(tok::kw_delete)) // ::delete
3682 return false;
3683
Chris Lattner166a8fc2009-01-04 23:41:41 +00003684 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003685 return true;
3686 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003687
Reid Spencer5f016e22007-07-11 17:01:13 +00003688 // GNU attributes support.
3689 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003690 // GNU typeof support.
3691 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003692
Reid Spencer5f016e22007-07-11 17:01:13 +00003693 // type-specifiers
3694 case tok::kw_short:
3695 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003696 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003697 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003698 case tok::kw_signed:
3699 case tok::kw_unsigned:
3700 case tok::kw__Complex:
3701 case tok::kw__Imaginary:
3702 case tok::kw_void:
3703 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003704 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003705 case tok::kw_char16_t:
3706 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003707 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003708 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003709 case tok::kw_float:
3710 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003711 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003712 case tok::kw__Bool:
3713 case tok::kw__Decimal32:
3714 case tok::kw__Decimal64:
3715 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003716 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003717
Guy Benyeib13621d2012-12-18 14:38:23 +00003718 // OpenCL specific types:
3719 case tok::kw_image1d_t:
3720 case tok::kw_image1d_array_t:
3721 case tok::kw_image1d_buffer_t:
3722 case tok::kw_image2d_t:
3723 case tok::kw_image2d_array_t:
3724 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003725 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003726 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003727
Chris Lattner99dc9142008-04-13 18:59:07 +00003728 // struct-or-union-specifier (C99) or class-specifier (C++)
3729 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003731 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003732 case tok::kw_union:
3733 // enum-specifier
3734 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003735
Reid Spencer5f016e22007-07-11 17:01:13 +00003736 // type-qualifier
3737 case tok::kw_const:
3738 case tok::kw_volatile:
3739 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003740
John McCallb8a8de32012-11-14 00:49:39 +00003741 // Debugger support.
3742 case tok::kw___unknown_anytype:
3743
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003744 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003745 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003747
Chris Lattner7c186be2008-10-20 00:25:30 +00003748 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3749 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003750 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003751
Steve Naroff239f0732008-12-25 14:16:32 +00003752 case tok::kw___cdecl:
3753 case tok::kw___stdcall:
3754 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003755 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003756 case tok::kw___w64:
3757 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003758 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003759 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003760 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003761
3762 case tok::kw___private:
3763 case tok::kw___local:
3764 case tok::kw___global:
3765 case tok::kw___constant:
3766 case tok::kw___read_only:
3767 case tok::kw___read_write:
3768 case tok::kw___write_only:
3769
Eli Friedman290eeb02009-06-08 23:27:34 +00003770 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003771
3772 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003773 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003774
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003775 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003776 case tok::kw__Atomic:
3777 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003778 }
3779}
3780
3781/// isDeclarationSpecifier() - Return true if the current token is part of a
3782/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003783///
3784/// \param DisambiguatingWithExpression True to indicate that the purpose of
3785/// this check is to disambiguate between an expression and a declaration.
3786bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003787 switch (Tok.getKind()) {
3788 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003789
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003790 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003791 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003792
Chris Lattner166a8fc2009-01-04 23:41:41 +00003793 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003794 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003795 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003796 return false;
John Thompson82287d12010-02-05 00:12:22 +00003797 if (TryAltiVecVectorToken())
3798 return true;
3799 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003800 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003801 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003802 // Annotate typenames and C++ scope specifiers. If we get one, just
3803 // recurse to handle whatever we get.
3804 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003805 return true;
3806 if (Tok.is(tok::identifier))
3807 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003808
Douglas Gregor9497a732010-09-16 01:51:54 +00003809 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003810 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003811 // expression is permitted, then this is probably a class message send
3812 // missing the initial '['. In this case, we won't consider this to be
3813 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003814 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003815 isStartOfObjCClassMessageMissingOpenBracket())
3816 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003817
John McCall9ba61662010-02-26 08:45:28 +00003818 return isDeclarationSpecifier();
3819
Chris Lattner166a8fc2009-01-04 23:41:41 +00003820 case tok::coloncolon: // ::foo::bar
3821 if (NextToken().is(tok::kw_new) || // ::new
3822 NextToken().is(tok::kw_delete)) // ::delete
3823 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003824
Chris Lattner166a8fc2009-01-04 23:41:41 +00003825 // Annotate typenames and C++ scope specifiers. If we get one, just
3826 // recurse to handle whatever we get.
3827 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003828 return true;
3829 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003830
Reid Spencer5f016e22007-07-11 17:01:13 +00003831 // storage-class-specifier
3832 case tok::kw_typedef:
3833 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003834 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003835 case tok::kw_static:
3836 case tok::kw_auto:
3837 case tok::kw_register:
3838 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003839
Douglas Gregor8d267c52011-09-09 02:06:17 +00003840 // Modules
3841 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003842
John McCallb8a8de32012-11-14 00:49:39 +00003843 // Debugger support
3844 case tok::kw___unknown_anytype:
3845
Reid Spencer5f016e22007-07-11 17:01:13 +00003846 // type-specifiers
3847 case tok::kw_short:
3848 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003849 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003850 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 case tok::kw_signed:
3852 case tok::kw_unsigned:
3853 case tok::kw__Complex:
3854 case tok::kw__Imaginary:
3855 case tok::kw_void:
3856 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003857 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003858 case tok::kw_char16_t:
3859 case tok::kw_char32_t:
3860
Reid Spencer5f016e22007-07-11 17:01:13 +00003861 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003862 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003863 case tok::kw_float:
3864 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003865 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003866 case tok::kw__Bool:
3867 case tok::kw__Decimal32:
3868 case tok::kw__Decimal64:
3869 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003870 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003871
Guy Benyeib13621d2012-12-18 14:38:23 +00003872 // OpenCL specific types:
3873 case tok::kw_image1d_t:
3874 case tok::kw_image1d_array_t:
3875 case tok::kw_image1d_buffer_t:
3876 case tok::kw_image2d_t:
3877 case tok::kw_image2d_array_t:
3878 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003879 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003880 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003881
Chris Lattner99dc9142008-04-13 18:59:07 +00003882 // struct-or-union-specifier (C99) or class-specifier (C++)
3883 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 case tok::kw_struct:
3885 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003886 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003887 // enum-specifier
3888 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003889
Reid Spencer5f016e22007-07-11 17:01:13 +00003890 // type-qualifier
3891 case tok::kw_const:
3892 case tok::kw_volatile:
3893 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003894
Reid Spencer5f016e22007-07-11 17:01:13 +00003895 // function-specifier
3896 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003897 case tok::kw_virtual:
3898 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003899 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003900
Richard Smith4cd81c52013-01-29 09:02:09 +00003901 // alignment-specifier
3902 case tok::kw__Alignas:
3903
Richard Smith53aec2a2012-10-25 00:00:53 +00003904 // friend keyword.
3905 case tok::kw_friend:
3906
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003907 // static_assert-declaration
3908 case tok::kw__Static_assert:
3909
Chris Lattner1ef08762007-08-09 17:01:07 +00003910 // GNU typeof support.
3911 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003912
Chris Lattner1ef08762007-08-09 17:01:07 +00003913 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003914 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003915
Richard Smith53aec2a2012-10-25 00:00:53 +00003916 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003917 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003918 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003919
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003920 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003921 case tok::kw__Atomic:
3922 return true;
3923
Chris Lattnerf3948c42008-07-26 03:38:44 +00003924 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3925 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003926 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003927
Douglas Gregord9d75e52011-04-27 05:41:15 +00003928 // typedef-name
3929 case tok::annot_typename:
3930 return !DisambiguatingWithExpression ||
3931 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003932
Steve Naroff47f52092009-01-06 19:34:12 +00003933 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003934 case tok::kw___cdecl:
3935 case tok::kw___stdcall:
3936 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003937 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003938 case tok::kw___w64:
3939 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003940 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003941 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003942 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003943 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003944
3945 case tok::kw___private:
3946 case tok::kw___local:
3947 case tok::kw___global:
3948 case tok::kw___constant:
3949 case tok::kw___read_only:
3950 case tok::kw___read_write:
3951 case tok::kw___write_only:
3952
Eli Friedman290eeb02009-06-08 23:27:34 +00003953 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003954 }
3955}
3956
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003957bool Parser::isConstructorDeclarator() {
3958 TentativeParsingAction TPA(*this);
3959
3960 // Parse the C++ scope specifier.
3961 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003962 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003963 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003964 TPA.Revert();
3965 return false;
3966 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003967
3968 // Parse the constructor name.
3969 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3970 // We already know that we have a constructor name; just consume
3971 // the token.
3972 ConsumeToken();
3973 } else {
3974 TPA.Revert();
3975 return false;
3976 }
3977
Richard Smith22592862012-03-27 23:05:05 +00003978 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003979 if (Tok.isNot(tok::l_paren)) {
3980 TPA.Revert();
3981 return false;
3982 }
3983 ConsumeParen();
3984
Richard Smith22592862012-03-27 23:05:05 +00003985 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3986 // that we have a constructor.
3987 if (Tok.is(tok::r_paren) ||
3988 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003989 TPA.Revert();
3990 return true;
3991 }
3992
3993 // If we need to, enter the specified scope.
3994 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003995 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003996 DeclScopeObj.EnterDeclaratorScope();
3997
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003998 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003999 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004000 MaybeParseMicrosoftAttributes(Attrs);
4001
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004002 // Check whether the next token(s) are part of a declaration
4003 // specifier, in which case we have the start of a parameter and,
4004 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004005 bool IsConstructor = false;
4006 if (isDeclarationSpecifier())
4007 IsConstructor = true;
4008 else if (Tok.is(tok::identifier) ||
4009 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4010 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4011 // This might be a parenthesized member name, but is more likely to
4012 // be a constructor declaration with an invalid argument type. Keep
4013 // looking.
4014 if (Tok.is(tok::annot_cxxscope))
4015 ConsumeToken();
4016 ConsumeToken();
4017
4018 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004019 // which must have one of the following syntactic forms (see the
4020 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004021 switch (Tok.getKind()) {
4022 case tok::l_paren:
4023 // C(X ( int));
4024 case tok::l_square:
4025 // C(X [ 5]);
4026 // C(X [ [attribute]]);
4027 case tok::coloncolon:
4028 // C(X :: Y);
4029 // C(X :: *p);
4030 case tok::r_paren:
4031 // C(X )
4032 // Assume this isn't a constructor, rather than assuming it's a
4033 // constructor with an unnamed parameter of an ill-formed type.
4034 break;
4035
4036 default:
4037 IsConstructor = true;
4038 break;
4039 }
4040 }
4041
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004042 TPA.Revert();
4043 return IsConstructor;
4044}
Reid Spencer5f016e22007-07-11 17:01:13 +00004045
4046/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004047/// type-qualifier-list: [C99 6.7.5]
4048/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004049/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004050/// [ only if VendorAttributesAllowed=true ]
4051/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004052/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004053/// [ only if VendorAttributesAllowed=true ]
4054/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004055/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004056/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004057///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004058void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4059 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004060 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004061 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004062 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004063 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004064 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004065 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004066 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004067
4068 SourceLocation EndLoc;
4069
Reid Spencer5f016e22007-07-11 17:01:13 +00004070 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004071 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004072 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004073 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004074 SourceLocation Loc = Tok.getLocation();
4075
4076 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004077 case tok::code_completion:
4078 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004079 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004080
Reid Spencer5f016e22007-07-11 17:01:13 +00004081 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004083 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004084 break;
4085 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004087 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 break;
4089 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004090 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004091 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004092 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004093
4094 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004095 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004096 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004097 goto DoneWithTypeQuals;
4098 case tok::kw___private:
4099 case tok::kw___global:
4100 case tok::kw___local:
4101 case tok::kw___constant:
4102 case tok::kw___read_only:
4103 case tok::kw___write_only:
4104 case tok::kw___read_write:
4105 ParseOpenCLQualifiers(DS);
4106 break;
4107
Eli Friedman290eeb02009-06-08 23:27:34 +00004108 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004109 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004110 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004111 case tok::kw___cdecl:
4112 case tok::kw___stdcall:
4113 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004114 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004115 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004116 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004117 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004118 continue;
4119 }
4120 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004121 case tok::kw___pascal:
4122 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004123 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004124 continue;
4125 }
4126 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004127 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004128 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004129 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004130 continue; // do *not* consume the next token!
4131 }
4132 // otherwise, FALL THROUGH!
4133 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004134 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004135 // If this is not a type-qualifier token, we're done reading type
4136 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004137 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004138 if (EndLoc.isValid())
4139 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004140 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004141 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004142
Reid Spencer5f016e22007-07-11 17:01:13 +00004143 // If the specifier combination wasn't legal, issue a diagnostic.
4144 if (isInvalid) {
4145 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004146 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004147 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004148 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004149 }
4150}
4151
4152
4153/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4154///
4155void Parser::ParseDeclarator(Declarator &D) {
4156 /// This implements the 'declarator' production in the C grammar, then checks
4157 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004158 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004159}
4160
Richard Smith9988f282012-03-29 01:16:42 +00004161static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4162 if (Kind == tok::star || Kind == tok::caret)
4163 return true;
4164
4165 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4166 if (!Lang.CPlusPlus)
4167 return false;
4168
4169 return Kind == tok::amp || Kind == tok::ampamp;
4170}
4171
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004172/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4173/// is parsed by the function passed to it. Pass null, and the direct-declarator
4174/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004175/// ptr-operator production.
4176///
Richard Smith0706df42011-10-19 21:33:05 +00004177/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004178/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4179/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004180///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004181/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4182/// [C] pointer[opt] direct-declarator
4183/// [C++] direct-declarator
4184/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004185///
4186/// pointer: [C99 6.7.5]
4187/// '*' type-qualifier-list[opt]
4188/// '*' type-qualifier-list[opt] pointer
4189///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004190/// ptr-operator:
4191/// '*' cv-qualifier-seq[opt]
4192/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004193/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004194/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004195/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004196/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004197void Parser::ParseDeclaratorInternal(Declarator &D,
4198 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004199 if (Diags.hasAllExtensionsSilenced())
4200 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004201
Sebastian Redlf30208a2009-01-24 21:16:55 +00004202 // C++ member pointers start with a '::' or a nested-name.
4203 // Member pointers get special handling, since there's no place for the
4204 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004205 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004206 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4207 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004208 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4209 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004210 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004211 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004212
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004213 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004214 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004215 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004216 if (D.mayHaveIdentifier())
4217 D.getCXXScopeSpec() = SS;
4218 else
4219 AnnotateScopeToken(SS, true);
4220
Sebastian Redlf30208a2009-01-24 21:16:55 +00004221 if (DirectDeclParser)
4222 (this->*DirectDeclParser)(D);
4223 return;
4224 }
4225
4226 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004227 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004228 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004229 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004230 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004231
4232 // Recurse to parse whatever is left.
4233 ParseDeclaratorInternal(D, DirectDeclParser);
4234
4235 // Sema will have to catch (syntactically invalid) pointers into global
4236 // scope. It has to catch pointers into namespace scope anyway.
4237 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004238 Loc),
4239 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004240 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004241 return;
4242 }
4243 }
4244
4245 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004246 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004247 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004248 if (DirectDeclParser)
4249 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004250 return;
4251 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004252
Sebastian Redl05532f22009-03-15 22:02:01 +00004253 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4254 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004255 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004256 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004257
Chris Lattner9af55002009-03-27 04:18:06 +00004258 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004259 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004260 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004261
Richard Smith6ee326a2012-04-10 01:32:12 +00004262 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004263 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004264 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004265
Reid Spencer5f016e22007-07-11 17:01:13 +00004266 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004267 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004268 if (Kind == tok::star)
4269 // Remember that we parsed a pointer type, and remember the type-quals.
4270 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004271 DS.getConstSpecLoc(),
4272 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004273 DS.getRestrictSpecLoc()),
4274 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004275 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004276 else
4277 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004278 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004279 Loc),
4280 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004281 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004282 } else {
4283 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004284 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004285
Sebastian Redl743de1f2009-03-23 00:00:23 +00004286 // Complain about rvalue references in C++03, but then go on and build
4287 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004288 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004289 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004290 diag::warn_cxx98_compat_rvalue_reference :
4291 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004292
Richard Smith6ee326a2012-04-10 01:32:12 +00004293 // GNU-style and C++11 attributes are allowed here, as is restrict.
4294 ParseTypeQualifierListOpt(DS);
4295 D.ExtendWithDeclSpec(DS);
4296
Reid Spencer5f016e22007-07-11 17:01:13 +00004297 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4298 // cv-qualifiers are introduced through the use of a typedef or of a
4299 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004300 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4301 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4302 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004303 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004304 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4305 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004306 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004307 }
4308
4309 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004310 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004311
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004312 if (D.getNumTypeObjects() > 0) {
4313 // C++ [dcl.ref]p4: There shall be no references to references.
4314 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4315 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004316 if (const IdentifierInfo *II = D.getIdentifier())
4317 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4318 << II;
4319 else
4320 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4321 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004322
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004323 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004324 // can go ahead and build the (technically ill-formed)
4325 // declarator: reference collapsing will take care of it.
4326 }
4327 }
4328
Reid Spencer5f016e22007-07-11 17:01:13 +00004329 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004330 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004331 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004332 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004333 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004334 }
4335}
4336
Richard Smith9988f282012-03-29 01:16:42 +00004337static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4338 SourceLocation EllipsisLoc) {
4339 if (EllipsisLoc.isValid()) {
4340 FixItHint Insertion;
4341 if (!D.getEllipsisLoc().isValid()) {
4342 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4343 D.setEllipsisLoc(EllipsisLoc);
4344 }
4345 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4346 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4347 }
4348}
4349
Reid Spencer5f016e22007-07-11 17:01:13 +00004350/// ParseDirectDeclarator
4351/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004352/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004353/// '(' declarator ')'
4354/// [GNU] '(' attributes declarator ')'
4355/// [C90] direct-declarator '[' constant-expression[opt] ']'
4356/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4357/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4358/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4359/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004360/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4361/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004362/// direct-declarator '(' parameter-type-list ')'
4363/// direct-declarator '(' identifier-list[opt] ')'
4364/// [GNU] direct-declarator '(' parameter-forward-declarations
4365/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004366/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4367/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004368/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4369/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4370/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004371/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004372/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004373///
4374/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004375/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004376/// '::'[opt] nested-name-specifier[opt] type-name
4377///
4378/// id-expression: [C++ 5.1]
4379/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004380/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004381///
4382/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004383/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004384/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004385/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004386/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004387/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004388///
Richard Smith5d8388c2012-03-27 01:42:32 +00004389/// Note, any additional constructs added here may need corresponding changes
4390/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004391void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004392 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004393
David Blaikie4e4d0842012-03-11 07:00:24 +00004394 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004395 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004396 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004397 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4398 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004399 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004400 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004401 }
4402
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004403 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004404 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004405 // Change the declaration context for name lookup, until this function
4406 // is exited (and the declarator has been parsed).
4407 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004408 }
4409
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004410 // C++0x [dcl.fct]p14:
4411 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004412 // of a parameter-declaration-clause without a preceding comma. In
4413 // this case, the ellipsis is parsed as part of the
4414 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004415 // parameter pack that has not been expanded; otherwise, it is parsed
4416 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004417 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004418 !((D.getContext() == Declarator::PrototypeContext ||
4419 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004420 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004421 !Actions.containsUnexpandedParameterPacks(D))) {
4422 SourceLocation EllipsisLoc = ConsumeToken();
4423 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4424 // The ellipsis was put in the wrong place. Recover, and explain to
4425 // the user what they should have done.
4426 ParseDeclarator(D);
4427 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4428 return;
4429 } else
4430 D.setEllipsisLoc(EllipsisLoc);
4431
4432 // The ellipsis can't be followed by a parenthesized declarator. We
4433 // check for that in ParseParenDeclarator, after we have disambiguated
4434 // the l_paren token.
4435 }
4436
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004437 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4438 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4439 // We found something that indicates the start of an unqualified-id.
4440 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004441 bool AllowConstructorName;
4442 if (D.getDeclSpec().hasTypeSpecifier())
4443 AllowConstructorName = false;
4444 else if (D.getCXXScopeSpec().isSet())
4445 AllowConstructorName =
4446 (D.getContext() == Declarator::FileContext ||
4447 (D.getContext() == Declarator::MemberContext &&
4448 D.getDeclSpec().isFriendSpecified()));
4449 else
4450 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4451
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004452 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004453 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4454 /*EnteringContext=*/true,
4455 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004456 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004457 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004458 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004459 D.getName()) ||
4460 // Once we're past the identifier, if the scope was bad, mark the
4461 // whole declarator bad.
4462 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004463 D.SetIdentifier(0, Tok.getLocation());
4464 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004465 } else {
4466 // Parsed the unqualified-id; update range information and move along.
4467 if (D.getSourceRange().getBegin().isInvalid())
4468 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4469 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004470 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004471 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004472 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004473 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004474 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004475 "There's a C++-specific check for tok::identifier above");
4476 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4477 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4478 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004479 goto PastIdentifier;
4480 }
Richard Smith9988f282012-03-29 01:16:42 +00004481
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004482 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004483 // direct-declarator: '(' declarator ')'
4484 // direct-declarator: '(' attributes declarator ')'
4485 // Example: 'char (*X)' or 'int (*XX)(void)'
4486 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004487
4488 // If the declarator was parenthesized, we entered the declarator
4489 // scope when parsing the parenthesized declarator, then exited
4490 // the scope already. Re-enter the scope, if we need to.
4491 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004492 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004493 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004494 if (!D.isInvalidType() &&
4495 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004496 // Change the declaration context for name lookup, until this function
4497 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004498 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004499 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004500 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004501 // This could be something simple like "int" (in which case the declarator
4502 // portion is empty), if an abstract-declarator is allowed.
4503 D.SetIdentifier(0, Tok.getLocation());
4504 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004505 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004506 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004507 if (D.getContext() == Declarator::MemberContext)
4508 Diag(Tok, diag::err_expected_member_name_or_semi)
4509 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004510 else if (getLangOpts().CPlusPlus) {
4511 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4512 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4513 else
4514 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4515 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004516 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004517 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004518 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004519 }
Mike Stump1eb44332009-09-09 15:08:12 +00004520
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004521 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004522 assert(D.isPastIdentifier() &&
4523 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004524
Richard Smith6ee326a2012-04-10 01:32:12 +00004525 // Don't parse attributes unless we have parsed an unparenthesized name.
4526 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004527 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004528
Reid Spencer5f016e22007-07-11 17:01:13 +00004529 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004530 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004531 // Enter function-declaration scope, limiting any declarators to the
4532 // function prototype scope, including parameter declarators.
4533 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004534 Scope::FunctionPrototypeScope|Scope::DeclScope|
4535 (D.isFunctionDeclaratorAFunctionDeclaration()
4536 ? Scope::FunctionDeclarationScope : 0));
4537
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004538 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4539 // In such a case, check if we actually have a function declarator; if it
4540 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004541 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004542 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4543 // The name of the declarator, if any, is tentatively declared within
4544 // a possible direct initializer.
4545 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4546 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4547 TentativelyDeclaredIdentifiers.pop_back();
4548 if (!IsFunctionDecl)
4549 break;
4550 }
John McCall0b7e6782011-03-24 11:26:52 +00004551 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004552 BalancedDelimiterTracker T(*this, tok::l_paren);
4553 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004554 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004555 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004556 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004557 ParseBracketDeclarator(D);
4558 } else {
4559 break;
4560 }
4561 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004562}
Reid Spencer5f016e22007-07-11 17:01:13 +00004563
Chris Lattneref4715c2008-04-06 05:45:57 +00004564/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4565/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004566/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004567/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4568///
4569/// direct-declarator:
4570/// '(' declarator ')'
4571/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004572/// direct-declarator '(' parameter-type-list ')'
4573/// direct-declarator '(' identifier-list[opt] ')'
4574/// [GNU] direct-declarator '(' parameter-forward-declarations
4575/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004576///
4577void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004578 BalancedDelimiterTracker T(*this, tok::l_paren);
4579 T.consumeOpen();
4580
Chris Lattneref4715c2008-04-06 05:45:57 +00004581 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004582
Chris Lattner7399ee02008-10-20 02:05:46 +00004583 // Eat any attributes before we look at whether this is a grouping or function
4584 // declarator paren. If this is a grouping paren, the attribute applies to
4585 // the type being built up, for example:
4586 // int (__attribute__(()) *x)(long y)
4587 // If this ends up not being a grouping paren, the attribute applies to the
4588 // first argument, for example:
4589 // int (__attribute__(()) int x)
4590 // In either case, we need to eat any attributes to be able to determine what
4591 // sort of paren this is.
4592 //
John McCall0b7e6782011-03-24 11:26:52 +00004593 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004594 bool RequiresArg = false;
4595 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004596 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004597
Chris Lattner7399ee02008-10-20 02:05:46 +00004598 // We require that the argument list (if this is a non-grouping paren) be
4599 // present even if the attribute list was empty.
4600 RequiresArg = true;
4601 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004602
Steve Naroff239f0732008-12-25 14:16:32 +00004603 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004604 ParseMicrosoftTypeAttributes(attrs);
4605
Dawn Perchik52fc3142010-09-03 01:29:35 +00004606 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004607 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004608 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004609
Chris Lattneref4715c2008-04-06 05:45:57 +00004610 // If we haven't past the identifier yet (or where the identifier would be
4611 // stored, if this is an abstract declarator), then this is probably just
4612 // grouping parens. However, if this could be an abstract-declarator, then
4613 // this could also be the start of function arguments (consider 'void()').
4614 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004615
Chris Lattneref4715c2008-04-06 05:45:57 +00004616 if (!D.mayOmitIdentifier()) {
4617 // If this can't be an abstract-declarator, this *must* be a grouping
4618 // paren, because we haven't seen the identifier yet.
4619 isGrouping = true;
4620 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004621 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4622 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004623 isDeclarationSpecifier() || // 'int(int)' is a function.
4624 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004625 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4626 // considered to be a type, not a K&R identifier-list.
4627 isGrouping = false;
4628 } else {
4629 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4630 isGrouping = true;
4631 }
Mike Stump1eb44332009-09-09 15:08:12 +00004632
Chris Lattneref4715c2008-04-06 05:45:57 +00004633 // If this is a grouping paren, handle:
4634 // direct-declarator: '(' declarator ')'
4635 // direct-declarator: '(' attributes declarator ')'
4636 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004637 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4638 D.setEllipsisLoc(SourceLocation());
4639
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004640 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004641 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004642 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004643 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004644 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004645 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004646 T.getCloseLocation()),
4647 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004648
4649 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004650
4651 // An ellipsis cannot be placed outside parentheses.
4652 if (EllipsisLoc.isValid())
4653 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4654
Chris Lattneref4715c2008-04-06 05:45:57 +00004655 return;
4656 }
Mike Stump1eb44332009-09-09 15:08:12 +00004657
Chris Lattneref4715c2008-04-06 05:45:57 +00004658 // Okay, if this wasn't a grouping paren, it must be the start of a function
4659 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004660 // identifier (and remember where it would have been), then call into
4661 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004662 D.SetIdentifier(0, Tok.getLocation());
4663
David Blaikie42d6d0c2011-12-04 05:04:18 +00004664 // Enter function-declaration scope, limiting any declarators to the
4665 // function prototype scope, including parameter declarators.
4666 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004667 Scope::FunctionPrototypeScope | Scope::DeclScope |
4668 (D.isFunctionDeclaratorAFunctionDeclaration()
4669 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004670 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004671 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004672}
4673
4674/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4675/// declarator D up to a paren, which indicates that we are parsing function
4676/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004677///
Richard Smith6ee326a2012-04-10 01:32:12 +00004678/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4679/// immediately after the open paren - they should be considered to be the
4680/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004681///
Richard Smith6ee326a2012-04-10 01:32:12 +00004682/// If RequiresArg is true, then the first argument of the function is required
4683/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004684///
Richard Smith6ee326a2012-04-10 01:32:12 +00004685/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4686/// (C++11) ref-qualifier[opt], exception-specification[opt],
4687/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4688///
4689/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004690/// dynamic-exception-specification
4691/// noexcept-specification
4692///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004693void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004694 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004695 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004696 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004697 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004698 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004699 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004700 // lparen is already consumed!
4701 assert(D.isPastIdentifier() && "Should not call before identifier!");
4702
4703 // This should be true when the function has typed arguments.
4704 // Otherwise, it is treated as a K&R-style function.
4705 bool HasProto = false;
4706 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004707 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004708 // Remember where we see an ellipsis, if any.
4709 SourceLocation EllipsisLoc;
4710
4711 DeclSpec DS(AttrFactory);
4712 bool RefQualifierIsLValueRef = true;
4713 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004714 SourceLocation ConstQualifierLoc;
4715 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004716 ExceptionSpecificationType ESpecType = EST_None;
4717 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004718 SmallVector<ParsedType, 2> DynamicExceptions;
4719 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004720 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004721 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004722 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004723
James Molloy16f1f712012-02-29 10:24:19 +00004724 Actions.ActOnStartFunctionDeclarator();
4725
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004726 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4727 EndLoc is the end location for the function declarator.
4728 They differ for trailing return types. */
4729 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004730 SourceLocation LParenLoc, RParenLoc;
4731 LParenLoc = Tracker.getOpenLocation();
4732 StartLoc = LParenLoc;
4733
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004734 if (isFunctionDeclaratorIdentifierList()) {
4735 if (RequiresArg)
4736 Diag(Tok, diag::err_argument_required_after_attribute);
4737
4738 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4739
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004740 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004741 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004742 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004743 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004744 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004745 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004746 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004747 else if (RequiresArg)
4748 Diag(Tok, diag::err_argument_required_after_attribute);
4749
David Blaikie4e4d0842012-03-11 07:00:24 +00004750 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004751
4752 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004753 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004754 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004755 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004756 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004757
David Blaikie4e4d0842012-03-11 07:00:24 +00004758 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004759 // FIXME: Accept these components in any order, and produce fixits to
4760 // correct the order if the user gets it wrong. Ideally we should deal
4761 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004762
4763 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004764 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4765 if (!DS.getSourceRange().getEnd().isInvalid()) {
4766 EndLoc = DS.getSourceRange().getEnd();
4767 ConstQualifierLoc = DS.getConstSpecLoc();
4768 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4769 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004770
4771 // Parse ref-qualifier[opt].
4772 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004773 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004774 diag::warn_cxx98_compat_ref_qualifier :
4775 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004776
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004777 RefQualifierIsLValueRef = Tok.is(tok::amp);
4778 RefQualifierLoc = ConsumeToken();
4779 EndLoc = RefQualifierLoc;
4780 }
4781
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004782 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004783 // If a declaration declares a member function or member function
4784 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004785 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004786 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004787 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004788 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004789 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004790 (D.getContext() == Declarator::MemberContext ||
4791 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004792 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004793 Actions.CurContext->isRecord()));
4794 Sema::CXXThisScopeRAII ThisScope(Actions,
4795 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004796 DS.getTypeQualifiers() |
4797 (D.getDeclSpec().isConstexprSpecified()
4798 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004799 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004800
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004801 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004802 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004803 DynamicExceptions,
4804 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004805 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004806 if (ESpecType != EST_None)
4807 EndLoc = ESpecRange.getEnd();
4808
Richard Smith6ee326a2012-04-10 01:32:12 +00004809 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4810 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004811 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004812
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004813 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004814 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004815 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004816 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004817 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4818 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004819 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004820 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004821 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004822 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004823 }
4824 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004825 }
4826
4827 // Remember that we parsed a function type, and remember the attributes.
4828 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004829 IsAmbiguous,
4830 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004831 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004832 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004833 DS.getTypeQualifiers(),
4834 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004835 RefQualifierLoc, ConstQualifierLoc,
4836 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004837 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004838 ESpecType, ESpecRange.getBegin(),
4839 DynamicExceptions.data(),
4840 DynamicExceptionRanges.data(),
4841 DynamicExceptions.size(),
4842 NoexceptExpr.isUsable() ?
4843 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004844 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004845 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004846 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004847
4848 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004849}
4850
4851/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4852/// identifier list form for a K&R-style function: void foo(a,b,c)
4853///
4854/// Note that identifier-lists are only allowed for normal declarators, not for
4855/// abstract-declarators.
4856bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004857 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004858 && Tok.is(tok::identifier)
4859 && !TryAltiVecVectorToken()
4860 // K&R identifier lists can't have typedefs as identifiers, per C99
4861 // 6.7.5.3p11.
4862 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4863 // Identifier lists follow a really simple grammar: the identifiers can
4864 // be followed *only* by a ", identifier" or ")". However, K&R
4865 // identifier lists are really rare in the brave new modern world, and
4866 // it is very common for someone to typo a type in a non-K&R style
4867 // list. If we are presented with something like: "void foo(intptr x,
4868 // float y)", we don't want to start parsing the function declarator as
4869 // though it is a K&R style declarator just because intptr is an
4870 // invalid type.
4871 //
4872 // To handle this, we check to see if the token after the first
4873 // identifier is a "," or ")". Only then do we parse it as an
4874 // identifier list.
4875 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4876}
4877
4878/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4879/// we found a K&R-style identifier list instead of a typed parameter list.
4880///
4881/// After returning, ParamInfo will hold the parsed parameters.
4882///
4883/// identifier-list: [C99 6.7.5]
4884/// identifier
4885/// identifier-list ',' identifier
4886///
4887void Parser::ParseFunctionDeclaratorIdentifierList(
4888 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004889 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004890 // If there was no identifier specified for the declarator, either we are in
4891 // an abstract-declarator, or we are in a parameter declarator which was found
4892 // to be abstract. In abstract-declarators, identifier lists are not valid:
4893 // diagnose this.
4894 if (!D.getIdentifier())
4895 Diag(Tok, diag::ext_ident_list_in_param);
4896
4897 // Maintain an efficient lookup of params we have seen so far.
4898 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4899
4900 while (1) {
4901 // If this isn't an identifier, report the error and skip until ')'.
4902 if (Tok.isNot(tok::identifier)) {
4903 Diag(Tok, diag::err_expected_ident);
4904 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4905 // Forget we parsed anything.
4906 ParamInfo.clear();
4907 return;
4908 }
4909
4910 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4911
4912 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4913 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4914 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4915
4916 // Verify that the argument identifier has not already been mentioned.
4917 if (!ParamsSoFar.insert(ParmII)) {
4918 Diag(Tok, diag::err_param_redefinition) << ParmII;
4919 } else {
4920 // Remember this identifier in ParamInfo.
4921 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4922 Tok.getLocation(),
4923 0));
4924 }
4925
4926 // Eat the identifier.
4927 ConsumeToken();
4928
4929 // The list continues if we see a comma.
4930 if (Tok.isNot(tok::comma))
4931 break;
4932 ConsumeToken();
4933 }
4934}
4935
4936/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4937/// after the opening parenthesis. This function will not parse a K&R-style
4938/// identifier list.
4939///
Richard Smith6ce48a72012-04-11 04:01:28 +00004940/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4941/// caller parsed those arguments immediately after the open paren - they should
4942/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004943///
4944/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4945/// be the location of the ellipsis, if any was parsed.
4946///
Reid Spencer5f016e22007-07-11 17:01:13 +00004947/// parameter-type-list: [C99 6.7.5]
4948/// parameter-list
4949/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004950/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004951///
4952/// parameter-list: [C99 6.7.5]
4953/// parameter-declaration
4954/// parameter-list ',' parameter-declaration
4955///
4956/// parameter-declaration: [C99 6.7.5]
4957/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004958/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004959/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004960/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004961/// declaration-specifiers abstract-declarator[opt]
4962/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004963/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004964/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004965/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004966///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004967void Parser::ParseParameterDeclarationClause(
4968 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004969 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004970 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004971 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004972
Chris Lattnerf97409f2008-04-06 06:57:35 +00004973 while (1) {
4974 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004975 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4976 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004977 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004978 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004979 }
Mike Stump1eb44332009-09-09 15:08:12 +00004980
Chris Lattnerf97409f2008-04-06 06:57:35 +00004981 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004982 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004983 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004984
Richard Smith6ce48a72012-04-11 04:01:28 +00004985 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004986 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00004987
John McCall7f040a92010-12-24 02:08:15 +00004988 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00004989 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00004990
4991 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004992
4993 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004994 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004995 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004996 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4997 // too much hassle.
4998 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004999
Chris Lattnere64c5492009-02-27 18:38:20 +00005000 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005001
Chris Lattnerf97409f2008-04-06 06:57:35 +00005002 // Parse the declarator. This is "PrototypeContext", because we must
5003 // accept either 'declarator' or 'abstract-declarator' here.
5004 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5005 ParseDeclarator(ParmDecl);
5006
5007 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005008 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005009
Chris Lattnerf97409f2008-04-06 06:57:35 +00005010 // Remember this parsed parameter in ParamInfo.
5011 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005012
Douglas Gregor72b505b2008-12-16 21:30:33 +00005013 // DefArgToks is used when the parsing of default arguments needs
5014 // to be delayed.
5015 CachedTokens *DefArgToks = 0;
5016
Chris Lattnerf97409f2008-04-06 06:57:35 +00005017 // If no parameter was specified, verify that *something* was specified,
5018 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005019 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5020 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005021 // Completely missing, emit error.
5022 Diag(DSStart, diag::err_missing_param);
5023 } else {
5024 // Otherwise, we have something. Add it and let semantic analysis try
5025 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005026
Chris Lattnerf97409f2008-04-06 06:57:35 +00005027 // Inform the actions module about the parameter declarator, so it gets
5028 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005029 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005030
5031 // Parse the default argument, if any. We parse the default
5032 // arguments in all dialects; the semantic analysis in
5033 // ActOnParamDefaultArgument will reject the default argument in
5034 // C.
5035 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005036 SourceLocation EqualLoc = Tok.getLocation();
5037
Chris Lattner04421082008-04-08 04:40:51 +00005038 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005039 if (D.getContext() == Declarator::MemberContext) {
5040 // If we're inside a class definition, cache the tokens
5041 // corresponding to the default argument. We'll actually parse
5042 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005043 // FIXME: Can we use a smart pointer for Toks?
5044 DefArgToks = new CachedTokens;
5045
Mike Stump1eb44332009-09-09 15:08:12 +00005046 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005047 /*StopAtSemi=*/true,
5048 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005049 delete DefArgToks;
5050 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005051 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005052 } else {
5053 // Mark the end of the default argument so that we know when to
5054 // stop when we parse it later on.
5055 Token DefArgEnd;
5056 DefArgEnd.startToken();
5057 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5058 DefArgEnd.setLocation(Tok.getLocation());
5059 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005060 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005061 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005062 }
Chris Lattner04421082008-04-08 04:40:51 +00005063 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005064 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005065 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005066
Chad Rosier8decdee2012-06-26 22:30:43 +00005067 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005068 // used.
5069 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005070 Sema::PotentiallyEvaluatedIfUsed,
5071 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005072
Sebastian Redl84407ba2012-03-14 15:54:00 +00005073 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005074 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005075 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005076 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005077 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005078 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005079 if (DefArgResult.isInvalid()) {
5080 Actions.ActOnParamDefaultArgumentError(Param);
5081 SkipUntil(tok::comma, tok::r_paren, true, true);
5082 } else {
5083 // Inform the actions module about the default argument
5084 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005085 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005086 }
Chris Lattner04421082008-04-08 04:40:51 +00005087 }
5088 }
Mike Stump1eb44332009-09-09 15:08:12 +00005089
5090 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5091 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005092 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005093 }
5094
5095 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005096 if (Tok.isNot(tok::comma)) {
5097 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005098 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005099
David Blaikie4e4d0842012-03-11 07:00:24 +00005100 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005101 // We have ellipsis without a preceding ',', which is ill-formed
5102 // in C. Complain and provide the fix.
5103 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005104 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005105 }
5106 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005107
Douglas Gregored5d6512009-09-22 21:41:40 +00005108 break;
5109 }
Mike Stump1eb44332009-09-09 15:08:12 +00005110
Chris Lattnerf97409f2008-04-06 06:57:35 +00005111 // Consume the comma.
5112 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005113 }
Mike Stump1eb44332009-09-09 15:08:12 +00005114
Chris Lattner66d28652008-04-06 06:34:08 +00005115}
Chris Lattneref4715c2008-04-06 05:45:57 +00005116
Reid Spencer5f016e22007-07-11 17:01:13 +00005117/// [C90] direct-declarator '[' constant-expression[opt] ']'
5118/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5119/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5120/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5121/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005122/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5123/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005124void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005125 if (CheckProhibitedCXX11Attribute())
5126 return;
5127
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005128 BalancedDelimiterTracker T(*this, tok::l_square);
5129 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005130
Chris Lattner378c7e42008-12-18 07:27:21 +00005131 // C array syntax has many features, but by-far the most common is [] and [4].
5132 // This code does a fast path to handle some of the most obvious cases.
5133 if (Tok.getKind() == tok::r_square) {
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);
Chad Rosier8decdee2012-06-26 22:30:43 +00005137
Chris Lattner378c7e42008-12-18 07:27:21 +00005138 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005139 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005140 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
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 } else if (Tok.getKind() == tok::numeric_constant &&
5146 GetLookAheadToken(1).is(tok::r_square)) {
5147 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005148 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005149 ConsumeToken();
5150
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005151 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005152 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005153 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005154
Chris Lattner378c7e42008-12-18 07:27:21 +00005155 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005156 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005157 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005158 T.getOpenLocation(),
5159 T.getCloseLocation()),
5160 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005161 return;
5162 }
Mike Stump1eb44332009-09-09 15:08:12 +00005163
Reid Spencer5f016e22007-07-11 17:01:13 +00005164 // If valid, this location is the position where we read the 'static' keyword.
5165 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005166 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005167 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005168
Reid Spencer5f016e22007-07-11 17:01:13 +00005169 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005170 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005171 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005172 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005173
Reid Spencer5f016e22007-07-11 17:01:13 +00005174 // If we haven't already read 'static', check to see if there is one after the
5175 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005176 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005177 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005178
Reid Spencer5f016e22007-07-11 17:01:13 +00005179 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5180 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005181 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005182
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005183 // Handle the case where we have '[*]' as the array size. However, a leading
5184 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005185 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005186 // infrequent, use of lookahead is not costly here.
5187 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005188 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005189
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005190 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005191 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005192 StaticLoc = SourceLocation(); // Drop the static.
5193 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005194 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005195 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005196 // Note, in C89, this production uses the constant-expr production instead
5197 // of assignment-expr. The only difference is that assignment-expr allows
5198 // things like '=' and '*='. Sema rejects these in C89 mode because they
5199 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005200
Douglas Gregore0762c92009-06-19 23:52:42 +00005201 // Parse the constant-expression or assignment-expression now (depending
5202 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005203 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005204 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005205 } else {
5206 EnterExpressionEvaluationContext Unevaluated(Actions,
5207 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005208 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005209 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005210 }
Mike Stump1eb44332009-09-09 15:08:12 +00005211
Reid Spencer5f016e22007-07-11 17:01:13 +00005212 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005213 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005214 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005215 // If the expression was invalid, skip it.
5216 SkipUntil(tok::r_square);
5217 return;
5218 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005219
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005220 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005221
John McCall0b7e6782011-03-24 11:26:52 +00005222 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005223 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005224
Chris Lattner378c7e42008-12-18 07:27:21 +00005225 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005226 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005227 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005228 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005229 T.getOpenLocation(),
5230 T.getCloseLocation()),
5231 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005232}
5233
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005234/// [GNU] typeof-specifier:
5235/// typeof ( expressions )
5236/// typeof ( type-name )
5237/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005238///
5239void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005240 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005241 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005242 SourceLocation StartLoc = ConsumeToken();
5243
John McCallcfb708c2010-01-13 20:03:27 +00005244 const bool hasParens = Tok.is(tok::l_paren);
5245
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005246 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5247 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005248
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005249 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005250 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005251 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005252 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5253 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005254 if (hasParens)
5255 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005256
5257 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005258 // FIXME: Not accurate, the range gets one token more than it should.
5259 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005260 else
5261 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005262
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005263 if (isCastExpr) {
5264 if (!CastTy) {
5265 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005266 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005267 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005268
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005269 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005270 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005271 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5272 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005273 DiagID, CastTy))
5274 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005275 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005276 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005277
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005278 // If we get here, the operand to the typeof was an expresion.
5279 if (Operand.isInvalid()) {
5280 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005281 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005282 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005283
Eli Friedman71b8fb52012-01-21 01:01:51 +00005284 // We might need to transform the operand if it is potentially evaluated.
5285 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5286 if (Operand.isInvalid()) {
5287 DS.SetTypeSpecError();
5288 return;
5289 }
5290
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005291 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005292 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005293 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5294 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005295 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005296 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005297}
Chris Lattner1b492422010-02-28 18:33:55 +00005298
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005299/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005300/// _Atomic ( type-name )
5301///
5302void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5303 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5304
5305 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005306 BalancedDelimiterTracker T(*this, tok::l_paren);
5307 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005308 SkipUntil(tok::r_paren);
5309 return;
5310 }
5311
5312 TypeResult Result = ParseTypeName();
5313 if (Result.isInvalid()) {
5314 SkipUntil(tok::r_paren);
5315 return;
5316 }
5317
5318 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005319 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005320
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005321 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005322 return;
5323
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005324 DS.setTypeofParensRange(T.getRange());
5325 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005326
5327 const char *PrevSpec = 0;
5328 unsigned DiagID;
5329 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5330 DiagID, Result.release()))
5331 Diag(StartLoc, DiagID) << PrevSpec;
5332}
5333
Chris Lattner1b492422010-02-28 18:33:55 +00005334
5335/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5336/// from TryAltiVecVectorToken.
5337bool Parser::TryAltiVecVectorTokenOutOfLine() {
5338 Token Next = NextToken();
5339 switch (Next.getKind()) {
5340 default: return false;
5341 case tok::kw_short:
5342 case tok::kw_long:
5343 case tok::kw_signed:
5344 case tok::kw_unsigned:
5345 case tok::kw_void:
5346 case tok::kw_char:
5347 case tok::kw_int:
5348 case tok::kw_float:
5349 case tok::kw_double:
5350 case tok::kw_bool:
5351 case tok::kw___pixel:
5352 Tok.setKind(tok::kw___vector);
5353 return true;
5354 case tok::identifier:
5355 if (Next.getIdentifierInfo() == Ident_pixel) {
5356 Tok.setKind(tok::kw___vector);
5357 return true;
5358 }
5359 return false;
5360 }
5361}
5362
5363bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5364 const char *&PrevSpec, unsigned &DiagID,
5365 bool &isInvalid) {
5366 if (Tok.getIdentifierInfo() == Ident_vector) {
5367 Token Next = NextToken();
5368 switch (Next.getKind()) {
5369 case tok::kw_short:
5370 case tok::kw_long:
5371 case tok::kw_signed:
5372 case tok::kw_unsigned:
5373 case tok::kw_void:
5374 case tok::kw_char:
5375 case tok::kw_int:
5376 case tok::kw_float:
5377 case tok::kw_double:
5378 case tok::kw_bool:
5379 case tok::kw___pixel:
5380 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5381 return true;
5382 case tok::identifier:
5383 if (Next.getIdentifierInfo() == Ident_pixel) {
5384 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5385 return true;
5386 }
5387 break;
5388 default:
5389 break;
5390 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005391 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005392 DS.isTypeAltiVecVector()) {
5393 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5394 return true;
5395 }
5396 return false;
5397}