blob: 8e24a14d0b93d417d950d11d9b8c5b3a4f8a91ca [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)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001026 ExprResult ArgExpr(ParseAssignmentExpression());
1027 if (ArgExpr.isInvalid()) {
1028 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001029 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001030 break;
1031 } else {
1032 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001033 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001034 if (Tok.isNot(tok::comma))
1035 break;
1036 ConsumeToken(); // Eat the comma, move to the next argument
1037 }
1038 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001039 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001040 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001041 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001042 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001043 if (EndLoc)
1044 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001045}
1046
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001047void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1048 SourceLocation AttrNameLoc,
1049 ParsedAttributes &Attrs,
1050 SourceLocation *EndLoc) {
1051 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1052
1053 BalancedDelimiterTracker T(*this, tok::l_paren);
1054 T.consumeOpen();
1055
1056 if (Tok.isNot(tok::identifier)) {
1057 Diag(Tok, diag::err_expected_ident);
1058 T.skipToEnd();
1059 return;
1060 }
1061 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1062 SourceLocation ArgumentKindLoc = ConsumeToken();
1063
1064 if (Tok.isNot(tok::comma)) {
1065 Diag(Tok, diag::err_expected_comma);
1066 T.skipToEnd();
1067 return;
1068 }
1069 ConsumeToken();
1070
1071 SourceRange MatchingCTypeRange;
1072 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1073 if (MatchingCType.isInvalid()) {
1074 T.skipToEnd();
1075 return;
1076 }
1077
1078 bool LayoutCompatible = false;
1079 bool MustBeNull = false;
1080 while (Tok.is(tok::comma)) {
1081 ConsumeToken();
1082 if (Tok.isNot(tok::identifier)) {
1083 Diag(Tok, diag::err_expected_ident);
1084 T.skipToEnd();
1085 return;
1086 }
1087 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1088 if (Flag->isStr("layout_compatible"))
1089 LayoutCompatible = true;
1090 else if (Flag->isStr("must_be_null"))
1091 MustBeNull = true;
1092 else {
1093 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1094 T.skipToEnd();
1095 return;
1096 }
1097 ConsumeToken(); // consume flag
1098 }
1099
1100 if (!T.consumeClose()) {
1101 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1102 ArgumentKind, ArgumentKindLoc,
1103 MatchingCType.release(), LayoutCompatible,
1104 MustBeNull, AttributeList::AS_GNU);
1105 }
1106
1107 if (EndLoc)
1108 *EndLoc = T.getCloseLocation();
1109}
1110
Richard Smith6ee326a2012-04-10 01:32:12 +00001111/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1112/// of a C++11 attribute-specifier in a location where an attribute is not
1113/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1114/// situation.
1115///
1116/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1117/// this doesn't appear to actually be an attribute-specifier, and the caller
1118/// should try to parse it.
1119bool Parser::DiagnoseProhibitedCXX11Attribute() {
1120 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1121
1122 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1123 case CAK_NotAttributeSpecifier:
1124 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1125 return false;
1126
1127 case CAK_InvalidAttributeSpecifier:
1128 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1129 return false;
1130
1131 case CAK_AttributeSpecifier:
1132 // Parse and discard the attributes.
1133 SourceLocation BeginLoc = ConsumeBracket();
1134 ConsumeBracket();
1135 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1136 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1137 SourceLocation EndLoc = ConsumeBracket();
1138 Diag(BeginLoc, diag::err_attributes_not_allowed)
1139 << SourceRange(BeginLoc, EndLoc);
1140 return true;
1141 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001142 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001143}
1144
John McCall7f040a92010-12-24 02:08:15 +00001145void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1146 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1147 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001148}
1149
Michael Hanf64231e2012-11-06 19:34:54 +00001150void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1151 AttributeList *AttrList = attrs.getList();
1152 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001153 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001154 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001155 << AttrList->getName();
1156 AttrList->setInvalid();
1157 }
1158 AttrList = AttrList->getNext();
1159 }
1160}
1161
Reid Spencer5f016e22007-07-11 17:01:13 +00001162/// ParseDeclaration - Parse a full 'declaration', which consists of
1163/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001164/// 'Context' should be a Declarator::TheContext value. This returns the
1165/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001166///
1167/// declaration: [C99 6.7]
1168/// block-declaration ->
1169/// simple-declaration
1170/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001171/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001172/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001173/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001174/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001175/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001176/// others... [FIXME]
1177///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001178Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1179 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001180 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001181 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001182 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001183 // Must temporarily exit the objective-c container scope for
1184 // parsing c none objective-c decls.
1185 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001186
John McCalld226f652010-08-21 09:40:31 +00001187 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001188 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001189 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001190 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001191 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001192 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001193 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001194 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001195 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001196 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001197 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001198 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001199 SourceLocation InlineLoc = ConsumeToken();
1200 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1201 break;
1202 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001203 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001204 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001205 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001206 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001207 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001208 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001209 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001210 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001211 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001212 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001213 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001214 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001215 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001216 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001217 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001218 default:
John McCall7f040a92010-12-24 02:08:15 +00001219 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001220 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001221
Chris Lattner682bf922009-03-29 16:50:03 +00001222 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001223 // single decl, convert it now. Alias declarations can also declare a type;
1224 // include that too if it is present.
1225 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001226}
1227
1228/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1229/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001230/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1231/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001232///[C90/C++]init-declarator-list ';' [TODO]
1233/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001234///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001235/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001236/// attribute-specifier-seq[opt] type-specifier-seq declarator
1237///
Chris Lattnercd147752009-03-29 17:27:48 +00001238/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001239/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001240///
1241/// If FRI is non-null, we might be parsing a for-range-declaration instead
1242/// of a simple-declaration. If we find that we are, we also parse the
1243/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001244Parser::DeclGroupPtrTy
1245Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1246 SourceLocation &DeclEnd,
1247 ParsedAttributesWithRange &attrs,
1248 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001250 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001251 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001252
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001253 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001254 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1257 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001258 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001259 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001260 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001261 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001262 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001263 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001264 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001266
1267 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001268}
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Richard Smith0706df42011-10-19 21:33:05 +00001270/// Returns true if this might be the start of a declarator, or a common typo
1271/// for a declarator.
1272bool Parser::MightBeDeclarator(unsigned Context) {
1273 switch (Tok.getKind()) {
1274 case tok::annot_cxxscope:
1275 case tok::annot_template_id:
1276 case tok::caret:
1277 case tok::code_completion:
1278 case tok::coloncolon:
1279 case tok::ellipsis:
1280 case tok::kw___attribute:
1281 case tok::kw_operator:
1282 case tok::l_paren:
1283 case tok::star:
1284 return true;
1285
1286 case tok::amp:
1287 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001288 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001289
Richard Smith1c94c162012-01-09 22:31:44 +00001290 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001291 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001292 NextToken().is(tok::l_square);
1293
1294 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001295 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001296
Richard Smith0706df42011-10-19 21:33:05 +00001297 case tok::identifier:
1298 switch (NextToken().getKind()) {
1299 case tok::code_completion:
1300 case tok::coloncolon:
1301 case tok::comma:
1302 case tok::equal:
1303 case tok::equalequal: // Might be a typo for '='.
1304 case tok::kw_alignas:
1305 case tok::kw_asm:
1306 case tok::kw___attribute:
1307 case tok::l_brace:
1308 case tok::l_paren:
1309 case tok::l_square:
1310 case tok::less:
1311 case tok::r_brace:
1312 case tok::r_paren:
1313 case tok::r_square:
1314 case tok::semi:
1315 return true;
1316
1317 case tok::colon:
1318 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001319 // and in block scope it's probably a label. Inside a class definition,
1320 // this is a bit-field.
1321 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001322 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001323
1324 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001325 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001326
1327 default:
1328 return false;
1329 }
1330
1331 default:
1332 return false;
1333 }
1334}
1335
Richard Smith994d73f2012-04-11 20:59:20 +00001336/// Skip until we reach something which seems like a sensible place to pick
1337/// up parsing after a malformed declaration. This will sometimes stop sooner
1338/// than SkipUntil(tok::r_brace) would, but will never stop later.
1339void Parser::SkipMalformedDecl() {
1340 while (true) {
1341 switch (Tok.getKind()) {
1342 case tok::l_brace:
1343 // Skip until matching }, then stop. We've probably skipped over
1344 // a malformed class or function definition or similar.
1345 ConsumeBrace();
1346 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1347 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1348 // This declaration isn't over yet. Keep skipping.
1349 continue;
1350 }
1351 if (Tok.is(tok::semi))
1352 ConsumeToken();
1353 return;
1354
1355 case tok::l_square:
1356 ConsumeBracket();
1357 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1358 continue;
1359
1360 case tok::l_paren:
1361 ConsumeParen();
1362 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1363 continue;
1364
1365 case tok::r_brace:
1366 return;
1367
1368 case tok::semi:
1369 ConsumeToken();
1370 return;
1371
1372 case tok::kw_inline:
1373 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001374 // a good place to pick back up parsing, except in an Objective-C
1375 // @interface context.
1376 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1377 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001378 return;
1379 break;
1380
1381 case tok::kw_namespace:
1382 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001383 // place to pick back up parsing, except in an Objective-C
1384 // @interface context.
1385 if (Tok.isAtStartOfLine() &&
1386 (!ParsingInObjCContainer || CurParsedObjCImpl))
1387 return;
1388 break;
1389
1390 case tok::at:
1391 // @end is very much like } in Objective-C contexts.
1392 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1393 ParsingInObjCContainer)
1394 return;
1395 break;
1396
1397 case tok::minus:
1398 case tok::plus:
1399 // - and + probably start new method declarations in Objective-C contexts.
1400 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001401 return;
1402 break;
1403
1404 case tok::eof:
1405 return;
1406
1407 default:
1408 break;
1409 }
1410
1411 ConsumeAnyToken();
1412 }
1413}
1414
John McCalld8ac0572009-11-03 19:26:08 +00001415/// ParseDeclGroup - Having concluded that this is either a function
1416/// definition or a group of object declarations, actually parse the
1417/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001418Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1419 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001420 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001421 SourceLocation *DeclEnd,
1422 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001423 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001424 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001425 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001426
John McCalld8ac0572009-11-03 19:26:08 +00001427 // Bail out if the first declarator didn't seem well-formed.
1428 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001429 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001430 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001431 }
Mike Stump1eb44332009-09-09 15:08:12 +00001432
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001433 // Save late-parsed attributes for now; they need to be parsed in the
1434 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001435 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1436 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001437 if (D.isFunctionDeclarator())
1438 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1439
Chris Lattnerc82daef2010-07-11 22:24:20 +00001440 // Check to see if we have a function *definition* which must have a body.
1441 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1442 // Look at the next token to make sure that this isn't a function
1443 // declaration. We have to check this because __attribute__ might be the
1444 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001445 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001446
Chris Lattner004659a2010-07-11 22:42:07 +00001447 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001448 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1449 Diag(Tok, diag::err_function_declared_typedef);
1450
1451 // Recover by treating the 'typedef' as spurious.
1452 DS.ClearStorageClassSpecs();
1453 }
1454
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001455 Decl *TheDecl =
1456 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001457 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001458 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001459
Chris Lattner004659a2010-07-11 22:42:07 +00001460 if (isDeclarationSpecifier()) {
1461 // If there is an invalid declaration specifier right after the function
1462 // prototype, then we must be in a missing semicolon case where this isn't
1463 // actually a body. Just fall through into the code that handles it as a
1464 // prototype, and let the top-level code handle the erroneous declspec
1465 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001466 } else {
1467 Diag(Tok, diag::err_expected_fn_body);
1468 SkipUntil(tok::semi);
1469 return DeclGroupPtrTy();
1470 }
1471 }
1472
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001473 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001474 return DeclGroupPtrTy();
1475
1476 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1477 // must parse and analyze the for-range-initializer before the declaration is
1478 // analyzed.
1479 if (FRI && Tok.is(tok::colon)) {
1480 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001481 if (Tok.is(tok::l_brace))
1482 FRI->RangeExpr = ParseBraceInitializer();
1483 else
1484 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001485 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1486 Actions.ActOnCXXForRangeDecl(ThisDecl);
1487 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001488 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001489 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1490 }
1491
Chris Lattner5f9e2722011-07-23 10:55:15 +00001492 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001493 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001494 if (LateParsedAttrs.size() > 0)
1495 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001496 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001497 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001498 DeclsInGroup.push_back(FirstDecl);
1499
Richard Smith0706df42011-10-19 21:33:05 +00001500 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001501
John McCalld8ac0572009-11-03 19:26:08 +00001502 // If we don't have a comma, it is either the end of the list (a ';') or an
1503 // error, bail out.
1504 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001505 SourceLocation CommaLoc = ConsumeToken();
1506
1507 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1508 // This comma was followed by a line-break and something which can't be
1509 // the start of a declarator. The comma was probably a typo for a
1510 // semicolon.
1511 Diag(CommaLoc, diag::err_expected_semi_declaration)
1512 << FixItHint::CreateReplacement(CommaLoc, ";");
1513 ExpectSemi = false;
1514 break;
1515 }
John McCalld8ac0572009-11-03 19:26:08 +00001516
1517 // Parse the next declarator.
1518 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001519 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001520
1521 // Accept attributes in an init-declarator. In the first declarator in a
1522 // declaration, these would be part of the declspec. In subsequent
1523 // declarators, they become part of the declarator itself, so that they
1524 // don't apply to declarators after *this* one. Examples:
1525 // short __attribute__((common)) var; -> declspec
1526 // short var __attribute__((common)); -> declarator
1527 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001528 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001529
1530 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001531 if (!D.isInvalidType()) {
1532 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1533 D.complete(ThisDecl);
1534 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001535 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001536 }
John McCalld8ac0572009-11-03 19:26:08 +00001537 }
1538
1539 if (DeclEnd)
1540 *DeclEnd = Tok.getLocation();
1541
Richard Smith0706df42011-10-19 21:33:05 +00001542 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001543 ExpectAndConsumeSemi(Context == Declarator::FileContext
1544 ? diag::err_invalid_token_after_toplevel_declarator
1545 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001546 // Okay, there was no semicolon and one was expected. If we see a
1547 // declaration specifier, just assume it was missing and continue parsing.
1548 // Otherwise things are very confused and we skip to recover.
1549 if (!isDeclarationSpecifier()) {
1550 SkipUntil(tok::r_brace, true, true);
1551 if (Tok.is(tok::semi))
1552 ConsumeToken();
1553 }
John McCalld8ac0572009-11-03 19:26:08 +00001554 }
1555
Douglas Gregor23c94db2010-07-02 17:43:08 +00001556 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001557 DeclsInGroup.data(),
1558 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001559}
1560
Richard Smithad762fc2011-04-14 22:09:26 +00001561/// Parse an optional simple-asm-expr and attributes, and attach them to a
1562/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001563bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001564 // If a simple-asm-expr is present, parse it.
1565 if (Tok.is(tok::kw_asm)) {
1566 SourceLocation Loc;
1567 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1568 if (AsmLabel.isInvalid()) {
1569 SkipUntil(tok::semi, true, true);
1570 return true;
1571 }
1572
1573 D.setAsmLabel(AsmLabel.release());
1574 D.SetRangeEnd(Loc);
1575 }
1576
1577 MaybeParseGNUAttributes(D);
1578 return false;
1579}
1580
Douglas Gregor1426e532009-05-12 21:31:51 +00001581/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1582/// declarator'. This method parses the remainder of the declaration
1583/// (including any attributes or initializer, among other things) and
1584/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001585///
Reid Spencer5f016e22007-07-11 17:01:13 +00001586/// init-declarator: [C99 6.7]
1587/// declarator
1588/// declarator '=' initializer
1589/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1590/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001591/// [C++] declarator initializer[opt]
1592///
1593/// [C++] initializer:
1594/// [C++] '=' initializer-clause
1595/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001596/// [C++0x] '=' 'default' [TODO]
1597/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001598/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001599///
1600/// According to the standard grammar, =default and =delete are function
1601/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001602///
John McCalld226f652010-08-21 09:40:31 +00001603Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001604 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001605 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001606 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Richard Smithad762fc2011-04-14 22:09:26 +00001608 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1609}
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Richard Smithad762fc2011-04-14 22:09:26 +00001611Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1612 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001613 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001614 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001615 switch (TemplateInfo.Kind) {
1616 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001617 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001618 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001619
Douglas Gregord5a423b2009-09-25 18:43:00 +00001620 case ParsedTemplateInfo::Template:
1621 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001622 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001623 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001624 D);
1625 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001626
Douglas Gregord5a423b2009-09-25 18:43:00 +00001627 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001628 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001629 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001630 TemplateInfo.ExternLoc,
1631 TemplateInfo.TemplateLoc,
1632 D);
1633 if (ThisRes.isInvalid()) {
1634 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001635 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001636 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001637
Douglas Gregord5a423b2009-09-25 18:43:00 +00001638 ThisDecl = ThisRes.get();
1639 break;
1640 }
1641 }
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Richard Smith34b41d92011-02-20 03:19:35 +00001643 bool TypeContainsAuto =
1644 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1645
Douglas Gregor1426e532009-05-12 21:31:51 +00001646 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001647 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001648 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001649 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001650 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001651 if (D.isFunctionDeclarator())
1652 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1653 << 1 /* delete */;
1654 else
1655 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001656 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001657 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001658 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1659 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001660 else
1661 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001662 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001663 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001664 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001665 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001666 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001667
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001668 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001669 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001670 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001671 cutOffParsing();
1672 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001673 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001674
John McCall60d7b3a2010-08-24 06:29:42 +00001675 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001676
David Blaikie4e4d0842012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001678 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001679 ExitScope();
1680 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001681
Douglas Gregor1426e532009-05-12 21:31:51 +00001682 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001683 SkipUntil(tok::comma, true, true);
1684 Actions.ActOnInitializerError(ThisDecl);
1685 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001686 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1687 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001688 }
1689 } else if (Tok.is(tok::l_paren)) {
1690 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001691 BalancedDelimiterTracker T(*this, tok::l_paren);
1692 T.consumeOpen();
1693
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001694 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001695 CommaLocsTy CommaLocs;
1696
David Blaikie4e4d0842012-03-11 07:00:24 +00001697 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001698 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001699 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001700 }
1701
Douglas Gregor1426e532009-05-12 21:31:51 +00001702 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001703 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001704 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001705
David Blaikie4e4d0842012-03-11 07:00:24 +00001706 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001707 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001708 ExitScope();
1709 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001710 } else {
1711 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001712 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001713
1714 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1715 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001716
David Blaikie4e4d0842012-03-11 07:00:24 +00001717 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001718 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001719 ExitScope();
1720 }
1721
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001722 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1723 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001724 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001725 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1726 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001727 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001728 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001729 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001730 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001731 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1732
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001733 if (D.getCXXScopeSpec().isSet()) {
1734 EnterScope(0);
1735 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1736 }
1737
1738 ExprResult Init(ParseBraceInitializer());
1739
1740 if (D.getCXXScopeSpec().isSet()) {
1741 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1742 ExitScope();
1743 }
1744
1745 if (Init.isInvalid()) {
1746 Actions.ActOnInitializerError(ThisDecl);
1747 } else
1748 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1749 /*DirectInit=*/true, TypeContainsAuto);
1750
Douglas Gregor1426e532009-05-12 21:31:51 +00001751 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001752 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001753 }
1754
Richard Smith483b9f32011-02-21 20:05:19 +00001755 Actions.FinalizeDeclaration(ThisDecl);
1756
Douglas Gregor1426e532009-05-12 21:31:51 +00001757 return ThisDecl;
1758}
1759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760/// ParseSpecifierQualifierList
1761/// specifier-qualifier-list:
1762/// type-specifier specifier-qualifier-list[opt]
1763/// type-qualifier specifier-qualifier-list[opt]
1764/// [GNU] attributes specifier-qualifier-list[opt]
1765///
Richard Smith69730c12012-03-12 07:56:15 +00001766void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1767 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1769 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001770 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001771 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 // Validate declspec for type-name.
1774 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001775 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1776 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001777 Diag(Tok, diag::err_expected_type);
1778 DS.SetTypeSpecError();
1779 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1780 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001782 if (!DS.hasTypeSpecifier())
1783 DS.SetTypeSpecError();
1784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 // Issue diagnostic and remove storage class if present.
1787 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1788 if (DS.getStorageClassSpecLoc().isValid())
1789 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1790 else
1791 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1792 DS.ClearStorageClassSpecs();
1793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 // Issue diagnostic and remove function specfier if present.
1796 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001797 if (DS.isInlineSpecified())
1798 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1799 if (DS.isVirtualSpecified())
1800 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1801 if (DS.isExplicitSpecified())
1802 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 DS.ClearFunctionSpecs();
1804 }
Richard Smith69730c12012-03-12 07:56:15 +00001805
1806 // Issue diagnostic and remove constexpr specfier if present.
1807 if (DS.isConstexprSpecified()) {
1808 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1809 DS.ClearConstexprSpec();
1810 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001811}
1812
Chris Lattnerc199ab32009-04-12 20:42:31 +00001813/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1814/// specified token is valid after the identifier in a declarator which
1815/// immediately follows the declspec. For example, these things are valid:
1816///
1817/// int x [ 4]; // direct-declarator
1818/// int x ( int y); // direct-declarator
1819/// int(int x ) // direct-declarator
1820/// int x ; // simple-declaration
1821/// int x = 17; // init-declarator-list
1822/// int x , y; // init-declarator-list
1823/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001824/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001825/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001826///
1827/// This is not, because 'x' does not immediately follow the declspec (though
1828/// ')' happens to be valid anyway).
1829/// int (x)
1830///
1831static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1832 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1833 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001834 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001835}
1836
Chris Lattnere40c2952009-04-14 21:34:55 +00001837
1838/// ParseImplicitInt - This method is called when we have an non-typename
1839/// identifier in a declspec (which normally terminates the decl spec) when
1840/// the declspec has no type specifier. In this case, the declspec is either
1841/// malformed or is "implicit int" (in K&R and C89).
1842///
1843/// This method handles diagnosing this prettily and returns false if the
1844/// declspec is done being processed. If it recovers and thinks there may be
1845/// other pieces of declspec after it, it returns true.
1846///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001847bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001848 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001849 AccessSpecifier AS, DeclSpecContext DSC,
1850 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001851 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Chris Lattnere40c2952009-04-14 21:34:55 +00001853 SourceLocation Loc = Tok.getLocation();
1854 // If we see an identifier that is not a type name, we normally would
1855 // parse it as the identifer being declared. However, when a typename
1856 // is typo'd or the definition is not included, this will incorrectly
1857 // parse the typename as the identifier name and fall over misparsing
1858 // later parts of the diagnostic.
1859 //
1860 // As such, we try to do some look-ahead in cases where this would
1861 // otherwise be an "implicit-int" case to see if this is invalid. For
1862 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1863 // an identifier with implicit int, we'd get a parse error because the
1864 // next token is obviously invalid for a type. Parse these as a case
1865 // with an invalid type specifier.
1866 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Chris Lattnere40c2952009-04-14 21:34:55 +00001868 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001869 // error, do lookahead to try to do better recovery. This never applies
1870 // within a type specifier. Outside of C++, we allow this even if the
1871 // language doesn't "officially" support implicit int -- we support
1872 // implicit int as an extension in C99 and C11. Allegedly, MS also
1873 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001874 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001875 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001876 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001877 // If this token is valid for implicit int, e.g. "static x = 4", then
1878 // we just avoid eating the identifier, so it will be parsed as the
1879 // identifier in the declarator.
1880 return false;
1881 }
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Richard Smith827adaf2012-05-15 21:01:51 +00001883 if (getLangOpts().CPlusPlus &&
1884 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1885 // Don't require a type specifier if we have the 'auto' storage class
1886 // specifier in C++98 -- we'll promote it to a type specifier.
1887 return false;
1888 }
1889
Chris Lattnere40c2952009-04-14 21:34:55 +00001890 // Otherwise, if we don't consume this token, we are going to emit an
1891 // error anyway. Try to recover from various common problems. Check
1892 // to see if this was a reference to a tag name without a tag specified.
1893 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001894 //
1895 // C++ doesn't need this, and isTagName doesn't take SS.
1896 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001897 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001898 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Douglas Gregor23c94db2010-07-02 17:43:08 +00001900 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001901 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001902 case DeclSpec::TST_enum:
1903 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1904 case DeclSpec::TST_union:
1905 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1906 case DeclSpec::TST_struct:
1907 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001908 case DeclSpec::TST_interface:
1909 TagName="__interface"; FixitTagName = "__interface ";
1910 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001911 case DeclSpec::TST_class:
1912 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Chris Lattnerf4382f52009-04-14 22:17:06 +00001915 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001916 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1917 LookupResult R(Actions, TokenName, SourceLocation(),
1918 Sema::LookupOrdinaryName);
1919
Chris Lattnerf4382f52009-04-14 22:17:06 +00001920 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001921 << TokenName << TagName << getLangOpts().CPlusPlus
1922 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1923
1924 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1925 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1926 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001927 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001928 << TokenName << TagName;
1929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Chris Lattnerf4382f52009-04-14 22:17:06 +00001931 // Parse this as a tag as if the missing tag were present.
1932 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001933 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001934 else
Richard Smith69730c12012-03-12 07:56:15 +00001935 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001936 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001937 return true;
1938 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001939 }
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Richard Smith8f0a7e72012-05-15 21:29:55 +00001941 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001942 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001943 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1944 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001945 // Look ahead to the next token to try to figure out what this declaration
1946 // was supposed to be.
1947 switch (NextToken().getKind()) {
1948 case tok::comma:
1949 case tok::equal:
1950 case tok::kw_asm:
1951 case tok::l_brace:
1952 case tok::l_square:
1953 case tok::semi:
1954 // This looks like a variable declaration. The type is probably missing.
1955 // We're done parsing decl-specifiers.
1956 return false;
1957
1958 case tok::l_paren: {
1959 // static x(4); // 'x' is not a type
1960 // x(int n); // 'x' is not a type
1961 // x (*p)[]; // 'x' is a type
1962 //
1963 // Since we're in an error case (or the rare 'implicit int in C++' MS
1964 // extension), we can afford to perform a tentative parse to determine
1965 // which case we're in.
1966 TentativeParsingAction PA(*this);
1967 ConsumeToken();
1968 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1969 PA.Revert();
1970 if (TPR == TPResult::False())
1971 return false;
1972 // The identifier is followed by a parenthesized declarator.
1973 // It's supposed to be a type.
1974 break;
1975 }
1976
1977 default:
1978 // This is probably supposed to be a type. This includes cases like:
1979 // int f(itn);
1980 // struct S { unsinged : 4; };
1981 break;
1982 }
1983 }
1984
Chad Rosier8decdee2012-06-26 22:30:43 +00001985 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001986 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001987 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001988 IdentifierInfo *II = Tok.getIdentifierInfo();
1989 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001990 // The action emitted a diagnostic, so we don't have to.
1991 if (T) {
1992 // The action has suggested that the type T could be used. Set that as
1993 // the type in the declaration specifiers, consume the would-be type
1994 // name token, and we're done.
1995 const char *PrevSpec;
1996 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001997 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001998 DS.SetRangeEnd(Tok.getLocation());
1999 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002000 // There may be other declaration specifiers after this.
2001 return true;
2002 } else if (II != Tok.getIdentifierInfo()) {
2003 // If no type was suggested, the correction is to a keyword
2004 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002005 // There may be other declaration specifiers after this.
2006 return true;
2007 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002008
Douglas Gregora786fdb2009-10-13 23:27:22 +00002009 // Fall through; the action had no suggestion for us.
2010 } else {
2011 // The action did not emit a diagnostic, so emit one now.
2012 SourceRange R;
2013 if (SS) R = SS->getRange();
2014 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregora786fdb2009-10-13 23:27:22 +00002017 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002018 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002019 DS.SetRangeEnd(Tok.getLocation());
2020 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Chris Lattnere40c2952009-04-14 21:34:55 +00002022 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2023 // avoid rippling error messages on subsequent uses of the same type,
2024 // could be useful if #include was forgotten.
2025 return false;
2026}
2027
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002028/// \brief Determine the declaration specifier context from the declarator
2029/// context.
2030///
2031/// \param Context the declarator context, which is one of the
2032/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002033Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002034Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2035 if (Context == Declarator::MemberContext)
2036 return DSC_class;
2037 if (Context == Declarator::FileContext)
2038 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002039 if (Context == Declarator::TrailingReturnContext)
2040 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002041 return DSC_normal;
2042}
2043
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002044/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2045///
2046/// FIXME: Simply returns an alignof() expression if the argument is a
2047/// type. Ideally, the type should be propagated directly into Sema.
2048///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002049/// [C11] type-id
2050/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002051/// [C++0x] type-id ...[opt]
2052/// [C++0x] assignment-expression ...[opt]
2053ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2054 SourceLocation &EllipsisLoc) {
2055 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002056 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002057 SourceLocation TypeLoc = Tok.getLocation();
2058 ParsedType Ty = ParseTypeName().get();
2059 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002060 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2061 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002062 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002063 ER = ParseConstantExpression();
2064
Richard Smith80ad52f2013-01-02 11:42:31 +00002065 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002066 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002067
2068 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002069}
2070
2071/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2072/// attribute to Attrs.
2073///
2074/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002075/// [C11] '_Alignas' '(' type-id ')'
2076/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002077/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2078/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002079void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2080 SourceLocation *endLoc) {
2081 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2082 "Not an alignment-specifier!");
2083
Richard Smith33f04a22013-01-29 01:48:07 +00002084 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2085 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002087 BalancedDelimiterTracker T(*this, tok::l_paren);
2088 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002089 return;
2090
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002091 SourceLocation EllipsisLoc;
2092 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002093 if (ArgExpr.isInvalid()) {
2094 SkipUntil(tok::r_paren);
2095 return;
2096 }
2097
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002098 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002099 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002100 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002101
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002102 // FIXME: Handle pack-expansions here.
2103 if (EllipsisLoc.isValid()) {
2104 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2105 return;
2106 }
2107
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002108 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002109 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002110 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2111 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002112}
2113
Reid Spencer5f016e22007-07-11 17:01:13 +00002114/// ParseDeclarationSpecifiers
2115/// declaration-specifiers: [C99 6.7]
2116/// storage-class-specifier declaration-specifiers[opt]
2117/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002118/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002119/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002120/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002121/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002122///
2123/// storage-class-specifier: [C99 6.7.1]
2124/// 'typedef'
2125/// 'extern'
2126/// 'static'
2127/// 'auto'
2128/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002129/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002130/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002131/// function-specifier: [C99 6.7.4]
2132/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002133/// [C++] 'virtual'
2134/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002135/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002136/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002137/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002140void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002141 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002142 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002143 DeclSpecContext DSContext,
2144 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002145 if (DS.getSourceRange().isInvalid()) {
2146 DS.SetRangeStart(Tok.getLocation());
2147 DS.SetRangeEnd(Tok.getLocation());
2148 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002149
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002150 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002151 bool AttrsLastTime = false;
2152 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002154 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002156 unsigned DiagID = 0;
2157
Reid Spencer5f016e22007-07-11 17:01:13 +00002158 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002159
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002161 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002162 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002163 if (!AttrsLastTime)
2164 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002165 else {
2166 // Reject C++11 attributes that appertain to decl specifiers as
2167 // we don't support any C++11 attributes that appertain to decl
2168 // specifiers. This also conforms to what g++ 4.8 is doing.
2169 ProhibitCXX11Attributes(attrs);
2170
Sean Hunt2edf0a22012-06-23 05:07:58 +00002171 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002172 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002173
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 // If this is not a declaration specifier token, we're done reading decl
2175 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002176 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Sean Hunt2edf0a22012-06-23 05:07:58 +00002179 case tok::l_square:
2180 case tok::kw_alignas:
2181 if (!isCXX11AttributeSpecifier())
2182 goto DoneWithDeclSpec;
2183
2184 ProhibitAttributes(attrs);
2185 // FIXME: It would be good to recover by accepting the attributes,
2186 // but attempting to do that now would cause serious
2187 // madness in terms of diagnostics.
2188 attrs.clear();
2189 attrs.Range = SourceRange();
2190
2191 ParseCXX11Attributes(attrs);
2192 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002193 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002194
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002195 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002196 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002197 if (DS.hasTypeSpecifier()) {
2198 bool AllowNonIdentifiers
2199 = (getCurScope()->getFlags() & (Scope::ControlScope |
2200 Scope::BlockScope |
2201 Scope::TemplateParamScope |
2202 Scope::FunctionPrototypeScope |
2203 Scope::AtCatchScope)) == 0;
2204 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002205 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002206 (DSContext == DSC_class && DS.isFriendSpecified());
2207
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002208 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002209 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002210 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002211 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002212 }
2213
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002214 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2215 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2216 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002217 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002218 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002219 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002220 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002221 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002222 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002223
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002224 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002225 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002226 }
2227
Chris Lattner5e02c472009-01-05 00:07:25 +00002228 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002229 // C++ scope specifier. Annotate and loop, or bail out on error.
2230 if (TryAnnotateCXXScopeToken(true)) {
2231 if (!DS.hasTypeSpecifier())
2232 DS.SetTypeSpecError();
2233 goto DoneWithDeclSpec;
2234 }
John McCall2e0a7152010-03-01 18:20:46 +00002235 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2236 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002237 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002238
2239 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002240 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002241 goto DoneWithDeclSpec;
2242
John McCallaa87d332009-12-12 11:40:51 +00002243 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002244 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2245 Tok.getAnnotationRange(),
2246 SS);
John McCallaa87d332009-12-12 11:40:51 +00002247
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002248 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002249 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002251 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002252 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002253 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002254
2255 // C++ [class.qual]p2:
2256 // In a lookup in which the constructor is an acceptable lookup
2257 // result and the nested-name-specifier nominates a class C:
2258 //
2259 // - if the name specified after the
2260 // nested-name-specifier, when looked up in C, is the
2261 // injected-class-name of C (Clause 9), or
2262 //
2263 // - if the name specified after the nested-name-specifier
2264 // is the same as the identifier or the
2265 // simple-template-id's template-name in the last
2266 // component of the nested-name-specifier,
2267 //
2268 // the name is instead considered to name the constructor of
2269 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002270 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002271 // Thus, if the template-name is actually the constructor
2272 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002273 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002274 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002275 if ((DSContext == DSC_top_level ||
2276 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2277 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002278 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002279 if (isConstructorDeclarator()) {
2280 // The user meant this to be an out-of-line constructor
2281 // definition, but template arguments are not allowed
2282 // there. Just allow this as a constructor; we'll
2283 // complain about it later.
2284 goto DoneWithDeclSpec;
2285 }
2286
2287 // The user meant this to name a type, but it actually names
2288 // a constructor with some extraneous template
2289 // arguments. Complain, then parse it as a type as the user
2290 // intended.
2291 Diag(TemplateId->TemplateNameLoc,
2292 diag::err_out_of_line_template_id_names_constructor)
2293 << TemplateId->Name;
2294 }
2295
John McCallaa87d332009-12-12 11:40:51 +00002296 DS.getTypeSpecScope() = SS;
2297 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002298 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002299 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002300 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002301 continue;
2302 }
2303
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002304 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002305 DS.getTypeSpecScope() = SS;
2306 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002307 if (Tok.getAnnotationValue()) {
2308 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002310 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002311 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002312 if (isInvalid)
2313 break;
John McCallb3d87482010-08-24 05:47:05 +00002314 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002315 else
2316 DS.SetTypeSpecError();
2317 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2318 ConsumeToken(); // The typename
2319 }
2320
Douglas Gregor9135c722009-03-25 15:40:00 +00002321 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002322 goto DoneWithDeclSpec;
2323
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002324 // If we're in a context where the identifier could be a class name,
2325 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002326 if ((DSContext == DSC_top_level ||
2327 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002328 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002329 &SS)) {
2330 if (isConstructorDeclarator())
2331 goto DoneWithDeclSpec;
2332
2333 // As noted in C++ [class.qual]p2 (cited above), when the name
2334 // of the class is qualified in a context where it could name
2335 // a constructor, its a constructor name. However, we've
2336 // looked at the declarator, and the user probably meant this
2337 // to be a type. Complain that it isn't supposed to be treated
2338 // as a type, then proceed to parse it as a type.
2339 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2340 << Next.getIdentifierInfo();
2341 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002342
John McCallb3d87482010-08-24 05:47:05 +00002343 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2344 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002345 getCurScope(), &SS,
2346 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002347 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002348 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002349
Chris Lattnerf4382f52009-04-14 22:17:06 +00002350 // If the referenced identifier is not a type, then this declspec is
2351 // erroneous: We already checked about that it has no type specifier, and
2352 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002353 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002354 if (TypeRep == 0) {
2355 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002356 ParsedAttributesWithRange Attrs(AttrFactory);
2357 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2358 if (!Attrs.empty()) {
2359 AttrsLastTime = true;
2360 attrs.takeAllFrom(Attrs);
2361 }
2362 continue;
2363 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002364 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002365 }
Mike Stump1eb44332009-09-09 15:08:12 +00002366
John McCallaa87d332009-12-12 11:40:51 +00002367 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002368 ConsumeToken(); // The C++ scope.
2369
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002371 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002372 if (isInvalid)
2373 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002375 DS.SetRangeEnd(Tok.getLocation());
2376 ConsumeToken(); // The typename.
2377
2378 continue;
2379 }
Mike Stump1eb44332009-09-09 15:08:12 +00002380
Chris Lattner80d0c892009-01-21 19:48:37 +00002381 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002382 if (Tok.getAnnotationValue()) {
2383 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002385 DiagID, T);
2386 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002387 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002388
Chris Lattner5c5db552010-04-05 18:18:31 +00002389 if (isInvalid)
2390 break;
2391
Chris Lattner80d0c892009-01-21 19:48:37 +00002392 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2393 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Chris Lattner80d0c892009-01-21 19:48:37 +00002395 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2396 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002397 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002398 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002399 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002400
Chris Lattner80d0c892009-01-21 19:48:37 +00002401 continue;
2402 }
Mike Stump1eb44332009-09-09 15:08:12 +00002403
Douglas Gregorbfad9152011-04-28 15:48:45 +00002404 case tok::kw___is_signed:
2405 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2406 // typically treats it as a trait. If we see __is_signed as it appears
2407 // in libstdc++, e.g.,
2408 //
2409 // static const bool __is_signed;
2410 //
2411 // then treat __is_signed as an identifier rather than as a keyword.
2412 if (DS.getTypeSpecType() == TST_bool &&
2413 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2414 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2415 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2416 Tok.setKind(tok::identifier);
2417 }
2418
2419 // We're done with the declaration-specifiers.
2420 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002421
Chris Lattner3bd934a2008-07-26 01:18:38 +00002422 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002423 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002424 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002425 // In C++, check to see if this is a scope specifier like foo::bar::, if
2426 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002427 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002428 if (TryAnnotateCXXScopeToken(true)) {
2429 if (!DS.hasTypeSpecifier())
2430 DS.SetTypeSpecError();
2431 goto DoneWithDeclSpec;
2432 }
2433 if (!Tok.is(tok::identifier))
2434 continue;
2435 }
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Chris Lattner3bd934a2008-07-26 01:18:38 +00002437 // This identifier can only be a typedef name if we haven't already seen
2438 // a type-specifier. Without this check we misparse:
2439 // typedef int X; struct Y { short X; }; as 'short int'.
2440 if (DS.hasTypeSpecifier())
2441 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002442
John Thompson82287d12010-02-05 00:12:22 +00002443 // Check for need to substitute AltiVec keyword tokens.
2444 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2445 break;
2446
Richard Smithf63eee72012-05-09 18:56:43 +00002447 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2448 // allow the use of a typedef name as a type specifier.
2449 if (DS.isTypeAltiVecVector())
2450 goto DoneWithDeclSpec;
2451
John McCallb3d87482010-08-24 05:47:05 +00002452 ParsedType TypeRep =
2453 Actions.getTypeName(*Tok.getIdentifierInfo(),
2454 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002455
Chris Lattnerc199ab32009-04-12 20:42:31 +00002456 // If this is not a typedef name, don't parse it as part of the declspec,
2457 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002458 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002459 ParsedAttributesWithRange Attrs(AttrFactory);
2460 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2461 if (!Attrs.empty()) {
2462 AttrsLastTime = true;
2463 attrs.takeAllFrom(Attrs);
2464 }
2465 continue;
2466 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002467 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002468 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002469
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002470 // If we're in a context where the identifier could be a class name,
2471 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002472 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002474 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002475 goto DoneWithDeclSpec;
2476
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002478 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002479 if (isInvalid)
2480 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Chris Lattner3bd934a2008-07-26 01:18:38 +00002482 DS.SetRangeEnd(Tok.getLocation());
2483 ConsumeToken(); // The identifier
2484
2485 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2486 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002487 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002488 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002489 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002490
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002491 // Need to support trailing type qualifiers (e.g. "id<p> const").
2492 // If a type specifier follows, it will be diagnosed elsewhere.
2493 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002494 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002495
2496 // type-name
2497 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002498 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002499 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002500 // This template-id does not refer to a type name, so we're
2501 // done with the type-specifiers.
2502 goto DoneWithDeclSpec;
2503 }
2504
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002505 // If we're in a context where the template-id could be a
2506 // constructor name or specialization, check whether this is a
2507 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002508 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002509 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002510 isConstructorDeclarator())
2511 goto DoneWithDeclSpec;
2512
Douglas Gregor39a8de12009-02-25 19:37:18 +00002513 // Turn the template-id annotation token into a type annotation
2514 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002515 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002516 continue;
2517 }
2518
Reid Spencer5f016e22007-07-11 17:01:13 +00002519 // GNU attributes support.
2520 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002521 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002522 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002523
2524 // Microsoft declspec support.
2525 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002526 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002527 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Steve Naroff239f0732008-12-25 14:16:32 +00002529 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002530 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002531 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002532 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002533 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002534 // FIXME: This does not work correctly if it is set to be a declspec
2535 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002536 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002537 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002538 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002539 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002540
2541 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002542 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002543 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002544 case tok::kw___cdecl:
2545 case tok::kw___stdcall:
2546 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002547 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002548 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002549 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002550 continue;
2551
Dawn Perchik52fc3142010-09-03 01:29:35 +00002552 // Borland single token adornments.
2553 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002554 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002555 continue;
2556
Peter Collingbournef315fa82011-02-14 01:42:53 +00002557 // OpenCL single token adornments.
2558 case tok::kw___kernel:
2559 ParseOpenCLAttributes(DS.getAttributes());
2560 continue;
2561
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 // storage-class-specifier
2563 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002564 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2565 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002566 break;
2567 case tok::kw_extern:
2568 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002569 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002570 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2571 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002573 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002574 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2575 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002576 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 case tok::kw_static:
2578 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002579 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002580 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2581 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 break;
2583 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002584 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002585 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002586 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2587 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002588 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002589 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002590 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002591 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2593 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002594 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002595 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2596 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002597 break;
2598 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002599 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2600 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002602 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002603 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2604 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002605 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002607 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 // function-specifier
2611 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002612 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002614 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002615 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002616 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002617 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002618 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002619 break;
Richard Smithde03c152013-01-17 22:16:11 +00002620 case tok::kw__Noreturn:
2621 if (!getLangOpts().C11)
2622 Diag(Loc, diag::ext_c11_noreturn);
2623 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2624 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002625
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002626 // alignment-specifier
2627 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002628 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002629 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002630 ParseAlignmentSpecifier(DS.getAttributes());
2631 continue;
2632
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002633 // friend
2634 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002635 if (DSContext == DSC_class)
2636 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2637 else {
2638 PrevSpec = ""; // not actually used by the diagnostic
2639 DiagID = diag::err_friend_invalid_in_context;
2640 isInvalid = true;
2641 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002642 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Douglas Gregor8d267c52011-09-09 02:06:17 +00002644 // Modules
2645 case tok::kw___module_private__:
2646 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2647 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002648
Sebastian Redl2ac67232009-11-05 15:47:02 +00002649 // constexpr
2650 case tok::kw_constexpr:
2651 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2652 break;
2653
Chris Lattner80d0c892009-01-21 19:48:37 +00002654 // type-specifier
2655 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2657 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002658 break;
2659 case tok::kw_long:
2660 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002661 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2662 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002663 else
John McCallfec54012009-08-03 20:12:06 +00002664 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2665 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002666 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002667 case tok::kw___int64:
2668 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2669 DiagID);
2670 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002671 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002672 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2673 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002674 break;
2675 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002676 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2677 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002678 break;
2679 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2681 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002682 break;
2683 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002684 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2685 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002686 break;
2687 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2689 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2693 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002694 break;
2695 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2697 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002698 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002699 case tok::kw___int128:
2700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2701 DiagID);
2702 break;
2703 case tok::kw_half:
2704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2705 DiagID);
2706 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002707 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002710 break;
2711 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2713 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002714 break;
2715 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2717 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002718 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002719 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2721 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002722 break;
2723 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2725 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002726 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002727 case tok::kw_bool:
2728 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002729 if (Tok.is(tok::kw_bool) &&
2730 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2731 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2732 PrevSpec = ""; // Not used by the diagnostic.
2733 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002734 // For better error recovery.
2735 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002736 isInvalid = true;
2737 } else {
2738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2739 DiagID);
2740 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002741 break;
2742 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2744 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002745 break;
2746 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2748 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002749 break;
2750 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2752 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002753 break;
John Thompson82287d12010-02-05 00:12:22 +00002754 case tok::kw___vector:
2755 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2756 break;
2757 case tok::kw___pixel:
2758 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2759 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002760 case tok::kw_image1d_t:
2761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2762 PrevSpec, DiagID);
2763 break;
2764 case tok::kw_image1d_array_t:
2765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2766 PrevSpec, DiagID);
2767 break;
2768 case tok::kw_image1d_buffer_t:
2769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2770 PrevSpec, DiagID);
2771 break;
2772 case tok::kw_image2d_t:
2773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2774 PrevSpec, DiagID);
2775 break;
2776 case tok::kw_image2d_array_t:
2777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2778 PrevSpec, DiagID);
2779 break;
2780 case tok::kw_image3d_t:
2781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2782 PrevSpec, DiagID);
2783 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002784 case tok::kw_sampler_t:
2785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2786 PrevSpec, DiagID);
2787 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002788 case tok::kw_event_t:
2789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2790 PrevSpec, DiagID);
2791 break;
John McCalla5fc4722011-04-09 22:50:59 +00002792 case tok::kw___unknown_anytype:
2793 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2794 PrevSpec, DiagID);
2795 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002796
2797 // class-specifier:
2798 case tok::kw_class:
2799 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002800 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002801 case tok::kw_union: {
2802 tok::TokenKind Kind = Tok.getKind();
2803 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002804
2805 // These are attributes following class specifiers.
2806 // To produce better diagnostic, we parse them when
2807 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002808 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002809 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002810 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002811
2812 // If there are attributes following class specifier,
2813 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002814 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002815 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002816 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002817 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002818 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002819 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002820
2821 // enum-specifier:
2822 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002823 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002824 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002825 continue;
2826
2827 // cv-qualifier:
2828 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002829 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002830 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002831 break;
2832 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002833 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002834 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002835 break;
2836 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002837 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002838 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002839 break;
2840
Douglas Gregord57959a2009-03-27 23:10:48 +00002841 // C++ typename-specifier:
2842 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002843 if (TryAnnotateTypeOrScopeToken()) {
2844 DS.SetTypeSpecError();
2845 goto DoneWithDeclSpec;
2846 }
2847 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002848 continue;
2849 break;
2850
Chris Lattner80d0c892009-01-21 19:48:37 +00002851 // GNU typeof support.
2852 case tok::kw_typeof:
2853 ParseTypeofSpecifier(DS);
2854 continue;
2855
David Blaikie42d6d0c2011-12-04 05:04:18 +00002856 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002857 ParseDecltypeSpecifier(DS);
2858 continue;
2859
Sean Huntdb5d44b2011-05-19 05:37:45 +00002860 case tok::kw___underlying_type:
2861 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002862 continue;
2863
2864 case tok::kw__Atomic:
2865 ParseAtomicSpecifier(DS);
2866 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002867
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002868 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002869 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002870 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002871 goto DoneWithDeclSpec;
2872 case tok::kw___private:
2873 case tok::kw___global:
2874 case tok::kw___local:
2875 case tok::kw___constant:
2876 case tok::kw___read_only:
2877 case tok::kw___write_only:
2878 case tok::kw___read_write:
2879 ParseOpenCLQualifiers(DS);
2880 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002881
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002882 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002883 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002884 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2885 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002886 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002887 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Douglas Gregor46f936e2010-11-19 17:10:50 +00002889 if (!ParseObjCProtocolQualifiers(DS))
2890 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2891 << FixItHint::CreateInsertion(Loc, "id")
2892 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002893
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002894 // Need to support trailing type qualifiers (e.g. "id<p> const").
2895 // If a type specifier follows, it will be diagnosed elsewhere.
2896 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002897 }
John McCallfec54012009-08-03 20:12:06 +00002898 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002899 if (isInvalid) {
2900 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002901 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002902
Douglas Gregorae2fb142010-08-23 14:34:43 +00002903 if (DiagID == diag::ext_duplicate_declspec)
2904 Diag(Tok, DiagID)
2905 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2906 else
2907 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002908 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002909
Chris Lattner81c018d2008-03-13 06:29:04 +00002910 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002911 if (DiagID != diag::err_bool_redeclaration)
2912 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002913
2914 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 }
2916}
Douglas Gregoradcac882008-12-01 23:54:00 +00002917
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002918/// ParseStructDeclaration - Parse a struct declaration without the terminating
2919/// semicolon.
2920///
Reid Spencer5f016e22007-07-11 17:01:13 +00002921/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002922/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002923/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002924/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002925/// struct-declarator-list:
2926/// struct-declarator
2927/// struct-declarator-list ',' struct-declarator
2928/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2929/// struct-declarator:
2930/// declarator
2931/// [GNU] declarator attributes[opt]
2932/// declarator[opt] ':' constant-expression
2933/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2934///
Chris Lattnere1359422008-04-10 06:46:29 +00002935void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002936ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002937
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002938 if (Tok.is(tok::kw___extension__)) {
2939 // __extension__ silences extension warnings in the subexpression.
2940 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002941 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002942 return ParseStructDeclaration(DS, Fields);
2943 }
Mike Stump1eb44332009-09-09 15:08:12 +00002944
Steve Naroff28a7ca82007-08-20 22:28:22 +00002945 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002946 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002947
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002948 // If there are no declarators, this is a free-standing declaration
2949 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002950 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002951 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2952 DS);
2953 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002954 return;
2955 }
2956
2957 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002958 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002959 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002960 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002961 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002962 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002963
Bill Wendlingad017fa2012-12-20 19:22:21 +00002964 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002965 if (!FirstDeclarator)
2966 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002967
Steve Naroff28a7ca82007-08-20 22:28:22 +00002968 /// struct-declarator: declarator
2969 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002970 if (Tok.isNot(tok::colon)) {
2971 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2972 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002973 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002974 }
Mike Stump1eb44332009-09-09 15:08:12 +00002975
Chris Lattner04d66662007-10-09 17:33:22 +00002976 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002977 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002978 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002979 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002980 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002981 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002982 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002983 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002984
Steve Naroff28a7ca82007-08-20 22:28:22 +00002985 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002986 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002987
John McCallbdd563e2009-11-03 02:38:08 +00002988 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002989 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002990
Steve Naroff28a7ca82007-08-20 22:28:22 +00002991 // If we don't have a comma, it is either the end of the list (a ';')
2992 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002993 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002994 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002995
Steve Naroff28a7ca82007-08-20 22:28:22 +00002996 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002997 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002998
John McCallbdd563e2009-11-03 02:38:08 +00002999 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003000 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003001}
3002
3003/// ParseStructUnionBody
3004/// struct-contents:
3005/// struct-declaration-list
3006/// [EXT] empty
3007/// [GNU] "struct-declaration-list" without terminatoring ';'
3008/// struct-declaration-list:
3009/// struct-declaration
3010/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003011/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003012///
Reid Spencer5f016e22007-07-11 17:01:13 +00003013void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003014 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003015 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3016 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003017
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003018 BalancedDelimiterTracker T(*this, tok::l_brace);
3019 if (T.consumeOpen())
3020 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003021
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003022 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003023 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003024
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3026 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003027 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003028 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3029 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003031
Chris Lattner5f9e2722011-07-23 10:55:15 +00003032 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003033
Reid Spencer5f016e22007-07-11 17:01:13 +00003034 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003035 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Reid Spencer5f016e22007-07-11 17:01:13 +00003038 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003039 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003040 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003041 continue;
3042 }
Chris Lattnere1359422008-04-10 06:46:29 +00003043
John McCallbdd563e2009-11-03 02:38:08 +00003044 if (!Tok.is(tok::at)) {
3045 struct CFieldCallback : FieldCallback {
3046 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003047 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003049
John McCalld226f652010-08-21 09:40:31 +00003050 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003051 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003052 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3053
Eli Friedmandcdff462012-08-08 23:53:27 +00003054 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003055 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003056 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003057 FD.D.getDeclSpec().getSourceRange().getBegin(),
3058 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003059 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003060 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003061 }
John McCallbdd563e2009-11-03 02:38:08 +00003062 } Callback(*this, TagDecl, FieldDecls);
3063
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003064 // Parse all the comma separated declarators.
3065 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003066 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003067 } else { // Handle @defs
3068 ConsumeToken();
3069 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3070 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003071 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003072 continue;
3073 }
3074 ConsumeToken();
3075 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3076 if (!Tok.is(tok::identifier)) {
3077 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003078 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003079 continue;
3080 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003081 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003082 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003083 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003084 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3085 ConsumeToken();
3086 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003087 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003088
Chris Lattner04d66662007-10-09 17:33:22 +00003089 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003091 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003092 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 break;
3094 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003095 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3096 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003098 // If we stopped at a ';', eat it.
3099 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 }
3101 }
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003103 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003104
John McCall0b7e6782011-03-24 11:26:52 +00003105 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003107 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003108
Douglas Gregor23c94db2010-07-02 17:43:08 +00003109 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003110 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003111 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003112 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003113 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003114 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3115 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003116}
3117
Reid Spencer5f016e22007-07-11 17:01:13 +00003118/// ParseEnumSpecifier
3119/// enum-specifier: [C99 6.7.2.2]
3120/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003121///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003122/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3123/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003124/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3125/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003126/// 'enum' identifier
3127/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003128///
Richard Smith1af83c42012-03-23 03:33:32 +00003129/// [C++11] enum-head '{' enumerator-list[opt] '}'
3130/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003131///
Richard Smith1af83c42012-03-23 03:33:32 +00003132/// enum-head: [C++11]
3133/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3134/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3135/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003136///
Richard Smith1af83c42012-03-23 03:33:32 +00003137/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003138/// 'enum'
3139/// 'enum' 'class'
3140/// 'enum' 'struct'
3141///
Richard Smith1af83c42012-03-23 03:33:32 +00003142/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003143/// ':' type-specifier-seq
3144///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003145/// [C++] elaborated-type-specifier:
3146/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3147///
Chris Lattner4c97d762009-04-12 21:49:30 +00003148void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003149 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003150 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003151 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003152 if (Tok.is(tok::code_completion)) {
3153 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003154 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003155 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003156 }
John McCall57c13002011-07-06 05:58:41 +00003157
Sean Hunt2edf0a22012-06-23 05:07:58 +00003158 // If attributes exist after tag, parse them.
3159 ParsedAttributesWithRange attrs(AttrFactory);
3160 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003161 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003162
3163 // If declspecs exist after tag, parse them.
3164 while (Tok.is(tok::kw___declspec))
3165 ParseMicrosoftDeclSpec(attrs);
3166
Richard Smithbdad7a22012-01-10 01:33:14 +00003167 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003168 bool IsScopedUsingClassTag = false;
3169
John McCall1e12b3d2012-06-23 22:30:04 +00003170 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003171 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003172 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003173 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003174 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003175 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003176
Bill Wendlingad017fa2012-12-20 19:22:21 +00003177 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003178 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003179 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003180
3181 // They are allowed afterwards, though.
3182 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003183 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003184 while (Tok.is(tok::kw___declspec))
3185 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003186 }
Richard Smith1af83c42012-03-23 03:33:32 +00003187
John McCall13489672012-05-07 06:16:58 +00003188 // C++11 [temp.explicit]p12:
3189 // The usual access controls do not apply to names used to specify
3190 // explicit instantiations.
3191 // We extend this to also cover explicit specializations. Note that
3192 // we don't suppress if this turns out to be an elaborated type
3193 // specifier.
3194 bool shouldDelayDiagsInTag =
3195 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3196 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3197 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003198
Richard Smith7796eb52012-03-12 08:56:40 +00003199 // Enum definitions should not be parsed in a trailing-return-type.
3200 bool AllowDeclaration = DSC != DSC_trailing;
3201
3202 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003203 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003204 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003205
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003206 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003207 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003208 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3209 // if a fixed underlying type is allowed.
3210 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003211
3212 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003213 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003214 return;
3215
3216 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003217 Diag(Tok, diag::err_expected_ident);
3218 if (Tok.isNot(tok::l_brace)) {
3219 // Has no name and is not a definition.
3220 // Skip the rest of this declarator, up until the comma or semicolon.
3221 SkipUntil(tok::comma, true);
3222 return;
3223 }
3224 }
3225 }
Mike Stump1eb44332009-09-09 15:08:12 +00003226
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003227 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003228 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003229 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003230 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003231
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003232 // Skip the rest of this declarator, up until the comma or semicolon.
3233 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003235 }
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003237 // If an identifier is present, consume and remember it.
3238 IdentifierInfo *Name = 0;
3239 SourceLocation NameLoc;
3240 if (Tok.is(tok::identifier)) {
3241 Name = Tok.getIdentifierInfo();
3242 NameLoc = ConsumeToken();
3243 }
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Richard Smithbdad7a22012-01-10 01:33:14 +00003245 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003246 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3247 // declaration of a scoped enumeration.
3248 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003249 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003250 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003251 }
3252
John McCall13489672012-05-07 06:16:58 +00003253 // Okay, end the suppression area. We'll decide whether to emit the
3254 // diagnostics in a second.
3255 if (shouldDelayDiagsInTag)
3256 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003257
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003258 TypeResult BaseType;
3259
Douglas Gregora61b3e72010-12-01 17:42:47 +00003260 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003261 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003262 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003263 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003264 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003265 // If we're in class scope, this can either be an enum declaration with
3266 // an underlying type, or a declaration of a bitfield member. We try to
3267 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003268 // (integer literal, sizeof); if it's still ambiguous, we then consider
3269 // anything that's a simple-type-specifier followed by '(' as an
3270 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003271 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003272 EnterExpressionEvaluationContext Unevaluated(Actions,
3273 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003274 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003275 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003276 // bit-field. This is the common case.
3277 if (TPR == TPResult::True())
3278 PossibleBitfield = true;
3279 // If the next token starts a type-specifier-seq, it may be either a
3280 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003281 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003282 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003283 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003284 GetLookAheadToken(2).getKind() == tok::semi) {
3285 // Consume the ':'.
3286 ConsumeToken();
3287 } else {
3288 // We have the start of a type-specifier-seq, so we have to perform
3289 // tentative parsing to determine whether we have an expression or a
3290 // type.
3291 TentativeParsingAction TPA(*this);
3292
3293 // Consume the ':'.
3294 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003295
3296 // If we see a type specifier followed by an open-brace, we have an
3297 // ambiguity between an underlying type and a C++11 braced
3298 // function-style cast. Resolve this by always treating it as an
3299 // underlying type.
3300 // FIXME: The standard is not entirely clear on how to disambiguate in
3301 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003302 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003303 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003304 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003305 // We'll parse this as a bitfield later.
3306 PossibleBitfield = true;
3307 TPA.Revert();
3308 } else {
3309 // We have a type-specifier-seq.
3310 TPA.Commit();
3311 }
3312 }
3313 } else {
3314 // Consume the ':'.
3315 ConsumeToken();
3316 }
3317
3318 if (!PossibleBitfield) {
3319 SourceRange Range;
3320 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003321
Richard Smith80ad52f2013-01-02 11:42:31 +00003322 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003323 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003324 } else if (!getLangOpts().ObjC2) {
3325 if (getLangOpts().CPlusPlus)
3326 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3327 else
3328 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3329 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003330 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003331 }
3332
Richard Smithbdad7a22012-01-10 01:33:14 +00003333 // There are four options here. If we have 'friend enum foo;' then this is a
3334 // friend declaration, and cannot have an accompanying definition. If we have
3335 // 'enum foo;', then this is a forward declaration. If we have
3336 // 'enum foo {...' then this is a definition. Otherwise we have something
3337 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003338 //
3339 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3340 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3341 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3342 //
John McCallf312b1e2010-08-26 23:41:50 +00003343 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003344 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003345 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003346 } else if (Tok.is(tok::l_brace)) {
3347 if (DS.isFriendSpecified()) {
3348 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3349 << SourceRange(DS.getFriendSpecLoc());
3350 ConsumeBrace();
3351 SkipUntil(tok::r_brace);
3352 TUK = Sema::TUK_Friend;
3353 } else {
3354 TUK = Sema::TUK_Definition;
3355 }
Richard Smithc9f35172012-06-25 21:37:02 +00003356 } else if (DSC != DSC_type_specifier &&
3357 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003358 (Tok.isAtStartOfLine() &&
3359 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003360 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3361 if (Tok.isNot(tok::semi)) {
3362 // A semicolon was missing after this declaration. Diagnose and recover.
3363 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3364 "enum");
3365 PP.EnterToken(Tok);
3366 Tok.setKind(tok::semi);
3367 }
John McCall13489672012-05-07 06:16:58 +00003368 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003369 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003370 }
3371
3372 // If this is an elaborated type specifier, and we delayed
3373 // diagnostics before, just merge them into the current pool.
3374 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3375 diagsFromTag.redelay();
3376 }
Richard Smith1af83c42012-03-23 03:33:32 +00003377
3378 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003379 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003380 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003381 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003382 // Skip the rest of this declarator, up until the comma or semicolon.
3383 Diag(Tok, diag::err_enum_template);
3384 SkipUntil(tok::comma, true);
3385 return;
3386 }
3387
3388 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3389 // Enumerations can't be explicitly instantiated.
3390 DS.SetTypeSpecError();
3391 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3392 return;
3393 }
3394
3395 assert(TemplateInfo.TemplateParams && "no template parameters");
3396 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3397 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003398 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003399
Sean Hunt2edf0a22012-06-23 05:07:58 +00003400 if (TUK == Sema::TUK_Reference)
3401 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003402
Douglas Gregorb9075602011-02-22 02:55:24 +00003403 if (!Name && TUK != Sema::TUK_Definition) {
3404 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003405
Douglas Gregorb9075602011-02-22 02:55:24 +00003406 // Skip the rest of this declarator, up until the comma or semicolon.
3407 SkipUntil(tok::comma, true);
3408 return;
3409 }
Richard Smith1af83c42012-03-23 03:33:32 +00003410
Douglas Gregor402abb52009-05-28 23:31:59 +00003411 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003412 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003413 const char *PrevSpec = 0;
3414 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003415 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003416 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003417 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003418 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003419 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003420
Douglas Gregor48c89f42010-04-24 16:38:41 +00003421 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003422 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003423 // dependent tag.
3424 if (!Name) {
3425 DS.SetTypeSpecError();
3426 Diag(Tok, diag::err_expected_type_name_after_typename);
3427 return;
3428 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003429
Douglas Gregor23c94db2010-07-02 17:43:08 +00003430 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003431 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003432 NameLoc);
3433 if (Type.isInvalid()) {
3434 DS.SetTypeSpecError();
3435 return;
3436 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003437
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003438 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3439 NameLoc.isValid() ? NameLoc : StartLoc,
3440 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003441 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003442
Douglas Gregor48c89f42010-04-24 16:38:41 +00003443 return;
3444 }
Mike Stump1eb44332009-09-09 15:08:12 +00003445
John McCalld226f652010-08-21 09:40:31 +00003446 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003447 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003448 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003449 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003450 ConsumeBrace();
3451 SkipUntil(tok::r_brace);
3452 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003453
Douglas Gregor48c89f42010-04-24 16:38:41 +00003454 DS.SetTypeSpecError();
3455 return;
3456 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003457
Richard Smithc9f35172012-06-25 21:37:02 +00003458 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003459 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003461 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3462 NameLoc.isValid() ? NameLoc : StartLoc,
3463 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003464 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003465}
3466
3467/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3468/// enumerator-list:
3469/// enumerator
3470/// enumerator-list ',' enumerator
3471/// enumerator:
3472/// enumeration-constant
3473/// enumeration-constant '=' constant-expression
3474/// enumeration-constant:
3475/// identifier
3476///
John McCalld226f652010-08-21 09:40:31 +00003477void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003478 // Enter the scope of the enum body and start the definition.
3479 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003480 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003481
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003482 BalancedDelimiterTracker T(*this, tok::l_brace);
3483 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003484
Chris Lattner7946dd32007-08-27 17:24:30 +00003485 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003486 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003487 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003488
Chris Lattner5f9e2722011-07-23 10:55:15 +00003489 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003490
John McCalld226f652010-08-21 09:40:31 +00003491 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003492
Reid Spencer5f016e22007-07-11 17:01:13 +00003493 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003494 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3496 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003497
John McCall5b629aa2010-10-22 23:36:17 +00003498 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003499 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003500 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003501 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003502 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003503
Reid Spencer5f016e22007-07-11 17:01:13 +00003504 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003505 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003506 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003507
Chris Lattner04d66662007-10-09 17:33:22 +00003508 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003509 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003510 AssignedVal = ParseConstantExpression();
3511 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003512 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 }
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003516 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3517 LastEnumConstDecl,
3518 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003519 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003520 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003521 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003522
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 EnumConstantDecls.push_back(EnumConstDecl);
3524 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Douglas Gregor751f6922010-09-07 14:51:08 +00003526 if (Tok.is(tok::identifier)) {
3527 // We're missing a comma between enumerators.
3528 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003529 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003530 << FixItHint::CreateInsertion(Loc, ", ");
3531 continue;
3532 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003533
Chris Lattner04d66662007-10-09 17:33:22 +00003534 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 break;
3536 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003537
Richard Smith7fe62082011-10-15 05:09:34 +00003538 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003539 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003540 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3541 diag::ext_enumerator_list_comma_cxx :
3542 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003543 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003544 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003545 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3546 << FixItHint::CreateRemoval(CommaLoc);
3547 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003548 }
Mike Stump1eb44332009-09-09 15:08:12 +00003549
Reid Spencer5f016e22007-07-11 17:01:13 +00003550 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003551 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003552
Reid Spencer5f016e22007-07-11 17:01:13 +00003553 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003554 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003555 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003556
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003557 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3558 EnumDecl, EnumConstantDecls.data(),
3559 EnumConstantDecls.size(), getCurScope(),
3560 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Douglas Gregor72de6672009-01-08 20:45:30 +00003562 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003563 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3564 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003565
3566 // The next token must be valid after an enum definition. If not, a ';'
3567 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003568 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3569 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003570 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3571 // Push this token back into the preprocessor and change our current token
3572 // to ';' so that the rest of the code recovers as though there were an
3573 // ';' after the definition.
3574 PP.EnterToken(Tok);
3575 Tok.setKind(tok::semi);
3576 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003577}
3578
3579/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003580/// start of a type-qualifier-list.
3581bool Parser::isTypeQualifier() const {
3582 switch (Tok.getKind()) {
3583 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003584
3585 // type-qualifier only in OpenCL
3586 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003587 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003588
Steve Naroff5f8aa692008-02-11 23:15:56 +00003589 // type-qualifier
3590 case tok::kw_const:
3591 case tok::kw_volatile:
3592 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003593 case tok::kw___private:
3594 case tok::kw___local:
3595 case tok::kw___global:
3596 case tok::kw___constant:
3597 case tok::kw___read_only:
3598 case tok::kw___read_write:
3599 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003600 return true;
3601 }
3602}
3603
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003604/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3605/// is definitely a type-specifier. Return false if it isn't part of a type
3606/// specifier or if we're not sure.
3607bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3608 switch (Tok.getKind()) {
3609 default: return false;
3610 // type-specifiers
3611 case tok::kw_short:
3612 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003613 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003614 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003615 case tok::kw_signed:
3616 case tok::kw_unsigned:
3617 case tok::kw__Complex:
3618 case tok::kw__Imaginary:
3619 case tok::kw_void:
3620 case tok::kw_char:
3621 case tok::kw_wchar_t:
3622 case tok::kw_char16_t:
3623 case tok::kw_char32_t:
3624 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003625 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003626 case tok::kw_float:
3627 case tok::kw_double:
3628 case tok::kw_bool:
3629 case tok::kw__Bool:
3630 case tok::kw__Decimal32:
3631 case tok::kw__Decimal64:
3632 case tok::kw__Decimal128:
3633 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003634
Guy Benyeib13621d2012-12-18 14:38:23 +00003635 // OpenCL specific types:
3636 case tok::kw_image1d_t:
3637 case tok::kw_image1d_array_t:
3638 case tok::kw_image1d_buffer_t:
3639 case tok::kw_image2d_t:
3640 case tok::kw_image2d_array_t:
3641 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003642 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003643 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003644
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003645 // struct-or-union-specifier (C99) or class-specifier (C++)
3646 case tok::kw_class:
3647 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003648 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003649 case tok::kw_union:
3650 // enum-specifier
3651 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003652
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003653 // typedef-name
3654 case tok::annot_typename:
3655 return true;
3656 }
3657}
3658
Steve Naroff5f8aa692008-02-11 23:15:56 +00003659/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003660/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003661bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 switch (Tok.getKind()) {
3663 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003664
Chris Lattner166a8fc2009-01-04 23:41:41 +00003665 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003666 if (TryAltiVecVectorToken())
3667 return true;
3668 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003669 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003670 // Annotate typenames and C++ scope specifiers. If we get one, just
3671 // recurse to handle whatever we get.
3672 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003673 return true;
3674 if (Tok.is(tok::identifier))
3675 return false;
3676 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003677
Chris Lattner166a8fc2009-01-04 23:41:41 +00003678 case tok::coloncolon: // ::foo::bar
3679 if (NextToken().is(tok::kw_new) || // ::new
3680 NextToken().is(tok::kw_delete)) // ::delete
3681 return false;
3682
Chris Lattner166a8fc2009-01-04 23:41:41 +00003683 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003684 return true;
3685 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003686
Reid Spencer5f016e22007-07-11 17:01:13 +00003687 // GNU attributes support.
3688 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003689 // GNU typeof support.
3690 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003691
Reid Spencer5f016e22007-07-11 17:01:13 +00003692 // type-specifiers
3693 case tok::kw_short:
3694 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003695 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003696 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003697 case tok::kw_signed:
3698 case tok::kw_unsigned:
3699 case tok::kw__Complex:
3700 case tok::kw__Imaginary:
3701 case tok::kw_void:
3702 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003703 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003704 case tok::kw_char16_t:
3705 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003706 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003707 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 case tok::kw_float:
3709 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003710 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003711 case tok::kw__Bool:
3712 case tok::kw__Decimal32:
3713 case tok::kw__Decimal64:
3714 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003715 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003716
Guy Benyeib13621d2012-12-18 14:38:23 +00003717 // OpenCL specific types:
3718 case tok::kw_image1d_t:
3719 case tok::kw_image1d_array_t:
3720 case tok::kw_image1d_buffer_t:
3721 case tok::kw_image2d_t:
3722 case tok::kw_image2d_array_t:
3723 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003724 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003725 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003726
Chris Lattner99dc9142008-04-13 18:59:07 +00003727 // struct-or-union-specifier (C99) or class-specifier (C++)
3728 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003729 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003730 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 case tok::kw_union:
3732 // enum-specifier
3733 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003734
Reid Spencer5f016e22007-07-11 17:01:13 +00003735 // type-qualifier
3736 case tok::kw_const:
3737 case tok::kw_volatile:
3738 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003739
John McCallb8a8de32012-11-14 00:49:39 +00003740 // Debugger support.
3741 case tok::kw___unknown_anytype:
3742
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003743 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003744 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003745 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Chris Lattner7c186be2008-10-20 00:25:30 +00003747 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3748 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003749 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Steve Naroff239f0732008-12-25 14:16:32 +00003751 case tok::kw___cdecl:
3752 case tok::kw___stdcall:
3753 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003754 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003755 case tok::kw___w64:
3756 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003757 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003758 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003759 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003760
3761 case tok::kw___private:
3762 case tok::kw___local:
3763 case tok::kw___global:
3764 case tok::kw___constant:
3765 case tok::kw___read_only:
3766 case tok::kw___read_write:
3767 case tok::kw___write_only:
3768
Eli Friedman290eeb02009-06-08 23:27:34 +00003769 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003770
3771 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003772 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003773
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003774 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003775 case tok::kw__Atomic:
3776 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003777 }
3778}
3779
3780/// isDeclarationSpecifier() - Return true if the current token is part of a
3781/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003782///
3783/// \param DisambiguatingWithExpression True to indicate that the purpose of
3784/// this check is to disambiguate between an expression and a declaration.
3785bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 switch (Tok.getKind()) {
3787 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003788
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003789 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003790 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003791
Chris Lattner166a8fc2009-01-04 23:41:41 +00003792 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003793 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003794 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003795 return false;
John Thompson82287d12010-02-05 00:12:22 +00003796 if (TryAltiVecVectorToken())
3797 return true;
3798 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003799 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003800 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003801 // Annotate typenames and C++ scope specifiers. If we get one, just
3802 // recurse to handle whatever we get.
3803 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003804 return true;
3805 if (Tok.is(tok::identifier))
3806 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003807
Douglas Gregor9497a732010-09-16 01:51:54 +00003808 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003809 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003810 // expression is permitted, then this is probably a class message send
3811 // missing the initial '['. In this case, we won't consider this to be
3812 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003813 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003814 isStartOfObjCClassMessageMissingOpenBracket())
3815 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003816
John McCall9ba61662010-02-26 08:45:28 +00003817 return isDeclarationSpecifier();
3818
Chris Lattner166a8fc2009-01-04 23:41:41 +00003819 case tok::coloncolon: // ::foo::bar
3820 if (NextToken().is(tok::kw_new) || // ::new
3821 NextToken().is(tok::kw_delete)) // ::delete
3822 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003823
Chris Lattner166a8fc2009-01-04 23:41:41 +00003824 // Annotate typenames and C++ scope specifiers. If we get one, just
3825 // recurse to handle whatever we get.
3826 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003827 return true;
3828 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003829
Reid Spencer5f016e22007-07-11 17:01:13 +00003830 // storage-class-specifier
3831 case tok::kw_typedef:
3832 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003833 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003834 case tok::kw_static:
3835 case tok::kw_auto:
3836 case tok::kw_register:
3837 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003838
Douglas Gregor8d267c52011-09-09 02:06:17 +00003839 // Modules
3840 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003841
John McCallb8a8de32012-11-14 00:49:39 +00003842 // Debugger support
3843 case tok::kw___unknown_anytype:
3844
Reid Spencer5f016e22007-07-11 17:01:13 +00003845 // type-specifiers
3846 case tok::kw_short:
3847 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003848 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003849 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003850 case tok::kw_signed:
3851 case tok::kw_unsigned:
3852 case tok::kw__Complex:
3853 case tok::kw__Imaginary:
3854 case tok::kw_void:
3855 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003856 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003857 case tok::kw_char16_t:
3858 case tok::kw_char32_t:
3859
Reid Spencer5f016e22007-07-11 17:01:13 +00003860 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003861 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003862 case tok::kw_float:
3863 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003864 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 case tok::kw__Bool:
3866 case tok::kw__Decimal32:
3867 case tok::kw__Decimal64:
3868 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003869 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003870
Guy Benyeib13621d2012-12-18 14:38:23 +00003871 // OpenCL specific types:
3872 case tok::kw_image1d_t:
3873 case tok::kw_image1d_array_t:
3874 case tok::kw_image1d_buffer_t:
3875 case tok::kw_image2d_t:
3876 case tok::kw_image2d_array_t:
3877 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003878 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003879 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003880
Chris Lattner99dc9142008-04-13 18:59:07 +00003881 // struct-or-union-specifier (C99) or class-specifier (C++)
3882 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003883 case tok::kw_struct:
3884 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003885 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003886 // enum-specifier
3887 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 // type-qualifier
3890 case tok::kw_const:
3891 case tok::kw_volatile:
3892 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003893
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 // function-specifier
3895 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003896 case tok::kw_virtual:
3897 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003898 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003899
Richard Smith4cd81c52013-01-29 09:02:09 +00003900 // alignment-specifier
3901 case tok::kw__Alignas:
3902
Richard Smith53aec2a2012-10-25 00:00:53 +00003903 // friend keyword.
3904 case tok::kw_friend:
3905
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003906 // static_assert-declaration
3907 case tok::kw__Static_assert:
3908
Chris Lattner1ef08762007-08-09 17:01:07 +00003909 // GNU typeof support.
3910 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003911
Chris Lattner1ef08762007-08-09 17:01:07 +00003912 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003913 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003914
Richard Smith53aec2a2012-10-25 00:00:53 +00003915 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003916 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003917 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003918
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003919 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003920 case tok::kw__Atomic:
3921 return true;
3922
Chris Lattnerf3948c42008-07-26 03:38:44 +00003923 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3924 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003925 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003926
Douglas Gregord9d75e52011-04-27 05:41:15 +00003927 // typedef-name
3928 case tok::annot_typename:
3929 return !DisambiguatingWithExpression ||
3930 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003931
Steve Naroff47f52092009-01-06 19:34:12 +00003932 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003933 case tok::kw___cdecl:
3934 case tok::kw___stdcall:
3935 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003936 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003937 case tok::kw___w64:
3938 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003939 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003940 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003941 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003942 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003943
3944 case tok::kw___private:
3945 case tok::kw___local:
3946 case tok::kw___global:
3947 case tok::kw___constant:
3948 case tok::kw___read_only:
3949 case tok::kw___read_write:
3950 case tok::kw___write_only:
3951
Eli Friedman290eeb02009-06-08 23:27:34 +00003952 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003953 }
3954}
3955
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003956bool Parser::isConstructorDeclarator() {
3957 TentativeParsingAction TPA(*this);
3958
3959 // Parse the C++ scope specifier.
3960 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003961 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003962 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003963 TPA.Revert();
3964 return false;
3965 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003966
3967 // Parse the constructor name.
3968 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3969 // We already know that we have a constructor name; just consume
3970 // the token.
3971 ConsumeToken();
3972 } else {
3973 TPA.Revert();
3974 return false;
3975 }
3976
Richard Smith22592862012-03-27 23:05:05 +00003977 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003978 if (Tok.isNot(tok::l_paren)) {
3979 TPA.Revert();
3980 return false;
3981 }
3982 ConsumeParen();
3983
Richard Smith22592862012-03-27 23:05:05 +00003984 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3985 // that we have a constructor.
3986 if (Tok.is(tok::r_paren) ||
3987 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003988 TPA.Revert();
3989 return true;
3990 }
3991
3992 // If we need to, enter the specified scope.
3993 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003994 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003995 DeclScopeObj.EnterDeclaratorScope();
3996
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003997 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003998 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003999 MaybeParseMicrosoftAttributes(Attrs);
4000
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004001 // Check whether the next token(s) are part of a declaration
4002 // specifier, in which case we have the start of a parameter and,
4003 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004004 bool IsConstructor = false;
4005 if (isDeclarationSpecifier())
4006 IsConstructor = true;
4007 else if (Tok.is(tok::identifier) ||
4008 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4009 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4010 // This might be a parenthesized member name, but is more likely to
4011 // be a constructor declaration with an invalid argument type. Keep
4012 // looking.
4013 if (Tok.is(tok::annot_cxxscope))
4014 ConsumeToken();
4015 ConsumeToken();
4016
4017 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004018 // which must have one of the following syntactic forms (see the
4019 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004020 switch (Tok.getKind()) {
4021 case tok::l_paren:
4022 // C(X ( int));
4023 case tok::l_square:
4024 // C(X [ 5]);
4025 // C(X [ [attribute]]);
4026 case tok::coloncolon:
4027 // C(X :: Y);
4028 // C(X :: *p);
4029 case tok::r_paren:
4030 // C(X )
4031 // Assume this isn't a constructor, rather than assuming it's a
4032 // constructor with an unnamed parameter of an ill-formed type.
4033 break;
4034
4035 default:
4036 IsConstructor = true;
4037 break;
4038 }
4039 }
4040
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004041 TPA.Revert();
4042 return IsConstructor;
4043}
Reid Spencer5f016e22007-07-11 17:01:13 +00004044
4045/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004046/// type-qualifier-list: [C99 6.7.5]
4047/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004048/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004049/// [ only if VendorAttributesAllowed=true ]
4050/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004051/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004052/// [ only if VendorAttributesAllowed=true ]
4053/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004054/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004055/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004056///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004057void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4058 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004059 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004060 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004061 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004062 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004063 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004064 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004065 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004066
4067 SourceLocation EndLoc;
4068
Reid Spencer5f016e22007-07-11 17:01:13 +00004069 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004070 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004071 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004072 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 SourceLocation Loc = Tok.getLocation();
4074
4075 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004076 case tok::code_completion:
4077 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004078 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004079
Reid Spencer5f016e22007-07-11 17:01:13 +00004080 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004082 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004083 break;
4084 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004086 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004087 break;
4088 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004090 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004092
4093 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004094 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004095 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004096 goto DoneWithTypeQuals;
4097 case tok::kw___private:
4098 case tok::kw___global:
4099 case tok::kw___local:
4100 case tok::kw___constant:
4101 case tok::kw___read_only:
4102 case tok::kw___write_only:
4103 case tok::kw___read_write:
4104 ParseOpenCLQualifiers(DS);
4105 break;
4106
Eli Friedman290eeb02009-06-08 23:27:34 +00004107 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004108 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004109 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004110 case tok::kw___cdecl:
4111 case tok::kw___stdcall:
4112 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004113 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004114 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004115 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004116 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004117 continue;
4118 }
4119 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004120 case tok::kw___pascal:
4121 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004122 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004123 continue;
4124 }
4125 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004127 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004128 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004129 continue; // do *not* consume the next token!
4130 }
4131 // otherwise, FALL THROUGH!
4132 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004133 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004134 // If this is not a type-qualifier token, we're done reading type
4135 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004136 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004137 if (EndLoc.isValid())
4138 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004139 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004141
Reid Spencer5f016e22007-07-11 17:01:13 +00004142 // If the specifier combination wasn't legal, issue a diagnostic.
4143 if (isInvalid) {
4144 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004145 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004146 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004147 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004148 }
4149}
4150
4151
4152/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4153///
4154void Parser::ParseDeclarator(Declarator &D) {
4155 /// This implements the 'declarator' production in the C grammar, then checks
4156 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004157 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004158}
4159
Richard Smith9988f282012-03-29 01:16:42 +00004160static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4161 if (Kind == tok::star || Kind == tok::caret)
4162 return true;
4163
4164 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4165 if (!Lang.CPlusPlus)
4166 return false;
4167
4168 return Kind == tok::amp || Kind == tok::ampamp;
4169}
4170
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004171/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4172/// is parsed by the function passed to it. Pass null, and the direct-declarator
4173/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004174/// ptr-operator production.
4175///
Richard Smith0706df42011-10-19 21:33:05 +00004176/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004177/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4178/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004179///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004180/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4181/// [C] pointer[opt] direct-declarator
4182/// [C++] direct-declarator
4183/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004184///
4185/// pointer: [C99 6.7.5]
4186/// '*' type-qualifier-list[opt]
4187/// '*' type-qualifier-list[opt] pointer
4188///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004189/// ptr-operator:
4190/// '*' cv-qualifier-seq[opt]
4191/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004192/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004193/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004194/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004195/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004196void Parser::ParseDeclaratorInternal(Declarator &D,
4197 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004198 if (Diags.hasAllExtensionsSilenced())
4199 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004200
Sebastian Redlf30208a2009-01-24 21:16:55 +00004201 // C++ member pointers start with a '::' or a nested-name.
4202 // Member pointers get special handling, since there's no place for the
4203 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004204 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004205 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4206 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004207 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4208 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004209 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004210 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004211
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004212 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004213 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004214 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004215 if (D.mayHaveIdentifier())
4216 D.getCXXScopeSpec() = SS;
4217 else
4218 AnnotateScopeToken(SS, true);
4219
Sebastian Redlf30208a2009-01-24 21:16:55 +00004220 if (DirectDeclParser)
4221 (this->*DirectDeclParser)(D);
4222 return;
4223 }
4224
4225 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004226 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004227 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004228 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004229 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004230
4231 // Recurse to parse whatever is left.
4232 ParseDeclaratorInternal(D, DirectDeclParser);
4233
4234 // Sema will have to catch (syntactically invalid) pointers into global
4235 // scope. It has to catch pointers into namespace scope anyway.
4236 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004237 Loc),
4238 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004239 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004240 return;
4241 }
4242 }
4243
4244 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004245 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004246 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004247 if (DirectDeclParser)
4248 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004249 return;
4250 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004251
Sebastian Redl05532f22009-03-15 22:02:01 +00004252 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4253 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004254 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004255 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004256
Chris Lattner9af55002009-03-27 04:18:06 +00004257 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004258 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004259 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004260
Richard Smith6ee326a2012-04-10 01:32:12 +00004261 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004262 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004263 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004264
Reid Spencer5f016e22007-07-11 17:01:13 +00004265 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004266 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004267 if (Kind == tok::star)
4268 // Remember that we parsed a pointer type, and remember the type-quals.
4269 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004270 DS.getConstSpecLoc(),
4271 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004272 DS.getRestrictSpecLoc()),
4273 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004274 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004275 else
4276 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004277 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004278 Loc),
4279 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004280 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004281 } else {
4282 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004283 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004284
Sebastian Redl743de1f2009-03-23 00:00:23 +00004285 // Complain about rvalue references in C++03, but then go on and build
4286 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004287 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004288 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004289 diag::warn_cxx98_compat_rvalue_reference :
4290 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004291
Richard Smith6ee326a2012-04-10 01:32:12 +00004292 // GNU-style and C++11 attributes are allowed here, as is restrict.
4293 ParseTypeQualifierListOpt(DS);
4294 D.ExtendWithDeclSpec(DS);
4295
Reid Spencer5f016e22007-07-11 17:01:13 +00004296 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4297 // cv-qualifiers are introduced through the use of a typedef or of a
4298 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004299 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4300 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4301 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004302 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004303 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4304 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004305 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004306 }
4307
4308 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004309 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004310
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004311 if (D.getNumTypeObjects() > 0) {
4312 // C++ [dcl.ref]p4: There shall be no references to references.
4313 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4314 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004315 if (const IdentifierInfo *II = D.getIdentifier())
4316 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4317 << II;
4318 else
4319 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4320 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004321
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004322 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004323 // can go ahead and build the (technically ill-formed)
4324 // declarator: reference collapsing will take care of it.
4325 }
4326 }
4327
Reid Spencer5f016e22007-07-11 17:01:13 +00004328 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004329 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004330 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004331 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004332 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004333 }
4334}
4335
Richard Smith9988f282012-03-29 01:16:42 +00004336static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4337 SourceLocation EllipsisLoc) {
4338 if (EllipsisLoc.isValid()) {
4339 FixItHint Insertion;
4340 if (!D.getEllipsisLoc().isValid()) {
4341 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4342 D.setEllipsisLoc(EllipsisLoc);
4343 }
4344 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4345 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4346 }
4347}
4348
Reid Spencer5f016e22007-07-11 17:01:13 +00004349/// ParseDirectDeclarator
4350/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004351/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004352/// '(' declarator ')'
4353/// [GNU] '(' attributes declarator ')'
4354/// [C90] direct-declarator '[' constant-expression[opt] ']'
4355/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4356/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4357/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4358/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004359/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4360/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004361/// direct-declarator '(' parameter-type-list ')'
4362/// direct-declarator '(' identifier-list[opt] ')'
4363/// [GNU] direct-declarator '(' parameter-forward-declarations
4364/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004365/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4366/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004367/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4368/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4369/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004370/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004371/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004372///
4373/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004374/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004375/// '::'[opt] nested-name-specifier[opt] type-name
4376///
4377/// id-expression: [C++ 5.1]
4378/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004379/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004380///
4381/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004382/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004383/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004384/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004385/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004386/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004387///
Richard Smith5d8388c2012-03-27 01:42:32 +00004388/// Note, any additional constructs added here may need corresponding changes
4389/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004390void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004391 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004392
David Blaikie4e4d0842012-03-11 07:00:24 +00004393 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004394 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004395 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004396 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4397 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004398 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004399 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004400 }
4401
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004402 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004403 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004404 // Change the declaration context for name lookup, until this function
4405 // is exited (and the declarator has been parsed).
4406 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004407 }
4408
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004409 // C++0x [dcl.fct]p14:
4410 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004411 // of a parameter-declaration-clause without a preceding comma. In
4412 // this case, the ellipsis is parsed as part of the
4413 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004414 // parameter pack that has not been expanded; otherwise, it is parsed
4415 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004416 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004417 !((D.getContext() == Declarator::PrototypeContext ||
4418 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004419 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004420 !Actions.containsUnexpandedParameterPacks(D))) {
4421 SourceLocation EllipsisLoc = ConsumeToken();
4422 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4423 // The ellipsis was put in the wrong place. Recover, and explain to
4424 // the user what they should have done.
4425 ParseDeclarator(D);
4426 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4427 return;
4428 } else
4429 D.setEllipsisLoc(EllipsisLoc);
4430
4431 // The ellipsis can't be followed by a parenthesized declarator. We
4432 // check for that in ParseParenDeclarator, after we have disambiguated
4433 // the l_paren token.
4434 }
4435
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004436 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4437 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4438 // We found something that indicates the start of an unqualified-id.
4439 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004440 bool AllowConstructorName;
4441 if (D.getDeclSpec().hasTypeSpecifier())
4442 AllowConstructorName = false;
4443 else if (D.getCXXScopeSpec().isSet())
4444 AllowConstructorName =
4445 (D.getContext() == Declarator::FileContext ||
4446 (D.getContext() == Declarator::MemberContext &&
4447 D.getDeclSpec().isFriendSpecified()));
4448 else
4449 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4450
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004451 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004452 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4453 /*EnteringContext=*/true,
4454 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004455 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004456 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004457 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004458 D.getName()) ||
4459 // Once we're past the identifier, if the scope was bad, mark the
4460 // whole declarator bad.
4461 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004462 D.SetIdentifier(0, Tok.getLocation());
4463 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004464 } else {
4465 // Parsed the unqualified-id; update range information and move along.
4466 if (D.getSourceRange().getBegin().isInvalid())
4467 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4468 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004469 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004470 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004471 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004472 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004473 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004474 "There's a C++-specific check for tok::identifier above");
4475 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4476 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4477 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004478 goto PastIdentifier;
4479 }
Richard Smith9988f282012-03-29 01:16:42 +00004480
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004481 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004482 // direct-declarator: '(' declarator ')'
4483 // direct-declarator: '(' attributes declarator ')'
4484 // Example: 'char (*X)' or 'int (*XX)(void)'
4485 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004486
4487 // If the declarator was parenthesized, we entered the declarator
4488 // scope when parsing the parenthesized declarator, then exited
4489 // the scope already. Re-enter the scope, if we need to.
4490 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004491 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004492 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004493 if (!D.isInvalidType() &&
4494 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004495 // Change the declaration context for name lookup, until this function
4496 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004497 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004498 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004499 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004500 // This could be something simple like "int" (in which case the declarator
4501 // portion is empty), if an abstract-declarator is allowed.
4502 D.SetIdentifier(0, Tok.getLocation());
4503 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004504 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004505 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004506 if (D.getContext() == Declarator::MemberContext)
4507 Diag(Tok, diag::err_expected_member_name_or_semi)
4508 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004509 else if (getLangOpts().CPlusPlus) {
4510 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4511 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4512 else
4513 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4514 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004515 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004516 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004517 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004518 }
Mike Stump1eb44332009-09-09 15:08:12 +00004519
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004520 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004521 assert(D.isPastIdentifier() &&
4522 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004523
Richard Smith6ee326a2012-04-10 01:32:12 +00004524 // Don't parse attributes unless we have parsed an unparenthesized name.
4525 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004526 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004527
Reid Spencer5f016e22007-07-11 17:01:13 +00004528 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004529 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004530 // Enter function-declaration scope, limiting any declarators to the
4531 // function prototype scope, including parameter declarators.
4532 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004533 Scope::FunctionPrototypeScope|Scope::DeclScope|
4534 (D.isFunctionDeclaratorAFunctionDeclaration()
4535 ? Scope::FunctionDeclarationScope : 0));
4536
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004537 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4538 // In such a case, check if we actually have a function declarator; if it
4539 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004540 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004541 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4542 // The name of the declarator, if any, is tentatively declared within
4543 // a possible direct initializer.
4544 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4545 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4546 TentativelyDeclaredIdentifiers.pop_back();
4547 if (!IsFunctionDecl)
4548 break;
4549 }
John McCall0b7e6782011-03-24 11:26:52 +00004550 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004551 BalancedDelimiterTracker T(*this, tok::l_paren);
4552 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004553 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004554 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004555 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004556 ParseBracketDeclarator(D);
4557 } else {
4558 break;
4559 }
4560 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004561}
Reid Spencer5f016e22007-07-11 17:01:13 +00004562
Chris Lattneref4715c2008-04-06 05:45:57 +00004563/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4564/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004565/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004566/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4567///
4568/// direct-declarator:
4569/// '(' declarator ')'
4570/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004571/// direct-declarator '(' parameter-type-list ')'
4572/// direct-declarator '(' identifier-list[opt] ')'
4573/// [GNU] direct-declarator '(' parameter-forward-declarations
4574/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004575///
4576void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004577 BalancedDelimiterTracker T(*this, tok::l_paren);
4578 T.consumeOpen();
4579
Chris Lattneref4715c2008-04-06 05:45:57 +00004580 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004581
Chris Lattner7399ee02008-10-20 02:05:46 +00004582 // Eat any attributes before we look at whether this is a grouping or function
4583 // declarator paren. If this is a grouping paren, the attribute applies to
4584 // the type being built up, for example:
4585 // int (__attribute__(()) *x)(long y)
4586 // If this ends up not being a grouping paren, the attribute applies to the
4587 // first argument, for example:
4588 // int (__attribute__(()) int x)
4589 // In either case, we need to eat any attributes to be able to determine what
4590 // sort of paren this is.
4591 //
John McCall0b7e6782011-03-24 11:26:52 +00004592 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004593 bool RequiresArg = false;
4594 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004595 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004596
Chris Lattner7399ee02008-10-20 02:05:46 +00004597 // We require that the argument list (if this is a non-grouping paren) be
4598 // present even if the attribute list was empty.
4599 RequiresArg = true;
4600 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004601
Steve Naroff239f0732008-12-25 14:16:32 +00004602 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004603 ParseMicrosoftTypeAttributes(attrs);
4604
Dawn Perchik52fc3142010-09-03 01:29:35 +00004605 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004606 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004607 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004608
Chris Lattneref4715c2008-04-06 05:45:57 +00004609 // If we haven't past the identifier yet (or where the identifier would be
4610 // stored, if this is an abstract declarator), then this is probably just
4611 // grouping parens. However, if this could be an abstract-declarator, then
4612 // this could also be the start of function arguments (consider 'void()').
4613 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004614
Chris Lattneref4715c2008-04-06 05:45:57 +00004615 if (!D.mayOmitIdentifier()) {
4616 // If this can't be an abstract-declarator, this *must* be a grouping
4617 // paren, because we haven't seen the identifier yet.
4618 isGrouping = true;
4619 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004620 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4621 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004622 isDeclarationSpecifier() || // 'int(int)' is a function.
4623 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004624 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4625 // considered to be a type, not a K&R identifier-list.
4626 isGrouping = false;
4627 } else {
4628 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4629 isGrouping = true;
4630 }
Mike Stump1eb44332009-09-09 15:08:12 +00004631
Chris Lattneref4715c2008-04-06 05:45:57 +00004632 // If this is a grouping paren, handle:
4633 // direct-declarator: '(' declarator ')'
4634 // direct-declarator: '(' attributes declarator ')'
4635 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004636 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4637 D.setEllipsisLoc(SourceLocation());
4638
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004639 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004640 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004641 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004642 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004643 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004644 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004645 T.getCloseLocation()),
4646 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004647
4648 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004649
4650 // An ellipsis cannot be placed outside parentheses.
4651 if (EllipsisLoc.isValid())
4652 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4653
Chris Lattneref4715c2008-04-06 05:45:57 +00004654 return;
4655 }
Mike Stump1eb44332009-09-09 15:08:12 +00004656
Chris Lattneref4715c2008-04-06 05:45:57 +00004657 // Okay, if this wasn't a grouping paren, it must be the start of a function
4658 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004659 // identifier (and remember where it would have been), then call into
4660 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004661 D.SetIdentifier(0, Tok.getLocation());
4662
David Blaikie42d6d0c2011-12-04 05:04:18 +00004663 // Enter function-declaration scope, limiting any declarators to the
4664 // function prototype scope, including parameter declarators.
4665 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004666 Scope::FunctionPrototypeScope | Scope::DeclScope |
4667 (D.isFunctionDeclaratorAFunctionDeclaration()
4668 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004669 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004670 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004671}
4672
4673/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4674/// declarator D up to a paren, which indicates that we are parsing function
4675/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004676///
Richard Smith6ee326a2012-04-10 01:32:12 +00004677/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4678/// immediately after the open paren - they should be considered to be the
4679/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004680///
Richard Smith6ee326a2012-04-10 01:32:12 +00004681/// If RequiresArg is true, then the first argument of the function is required
4682/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004683///
Richard Smith6ee326a2012-04-10 01:32:12 +00004684/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4685/// (C++11) ref-qualifier[opt], exception-specification[opt],
4686/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4687///
4688/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004689/// dynamic-exception-specification
4690/// noexcept-specification
4691///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004692void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004693 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004694 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004695 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004696 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004697 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004698 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004699 // lparen is already consumed!
4700 assert(D.isPastIdentifier() && "Should not call before identifier!");
4701
4702 // This should be true when the function has typed arguments.
4703 // Otherwise, it is treated as a K&R-style function.
4704 bool HasProto = false;
4705 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004706 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004707 // Remember where we see an ellipsis, if any.
4708 SourceLocation EllipsisLoc;
4709
4710 DeclSpec DS(AttrFactory);
4711 bool RefQualifierIsLValueRef = true;
4712 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004713 SourceLocation ConstQualifierLoc;
4714 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004715 ExceptionSpecificationType ESpecType = EST_None;
4716 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004717 SmallVector<ParsedType, 2> DynamicExceptions;
4718 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004719 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004720 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004721 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004722
James Molloy16f1f712012-02-29 10:24:19 +00004723 Actions.ActOnStartFunctionDeclarator();
4724
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004725 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4726 EndLoc is the end location for the function declarator.
4727 They differ for trailing return types. */
4728 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004729 SourceLocation LParenLoc, RParenLoc;
4730 LParenLoc = Tracker.getOpenLocation();
4731 StartLoc = LParenLoc;
4732
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004733 if (isFunctionDeclaratorIdentifierList()) {
4734 if (RequiresArg)
4735 Diag(Tok, diag::err_argument_required_after_attribute);
4736
4737 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4738
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004739 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004740 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004741 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004742 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004743 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004744 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004745 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004746 else if (RequiresArg)
4747 Diag(Tok, diag::err_argument_required_after_attribute);
4748
David Blaikie4e4d0842012-03-11 07:00:24 +00004749 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004750
4751 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004752 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004753 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004754 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004755 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004756
David Blaikie4e4d0842012-03-11 07:00:24 +00004757 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004758 // FIXME: Accept these components in any order, and produce fixits to
4759 // correct the order if the user gets it wrong. Ideally we should deal
4760 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004761
4762 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004763 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4764 if (!DS.getSourceRange().getEnd().isInvalid()) {
4765 EndLoc = DS.getSourceRange().getEnd();
4766 ConstQualifierLoc = DS.getConstSpecLoc();
4767 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4768 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004769
4770 // Parse ref-qualifier[opt].
4771 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004772 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004773 diag::warn_cxx98_compat_ref_qualifier :
4774 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004775
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004776 RefQualifierIsLValueRef = Tok.is(tok::amp);
4777 RefQualifierLoc = ConsumeToken();
4778 EndLoc = RefQualifierLoc;
4779 }
4780
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004781 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004782 // If a declaration declares a member function or member function
4783 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004784 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004785 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004786 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004787 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004788 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004789 (D.getContext() == Declarator::MemberContext ||
4790 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004791 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004792 Actions.CurContext->isRecord()));
4793 Sema::CXXThisScopeRAII ThisScope(Actions,
4794 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004795 DS.getTypeQualifiers() |
4796 (D.getDeclSpec().isConstexprSpecified()
4797 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004798 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004799
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004800 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004801 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004802 DynamicExceptions,
4803 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004804 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004805 if (ESpecType != EST_None)
4806 EndLoc = ESpecRange.getEnd();
4807
Richard Smith6ee326a2012-04-10 01:32:12 +00004808 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4809 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004810 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004811
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004812 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004813 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004814 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004815 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004816 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4817 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004818 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004819 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004820 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004821 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004822 }
4823 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004824 }
4825
4826 // Remember that we parsed a function type, and remember the attributes.
4827 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004828 IsAmbiguous,
4829 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004830 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004831 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004832 DS.getTypeQualifiers(),
4833 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004834 RefQualifierLoc, ConstQualifierLoc,
4835 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004836 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004837 ESpecType, ESpecRange.getBegin(),
4838 DynamicExceptions.data(),
4839 DynamicExceptionRanges.data(),
4840 DynamicExceptions.size(),
4841 NoexceptExpr.isUsable() ?
4842 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004843 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004844 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004845 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004846
4847 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004848}
4849
4850/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4851/// identifier list form for a K&R-style function: void foo(a,b,c)
4852///
4853/// Note that identifier-lists are only allowed for normal declarators, not for
4854/// abstract-declarators.
4855bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004856 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004857 && Tok.is(tok::identifier)
4858 && !TryAltiVecVectorToken()
4859 // K&R identifier lists can't have typedefs as identifiers, per C99
4860 // 6.7.5.3p11.
4861 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4862 // Identifier lists follow a really simple grammar: the identifiers can
4863 // be followed *only* by a ", identifier" or ")". However, K&R
4864 // identifier lists are really rare in the brave new modern world, and
4865 // it is very common for someone to typo a type in a non-K&R style
4866 // list. If we are presented with something like: "void foo(intptr x,
4867 // float y)", we don't want to start parsing the function declarator as
4868 // though it is a K&R style declarator just because intptr is an
4869 // invalid type.
4870 //
4871 // To handle this, we check to see if the token after the first
4872 // identifier is a "," or ")". Only then do we parse it as an
4873 // identifier list.
4874 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4875}
4876
4877/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4878/// we found a K&R-style identifier list instead of a typed parameter list.
4879///
4880/// After returning, ParamInfo will hold the parsed parameters.
4881///
4882/// identifier-list: [C99 6.7.5]
4883/// identifier
4884/// identifier-list ',' identifier
4885///
4886void Parser::ParseFunctionDeclaratorIdentifierList(
4887 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004888 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004889 // If there was no identifier specified for the declarator, either we are in
4890 // an abstract-declarator, or we are in a parameter declarator which was found
4891 // to be abstract. In abstract-declarators, identifier lists are not valid:
4892 // diagnose this.
4893 if (!D.getIdentifier())
4894 Diag(Tok, diag::ext_ident_list_in_param);
4895
4896 // Maintain an efficient lookup of params we have seen so far.
4897 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4898
4899 while (1) {
4900 // If this isn't an identifier, report the error and skip until ')'.
4901 if (Tok.isNot(tok::identifier)) {
4902 Diag(Tok, diag::err_expected_ident);
4903 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4904 // Forget we parsed anything.
4905 ParamInfo.clear();
4906 return;
4907 }
4908
4909 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4910
4911 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4912 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4913 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4914
4915 // Verify that the argument identifier has not already been mentioned.
4916 if (!ParamsSoFar.insert(ParmII)) {
4917 Diag(Tok, diag::err_param_redefinition) << ParmII;
4918 } else {
4919 // Remember this identifier in ParamInfo.
4920 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4921 Tok.getLocation(),
4922 0));
4923 }
4924
4925 // Eat the identifier.
4926 ConsumeToken();
4927
4928 // The list continues if we see a comma.
4929 if (Tok.isNot(tok::comma))
4930 break;
4931 ConsumeToken();
4932 }
4933}
4934
4935/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4936/// after the opening parenthesis. This function will not parse a K&R-style
4937/// identifier list.
4938///
Richard Smith6ce48a72012-04-11 04:01:28 +00004939/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4940/// caller parsed those arguments immediately after the open paren - they should
4941/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004942///
4943/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4944/// be the location of the ellipsis, if any was parsed.
4945///
Reid Spencer5f016e22007-07-11 17:01:13 +00004946/// parameter-type-list: [C99 6.7.5]
4947/// parameter-list
4948/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004949/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004950///
4951/// parameter-list: [C99 6.7.5]
4952/// parameter-declaration
4953/// parameter-list ',' parameter-declaration
4954///
4955/// parameter-declaration: [C99 6.7.5]
4956/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004957/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004958/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004959/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004960/// declaration-specifiers abstract-declarator[opt]
4961/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004962/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004963/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004964/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004965///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004966void Parser::ParseParameterDeclarationClause(
4967 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004968 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004969 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004970 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004971
Chris Lattnerf97409f2008-04-06 06:57:35 +00004972 while (1) {
4973 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004974 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4975 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004976 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004977 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004978 }
Mike Stump1eb44332009-09-09 15:08:12 +00004979
Chris Lattnerf97409f2008-04-06 06:57:35 +00004980 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004981 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004982 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004983
Richard Smith6ce48a72012-04-11 04:01:28 +00004984 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004985 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00004986
John McCall7f040a92010-12-24 02:08:15 +00004987 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00004988 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00004989
4990 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004991
4992 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004993 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004994 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004995 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4996 // too much hassle.
4997 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004998
Chris Lattnere64c5492009-02-27 18:38:20 +00004999 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005000
Chris Lattnerf97409f2008-04-06 06:57:35 +00005001 // Parse the declarator. This is "PrototypeContext", because we must
5002 // accept either 'declarator' or 'abstract-declarator' here.
5003 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5004 ParseDeclarator(ParmDecl);
5005
5006 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005007 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005008
Chris Lattnerf97409f2008-04-06 06:57:35 +00005009 // Remember this parsed parameter in ParamInfo.
5010 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005011
Douglas Gregor72b505b2008-12-16 21:30:33 +00005012 // DefArgToks is used when the parsing of default arguments needs
5013 // to be delayed.
5014 CachedTokens *DefArgToks = 0;
5015
Chris Lattnerf97409f2008-04-06 06:57:35 +00005016 // If no parameter was specified, verify that *something* was specified,
5017 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005018 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5019 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005020 // Completely missing, emit error.
5021 Diag(DSStart, diag::err_missing_param);
5022 } else {
5023 // Otherwise, we have something. Add it and let semantic analysis try
5024 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005025
Chris Lattnerf97409f2008-04-06 06:57:35 +00005026 // Inform the actions module about the parameter declarator, so it gets
5027 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005028 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005029
5030 // Parse the default argument, if any. We parse the default
5031 // arguments in all dialects; the semantic analysis in
5032 // ActOnParamDefaultArgument will reject the default argument in
5033 // C.
5034 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005035 SourceLocation EqualLoc = Tok.getLocation();
5036
Chris Lattner04421082008-04-08 04:40:51 +00005037 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005038 if (D.getContext() == Declarator::MemberContext) {
5039 // If we're inside a class definition, cache the tokens
5040 // corresponding to the default argument. We'll actually parse
5041 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005042 // FIXME: Can we use a smart pointer for Toks?
5043 DefArgToks = new CachedTokens;
5044
Mike Stump1eb44332009-09-09 15:08:12 +00005045 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005046 /*StopAtSemi=*/true,
5047 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005048 delete DefArgToks;
5049 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005050 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005051 } else {
5052 // Mark the end of the default argument so that we know when to
5053 // stop when we parse it later on.
5054 Token DefArgEnd;
5055 DefArgEnd.startToken();
5056 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5057 DefArgEnd.setLocation(Tok.getLocation());
5058 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005059 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005060 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005061 }
Chris Lattner04421082008-04-08 04:40:51 +00005062 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005063 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005064 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005065
Chad Rosier8decdee2012-06-26 22:30:43 +00005066 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005067 // used.
5068 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005069 Sema::PotentiallyEvaluatedIfUsed,
5070 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005071
Sebastian Redl84407ba2012-03-14 15:54:00 +00005072 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005073 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005074 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005075 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005076 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005077 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005078 if (DefArgResult.isInvalid()) {
5079 Actions.ActOnParamDefaultArgumentError(Param);
5080 SkipUntil(tok::comma, tok::r_paren, true, true);
5081 } else {
5082 // Inform the actions module about the default argument
5083 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005084 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005085 }
Chris Lattner04421082008-04-08 04:40:51 +00005086 }
5087 }
Mike Stump1eb44332009-09-09 15:08:12 +00005088
5089 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5090 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005091 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005092 }
5093
5094 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005095 if (Tok.isNot(tok::comma)) {
5096 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005097 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005098
David Blaikie4e4d0842012-03-11 07:00:24 +00005099 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005100 // We have ellipsis without a preceding ',', which is ill-formed
5101 // in C. Complain and provide the fix.
5102 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005103 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005104 }
5105 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005106
Douglas Gregored5d6512009-09-22 21:41:40 +00005107 break;
5108 }
Mike Stump1eb44332009-09-09 15:08:12 +00005109
Chris Lattnerf97409f2008-04-06 06:57:35 +00005110 // Consume the comma.
5111 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005112 }
Mike Stump1eb44332009-09-09 15:08:12 +00005113
Chris Lattner66d28652008-04-06 06:34:08 +00005114}
Chris Lattneref4715c2008-04-06 05:45:57 +00005115
Reid Spencer5f016e22007-07-11 17:01:13 +00005116/// [C90] direct-declarator '[' constant-expression[opt] ']'
5117/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5118/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5119/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5120/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005121/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5122/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005123void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005124 if (CheckProhibitedCXX11Attribute())
5125 return;
5126
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005127 BalancedDelimiterTracker T(*this, tok::l_square);
5128 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005129
Chris Lattner378c7e42008-12-18 07:27:21 +00005130 // C array syntax has many features, but by-far the most common is [] and [4].
5131 // This code does a fast path to handle some of the most obvious cases.
5132 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005133 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005134 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005135 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005136
Chris Lattner378c7e42008-12-18 07:27:21 +00005137 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005138 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005139 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005140 T.getOpenLocation(),
5141 T.getCloseLocation()),
5142 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005143 return;
5144 } else if (Tok.getKind() == tok::numeric_constant &&
5145 GetLookAheadToken(1).is(tok::r_square)) {
5146 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005147 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005148 ConsumeToken();
5149
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005150 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005151 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005152 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005153
Chris Lattner378c7e42008-12-18 07:27:21 +00005154 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005155 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005156 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005157 T.getOpenLocation(),
5158 T.getCloseLocation()),
5159 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005160 return;
5161 }
Mike Stump1eb44332009-09-09 15:08:12 +00005162
Reid Spencer5f016e22007-07-11 17:01:13 +00005163 // If valid, this location is the position where we read the 'static' keyword.
5164 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005165 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005166 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005167
Reid Spencer5f016e22007-07-11 17:01:13 +00005168 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005169 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005170 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005171 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005172
Reid Spencer5f016e22007-07-11 17:01:13 +00005173 // If we haven't already read 'static', check to see if there is one after the
5174 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005175 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005176 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005177
Reid Spencer5f016e22007-07-11 17:01:13 +00005178 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5179 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005180 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005181
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005182 // Handle the case where we have '[*]' as the array size. However, a leading
5183 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005184 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005185 // infrequent, use of lookahead is not costly here.
5186 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005187 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005188
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005189 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005190 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005191 StaticLoc = SourceLocation(); // Drop the static.
5192 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005193 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005194 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005195 // Note, in C89, this production uses the constant-expr production instead
5196 // of assignment-expr. The only difference is that assignment-expr allows
5197 // things like '=' and '*='. Sema rejects these in C89 mode because they
5198 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005199
Douglas Gregore0762c92009-06-19 23:52:42 +00005200 // Parse the constant-expression or assignment-expression now (depending
5201 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005202 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005203 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005204 } else {
5205 EnterExpressionEvaluationContext Unevaluated(Actions,
5206 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005207 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005208 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005209 }
Mike Stump1eb44332009-09-09 15:08:12 +00005210
Reid Spencer5f016e22007-07-11 17:01:13 +00005211 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005212 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005213 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005214 // If the expression was invalid, skip it.
5215 SkipUntil(tok::r_square);
5216 return;
5217 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005218
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005219 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005220
John McCall0b7e6782011-03-24 11:26:52 +00005221 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005222 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005223
Chris Lattner378c7e42008-12-18 07:27:21 +00005224 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005225 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005226 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005227 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005228 T.getOpenLocation(),
5229 T.getCloseLocation()),
5230 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005231}
5232
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005233/// [GNU] typeof-specifier:
5234/// typeof ( expressions )
5235/// typeof ( type-name )
5236/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005237///
5238void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005239 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005240 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005241 SourceLocation StartLoc = ConsumeToken();
5242
John McCallcfb708c2010-01-13 20:03:27 +00005243 const bool hasParens = Tok.is(tok::l_paren);
5244
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005245 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5246 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005247
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005248 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005249 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005250 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005251 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5252 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005253 if (hasParens)
5254 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005255
5256 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005257 // FIXME: Not accurate, the range gets one token more than it should.
5258 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005259 else
5260 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005261
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005262 if (isCastExpr) {
5263 if (!CastTy) {
5264 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005265 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005266 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005267
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005268 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005269 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005270 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5271 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005272 DiagID, CastTy))
5273 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005274 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005275 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005276
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005277 // If we get here, the operand to the typeof was an expresion.
5278 if (Operand.isInvalid()) {
5279 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005280 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005281 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005282
Eli Friedman71b8fb52012-01-21 01:01:51 +00005283 // We might need to transform the operand if it is potentially evaluated.
5284 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5285 if (Operand.isInvalid()) {
5286 DS.SetTypeSpecError();
5287 return;
5288 }
5289
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005290 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005291 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005292 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5293 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005294 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005295 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005296}
Chris Lattner1b492422010-02-28 18:33:55 +00005297
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005298/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005299/// _Atomic ( type-name )
5300///
5301void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5302 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5303
5304 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005305 BalancedDelimiterTracker T(*this, tok::l_paren);
5306 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005307 SkipUntil(tok::r_paren);
5308 return;
5309 }
5310
5311 TypeResult Result = ParseTypeName();
5312 if (Result.isInvalid()) {
5313 SkipUntil(tok::r_paren);
5314 return;
5315 }
5316
5317 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005318 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005319
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005320 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005321 return;
5322
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005323 DS.setTypeofParensRange(T.getRange());
5324 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005325
5326 const char *PrevSpec = 0;
5327 unsigned DiagID;
5328 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5329 DiagID, Result.release()))
5330 Diag(StartLoc, DiagID) << PrevSpec;
5331}
5332
Chris Lattner1b492422010-02-28 18:33:55 +00005333
5334/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5335/// from TryAltiVecVectorToken.
5336bool Parser::TryAltiVecVectorTokenOutOfLine() {
5337 Token Next = NextToken();
5338 switch (Next.getKind()) {
5339 default: return false;
5340 case tok::kw_short:
5341 case tok::kw_long:
5342 case tok::kw_signed:
5343 case tok::kw_unsigned:
5344 case tok::kw_void:
5345 case tok::kw_char:
5346 case tok::kw_int:
5347 case tok::kw_float:
5348 case tok::kw_double:
5349 case tok::kw_bool:
5350 case tok::kw___pixel:
5351 Tok.setKind(tok::kw___vector);
5352 return true;
5353 case tok::identifier:
5354 if (Next.getIdentifierInfo() == Ident_pixel) {
5355 Tok.setKind(tok::kw___vector);
5356 return true;
5357 }
5358 return false;
5359 }
5360}
5361
5362bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5363 const char *&PrevSpec, unsigned &DiagID,
5364 bool &isInvalid) {
5365 if (Tok.getIdentifierInfo() == Ident_vector) {
5366 Token Next = NextToken();
5367 switch (Next.getKind()) {
5368 case tok::kw_short:
5369 case tok::kw_long:
5370 case tok::kw_signed:
5371 case tok::kw_unsigned:
5372 case tok::kw_void:
5373 case tok::kw_char:
5374 case tok::kw_int:
5375 case tok::kw_float:
5376 case tok::kw_double:
5377 case tok::kw_bool:
5378 case tok::kw___pixel:
5379 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5380 return true;
5381 case tok::identifier:
5382 if (Next.getIdentifierInfo() == Ident_pixel) {
5383 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5384 return true;
5385 }
5386 break;
5387 default:
5388 break;
5389 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005390 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005391 DS.isTypeAltiVecVector()) {
5392 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5393 return true;
5394 }
5395 return false;
5396}