blob: cb013e77f139534369524fd73193620cd7c2a21b [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"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
33/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000040 AccessSpecifier AS,
41 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000042 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000043 if (DSC == DSC_normal)
44 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000047 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000048 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000049 if (OwnedType)
50 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000053 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000055 if (Range)
56 *Range = DeclaratorInfo.getSourceRange();
57
Chris Lattnereaaebc72009-04-25 08:06:05 +000058 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000059 return true;
60
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000064
65/// isAttributeLateParsed - Return true if the attribute has arguments that
66/// require late parsing.
67static bool isAttributeLateParsed(const IdentifierInfo &II) {
68 return llvm::StringSwitch<bool>(II.getName())
69#include "clang/Parse/AttrLateParsed.inc"
70 .Default(false);
71}
72
Sean Huntbbd37c62009-11-21 08:43:09 +000073/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000074///
75/// [GNU] attributes:
76/// attribute
77/// attributes attribute
78///
79/// [GNU] attribute:
80/// '__attribute__' '(' '(' attribute-list ')' ')'
81///
82/// [GNU] attribute-list:
83/// attrib
84/// attribute_list ',' attrib
85///
86/// [GNU] attrib:
87/// empty
88/// attrib-name
89/// attrib-name '(' identifier ')'
90/// attrib-name '(' identifier ',' nonempty-expr-list ')'
91/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
92///
93/// [GNU] attrib-name:
94/// identifier
95/// typespec
96/// typequal
97/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000098///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// token lookahead. Comment from gcc: "If they start with an identifier
101/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// start with that identifier; otherwise they are an expression list."
103///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000104/// GCC does not require the ',' between attribs in an attribute-list.
105///
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// At the moment, I am not doing 2 token lookahead. I am also unaware of
107/// any attributes that don't work (based on my limited testing). Most
108/// attributes are very simple in practice. Until we find a bug, I don't see
109/// a pressing need to implement the 2 token lookahead.
110
John McCall7f040a92010-12-24 02:08:15 +0000111void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000112 SourceLocation *endLoc,
113 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000114 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ConsumeToken();
118 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
119 "attribute")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000125 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000128 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
129 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000130 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
132 ConsumeToken();
133 continue;
134 }
135 // we have an identifier or declaration specifier (const, int, etc.)
136 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
137 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000139 if (Tok.is(tok::l_paren)) {
140 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 LateParsedAttribute *LA =
143 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
144 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000145
Bill Wendlingad017fa2012-12-20 19:22:21 +0000146 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000147 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000148 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000149 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000151 // consume everything up to and including the matching right parens
152 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 Token Eof;
155 Eof.startToken();
156 Eof.setLocation(Tok.getLocation());
157 LA->Toks.push_back(Eof);
158 } else {
Michael Han6880f492012-10-03 01:56:22 +0000159 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000160 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
162 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000164 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
166 }
167 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000169 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
171 SkipUntil(tok::r_paren, false);
172 }
John McCall7f040a92010-12-24 02:08:15 +0000173 if (endLoc)
174 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000178
Michael Han6880f492012-10-03 01:56:22 +0000179/// Parse the arguments to a parameterized GNU attribute or
180/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000181void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
182 SourceLocation AttrNameLoc,
183 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000184 SourceLocation *EndLoc,
185 IdentifierInfo *ScopeName,
186 SourceLocation ScopeLoc,
187 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000188
189 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
190
191 // Availability attributes have their own grammar.
192 if (AttrName->isStr("availability")) {
193 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196 // Thread safety attributes fit into the FIXME case above, so we
197 // just parse the arguments as a list of expressions
198 if (IsThreadSafetyAttribute(AttrName->getName())) {
199 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
200 return;
201 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000202 // Type safety attributes have their own grammar.
203 if (AttrName->isStr("type_tag_for_datatype")) {
204 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
205 return;
206 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000207
208 ConsumeParen(); // ignore the left paren loc for now
209
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000210 IdentifierInfo *ParmName = 0;
211 SourceLocation ParmLoc;
212 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000213
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000214 switch (Tok.getKind()) {
215 case tok::kw_char:
216 case tok::kw_wchar_t:
217 case tok::kw_char16_t:
218 case tok::kw_char32_t:
219 case tok::kw_bool:
220 case tok::kw_short:
221 case tok::kw_int:
222 case tok::kw_long:
223 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000224 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000225 case tok::kw_signed:
226 case tok::kw_unsigned:
227 case tok::kw_float:
228 case tok::kw_double:
229 case tok::kw_void:
230 case tok::kw_typeof:
231 // __attribute__(( vec_type_hint(char) ))
232 // FIXME: Don't just discard the builtin type token.
233 ConsumeToken();
234 BuiltinType = true;
235 break;
236
237 case tok::identifier:
238 ParmName = Tok.getIdentifierInfo();
239 ParmLoc = ConsumeToken();
240 break;
241
242 default:
243 break;
244 }
245
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000246 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247
248 if (!BuiltinType &&
249 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
250 // Eat the comma.
251 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000252 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000253
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000254 // Parse the non-empty comma-separated list of expressions.
255 while (1) {
256 ExprResult ArgExpr(ParseAssignmentExpression());
257 if (ArgExpr.isInvalid()) {
258 SkipUntil(tok::r_paren);
259 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000260 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000261 ArgExprs.push_back(ArgExpr.release());
262 if (Tok.isNot(tok::comma))
263 break;
264 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000265 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000266 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000267 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
268 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
269 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000270 while (Tok.is(tok::identifier)) {
271 ConsumeToken();
272 if (Tok.is(tok::greater))
273 break;
274 if (Tok.is(tok::comma)) {
275 ConsumeToken();
276 continue;
277 }
278 }
279 if (Tok.isNot(tok::greater))
280 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000281 SkipUntil(tok::r_paren, false, true); // skip until ')'
282 }
283 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000284
285 SourceLocation RParen = Tok.getLocation();
286 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000287 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000288 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000289 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000290 ScopeName, ScopeLoc, ParmName, ParmLoc,
291 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000292 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000293 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000294 }
295}
296
Chad Rosier8decdee2012-06-26 22:30:43 +0000297/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000298/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000299void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000300 SourceLocation AttrNameLoc,
301 ParsedAttributes &Attrs)
302{
303 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000304 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000305 AttrName->getNameStart(), tok::r_paren))
306 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000307
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000308 ExprResult ArgExpr(ParseConstantExpression());
309 if (ArgExpr.isInvalid()) {
310 T.skipToEnd();
311 return;
312 }
313 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000314 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000315 &ExprList, 1, AttributeList::AS_Declspec);
316
317 T.consumeClose();
318}
319
Chad Rosier8decdee2012-06-26 22:30:43 +0000320/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000321/// arguments.
322bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
323 return llvm::StringSwitch<bool>(Ident->getName())
324 .Case("dllimport", true)
325 .Case("dllexport", true)
326 .Case("noreturn", true)
327 .Case("nothrow", true)
328 .Case("noinline", true)
329 .Case("naked", true)
330 .Case("appdomain", true)
331 .Case("process", true)
332 .Case("jitintrinsic", true)
333 .Case("noalias", true)
334 .Case("restrict", true)
335 .Case("novtable", true)
336 .Case("selectany", true)
337 .Case("thread", true)
338 .Default(false);
339}
340
Chad Rosier8decdee2012-06-26 22:30:43 +0000341/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000342/// parameters). Will return false if we properly handled the declspec, or
343/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000344void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345 SourceLocation Loc,
346 ParsedAttributes &Attrs) {
347 // Try to handle the easy case first -- these declspecs all take a single
348 // parameter as their argument.
349 if (llvm::StringSwitch<bool>(Ident->getName())
350 .Case("uuid", true)
351 .Case("align", true)
352 .Case("allocate", true)
353 .Default(false)) {
354 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
355 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000356 // The deprecated declspec has an optional single argument, so we will
357 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000358 // not.
359 if (Tok.getKind() == tok::l_paren)
360 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
361 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000362 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000363 AttributeList::AS_Declspec);
364 } else if (Ident->getName() == "property") {
365 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000366 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000367 // must be named get or put.
368 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000369 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000370 // property expression.
371 //
372 // FIXME: we should deal with __declspec(property) at some point because it
373 // is used in the platform SDK headers for the Parallel Patterns Library
374 // and ATL.
375 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000376 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000377 Ident->getNameStart(), tok::r_paren))
378 return;
379 T.skipToEnd();
380 } else {
381 // We don't recognize this as a valid declspec, but instead of creating the
382 // attribute and allowing sema to warn about it, we will warn here instead.
383 // This is because some attributes have multiple spellings, but we need to
384 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000385 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000386 // both locations.
387 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
388
389 // If there's an open paren, we should eat the open and close parens under
390 // the assumption that this unknown declspec has parameters.
391 BalancedDelimiterTracker T(*this, tok::l_paren);
392 if (!T.consumeOpen())
393 T.skipToEnd();
394 }
395}
396
Eli Friedmana23b4852009-06-08 07:21:15 +0000397/// [MS] decl-specifier:
398/// __declspec ( extended-decl-modifier-seq )
399///
400/// [MS] extended-decl-modifier-seq:
401/// extended-decl-modifier[opt]
402/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000403void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000404 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000405
Steve Narofff59e17e2008-12-24 20:59:21 +0000406 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000407 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000408 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000409 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000410 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000411
Chad Rosier8decdee2012-06-26 22:30:43 +0000412 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000413 // you can specify multiple attributes per declspec.
414 while (Tok.getKind() != tok::r_paren) {
415 // We expect either a well-known identifier or a generic string. Anything
416 // else is a malformed declspec.
417 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000418 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000419 Tok.getKind() != tok::kw_restrict) {
420 Diag(Tok, diag::err_ms_declspec_type);
421 T.skipToEnd();
422 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000423 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000424
425 IdentifierInfo *AttrName;
426 SourceLocation AttrNameLoc;
427 if (IsString) {
428 SmallString<8> StrBuffer;
429 bool Invalid = false;
430 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
431 if (Invalid) {
432 T.skipToEnd();
433 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000434 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000435 AttrName = PP.getIdentifierInfo(Str);
436 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000437 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 AttrName = Tok.getIdentifierInfo();
439 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000440 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000441
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000443 // If we have a generic string, we will allow it because there is no
444 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000445 // (for instance, SAL declspecs in older versions of MSVC).
446 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000447 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000449 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000450 0, 0, AttributeList::AS_Declspec);
451 else
452 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000453 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000454 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000455}
456
John McCall7f040a92010-12-24 02:08:15 +0000457void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000458 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000459 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000460 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000461 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000462 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000463 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
464 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000465 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000466 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000467 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000468}
469
John McCall7f040a92010-12-24 02:08:15 +0000470void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000471 // Treat these like attributes
472 while (Tok.is(tok::kw___pascal)) {
473 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
474 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000475 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000476 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000477 }
John McCall7f040a92010-12-24 02:08:15 +0000478}
479
Peter Collingbournef315fa82011-02-14 01:42:53 +0000480void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
481 // Treat these like attributes
482 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000483 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000484 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000485 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
486 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000487 }
488}
489
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000490void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000491 // FIXME: The mapping from attribute spelling to semantics should be
492 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000493 SourceLocation Loc = Tok.getLocation();
494 switch(Tok.getKind()) {
495 // OpenCL qualifiers:
496 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000497 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000498 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000499 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000500 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000501 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000502
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000503 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000504 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000505 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000506 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000507 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000508
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000509 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000510 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000511 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000512 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000513 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000514
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000515 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000516 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000517 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000518 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000519 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000520
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000521 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000522 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000523 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000524 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000525 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000526
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000527 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000528 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000529 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000530 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000531 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000532
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000533 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000534 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000535 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000536 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000537 break;
538 default: break;
539 }
540}
541
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000542/// \brief Parse a version number.
543///
544/// version:
545/// simple-integer
546/// simple-integer ',' simple-integer
547/// simple-integer ',' simple-integer ',' simple-integer
548VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
549 Range = Tok.getLocation();
550
551 if (!Tok.is(tok::numeric_constant)) {
552 Diag(Tok, diag::err_expected_version);
553 SkipUntil(tok::comma, tok::r_paren, true, true, true);
554 return VersionTuple();
555 }
556
557 // Parse the major (and possibly minor and subminor) versions, which
558 // are stored in the numeric constant. We utilize a quirk of the
559 // lexer, which is that it handles something like 1.2.3 as a single
560 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000561 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562 Buffer.resize(Tok.getLength()+1);
563 const char *ThisTokBegin = &Buffer[0];
564
565 // Get the spelling of the token, which eliminates trigraphs, etc.
566 bool Invalid = false;
567 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
568 if (Invalid)
569 return VersionTuple();
570
571 // Parse the major version.
572 unsigned AfterMajor = 0;
573 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000574 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000575 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
576 ++AfterMajor;
577 }
578
579 if (AfterMajor == 0) {
580 Diag(Tok, diag::err_expected_version);
581 SkipUntil(tok::comma, tok::r_paren, true, true, true);
582 return VersionTuple();
583 }
584
585 if (AfterMajor == ActualLength) {
586 ConsumeToken();
587
588 // We only had a single version component.
589 if (Major == 0) {
590 Diag(Tok, diag::err_zero_version);
591 return VersionTuple();
592 }
593
594 return VersionTuple(Major);
595 }
596
597 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
598 Diag(Tok, diag::err_expected_version);
599 SkipUntil(tok::comma, tok::r_paren, true, true, true);
600 return VersionTuple();
601 }
602
603 // Parse the minor version.
604 unsigned AfterMinor = AfterMajor + 1;
605 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000606 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000607 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
608 ++AfterMinor;
609 }
610
611 if (AfterMinor == ActualLength) {
612 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000613
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000614 // We had major.minor.
615 if (Major == 0 && Minor == 0) {
616 Diag(Tok, diag::err_zero_version);
617 return VersionTuple();
618 }
619
Chad Rosier8decdee2012-06-26 22:30:43 +0000620 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000621 }
622
623 // If what follows is not a '.', we have a problem.
624 if (ThisTokBegin[AfterMinor] != '.') {
625 Diag(Tok, diag::err_expected_version);
626 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000627 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628 }
629
630 // Parse the subminor version.
631 unsigned AfterSubminor = AfterMinor + 1;
632 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000633 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000634 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
635 ++AfterSubminor;
636 }
637
638 if (AfterSubminor != ActualLength) {
639 Diag(Tok, diag::err_expected_version);
640 SkipUntil(tok::comma, tok::r_paren, true, true, true);
641 return VersionTuple();
642 }
643 ConsumeToken();
644 return VersionTuple(Major, Minor, Subminor);
645}
646
647/// \brief Parse the contents of the "availability" attribute.
648///
649/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000650/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000651///
652/// platform:
653/// identifier
654///
655/// version-arg-list:
656/// version-arg
657/// version-arg ',' version-arg-list
658///
659/// version-arg:
660/// 'introduced' '=' version
661/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000662/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000663/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000664/// opt-message:
665/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000666void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
667 SourceLocation AvailabilityLoc,
668 ParsedAttributes &attrs,
669 SourceLocation *endLoc) {
670 SourceLocation PlatformLoc;
671 IdentifierInfo *Platform = 0;
672
673 enum { Introduced, Deprecated, Obsoleted, Unknown };
674 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000675 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676
677 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000678 BalancedDelimiterTracker T(*this, tok::l_paren);
679 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000680 Diag(Tok, diag::err_expected_lparen);
681 return;
682 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000683
684 // Parse the platform name,
685 if (Tok.isNot(tok::identifier)) {
686 Diag(Tok, diag::err_availability_expected_platform);
687 SkipUntil(tok::r_paren);
688 return;
689 }
690 Platform = Tok.getIdentifierInfo();
691 PlatformLoc = ConsumeToken();
692
693 // Parse the ',' following the platform name.
694 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
695 return;
696
697 // If we haven't grabbed the pointers for the identifiers
698 // "introduced", "deprecated", and "obsoleted", do so now.
699 if (!Ident_introduced) {
700 Ident_introduced = PP.getIdentifierInfo("introduced");
701 Ident_deprecated = PP.getIdentifierInfo("deprecated");
702 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000703 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705 }
706
707 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000708 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000709 do {
710 if (Tok.isNot(tok::identifier)) {
711 Diag(Tok, diag::err_availability_expected_change);
712 SkipUntil(tok::r_paren);
713 return;
714 }
715 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
716 SourceLocation KeywordLoc = ConsumeToken();
717
Douglas Gregorb53e4172011-03-26 03:35:55 +0000718 if (Keyword == Ident_unavailable) {
719 if (UnavailableLoc.isValid()) {
720 Diag(KeywordLoc, diag::err_availability_redundant)
721 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000722 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000723 UnavailableLoc = KeywordLoc;
724
725 if (Tok.isNot(tok::comma))
726 break;
727
728 ConsumeToken();
729 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000730 }
731
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000732 if (Tok.isNot(tok::equal)) {
733 Diag(Tok, diag::err_expected_equal_after)
734 << Keyword;
735 SkipUntil(tok::r_paren);
736 return;
737 }
738 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000739 if (Keyword == Ident_message) {
740 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000741 Diag(Tok, diag::err_expected_string_literal)
742 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000743 SkipUntil(tok::r_paren);
744 return;
745 }
746 MessageExpr = ParseStringLiteralExpression();
747 break;
748 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000749
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 SourceRange VersionRange;
751 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000752
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000753 if (Version.empty()) {
754 SkipUntil(tok::r_paren);
755 return;
756 }
757
758 unsigned Index;
759 if (Keyword == Ident_introduced)
760 Index = Introduced;
761 else if (Keyword == Ident_deprecated)
762 Index = Deprecated;
763 else if (Keyword == Ident_obsoleted)
764 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000765 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000766 Index = Unknown;
767
768 if (Index < Unknown) {
769 if (!Changes[Index].KeywordLoc.isInvalid()) {
770 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000771 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000772 << SourceRange(Changes[Index].KeywordLoc,
773 Changes[Index].VersionRange.getEnd());
774 }
775
776 Changes[Index].KeywordLoc = KeywordLoc;
777 Changes[Index].Version = Version;
778 Changes[Index].VersionRange = VersionRange;
779 } else {
780 Diag(KeywordLoc, diag::err_availability_unknown_change)
781 << Keyword << VersionRange;
782 }
783
784 if (Tok.isNot(tok::comma))
785 break;
786
787 ConsumeToken();
788 } while (true);
789
790 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000791 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000792 return;
793
794 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000795 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000796
Douglas Gregorb53e4172011-03-26 03:35:55 +0000797 // The 'unavailable' availability cannot be combined with any other
798 // availability changes. Make sure that hasn't happened.
799 if (UnavailableLoc.isValid()) {
800 bool Complained = false;
801 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
802 if (Changes[Index].KeywordLoc.isValid()) {
803 if (!Complained) {
804 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
805 << SourceRange(Changes[Index].KeywordLoc,
806 Changes[Index].VersionRange.getEnd());
807 Complained = true;
808 }
809
810 // Clear out the availability.
811 Changes[Index] = AvailabilityChange();
812 }
813 }
814 }
815
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000816 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000817 attrs.addNew(&Availability,
818 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000819 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000820 Platform, PlatformLoc,
821 Changes[Introduced],
822 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000823 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000824 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000825 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000826}
827
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000828
Bill Wendlingad017fa2012-12-20 19:22:21 +0000829// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000830// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
831
832void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
833
834void Parser::LateParsedClass::ParseLexedAttributes() {
835 Self->ParseLexedAttributes(*Class);
836}
837
838void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000839 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840}
841
842/// Wrapper class which calls ParseLexedAttribute, after setting up the
843/// scope appropriately.
844void Parser::ParseLexedAttributes(ParsingClass &Class) {
845 // Deal with templates
846 // FIXME: Test cases to make sure this does the right thing for templates.
847 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
848 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
849 HasTemplateScope);
850 if (HasTemplateScope)
851 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
852
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000853 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000854 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000855 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000856 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
857 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
858
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000859 // Enter the scope of nested classes
860 if (!AlreadyHasClassScope)
861 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
862 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000863 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000864 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
865 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
866 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000867 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000868
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000869 if (!AlreadyHasClassScope)
870 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
871 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000872}
873
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000874
875/// \brief Parse all attributes in LAs, and attach them to Decl D.
876void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
877 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000878 assert(LAs.parseSoon() &&
879 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000880 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000881 if (D)
882 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000884 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000885 }
886 LAs.clear();
887}
888
889
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000890/// \brief Finish parsing an attribute for which parsing was delayed.
891/// This will be called at the end of parsing a class declaration
892/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000893/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000895void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
896 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897 // Save the current token position.
898 SourceLocation OrigLoc = Tok.getLocation();
899
900 // Append the current token at the end of the new token stream so that it
901 // doesn't get lost.
902 LA.Toks.push_back(Tok);
903 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
904 // Consume the previously pushed token.
905 ConsumeAnyToken();
906
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000907 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000908 // FIXME: Do not warn on C++11 attributes, once we start supporting
909 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000910 Diag(Tok, diag::warn_attribute_on_function_definition)
911 << LA.AttrName.getName();
912 }
913
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000914 ParsedAttributes Attrs(AttrFactory);
915 SourceLocation endLoc;
916
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000917 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000918 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000919 NamedDecl *ND = dyn_cast<NamedDecl>(D);
920 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000921
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 // Allow 'this' within late-parsed attributes.
923 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
924 /*TypeQuals=*/0,
925 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000926
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000927 if (LA.Decls.size() == 1) {
928 // If the Decl is templatized, add template parameters to scope.
929 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
930 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
931 if (HasTemplateScope)
932 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000933
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000934 // If the Decl is on a function, add function parameters to the scope.
935 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
936 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
937 if (HasFunScope)
938 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000939
Michael Han6880f492012-10-03 01:56:22 +0000940 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000941 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000942
943 if (HasFunScope) {
944 Actions.ActOnExitFunctionContext();
945 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
946 }
947 if (HasTemplateScope) {
948 TempScope.Exit();
949 }
950 } else {
951 // If there are multiple decls, then the decl cannot be within the
952 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000953 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000954 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000955 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000956 } else {
957 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000958 }
959
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000960 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
961 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
962 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000963
964 if (Tok.getLocation() != OrigLoc) {
965 // Due to a parsing error, we either went over the cached tokens or
966 // there are still cached tokens left, so we skip the leftover tokens.
967 // Since this is an uncommon situation that should be avoided, use the
968 // expensive isBeforeInTranslationUnit call.
969 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
970 OrigLoc))
971 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000972 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000973 }
974}
975
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976/// \brief Wrapper around a case statement checking if AttrName is
977/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000978bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000979 return llvm::StringSwitch<bool>(AttrName)
980 .Case("guarded_by", true)
981 .Case("guarded_var", true)
982 .Case("pt_guarded_by", true)
983 .Case("pt_guarded_var", true)
984 .Case("lockable", true)
985 .Case("scoped_lockable", true)
986 .Case("no_thread_safety_analysis", true)
987 .Case("acquired_after", true)
988 .Case("acquired_before", true)
989 .Case("exclusive_lock_function", true)
990 .Case("shared_lock_function", true)
991 .Case("exclusive_trylock_function", true)
992 .Case("shared_trylock_function", true)
993 .Case("unlock_function", true)
994 .Case("lock_returned", true)
995 .Case("locks_excluded", true)
996 .Case("exclusive_locks_required", true)
997 .Case("shared_locks_required", true)
998 .Default(false);
999}
1000
1001/// \brief Parse the contents of thread safety attributes. These
1002/// should always be parsed as an expression list.
1003///
1004/// We need to special case the parsing due to the fact that if the first token
1005/// of the first argument is an identifier, the main parse loop will store
1006/// that token as a "parameter" and the rest of
1007/// the arguments will be added to a list of "arguments". However,
1008/// subsequent tokens in the first argument are lost. We instead parse each
1009/// argument as an expression and add all arguments to the list of "arguments".
1010/// In future, we will take advantage of this special case to also
1011/// deal with some argument scoping issues here (for example, referring to a
1012/// function parameter in the attribute on that function).
1013void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1014 SourceLocation AttrNameLoc,
1015 ParsedAttributes &Attrs,
1016 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001017 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001019 BalancedDelimiterTracker T(*this, tok::l_paren);
1020 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001021
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001022 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001024
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001025 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001026 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001027 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 ExprResult ArgExpr(ParseAssignmentExpression());
1029 if (ArgExpr.isInvalid()) {
1030 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001031 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001032 break;
1033 } else {
1034 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001035 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001036 if (Tok.isNot(tok::comma))
1037 break;
1038 ConsumeToken(); // Eat the comma, move to the next argument
1039 }
1040 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001041 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001042 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001043 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001044 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001045 if (EndLoc)
1046 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001047}
1048
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001049void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1050 SourceLocation AttrNameLoc,
1051 ParsedAttributes &Attrs,
1052 SourceLocation *EndLoc) {
1053 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1054
1055 BalancedDelimiterTracker T(*this, tok::l_paren);
1056 T.consumeOpen();
1057
1058 if (Tok.isNot(tok::identifier)) {
1059 Diag(Tok, diag::err_expected_ident);
1060 T.skipToEnd();
1061 return;
1062 }
1063 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1064 SourceLocation ArgumentKindLoc = ConsumeToken();
1065
1066 if (Tok.isNot(tok::comma)) {
1067 Diag(Tok, diag::err_expected_comma);
1068 T.skipToEnd();
1069 return;
1070 }
1071 ConsumeToken();
1072
1073 SourceRange MatchingCTypeRange;
1074 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1075 if (MatchingCType.isInvalid()) {
1076 T.skipToEnd();
1077 return;
1078 }
1079
1080 bool LayoutCompatible = false;
1081 bool MustBeNull = false;
1082 while (Tok.is(tok::comma)) {
1083 ConsumeToken();
1084 if (Tok.isNot(tok::identifier)) {
1085 Diag(Tok, diag::err_expected_ident);
1086 T.skipToEnd();
1087 return;
1088 }
1089 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1090 if (Flag->isStr("layout_compatible"))
1091 LayoutCompatible = true;
1092 else if (Flag->isStr("must_be_null"))
1093 MustBeNull = true;
1094 else {
1095 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1096 T.skipToEnd();
1097 return;
1098 }
1099 ConsumeToken(); // consume flag
1100 }
1101
1102 if (!T.consumeClose()) {
1103 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1104 ArgumentKind, ArgumentKindLoc,
1105 MatchingCType.release(), LayoutCompatible,
1106 MustBeNull, AttributeList::AS_GNU);
1107 }
1108
1109 if (EndLoc)
1110 *EndLoc = T.getCloseLocation();
1111}
1112
Richard Smith6ee326a2012-04-10 01:32:12 +00001113/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1114/// of a C++11 attribute-specifier in a location where an attribute is not
1115/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1116/// situation.
1117///
1118/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1119/// this doesn't appear to actually be an attribute-specifier, and the caller
1120/// should try to parse it.
1121bool Parser::DiagnoseProhibitedCXX11Attribute() {
1122 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1123
1124 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1125 case CAK_NotAttributeSpecifier:
1126 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1127 return false;
1128
1129 case CAK_InvalidAttributeSpecifier:
1130 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1131 return false;
1132
1133 case CAK_AttributeSpecifier:
1134 // Parse and discard the attributes.
1135 SourceLocation BeginLoc = ConsumeBracket();
1136 ConsumeBracket();
1137 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1138 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1139 SourceLocation EndLoc = ConsumeBracket();
1140 Diag(BeginLoc, diag::err_attributes_not_allowed)
1141 << SourceRange(BeginLoc, EndLoc);
1142 return true;
1143 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001144 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001145}
1146
Richard Smith975d52c2013-02-20 01:17:14 +00001147/// \brief We have found the opening square brackets of a C++11
1148/// attribute-specifier in a location where an attribute is not permitted, but
1149/// we know where the attributes ought to be written. Parse them anyway, and
1150/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001151void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1152 SourceLocation CorrectLocation) {
1153 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1154 Tok.is(tok::kw_alignas));
1155
1156 // Consume the attributes.
1157 SourceLocation Loc = Tok.getLocation();
1158 ParseCXX11Attributes(Attrs);
1159 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1160
1161 Diag(Loc, diag::err_attributes_not_allowed)
1162 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1163 << FixItHint::CreateRemoval(AttrRange);
1164}
1165
John McCall7f040a92010-12-24 02:08:15 +00001166void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1167 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1168 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001169}
1170
Michael Hanf64231e2012-11-06 19:34:54 +00001171void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1172 AttributeList *AttrList = attrs.getList();
1173 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001174 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001175 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001176 << AttrList->getName();
1177 AttrList->setInvalid();
1178 }
1179 AttrList = AttrList->getNext();
1180 }
1181}
1182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183/// ParseDeclaration - Parse a full 'declaration', which consists of
1184/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001185/// 'Context' should be a Declarator::TheContext value. This returns the
1186/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001187///
1188/// declaration: [C99 6.7]
1189/// block-declaration ->
1190/// simple-declaration
1191/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001192/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001193/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001194/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001195/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001196/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001197/// others... [FIXME]
1198///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001199Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1200 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001201 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001202 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001203 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001204 // Must temporarily exit the objective-c container scope for
1205 // parsing c none objective-c decls.
1206 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001207
John McCalld226f652010-08-21 09:40:31 +00001208 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001209 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001210 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001211 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001212 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001213 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001214 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001215 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001216 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001217 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001218 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001219 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001220 SourceLocation InlineLoc = ConsumeToken();
1221 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1222 break;
1223 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001224 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001225 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001226 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001227 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001228 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001229 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001230 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001231 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001232 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001233 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001234 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001235 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001236 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001237 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001238 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001239 default:
John McCall7f040a92010-12-24 02:08:15 +00001240 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001241 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001242
Chris Lattner682bf922009-03-29 16:50:03 +00001243 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001244 // single decl, convert it now. Alias declarations can also declare a type;
1245 // include that too if it is present.
1246 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001247}
1248
1249/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1250/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001251/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1252/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001253///[C90/C++]init-declarator-list ';' [TODO]
1254/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001255///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001256/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001257/// attribute-specifier-seq[opt] type-specifier-seq declarator
1258///
Chris Lattnercd147752009-03-29 17:27:48 +00001259/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001260/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001261///
1262/// If FRI is non-null, we might be parsing a for-range-declaration instead
1263/// of a simple-declaration. If we find that we are, we also parse the
1264/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001265Parser::DeclGroupPtrTy
1266Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1267 SourceLocation &DeclEnd,
1268 ParsedAttributesWithRange &attrs,
1269 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001271 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001272 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001273
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001274 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001275 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001276
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1278 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001279 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001280 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001281 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001282 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001283 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001284 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001285 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001287
1288 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001289}
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Richard Smith0706df42011-10-19 21:33:05 +00001291/// Returns true if this might be the start of a declarator, or a common typo
1292/// for a declarator.
1293bool Parser::MightBeDeclarator(unsigned Context) {
1294 switch (Tok.getKind()) {
1295 case tok::annot_cxxscope:
1296 case tok::annot_template_id:
1297 case tok::caret:
1298 case tok::code_completion:
1299 case tok::coloncolon:
1300 case tok::ellipsis:
1301 case tok::kw___attribute:
1302 case tok::kw_operator:
1303 case tok::l_paren:
1304 case tok::star:
1305 return true;
1306
1307 case tok::amp:
1308 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001309 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001310
Richard Smith1c94c162012-01-09 22:31:44 +00001311 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001312 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001313 NextToken().is(tok::l_square);
1314
1315 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001316 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001317
Richard Smith0706df42011-10-19 21:33:05 +00001318 case tok::identifier:
1319 switch (NextToken().getKind()) {
1320 case tok::code_completion:
1321 case tok::coloncolon:
1322 case tok::comma:
1323 case tok::equal:
1324 case tok::equalequal: // Might be a typo for '='.
1325 case tok::kw_alignas:
1326 case tok::kw_asm:
1327 case tok::kw___attribute:
1328 case tok::l_brace:
1329 case tok::l_paren:
1330 case tok::l_square:
1331 case tok::less:
1332 case tok::r_brace:
1333 case tok::r_paren:
1334 case tok::r_square:
1335 case tok::semi:
1336 return true;
1337
1338 case tok::colon:
1339 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001340 // and in block scope it's probably a label. Inside a class definition,
1341 // this is a bit-field.
1342 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001343 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001344
1345 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001346 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001347
1348 default:
1349 return false;
1350 }
1351
1352 default:
1353 return false;
1354 }
1355}
1356
Richard Smith994d73f2012-04-11 20:59:20 +00001357/// Skip until we reach something which seems like a sensible place to pick
1358/// up parsing after a malformed declaration. This will sometimes stop sooner
1359/// than SkipUntil(tok::r_brace) would, but will never stop later.
1360void Parser::SkipMalformedDecl() {
1361 while (true) {
1362 switch (Tok.getKind()) {
1363 case tok::l_brace:
1364 // Skip until matching }, then stop. We've probably skipped over
1365 // a malformed class or function definition or similar.
1366 ConsumeBrace();
1367 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1368 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1369 // This declaration isn't over yet. Keep skipping.
1370 continue;
1371 }
1372 if (Tok.is(tok::semi))
1373 ConsumeToken();
1374 return;
1375
1376 case tok::l_square:
1377 ConsumeBracket();
1378 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1379 continue;
1380
1381 case tok::l_paren:
1382 ConsumeParen();
1383 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1384 continue;
1385
1386 case tok::r_brace:
1387 return;
1388
1389 case tok::semi:
1390 ConsumeToken();
1391 return;
1392
1393 case tok::kw_inline:
1394 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001395 // a good place to pick back up parsing, except in an Objective-C
1396 // @interface context.
1397 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1398 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001399 return;
1400 break;
1401
1402 case tok::kw_namespace:
1403 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001404 // place to pick back up parsing, except in an Objective-C
1405 // @interface context.
1406 if (Tok.isAtStartOfLine() &&
1407 (!ParsingInObjCContainer || CurParsedObjCImpl))
1408 return;
1409 break;
1410
1411 case tok::at:
1412 // @end is very much like } in Objective-C contexts.
1413 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1414 ParsingInObjCContainer)
1415 return;
1416 break;
1417
1418 case tok::minus:
1419 case tok::plus:
1420 // - and + probably start new method declarations in Objective-C contexts.
1421 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001422 return;
1423 break;
1424
1425 case tok::eof:
1426 return;
1427
1428 default:
1429 break;
1430 }
1431
1432 ConsumeAnyToken();
1433 }
1434}
1435
John McCalld8ac0572009-11-03 19:26:08 +00001436/// ParseDeclGroup - Having concluded that this is either a function
1437/// definition or a group of object declarations, actually parse the
1438/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001439Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1440 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001441 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001442 SourceLocation *DeclEnd,
1443 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001444 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001445 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001446 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001447
John McCalld8ac0572009-11-03 19:26:08 +00001448 // Bail out if the first declarator didn't seem well-formed.
1449 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001450 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001451 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001452 }
Mike Stump1eb44332009-09-09 15:08:12 +00001453
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001454 // Save late-parsed attributes for now; they need to be parsed in the
1455 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001456 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1457 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001458 if (D.isFunctionDeclarator())
1459 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1460
Chris Lattnerc82daef2010-07-11 22:24:20 +00001461 // Check to see if we have a function *definition* which must have a body.
1462 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1463 // Look at the next token to make sure that this isn't a function
1464 // declaration. We have to check this because __attribute__ might be the
1465 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001466 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001467
Chris Lattner004659a2010-07-11 22:42:07 +00001468 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001469 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1470 Diag(Tok, diag::err_function_declared_typedef);
1471
1472 // Recover by treating the 'typedef' as spurious.
1473 DS.ClearStorageClassSpecs();
1474 }
1475
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001476 Decl *TheDecl =
1477 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001478 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001479 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001480
Chris Lattner004659a2010-07-11 22:42:07 +00001481 if (isDeclarationSpecifier()) {
1482 // If there is an invalid declaration specifier right after the function
1483 // prototype, then we must be in a missing semicolon case where this isn't
1484 // actually a body. Just fall through into the code that handles it as a
1485 // prototype, and let the top-level code handle the erroneous declspec
1486 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001487 } else {
1488 Diag(Tok, diag::err_expected_fn_body);
1489 SkipUntil(tok::semi);
1490 return DeclGroupPtrTy();
1491 }
1492 }
1493
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001494 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001495 return DeclGroupPtrTy();
1496
1497 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1498 // must parse and analyze the for-range-initializer before the declaration is
1499 // analyzed.
1500 if (FRI && Tok.is(tok::colon)) {
1501 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001502 if (Tok.is(tok::l_brace))
1503 FRI->RangeExpr = ParseBraceInitializer();
1504 else
1505 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001506 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1507 Actions.ActOnCXXForRangeDecl(ThisDecl);
1508 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001509 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001510 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1511 }
1512
Chris Lattner5f9e2722011-07-23 10:55:15 +00001513 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001514 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001515 if (LateParsedAttrs.size() > 0)
1516 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001517 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001518 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001519 DeclsInGroup.push_back(FirstDecl);
1520
Richard Smith0706df42011-10-19 21:33:05 +00001521 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001522
John McCalld8ac0572009-11-03 19:26:08 +00001523 // If we don't have a comma, it is either the end of the list (a ';') or an
1524 // error, bail out.
1525 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001526 SourceLocation CommaLoc = ConsumeToken();
1527
1528 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1529 // This comma was followed by a line-break and something which can't be
1530 // the start of a declarator. The comma was probably a typo for a
1531 // semicolon.
1532 Diag(CommaLoc, diag::err_expected_semi_declaration)
1533 << FixItHint::CreateReplacement(CommaLoc, ";");
1534 ExpectSemi = false;
1535 break;
1536 }
John McCalld8ac0572009-11-03 19:26:08 +00001537
1538 // Parse the next declarator.
1539 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001540 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001541
1542 // Accept attributes in an init-declarator. In the first declarator in a
1543 // declaration, these would be part of the declspec. In subsequent
1544 // declarators, they become part of the declarator itself, so that they
1545 // don't apply to declarators after *this* one. Examples:
1546 // short __attribute__((common)) var; -> declspec
1547 // short var __attribute__((common)); -> declarator
1548 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001549 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001550
1551 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001552 if (!D.isInvalidType()) {
1553 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1554 D.complete(ThisDecl);
1555 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001556 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001557 }
John McCalld8ac0572009-11-03 19:26:08 +00001558 }
1559
1560 if (DeclEnd)
1561 *DeclEnd = Tok.getLocation();
1562
Richard Smith0706df42011-10-19 21:33:05 +00001563 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001564 ExpectAndConsumeSemi(Context == Declarator::FileContext
1565 ? diag::err_invalid_token_after_toplevel_declarator
1566 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001567 // Okay, there was no semicolon and one was expected. If we see a
1568 // declaration specifier, just assume it was missing and continue parsing.
1569 // Otherwise things are very confused and we skip to recover.
1570 if (!isDeclarationSpecifier()) {
1571 SkipUntil(tok::r_brace, true, true);
1572 if (Tok.is(tok::semi))
1573 ConsumeToken();
1574 }
John McCalld8ac0572009-11-03 19:26:08 +00001575 }
1576
Douglas Gregor23c94db2010-07-02 17:43:08 +00001577 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001578 DeclsInGroup.data(),
1579 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001580}
1581
Richard Smithad762fc2011-04-14 22:09:26 +00001582/// Parse an optional simple-asm-expr and attributes, and attach them to a
1583/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001584bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001585 // If a simple-asm-expr is present, parse it.
1586 if (Tok.is(tok::kw_asm)) {
1587 SourceLocation Loc;
1588 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1589 if (AsmLabel.isInvalid()) {
1590 SkipUntil(tok::semi, true, true);
1591 return true;
1592 }
1593
1594 D.setAsmLabel(AsmLabel.release());
1595 D.SetRangeEnd(Loc);
1596 }
1597
1598 MaybeParseGNUAttributes(D);
1599 return false;
1600}
1601
Douglas Gregor1426e532009-05-12 21:31:51 +00001602/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1603/// declarator'. This method parses the remainder of the declaration
1604/// (including any attributes or initializer, among other things) and
1605/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001606///
Reid Spencer5f016e22007-07-11 17:01:13 +00001607/// init-declarator: [C99 6.7]
1608/// declarator
1609/// declarator '=' initializer
1610/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1611/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001612/// [C++] declarator initializer[opt]
1613///
1614/// [C++] initializer:
1615/// [C++] '=' initializer-clause
1616/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001617/// [C++0x] '=' 'default' [TODO]
1618/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001619/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001620///
1621/// According to the standard grammar, =default and =delete are function
1622/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001623///
John McCalld226f652010-08-21 09:40:31 +00001624Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001625 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001626 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001627 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Richard Smithad762fc2011-04-14 22:09:26 +00001629 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1630}
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Richard Smithad762fc2011-04-14 22:09:26 +00001632Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1633 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001634 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001635 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001636 switch (TemplateInfo.Kind) {
1637 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001638 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001639 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001640
Douglas Gregord5a423b2009-09-25 18:43:00 +00001641 case ParsedTemplateInfo::Template:
1642 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001643 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001644 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001645 D);
1646 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001647
Douglas Gregord5a423b2009-09-25 18:43:00 +00001648 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001649 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001650 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001651 TemplateInfo.ExternLoc,
1652 TemplateInfo.TemplateLoc,
1653 D);
1654 if (ThisRes.isInvalid()) {
1655 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001656 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001657 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001658
Douglas Gregord5a423b2009-09-25 18:43:00 +00001659 ThisDecl = ThisRes.get();
1660 break;
1661 }
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Richard Smith34b41d92011-02-20 03:19:35 +00001664 bool TypeContainsAuto =
1665 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1666
Douglas Gregor1426e532009-05-12 21:31:51 +00001667 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001668 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001669 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001670 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001671 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001672 if (D.isFunctionDeclarator())
1673 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1674 << 1 /* delete */;
1675 else
1676 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001677 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001678 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001679 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1680 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001681 else
1682 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001683 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001684 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001685 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001686 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001687 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001688
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001689 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001690 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001691 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001692 cutOffParsing();
1693 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001694 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001695
John McCall60d7b3a2010-08-24 06:29:42 +00001696 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001697
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001699 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001700 ExitScope();
1701 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001702
Douglas Gregor1426e532009-05-12 21:31:51 +00001703 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001704 SkipUntil(tok::comma, true, true);
1705 Actions.ActOnInitializerError(ThisDecl);
1706 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001707 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1708 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001709 }
1710 } else if (Tok.is(tok::l_paren)) {
1711 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001712 BalancedDelimiterTracker T(*this, tok::l_paren);
1713 T.consumeOpen();
1714
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001715 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001716 CommaLocsTy CommaLocs;
1717
David Blaikie4e4d0842012-03-11 07:00:24 +00001718 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001719 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001720 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001721 }
1722
Douglas Gregor1426e532009-05-12 21:31:51 +00001723 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001724 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001725 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001726
David Blaikie4e4d0842012-03-11 07:00:24 +00001727 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001728 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001729 ExitScope();
1730 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001731 } else {
1732 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001733 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001734
1735 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1736 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001737
David Blaikie4e4d0842012-03-11 07:00:24 +00001738 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001739 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001740 ExitScope();
1741 }
1742
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001743 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1744 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001745 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001746 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1747 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001748 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001749 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001750 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001751 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001752 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1753
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001754 if (D.getCXXScopeSpec().isSet()) {
1755 EnterScope(0);
1756 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1757 }
1758
1759 ExprResult Init(ParseBraceInitializer());
1760
1761 if (D.getCXXScopeSpec().isSet()) {
1762 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1763 ExitScope();
1764 }
1765
1766 if (Init.isInvalid()) {
1767 Actions.ActOnInitializerError(ThisDecl);
1768 } else
1769 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1770 /*DirectInit=*/true, TypeContainsAuto);
1771
Douglas Gregor1426e532009-05-12 21:31:51 +00001772 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001773 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001774 }
1775
Richard Smith483b9f32011-02-21 20:05:19 +00001776 Actions.FinalizeDeclaration(ThisDecl);
1777
Douglas Gregor1426e532009-05-12 21:31:51 +00001778 return ThisDecl;
1779}
1780
Reid Spencer5f016e22007-07-11 17:01:13 +00001781/// ParseSpecifierQualifierList
1782/// specifier-qualifier-list:
1783/// type-specifier specifier-qualifier-list[opt]
1784/// type-qualifier specifier-qualifier-list[opt]
1785/// [GNU] attributes specifier-qualifier-list[opt]
1786///
Richard Smith69730c12012-03-12 07:56:15 +00001787void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1788 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1790 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001791 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001792 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 // Validate declspec for type-name.
1795 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001796 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1797 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001798 Diag(Tok, diag::err_expected_type);
1799 DS.SetTypeSpecError();
1800 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1801 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001803 if (!DS.hasTypeSpecifier())
1804 DS.SetTypeSpecError();
1805 }
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 // Issue diagnostic and remove storage class if present.
1808 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1809 if (DS.getStorageClassSpecLoc().isValid())
1810 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1811 else
1812 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1813 DS.ClearStorageClassSpecs();
1814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 // Issue diagnostic and remove function specfier if present.
1817 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001818 if (DS.isInlineSpecified())
1819 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1820 if (DS.isVirtualSpecified())
1821 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1822 if (DS.isExplicitSpecified())
1823 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 DS.ClearFunctionSpecs();
1825 }
Richard Smith69730c12012-03-12 07:56:15 +00001826
1827 // Issue diagnostic and remove constexpr specfier if present.
1828 if (DS.isConstexprSpecified()) {
1829 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1830 DS.ClearConstexprSpec();
1831 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001832}
1833
Chris Lattnerc199ab32009-04-12 20:42:31 +00001834/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1835/// specified token is valid after the identifier in a declarator which
1836/// immediately follows the declspec. For example, these things are valid:
1837///
1838/// int x [ 4]; // direct-declarator
1839/// int x ( int y); // direct-declarator
1840/// int(int x ) // direct-declarator
1841/// int x ; // simple-declaration
1842/// int x = 17; // init-declarator-list
1843/// int x , y; // init-declarator-list
1844/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001845/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001846/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001847///
1848/// This is not, because 'x' does not immediately follow the declspec (though
1849/// ')' happens to be valid anyway).
1850/// int (x)
1851///
1852static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1853 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1854 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001855 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001856}
1857
Chris Lattnere40c2952009-04-14 21:34:55 +00001858
1859/// ParseImplicitInt - This method is called when we have an non-typename
1860/// identifier in a declspec (which normally terminates the decl spec) when
1861/// the declspec has no type specifier. In this case, the declspec is either
1862/// malformed or is "implicit int" (in K&R and C89).
1863///
1864/// This method handles diagnosing this prettily and returns false if the
1865/// declspec is done being processed. If it recovers and thinks there may be
1866/// other pieces of declspec after it, it returns true.
1867///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001868bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001869 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001870 AccessSpecifier AS, DeclSpecContext DSC,
1871 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001872 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Chris Lattnere40c2952009-04-14 21:34:55 +00001874 SourceLocation Loc = Tok.getLocation();
1875 // If we see an identifier that is not a type name, we normally would
1876 // parse it as the identifer being declared. However, when a typename
1877 // is typo'd or the definition is not included, this will incorrectly
1878 // parse the typename as the identifier name and fall over misparsing
1879 // later parts of the diagnostic.
1880 //
1881 // As such, we try to do some look-ahead in cases where this would
1882 // otherwise be an "implicit-int" case to see if this is invalid. For
1883 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1884 // an identifier with implicit int, we'd get a parse error because the
1885 // next token is obviously invalid for a type. Parse these as a case
1886 // with an invalid type specifier.
1887 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Chris Lattnere40c2952009-04-14 21:34:55 +00001889 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001890 // error, do lookahead to try to do better recovery. This never applies
1891 // within a type specifier. Outside of C++, we allow this even if the
1892 // language doesn't "officially" support implicit int -- we support
1893 // implicit int as an extension in C99 and C11. Allegedly, MS also
1894 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001895 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001896 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001897 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001898 // If this token is valid for implicit int, e.g. "static x = 4", then
1899 // we just avoid eating the identifier, so it will be parsed as the
1900 // identifier in the declarator.
1901 return false;
1902 }
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Richard Smith827adaf2012-05-15 21:01:51 +00001904 if (getLangOpts().CPlusPlus &&
1905 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1906 // Don't require a type specifier if we have the 'auto' storage class
1907 // specifier in C++98 -- we'll promote it to a type specifier.
1908 return false;
1909 }
1910
Chris Lattnere40c2952009-04-14 21:34:55 +00001911 // Otherwise, if we don't consume this token, we are going to emit an
1912 // error anyway. Try to recover from various common problems. Check
1913 // to see if this was a reference to a tag name without a tag specified.
1914 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001915 //
1916 // C++ doesn't need this, and isTagName doesn't take SS.
1917 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001918 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001919 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregor23c94db2010-07-02 17:43:08 +00001921 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001922 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001923 case DeclSpec::TST_enum:
1924 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1925 case DeclSpec::TST_union:
1926 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1927 case DeclSpec::TST_struct:
1928 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001929 case DeclSpec::TST_interface:
1930 TagName="__interface"; FixitTagName = "__interface ";
1931 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001932 case DeclSpec::TST_class:
1933 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Chris Lattnerf4382f52009-04-14 22:17:06 +00001936 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001937 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1938 LookupResult R(Actions, TokenName, SourceLocation(),
1939 Sema::LookupOrdinaryName);
1940
Chris Lattnerf4382f52009-04-14 22:17:06 +00001941 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001942 << TokenName << TagName << getLangOpts().CPlusPlus
1943 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1944
1945 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1946 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1947 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001948 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001949 << TokenName << TagName;
1950 }
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Chris Lattnerf4382f52009-04-14 22:17:06 +00001952 // Parse this as a tag as if the missing tag were present.
1953 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001954 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001955 else
Richard Smith69730c12012-03-12 07:56:15 +00001956 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001957 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001958 return true;
1959 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001960 }
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Richard Smith8f0a7e72012-05-15 21:29:55 +00001962 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001963 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001964 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1965 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001966 // Look ahead to the next token to try to figure out what this declaration
1967 // was supposed to be.
1968 switch (NextToken().getKind()) {
1969 case tok::comma:
1970 case tok::equal:
1971 case tok::kw_asm:
1972 case tok::l_brace:
1973 case tok::l_square:
1974 case tok::semi:
1975 // This looks like a variable declaration. The type is probably missing.
1976 // We're done parsing decl-specifiers.
1977 return false;
1978
1979 case tok::l_paren: {
1980 // static x(4); // 'x' is not a type
1981 // x(int n); // 'x' is not a type
1982 // x (*p)[]; // 'x' is a type
1983 //
1984 // Since we're in an error case (or the rare 'implicit int in C++' MS
1985 // extension), we can afford to perform a tentative parse to determine
1986 // which case we're in.
1987 TentativeParsingAction PA(*this);
1988 ConsumeToken();
1989 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1990 PA.Revert();
1991 if (TPR == TPResult::False())
1992 return false;
1993 // The identifier is followed by a parenthesized declarator.
1994 // It's supposed to be a type.
1995 break;
1996 }
1997
1998 default:
1999 // This is probably supposed to be a type. This includes cases like:
2000 // int f(itn);
2001 // struct S { unsinged : 4; };
2002 break;
2003 }
2004 }
2005
Chad Rosier8decdee2012-06-26 22:30:43 +00002006 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002007 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002008 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002009 IdentifierInfo *II = Tok.getIdentifierInfo();
2010 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002011 // The action emitted a diagnostic, so we don't have to.
2012 if (T) {
2013 // The action has suggested that the type T could be used. Set that as
2014 // the type in the declaration specifiers, consume the would-be type
2015 // name token, and we're done.
2016 const char *PrevSpec;
2017 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002018 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002019 DS.SetRangeEnd(Tok.getLocation());
2020 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002021 // There may be other declaration specifiers after this.
2022 return true;
2023 } else if (II != Tok.getIdentifierInfo()) {
2024 // If no type was suggested, the correction is to a keyword
2025 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002026 // There may be other declaration specifiers after this.
2027 return true;
2028 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002029
Douglas Gregora786fdb2009-10-13 23:27:22 +00002030 // Fall through; the action had no suggestion for us.
2031 } else {
2032 // The action did not emit a diagnostic, so emit one now.
2033 SourceRange R;
2034 if (SS) R = SS->getRange();
2035 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2036 }
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Douglas Gregora786fdb2009-10-13 23:27:22 +00002038 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002039 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002040 DS.SetRangeEnd(Tok.getLocation());
2041 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Chris Lattnere40c2952009-04-14 21:34:55 +00002043 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2044 // avoid rippling error messages on subsequent uses of the same type,
2045 // could be useful if #include was forgotten.
2046 return false;
2047}
2048
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002049/// \brief Determine the declaration specifier context from the declarator
2050/// context.
2051///
2052/// \param Context the declarator context, which is one of the
2053/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002054Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002055Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2056 if (Context == Declarator::MemberContext)
2057 return DSC_class;
2058 if (Context == Declarator::FileContext)
2059 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002060 if (Context == Declarator::TrailingReturnContext)
2061 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002062 return DSC_normal;
2063}
2064
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002065/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2066///
2067/// FIXME: Simply returns an alignof() expression if the argument is a
2068/// type. Ideally, the type should be propagated directly into Sema.
2069///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002070/// [C11] type-id
2071/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002072/// [C++0x] type-id ...[opt]
2073/// [C++0x] assignment-expression ...[opt]
2074ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2075 SourceLocation &EllipsisLoc) {
2076 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002077 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002078 SourceLocation TypeLoc = Tok.getLocation();
2079 ParsedType Ty = ParseTypeName().get();
2080 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002081 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2082 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002083 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002084 ER = ParseConstantExpression();
2085
Richard Smith80ad52f2013-01-02 11:42:31 +00002086 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002087 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002088
2089 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002090}
2091
2092/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2093/// attribute to Attrs.
2094///
2095/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002096/// [C11] '_Alignas' '(' type-id ')'
2097/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002098/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2099/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002100void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2101 SourceLocation *endLoc) {
2102 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2103 "Not an alignment-specifier!");
2104
Richard Smith33f04a22013-01-29 01:48:07 +00002105 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2106 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002108 BalancedDelimiterTracker T(*this, tok::l_paren);
2109 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002110 return;
2111
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002112 SourceLocation EllipsisLoc;
2113 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002114 if (ArgExpr.isInvalid()) {
2115 SkipUntil(tok::r_paren);
2116 return;
2117 }
2118
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002119 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002120 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002121 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002122
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002123 // FIXME: Handle pack-expansions here.
2124 if (EllipsisLoc.isValid()) {
2125 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2126 return;
2127 }
2128
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002129 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002130 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002131 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2132 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002133}
2134
Reid Spencer5f016e22007-07-11 17:01:13 +00002135/// ParseDeclarationSpecifiers
2136/// declaration-specifiers: [C99 6.7]
2137/// storage-class-specifier declaration-specifiers[opt]
2138/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002139/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002140/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002141/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002142/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002143///
2144/// storage-class-specifier: [C99 6.7.1]
2145/// 'typedef'
2146/// 'extern'
2147/// 'static'
2148/// 'auto'
2149/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002150/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002151/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002152/// function-specifier: [C99 6.7.4]
2153/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002154/// [C++] 'virtual'
2155/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002156/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002157/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002158/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002159
Reid Spencer5f016e22007-07-11 17:01:13 +00002160///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002161void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002162 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002163 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002164 DeclSpecContext DSContext,
2165 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002166 if (DS.getSourceRange().isInvalid()) {
2167 DS.SetRangeStart(Tok.getLocation());
2168 DS.SetRangeEnd(Tok.getLocation());
2169 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002170
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002171 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002172 bool AttrsLastTime = false;
2173 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002175 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002177 unsigned DiagID = 0;
2178
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002180
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002182 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002183 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002184 if (!AttrsLastTime)
2185 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002186 else {
2187 // Reject C++11 attributes that appertain to decl specifiers as
2188 // we don't support any C++11 attributes that appertain to decl
2189 // specifiers. This also conforms to what g++ 4.8 is doing.
2190 ProhibitCXX11Attributes(attrs);
2191
Sean Hunt2edf0a22012-06-23 05:07:58 +00002192 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002193 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002194
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 // If this is not a declaration specifier token, we're done reading decl
2196 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002197 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Sean Hunt2edf0a22012-06-23 05:07:58 +00002200 case tok::l_square:
2201 case tok::kw_alignas:
2202 if (!isCXX11AttributeSpecifier())
2203 goto DoneWithDeclSpec;
2204
2205 ProhibitAttributes(attrs);
2206 // FIXME: It would be good to recover by accepting the attributes,
2207 // but attempting to do that now would cause serious
2208 // madness in terms of diagnostics.
2209 attrs.clear();
2210 attrs.Range = SourceRange();
2211
2212 ParseCXX11Attributes(attrs);
2213 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002214 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002215
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002216 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002217 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002218 if (DS.hasTypeSpecifier()) {
2219 bool AllowNonIdentifiers
2220 = (getCurScope()->getFlags() & (Scope::ControlScope |
2221 Scope::BlockScope |
2222 Scope::TemplateParamScope |
2223 Scope::FunctionPrototypeScope |
2224 Scope::AtCatchScope)) == 0;
2225 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002226 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002227 (DSContext == DSC_class && DS.isFriendSpecified());
2228
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002229 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002230 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002231 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002232 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002233 }
2234
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002235 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2236 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2237 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002238 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002239 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002240 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002241 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002242 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002243 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002244
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002245 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002246 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002247 }
2248
Chris Lattner5e02c472009-01-05 00:07:25 +00002249 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002250 // C++ scope specifier. Annotate and loop, or bail out on error.
2251 if (TryAnnotateCXXScopeToken(true)) {
2252 if (!DS.hasTypeSpecifier())
2253 DS.SetTypeSpecError();
2254 goto DoneWithDeclSpec;
2255 }
John McCall2e0a7152010-03-01 18:20:46 +00002256 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2257 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002258 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002259
2260 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002261 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002262 goto DoneWithDeclSpec;
2263
John McCallaa87d332009-12-12 11:40:51 +00002264 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002265 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2266 Tok.getAnnotationRange(),
2267 SS);
John McCallaa87d332009-12-12 11:40:51 +00002268
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002269 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002270 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002271 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002272 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002273 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002274 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002275
2276 // C++ [class.qual]p2:
2277 // In a lookup in which the constructor is an acceptable lookup
2278 // result and the nested-name-specifier nominates a class C:
2279 //
2280 // - if the name specified after the
2281 // nested-name-specifier, when looked up in C, is the
2282 // injected-class-name of C (Clause 9), or
2283 //
2284 // - if the name specified after the nested-name-specifier
2285 // is the same as the identifier or the
2286 // simple-template-id's template-name in the last
2287 // component of the nested-name-specifier,
2288 //
2289 // the name is instead considered to name the constructor of
2290 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002291 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002292 // Thus, if the template-name is actually the constructor
2293 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002294 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002295 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002296 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002297 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002298 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002299 if (isConstructorDeclarator()) {
2300 // The user meant this to be an out-of-line constructor
2301 // definition, but template arguments are not allowed
2302 // there. Just allow this as a constructor; we'll
2303 // complain about it later.
2304 goto DoneWithDeclSpec;
2305 }
2306
2307 // The user meant this to name a type, but it actually names
2308 // a constructor with some extraneous template
2309 // arguments. Complain, then parse it as a type as the user
2310 // intended.
2311 Diag(TemplateId->TemplateNameLoc,
2312 diag::err_out_of_line_template_id_names_constructor)
2313 << TemplateId->Name;
2314 }
2315
John McCallaa87d332009-12-12 11:40:51 +00002316 DS.getTypeSpecScope() = SS;
2317 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002318 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002319 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002320 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002321 continue;
2322 }
2323
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002324 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002325 DS.getTypeSpecScope() = SS;
2326 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002327 if (Tok.getAnnotationValue()) {
2328 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002330 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002331 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002332 if (isInvalid)
2333 break;
John McCallb3d87482010-08-24 05:47:05 +00002334 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002335 else
2336 DS.SetTypeSpecError();
2337 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2338 ConsumeToken(); // The typename
2339 }
2340
Douglas Gregor9135c722009-03-25 15:40:00 +00002341 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002342 goto DoneWithDeclSpec;
2343
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002344 // If we're in a context where the identifier could be a class name,
2345 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002346 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002347 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002348 &SS)) {
2349 if (isConstructorDeclarator())
2350 goto DoneWithDeclSpec;
2351
2352 // As noted in C++ [class.qual]p2 (cited above), when the name
2353 // of the class is qualified in a context where it could name
2354 // a constructor, its a constructor name. However, we've
2355 // looked at the declarator, and the user probably meant this
2356 // to be a type. Complain that it isn't supposed to be treated
2357 // as a type, then proceed to parse it as a type.
2358 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2359 << Next.getIdentifierInfo();
2360 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002361
John McCallb3d87482010-08-24 05:47:05 +00002362 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2363 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002364 getCurScope(), &SS,
2365 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002366 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002367 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002368
Chris Lattnerf4382f52009-04-14 22:17:06 +00002369 // If the referenced identifier is not a type, then this declspec is
2370 // erroneous: We already checked about that it has no type specifier, and
2371 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002372 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002373 if (TypeRep == 0) {
2374 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002375 ParsedAttributesWithRange Attrs(AttrFactory);
2376 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2377 if (!Attrs.empty()) {
2378 AttrsLastTime = true;
2379 attrs.takeAllFrom(Attrs);
2380 }
2381 continue;
2382 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002383 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002384 }
Mike Stump1eb44332009-09-09 15:08:12 +00002385
John McCallaa87d332009-12-12 11:40:51 +00002386 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002387 ConsumeToken(); // The C++ scope.
2388
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002390 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002391 if (isInvalid)
2392 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002394 DS.SetRangeEnd(Tok.getLocation());
2395 ConsumeToken(); // The typename.
2396
2397 continue;
2398 }
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Chris Lattner80d0c892009-01-21 19:48:37 +00002400 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002401 if (Tok.getAnnotationValue()) {
2402 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002404 DiagID, T);
2405 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002406 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002407
Chris Lattner5c5db552010-04-05 18:18:31 +00002408 if (isInvalid)
2409 break;
2410
Chris Lattner80d0c892009-01-21 19:48:37 +00002411 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2412 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002413
Chris Lattner80d0c892009-01-21 19:48:37 +00002414 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2415 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002416 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002417 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002418 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002419
Chris Lattner80d0c892009-01-21 19:48:37 +00002420 continue;
2421 }
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Douglas Gregorbfad9152011-04-28 15:48:45 +00002423 case tok::kw___is_signed:
2424 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2425 // typically treats it as a trait. If we see __is_signed as it appears
2426 // in libstdc++, e.g.,
2427 //
2428 // static const bool __is_signed;
2429 //
2430 // then treat __is_signed as an identifier rather than as a keyword.
2431 if (DS.getTypeSpecType() == TST_bool &&
2432 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2433 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2434 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2435 Tok.setKind(tok::identifier);
2436 }
2437
2438 // We're done with the declaration-specifiers.
2439 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002440
Chris Lattner3bd934a2008-07-26 01:18:38 +00002441 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002442 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002443 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002444 // In C++, check to see if this is a scope specifier like foo::bar::, if
2445 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002446 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002447 if (TryAnnotateCXXScopeToken(true)) {
2448 if (!DS.hasTypeSpecifier())
2449 DS.SetTypeSpecError();
2450 goto DoneWithDeclSpec;
2451 }
2452 if (!Tok.is(tok::identifier))
2453 continue;
2454 }
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Chris Lattner3bd934a2008-07-26 01:18:38 +00002456 // This identifier can only be a typedef name if we haven't already seen
2457 // a type-specifier. Without this check we misparse:
2458 // typedef int X; struct Y { short X; }; as 'short int'.
2459 if (DS.hasTypeSpecifier())
2460 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002461
John Thompson82287d12010-02-05 00:12:22 +00002462 // Check for need to substitute AltiVec keyword tokens.
2463 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2464 break;
2465
Richard Smithf63eee72012-05-09 18:56:43 +00002466 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2467 // allow the use of a typedef name as a type specifier.
2468 if (DS.isTypeAltiVecVector())
2469 goto DoneWithDeclSpec;
2470
John McCallb3d87482010-08-24 05:47:05 +00002471 ParsedType TypeRep =
2472 Actions.getTypeName(*Tok.getIdentifierInfo(),
2473 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002474
Chris Lattnerc199ab32009-04-12 20:42:31 +00002475 // If this is not a typedef name, don't parse it as part of the declspec,
2476 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002477 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002478 ParsedAttributesWithRange Attrs(AttrFactory);
2479 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2480 if (!Attrs.empty()) {
2481 AttrsLastTime = true;
2482 attrs.takeAllFrom(Attrs);
2483 }
2484 continue;
2485 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002486 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002487 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002488
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002489 // If we're in a context where the identifier could be a class name,
2490 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002491 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002492 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002493 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002494 goto DoneWithDeclSpec;
2495
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002497 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002498 if (isInvalid)
2499 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Chris Lattner3bd934a2008-07-26 01:18:38 +00002501 DS.SetRangeEnd(Tok.getLocation());
2502 ConsumeToken(); // The identifier
2503
2504 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2505 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002506 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002507 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002508 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002509
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002510 // Need to support trailing type qualifiers (e.g. "id<p> const").
2511 // If a type specifier follows, it will be diagnosed elsewhere.
2512 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002513 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002514
2515 // type-name
2516 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002517 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002518 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002519 // This template-id does not refer to a type name, so we're
2520 // done with the type-specifiers.
2521 goto DoneWithDeclSpec;
2522 }
2523
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002524 // If we're in a context where the template-id could be a
2525 // constructor name or specialization, check whether this is a
2526 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002527 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002528 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002529 isConstructorDeclarator())
2530 goto DoneWithDeclSpec;
2531
Douglas Gregor39a8de12009-02-25 19:37:18 +00002532 // Turn the template-id annotation token into a type annotation
2533 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002534 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002535 continue;
2536 }
2537
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 // GNU attributes support.
2539 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002540 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002541 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002542
2543 // Microsoft declspec support.
2544 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002545 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002546 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Steve Naroff239f0732008-12-25 14:16:32 +00002548 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002549 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002550 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002551 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002552 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002553 // FIXME: This does not work correctly if it is set to be a declspec
2554 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002555 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002556 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002557 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002558 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002559
2560 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002561 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002562 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002563 case tok::kw___cdecl:
2564 case tok::kw___stdcall:
2565 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002566 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002567 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002568 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002569 continue;
2570
Dawn Perchik52fc3142010-09-03 01:29:35 +00002571 // Borland single token adornments.
2572 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002573 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002574 continue;
2575
Peter Collingbournef315fa82011-02-14 01:42:53 +00002576 // OpenCL single token adornments.
2577 case tok::kw___kernel:
2578 ParseOpenCLAttributes(DS.getAttributes());
2579 continue;
2580
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 // storage-class-specifier
2582 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002583 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2584 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 break;
2586 case tok::kw_extern:
2587 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002588 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002589 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2590 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002591 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002592 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002593 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2594 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002595 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 case tok::kw_static:
2597 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002598 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002599 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2600 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 break;
2602 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002603 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002604 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002605 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2606 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002607 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002608 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002609 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002610 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2612 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002613 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002614 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2615 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002616 break;
2617 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002618 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2619 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002620 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002621 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002622 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2623 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002624 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002626 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002627 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 // function-specifier
2630 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002631 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002632 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002633 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002634 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002635 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002636 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002637 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002638 break;
Richard Smithde03c152013-01-17 22:16:11 +00002639 case tok::kw__Noreturn:
2640 if (!getLangOpts().C11)
2641 Diag(Loc, diag::ext_c11_noreturn);
2642 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2643 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002644
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002645 // alignment-specifier
2646 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002647 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002648 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002649 ParseAlignmentSpecifier(DS.getAttributes());
2650 continue;
2651
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002652 // friend
2653 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002654 if (DSContext == DSC_class)
2655 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2656 else {
2657 PrevSpec = ""; // not actually used by the diagnostic
2658 DiagID = diag::err_friend_invalid_in_context;
2659 isInvalid = true;
2660 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002661 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002662
Douglas Gregor8d267c52011-09-09 02:06:17 +00002663 // Modules
2664 case tok::kw___module_private__:
2665 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2666 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002667
Sebastian Redl2ac67232009-11-05 15:47:02 +00002668 // constexpr
2669 case tok::kw_constexpr:
2670 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2671 break;
2672
Chris Lattner80d0c892009-01-21 19:48:37 +00002673 // type-specifier
2674 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002675 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2676 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002677 break;
2678 case tok::kw_long:
2679 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2681 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002682 else
John McCallfec54012009-08-03 20:12:06 +00002683 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2684 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002685 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002686 case tok::kw___int64:
2687 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2688 DiagID);
2689 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002690 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002691 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2692 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002693 break;
2694 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002695 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2696 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002697 break;
2698 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002699 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2700 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002701 break;
2702 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002703 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2704 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002705 break;
2706 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2708 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002709 break;
2710 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002711 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2712 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002713 break;
2714 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2716 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002717 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002718 case tok::kw___int128:
2719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2720 DiagID);
2721 break;
2722 case tok::kw_half:
2723 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2724 DiagID);
2725 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002726 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2728 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 break;
2730 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2732 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002733 break;
2734 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2736 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002737 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002738 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2740 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002741 break;
2742 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2744 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002745 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002746 case tok::kw_bool:
2747 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002748 if (Tok.is(tok::kw_bool) &&
2749 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2750 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2751 PrevSpec = ""; // Not used by the diagnostic.
2752 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002753 // For better error recovery.
2754 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002755 isInvalid = true;
2756 } else {
2757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2758 DiagID);
2759 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002760 break;
2761 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2763 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002764 break;
2765 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2767 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002768 break;
2769 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2771 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002772 break;
John Thompson82287d12010-02-05 00:12:22 +00002773 case tok::kw___vector:
2774 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2775 break;
2776 case tok::kw___pixel:
2777 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2778 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002779 case tok::kw_image1d_t:
2780 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2781 PrevSpec, DiagID);
2782 break;
2783 case tok::kw_image1d_array_t:
2784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2785 PrevSpec, DiagID);
2786 break;
2787 case tok::kw_image1d_buffer_t:
2788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2789 PrevSpec, DiagID);
2790 break;
2791 case tok::kw_image2d_t:
2792 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2793 PrevSpec, DiagID);
2794 break;
2795 case tok::kw_image2d_array_t:
2796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2797 PrevSpec, DiagID);
2798 break;
2799 case tok::kw_image3d_t:
2800 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2801 PrevSpec, DiagID);
2802 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002803 case tok::kw_sampler_t:
2804 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2805 PrevSpec, DiagID);
2806 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002807 case tok::kw_event_t:
2808 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2809 PrevSpec, DiagID);
2810 break;
John McCalla5fc4722011-04-09 22:50:59 +00002811 case tok::kw___unknown_anytype:
2812 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2813 PrevSpec, DiagID);
2814 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002815
2816 // class-specifier:
2817 case tok::kw_class:
2818 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002819 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002820 case tok::kw_union: {
2821 tok::TokenKind Kind = Tok.getKind();
2822 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002823
2824 // These are attributes following class specifiers.
2825 // To produce better diagnostic, we parse them when
2826 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002827 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002828 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002829 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002830
2831 // If there are attributes following class specifier,
2832 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002833 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002834 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002835 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002836 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002837 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002838 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002839
2840 // enum-specifier:
2841 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002842 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002843 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002844 continue;
2845
2846 // cv-qualifier:
2847 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002848 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002849 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002850 break;
2851 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002852 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002853 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002854 break;
2855 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002856 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002857 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002858 break;
2859
Douglas Gregord57959a2009-03-27 23:10:48 +00002860 // C++ typename-specifier:
2861 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002862 if (TryAnnotateTypeOrScopeToken()) {
2863 DS.SetTypeSpecError();
2864 goto DoneWithDeclSpec;
2865 }
2866 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002867 continue;
2868 break;
2869
Chris Lattner80d0c892009-01-21 19:48:37 +00002870 // GNU typeof support.
2871 case tok::kw_typeof:
2872 ParseTypeofSpecifier(DS);
2873 continue;
2874
David Blaikie42d6d0c2011-12-04 05:04:18 +00002875 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002876 ParseDecltypeSpecifier(DS);
2877 continue;
2878
Sean Huntdb5d44b2011-05-19 05:37:45 +00002879 case tok::kw___underlying_type:
2880 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002881 continue;
2882
2883 case tok::kw__Atomic:
2884 ParseAtomicSpecifier(DS);
2885 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002886
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002887 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002888 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002889 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002890 goto DoneWithDeclSpec;
2891 case tok::kw___private:
2892 case tok::kw___global:
2893 case tok::kw___local:
2894 case tok::kw___constant:
2895 case tok::kw___read_only:
2896 case tok::kw___write_only:
2897 case tok::kw___read_write:
2898 ParseOpenCLQualifiers(DS);
2899 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002900
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002901 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002902 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002903 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2904 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002905 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002906 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Douglas Gregor46f936e2010-11-19 17:10:50 +00002908 if (!ParseObjCProtocolQualifiers(DS))
2909 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2910 << FixItHint::CreateInsertion(Loc, "id")
2911 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002912
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002913 // Need to support trailing type qualifiers (e.g. "id<p> const").
2914 // If a type specifier follows, it will be diagnosed elsewhere.
2915 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 }
John McCallfec54012009-08-03 20:12:06 +00002917 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002918 if (isInvalid) {
2919 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002920 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002921
Douglas Gregorae2fb142010-08-23 14:34:43 +00002922 if (DiagID == diag::ext_duplicate_declspec)
2923 Diag(Tok, DiagID)
2924 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2925 else
2926 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002927 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002928
Chris Lattner81c018d2008-03-13 06:29:04 +00002929 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002930 if (DiagID != diag::err_bool_redeclaration)
2931 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002932
2933 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002934 }
2935}
Douglas Gregoradcac882008-12-01 23:54:00 +00002936
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002937/// ParseStructDeclaration - Parse a struct declaration without the terminating
2938/// semicolon.
2939///
Reid Spencer5f016e22007-07-11 17:01:13 +00002940/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002941/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002942/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002943/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002944/// struct-declarator-list:
2945/// struct-declarator
2946/// struct-declarator-list ',' struct-declarator
2947/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2948/// struct-declarator:
2949/// declarator
2950/// [GNU] declarator attributes[opt]
2951/// declarator[opt] ':' constant-expression
2952/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2953///
Chris Lattnere1359422008-04-10 06:46:29 +00002954void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002955ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002956
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002957 if (Tok.is(tok::kw___extension__)) {
2958 // __extension__ silences extension warnings in the subexpression.
2959 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002960 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002961 return ParseStructDeclaration(DS, Fields);
2962 }
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Steve Naroff28a7ca82007-08-20 22:28:22 +00002964 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002965 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002967 // If there are no declarators, this is a free-standing declaration
2968 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002969 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002970 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2971 DS);
2972 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002973 return;
2974 }
2975
2976 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002977 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002978 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002979 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002980 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002981 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002982
Bill Wendlingad017fa2012-12-20 19:22:21 +00002983 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002984 if (!FirstDeclarator)
2985 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Steve Naroff28a7ca82007-08-20 22:28:22 +00002987 /// struct-declarator: declarator
2988 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002989 if (Tok.isNot(tok::colon)) {
2990 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2991 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002992 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002993 }
Mike Stump1eb44332009-09-09 15:08:12 +00002994
Chris Lattner04d66662007-10-09 17:33:22 +00002995 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002996 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002997 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002998 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002999 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003000 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003001 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003003
Steve Naroff28a7ca82007-08-20 22:28:22 +00003004 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003005 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003006
John McCallbdd563e2009-11-03 02:38:08 +00003007 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003008 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003009
Steve Naroff28a7ca82007-08-20 22:28:22 +00003010 // If we don't have a comma, it is either the end of the list (a ';')
3011 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003012 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003013 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003014
Steve Naroff28a7ca82007-08-20 22:28:22 +00003015 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003016 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003017
John McCallbdd563e2009-11-03 02:38:08 +00003018 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003019 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003020}
3021
3022/// ParseStructUnionBody
3023/// struct-contents:
3024/// struct-declaration-list
3025/// [EXT] empty
3026/// [GNU] "struct-declaration-list" without terminatoring ';'
3027/// struct-declaration-list:
3028/// struct-declaration
3029/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003030/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003031///
Reid Spencer5f016e22007-07-11 17:01:13 +00003032void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003033 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003034 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3035 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003037 BalancedDelimiterTracker T(*this, tok::l_brace);
3038 if (T.consumeOpen())
3039 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003040
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003041 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003042 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003043
Reid Spencer5f016e22007-07-11 17:01:13 +00003044 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3045 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003046 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003047 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3048 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3049 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003050
Chris Lattner5f9e2722011-07-23 10:55:15 +00003051 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003052
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003054 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003055 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003056
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003058 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003059 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003060 continue;
3061 }
Chris Lattnere1359422008-04-10 06:46:29 +00003062
John McCallbdd563e2009-11-03 02:38:08 +00003063 if (!Tok.is(tok::at)) {
3064 struct CFieldCallback : FieldCallback {
3065 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003066 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003067 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003068
John McCalld226f652010-08-21 09:40:31 +00003069 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003070 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003071 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3072
Eli Friedmandcdff462012-08-08 23:53:27 +00003073 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003074 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003075 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003076 FD.D.getDeclSpec().getSourceRange().getBegin(),
3077 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003078 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003079 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003080 }
John McCallbdd563e2009-11-03 02:38:08 +00003081 } Callback(*this, TagDecl, FieldDecls);
3082
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003083 // Parse all the comma separated declarators.
3084 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003085 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003086 } else { // Handle @defs
3087 ConsumeToken();
3088 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3089 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003090 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003091 continue;
3092 }
3093 ConsumeToken();
3094 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3095 if (!Tok.is(tok::identifier)) {
3096 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003097 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003098 continue;
3099 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003100 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003101 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003102 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003103 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3104 ConsumeToken();
3105 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003106 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003107
Chris Lattner04d66662007-10-09 17:33:22 +00003108 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003109 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003110 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003111 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003112 break;
3113 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003114 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3115 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003117 // If we stopped at a ';', eat it.
3118 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 }
3120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003122 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003123
John McCall0b7e6782011-03-24 11:26:52 +00003124 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003125 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003126 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003127
Douglas Gregor23c94db2010-07-02 17:43:08 +00003128 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003129 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003130 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003131 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003132 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003133 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3134 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003135}
3136
Reid Spencer5f016e22007-07-11 17:01:13 +00003137/// ParseEnumSpecifier
3138/// enum-specifier: [C99 6.7.2.2]
3139/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003140///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003141/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3142/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003143/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3144/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003145/// 'enum' identifier
3146/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003147///
Richard Smith1af83c42012-03-23 03:33:32 +00003148/// [C++11] enum-head '{' enumerator-list[opt] '}'
3149/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003150///
Richard Smith1af83c42012-03-23 03:33:32 +00003151/// enum-head: [C++11]
3152/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3153/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3154/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003155///
Richard Smith1af83c42012-03-23 03:33:32 +00003156/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003157/// 'enum'
3158/// 'enum' 'class'
3159/// 'enum' 'struct'
3160///
Richard Smith1af83c42012-03-23 03:33:32 +00003161/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003162/// ':' type-specifier-seq
3163///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003164/// [C++] elaborated-type-specifier:
3165/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3166///
Chris Lattner4c97d762009-04-12 21:49:30 +00003167void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003168 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003169 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003171 if (Tok.is(tok::code_completion)) {
3172 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003173 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003174 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003175 }
John McCall57c13002011-07-06 05:58:41 +00003176
Sean Hunt2edf0a22012-06-23 05:07:58 +00003177 // If attributes exist after tag, parse them.
3178 ParsedAttributesWithRange attrs(AttrFactory);
3179 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003180 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003181
3182 // If declspecs exist after tag, parse them.
3183 while (Tok.is(tok::kw___declspec))
3184 ParseMicrosoftDeclSpec(attrs);
3185
Richard Smithbdad7a22012-01-10 01:33:14 +00003186 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003187 bool IsScopedUsingClassTag = false;
3188
John McCall1e12b3d2012-06-23 22:30:04 +00003189 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003190 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003191 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003192 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003193 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003194 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003195
Bill Wendlingad017fa2012-12-20 19:22:21 +00003196 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003197 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003198 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003199
3200 // They are allowed afterwards, though.
3201 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003202 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003203 while (Tok.is(tok::kw___declspec))
3204 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003205 }
Richard Smith1af83c42012-03-23 03:33:32 +00003206
John McCall13489672012-05-07 06:16:58 +00003207 // C++11 [temp.explicit]p12:
3208 // The usual access controls do not apply to names used to specify
3209 // explicit instantiations.
3210 // We extend this to also cover explicit specializations. Note that
3211 // we don't suppress if this turns out to be an elaborated type
3212 // specifier.
3213 bool shouldDelayDiagsInTag =
3214 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3215 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3216 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003217
Richard Smith7796eb52012-03-12 08:56:40 +00003218 // Enum definitions should not be parsed in a trailing-return-type.
3219 bool AllowDeclaration = DSC != DSC_trailing;
3220
3221 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003222 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003223 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003224
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003225 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003226 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003227 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3228 // if a fixed underlying type is allowed.
3229 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003230
3231 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003232 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003233 return;
3234
3235 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003236 Diag(Tok, diag::err_expected_ident);
3237 if (Tok.isNot(tok::l_brace)) {
3238 // Has no name and is not a definition.
3239 // Skip the rest of this declarator, up until the comma or semicolon.
3240 SkipUntil(tok::comma, true);
3241 return;
3242 }
3243 }
3244 }
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003246 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003247 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003248 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003249 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003250
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003251 // Skip the rest of this declarator, up until the comma or semicolon.
3252 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003253 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003254 }
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003256 // If an identifier is present, consume and remember it.
3257 IdentifierInfo *Name = 0;
3258 SourceLocation NameLoc;
3259 if (Tok.is(tok::identifier)) {
3260 Name = Tok.getIdentifierInfo();
3261 NameLoc = ConsumeToken();
3262 }
Mike Stump1eb44332009-09-09 15:08:12 +00003263
Richard Smithbdad7a22012-01-10 01:33:14 +00003264 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003265 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3266 // declaration of a scoped enumeration.
3267 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003268 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003269 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003270 }
3271
John McCall13489672012-05-07 06:16:58 +00003272 // Okay, end the suppression area. We'll decide whether to emit the
3273 // diagnostics in a second.
3274 if (shouldDelayDiagsInTag)
3275 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003276
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003277 TypeResult BaseType;
3278
Douglas Gregora61b3e72010-12-01 17:42:47 +00003279 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003280 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003281 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003282 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003283 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003284 // If we're in class scope, this can either be an enum declaration with
3285 // an underlying type, or a declaration of a bitfield member. We try to
3286 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003287 // (integer literal, sizeof); if it's still ambiguous, we then consider
3288 // anything that's a simple-type-specifier followed by '(' as an
3289 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003290 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003291 EnterExpressionEvaluationContext Unevaluated(Actions,
3292 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003293 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003294 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003295 // bit-field. This is the common case.
3296 if (TPR == TPResult::True())
3297 PossibleBitfield = true;
3298 // If the next token starts a type-specifier-seq, it may be either a
3299 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003300 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003301 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003302 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003303 GetLookAheadToken(2).getKind() == tok::semi) {
3304 // Consume the ':'.
3305 ConsumeToken();
3306 } else {
3307 // We have the start of a type-specifier-seq, so we have to perform
3308 // tentative parsing to determine whether we have an expression or a
3309 // type.
3310 TentativeParsingAction TPA(*this);
3311
3312 // Consume the ':'.
3313 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003314
3315 // If we see a type specifier followed by an open-brace, we have an
3316 // ambiguity between an underlying type and a C++11 braced
3317 // function-style cast. Resolve this by always treating it as an
3318 // underlying type.
3319 // FIXME: The standard is not entirely clear on how to disambiguate in
3320 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003321 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003322 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003323 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003324 // We'll parse this as a bitfield later.
3325 PossibleBitfield = true;
3326 TPA.Revert();
3327 } else {
3328 // We have a type-specifier-seq.
3329 TPA.Commit();
3330 }
3331 }
3332 } else {
3333 // Consume the ':'.
3334 ConsumeToken();
3335 }
3336
3337 if (!PossibleBitfield) {
3338 SourceRange Range;
3339 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003340
Richard Smith80ad52f2013-01-02 11:42:31 +00003341 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003342 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003343 } else if (!getLangOpts().ObjC2) {
3344 if (getLangOpts().CPlusPlus)
3345 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3346 else
3347 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3348 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003349 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003350 }
3351
Richard Smithbdad7a22012-01-10 01:33:14 +00003352 // There are four options here. If we have 'friend enum foo;' then this is a
3353 // friend declaration, and cannot have an accompanying definition. If we have
3354 // 'enum foo;', then this is a forward declaration. If we have
3355 // 'enum foo {...' then this is a definition. Otherwise we have something
3356 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003357 //
3358 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3359 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3360 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3361 //
John McCallf312b1e2010-08-26 23:41:50 +00003362 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003363 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003364 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003365 } else if (Tok.is(tok::l_brace)) {
3366 if (DS.isFriendSpecified()) {
3367 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3368 << SourceRange(DS.getFriendSpecLoc());
3369 ConsumeBrace();
3370 SkipUntil(tok::r_brace);
3371 TUK = Sema::TUK_Friend;
3372 } else {
3373 TUK = Sema::TUK_Definition;
3374 }
Richard Smithc9f35172012-06-25 21:37:02 +00003375 } else if (DSC != DSC_type_specifier &&
3376 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003377 (Tok.isAtStartOfLine() &&
3378 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003379 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3380 if (Tok.isNot(tok::semi)) {
3381 // A semicolon was missing after this declaration. Diagnose and recover.
3382 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3383 "enum");
3384 PP.EnterToken(Tok);
3385 Tok.setKind(tok::semi);
3386 }
John McCall13489672012-05-07 06:16:58 +00003387 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003388 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003389 }
3390
3391 // If this is an elaborated type specifier, and we delayed
3392 // diagnostics before, just merge them into the current pool.
3393 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3394 diagsFromTag.redelay();
3395 }
Richard Smith1af83c42012-03-23 03:33:32 +00003396
3397 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003398 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003399 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003400 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003401 // Skip the rest of this declarator, up until the comma or semicolon.
3402 Diag(Tok, diag::err_enum_template);
3403 SkipUntil(tok::comma, true);
3404 return;
3405 }
3406
3407 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3408 // Enumerations can't be explicitly instantiated.
3409 DS.SetTypeSpecError();
3410 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3411 return;
3412 }
3413
3414 assert(TemplateInfo.TemplateParams && "no template parameters");
3415 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3416 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003417 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003418
Sean Hunt2edf0a22012-06-23 05:07:58 +00003419 if (TUK == Sema::TUK_Reference)
3420 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003421
Douglas Gregorb9075602011-02-22 02:55:24 +00003422 if (!Name && TUK != Sema::TUK_Definition) {
3423 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003424
Douglas Gregorb9075602011-02-22 02:55:24 +00003425 // Skip the rest of this declarator, up until the comma or semicolon.
3426 SkipUntil(tok::comma, true);
3427 return;
3428 }
Richard Smith1af83c42012-03-23 03:33:32 +00003429
Douglas Gregor402abb52009-05-28 23:31:59 +00003430 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003431 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003432 const char *PrevSpec = 0;
3433 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003434 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003435 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003436 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003437 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003438 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003439
Douglas Gregor48c89f42010-04-24 16:38:41 +00003440 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003441 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003442 // dependent tag.
3443 if (!Name) {
3444 DS.SetTypeSpecError();
3445 Diag(Tok, diag::err_expected_type_name_after_typename);
3446 return;
3447 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003448
Douglas Gregor23c94db2010-07-02 17:43:08 +00003449 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003450 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003451 NameLoc);
3452 if (Type.isInvalid()) {
3453 DS.SetTypeSpecError();
3454 return;
3455 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003456
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003457 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3458 NameLoc.isValid() ? NameLoc : StartLoc,
3459 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003460 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003461
Douglas Gregor48c89f42010-04-24 16:38:41 +00003462 return;
3463 }
Mike Stump1eb44332009-09-09 15:08:12 +00003464
John McCalld226f652010-08-21 09:40:31 +00003465 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003466 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003467 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003468 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003469 ConsumeBrace();
3470 SkipUntil(tok::r_brace);
3471 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003472
Douglas Gregor48c89f42010-04-24 16:38:41 +00003473 DS.SetTypeSpecError();
3474 return;
3475 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003476
Richard Smithc9f35172012-06-25 21:37:02 +00003477 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003478 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003480 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3481 NameLoc.isValid() ? NameLoc : StartLoc,
3482 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003483 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003484}
3485
3486/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3487/// enumerator-list:
3488/// enumerator
3489/// enumerator-list ',' enumerator
3490/// enumerator:
3491/// enumeration-constant
3492/// enumeration-constant '=' constant-expression
3493/// enumeration-constant:
3494/// identifier
3495///
John McCalld226f652010-08-21 09:40:31 +00003496void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003497 // Enter the scope of the enum body and start the definition.
3498 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003499 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003500
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003501 BalancedDelimiterTracker T(*this, tok::l_brace);
3502 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003503
Chris Lattner7946dd32007-08-27 17:24:30 +00003504 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003505 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003506 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003507
Chris Lattner5f9e2722011-07-23 10:55:15 +00003508 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003509
John McCalld226f652010-08-21 09:40:31 +00003510 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003511
Reid Spencer5f016e22007-07-11 17:01:13 +00003512 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003513 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003514 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3515 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003516
John McCall5b629aa2010-10-22 23:36:17 +00003517 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003518 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003519 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003520 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003521 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003522
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003524 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003525 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003526
Chris Lattner04d66662007-10-09 17:33:22 +00003527 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003528 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003529 AssignedVal = ParseConstantExpression();
3530 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003532 }
Mike Stump1eb44332009-09-09 15:08:12 +00003533
Reid Spencer5f016e22007-07-11 17:01:13 +00003534 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003535 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3536 LastEnumConstDecl,
3537 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003538 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003539 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003540 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003541
Reid Spencer5f016e22007-07-11 17:01:13 +00003542 EnumConstantDecls.push_back(EnumConstDecl);
3543 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003544
Douglas Gregor751f6922010-09-07 14:51:08 +00003545 if (Tok.is(tok::identifier)) {
3546 // We're missing a comma between enumerators.
3547 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003548 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003549 << FixItHint::CreateInsertion(Loc, ", ");
3550 continue;
3551 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003552
Chris Lattner04d66662007-10-09 17:33:22 +00003553 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003554 break;
3555 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003556
Richard Smith7fe62082011-10-15 05:09:34 +00003557 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003558 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003559 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3560 diag::ext_enumerator_list_comma_cxx :
3561 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003562 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003563 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003564 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3565 << FixItHint::CreateRemoval(CommaLoc);
3566 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003567 }
Mike Stump1eb44332009-09-09 15:08:12 +00003568
Reid Spencer5f016e22007-07-11 17:01:13 +00003569 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003570 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003571
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003573 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003574 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003575
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003576 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3577 EnumDecl, EnumConstantDecls.data(),
3578 EnumConstantDecls.size(), getCurScope(),
3579 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Douglas Gregor72de6672009-01-08 20:45:30 +00003581 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003582 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3583 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003584
3585 // The next token must be valid after an enum definition. If not, a ';'
3586 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003587 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3588 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003589 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3590 // Push this token back into the preprocessor and change our current token
3591 // to ';' so that the rest of the code recovers as though there were an
3592 // ';' after the definition.
3593 PP.EnterToken(Tok);
3594 Tok.setKind(tok::semi);
3595 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003596}
3597
3598/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003599/// start of a type-qualifier-list.
3600bool Parser::isTypeQualifier() const {
3601 switch (Tok.getKind()) {
3602 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003603
3604 // type-qualifier only in OpenCL
3605 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003606 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003607
Steve Naroff5f8aa692008-02-11 23:15:56 +00003608 // type-qualifier
3609 case tok::kw_const:
3610 case tok::kw_volatile:
3611 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003612 case tok::kw___private:
3613 case tok::kw___local:
3614 case tok::kw___global:
3615 case tok::kw___constant:
3616 case tok::kw___read_only:
3617 case tok::kw___read_write:
3618 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003619 return true;
3620 }
3621}
3622
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003623/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3624/// is definitely a type-specifier. Return false if it isn't part of a type
3625/// specifier or if we're not sure.
3626bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3627 switch (Tok.getKind()) {
3628 default: return false;
3629 // type-specifiers
3630 case tok::kw_short:
3631 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003632 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003633 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003634 case tok::kw_signed:
3635 case tok::kw_unsigned:
3636 case tok::kw__Complex:
3637 case tok::kw__Imaginary:
3638 case tok::kw_void:
3639 case tok::kw_char:
3640 case tok::kw_wchar_t:
3641 case tok::kw_char16_t:
3642 case tok::kw_char32_t:
3643 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003644 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003645 case tok::kw_float:
3646 case tok::kw_double:
3647 case tok::kw_bool:
3648 case tok::kw__Bool:
3649 case tok::kw__Decimal32:
3650 case tok::kw__Decimal64:
3651 case tok::kw__Decimal128:
3652 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003653
Guy Benyeib13621d2012-12-18 14:38:23 +00003654 // OpenCL specific types:
3655 case tok::kw_image1d_t:
3656 case tok::kw_image1d_array_t:
3657 case tok::kw_image1d_buffer_t:
3658 case tok::kw_image2d_t:
3659 case tok::kw_image2d_array_t:
3660 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003661 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003662 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003663
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003664 // struct-or-union-specifier (C99) or class-specifier (C++)
3665 case tok::kw_class:
3666 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003667 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003668 case tok::kw_union:
3669 // enum-specifier
3670 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003671
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003672 // typedef-name
3673 case tok::annot_typename:
3674 return true;
3675 }
3676}
3677
Steve Naroff5f8aa692008-02-11 23:15:56 +00003678/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003679/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003680bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003681 switch (Tok.getKind()) {
3682 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003683
Chris Lattner166a8fc2009-01-04 23:41:41 +00003684 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003685 if (TryAltiVecVectorToken())
3686 return true;
3687 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003688 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003689 // Annotate typenames and C++ scope specifiers. If we get one, just
3690 // recurse to handle whatever we get.
3691 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003692 return true;
3693 if (Tok.is(tok::identifier))
3694 return false;
3695 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003696
Chris Lattner166a8fc2009-01-04 23:41:41 +00003697 case tok::coloncolon: // ::foo::bar
3698 if (NextToken().is(tok::kw_new) || // ::new
3699 NextToken().is(tok::kw_delete)) // ::delete
3700 return false;
3701
Chris Lattner166a8fc2009-01-04 23:41:41 +00003702 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003703 return true;
3704 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003705
Reid Spencer5f016e22007-07-11 17:01:13 +00003706 // GNU attributes support.
3707 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003708 // GNU typeof support.
3709 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003710
Reid Spencer5f016e22007-07-11 17:01:13 +00003711 // type-specifiers
3712 case tok::kw_short:
3713 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003714 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003715 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 case tok::kw_signed:
3717 case tok::kw_unsigned:
3718 case tok::kw__Complex:
3719 case tok::kw__Imaginary:
3720 case tok::kw_void:
3721 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003722 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003723 case tok::kw_char16_t:
3724 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003726 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003727 case tok::kw_float:
3728 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003729 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 case tok::kw__Bool:
3731 case tok::kw__Decimal32:
3732 case tok::kw__Decimal64:
3733 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003734 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003735
Guy Benyeib13621d2012-12-18 14:38:23 +00003736 // OpenCL specific types:
3737 case tok::kw_image1d_t:
3738 case tok::kw_image1d_array_t:
3739 case tok::kw_image1d_buffer_t:
3740 case tok::kw_image2d_t:
3741 case tok::kw_image2d_array_t:
3742 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003743 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003744 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003745
Chris Lattner99dc9142008-04-13 18:59:07 +00003746 // struct-or-union-specifier (C99) or class-specifier (C++)
3747 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003748 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003749 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003750 case tok::kw_union:
3751 // enum-specifier
3752 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003753
Reid Spencer5f016e22007-07-11 17:01:13 +00003754 // type-qualifier
3755 case tok::kw_const:
3756 case tok::kw_volatile:
3757 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003758
John McCallb8a8de32012-11-14 00:49:39 +00003759 // Debugger support.
3760 case tok::kw___unknown_anytype:
3761
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003762 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003763 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003764 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Chris Lattner7c186be2008-10-20 00:25:30 +00003766 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3767 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003768 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003769
Steve Naroff239f0732008-12-25 14:16:32 +00003770 case tok::kw___cdecl:
3771 case tok::kw___stdcall:
3772 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003773 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003774 case tok::kw___w64:
3775 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003776 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003777 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003778 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003779
3780 case tok::kw___private:
3781 case tok::kw___local:
3782 case tok::kw___global:
3783 case tok::kw___constant:
3784 case tok::kw___read_only:
3785 case tok::kw___read_write:
3786 case tok::kw___write_only:
3787
Eli Friedman290eeb02009-06-08 23:27:34 +00003788 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003789
3790 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003791 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003792
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003793 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003794 case tok::kw__Atomic:
3795 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003796 }
3797}
3798
3799/// isDeclarationSpecifier() - Return true if the current token is part of a
3800/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003801///
3802/// \param DisambiguatingWithExpression True to indicate that the purpose of
3803/// this check is to disambiguate between an expression and a declaration.
3804bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003805 switch (Tok.getKind()) {
3806 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003807
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003808 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003809 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003810
Chris Lattner166a8fc2009-01-04 23:41:41 +00003811 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003812 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003813 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003814 return false;
John Thompson82287d12010-02-05 00:12:22 +00003815 if (TryAltiVecVectorToken())
3816 return true;
3817 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003818 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003819 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003820 // Annotate typenames and C++ scope specifiers. If we get one, just
3821 // recurse to handle whatever we get.
3822 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003823 return true;
3824 if (Tok.is(tok::identifier))
3825 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003826
Douglas Gregor9497a732010-09-16 01:51:54 +00003827 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003828 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003829 // expression is permitted, then this is probably a class message send
3830 // missing the initial '['. In this case, we won't consider this to be
3831 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003832 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003833 isStartOfObjCClassMessageMissingOpenBracket())
3834 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003835
John McCall9ba61662010-02-26 08:45:28 +00003836 return isDeclarationSpecifier();
3837
Chris Lattner166a8fc2009-01-04 23:41:41 +00003838 case tok::coloncolon: // ::foo::bar
3839 if (NextToken().is(tok::kw_new) || // ::new
3840 NextToken().is(tok::kw_delete)) // ::delete
3841 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003842
Chris Lattner166a8fc2009-01-04 23:41:41 +00003843 // Annotate typenames and C++ scope specifiers. If we get one, just
3844 // recurse to handle whatever we get.
3845 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003846 return true;
3847 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003848
Reid Spencer5f016e22007-07-11 17:01:13 +00003849 // storage-class-specifier
3850 case tok::kw_typedef:
3851 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003852 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003853 case tok::kw_static:
3854 case tok::kw_auto:
3855 case tok::kw_register:
3856 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003857
Douglas Gregor8d267c52011-09-09 02:06:17 +00003858 // Modules
3859 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003860
John McCallb8a8de32012-11-14 00:49:39 +00003861 // Debugger support
3862 case tok::kw___unknown_anytype:
3863
Reid Spencer5f016e22007-07-11 17:01:13 +00003864 // type-specifiers
3865 case tok::kw_short:
3866 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003867 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003868 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003869 case tok::kw_signed:
3870 case tok::kw_unsigned:
3871 case tok::kw__Complex:
3872 case tok::kw__Imaginary:
3873 case tok::kw_void:
3874 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003875 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003876 case tok::kw_char16_t:
3877 case tok::kw_char32_t:
3878
Reid Spencer5f016e22007-07-11 17:01:13 +00003879 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003880 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003881 case tok::kw_float:
3882 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003883 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 case tok::kw__Bool:
3885 case tok::kw__Decimal32:
3886 case tok::kw__Decimal64:
3887 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003888 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003889
Guy Benyeib13621d2012-12-18 14:38:23 +00003890 // OpenCL specific types:
3891 case tok::kw_image1d_t:
3892 case tok::kw_image1d_array_t:
3893 case tok::kw_image1d_buffer_t:
3894 case tok::kw_image2d_t:
3895 case tok::kw_image2d_array_t:
3896 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003897 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003898 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003899
Chris Lattner99dc9142008-04-13 18:59:07 +00003900 // struct-or-union-specifier (C99) or class-specifier (C++)
3901 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 case tok::kw_struct:
3903 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003904 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003905 // enum-specifier
3906 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003907
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 // type-qualifier
3909 case tok::kw_const:
3910 case tok::kw_volatile:
3911 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003912
Reid Spencer5f016e22007-07-11 17:01:13 +00003913 // function-specifier
3914 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003915 case tok::kw_virtual:
3916 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003917 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003918
Richard Smith4cd81c52013-01-29 09:02:09 +00003919 // alignment-specifier
3920 case tok::kw__Alignas:
3921
Richard Smith53aec2a2012-10-25 00:00:53 +00003922 // friend keyword.
3923 case tok::kw_friend:
3924
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003925 // static_assert-declaration
3926 case tok::kw__Static_assert:
3927
Chris Lattner1ef08762007-08-09 17:01:07 +00003928 // GNU typeof support.
3929 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003930
Chris Lattner1ef08762007-08-09 17:01:07 +00003931 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003932 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003933
Richard Smith53aec2a2012-10-25 00:00:53 +00003934 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003935 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003936 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003937
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003938 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003939 case tok::kw__Atomic:
3940 return true;
3941
Chris Lattnerf3948c42008-07-26 03:38:44 +00003942 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3943 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003944 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003945
Douglas Gregord9d75e52011-04-27 05:41:15 +00003946 // typedef-name
3947 case tok::annot_typename:
3948 return !DisambiguatingWithExpression ||
3949 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003950
Steve Naroff47f52092009-01-06 19:34:12 +00003951 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003952 case tok::kw___cdecl:
3953 case tok::kw___stdcall:
3954 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003955 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003956 case tok::kw___w64:
3957 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003958 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003959 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003960 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003961 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003962
3963 case tok::kw___private:
3964 case tok::kw___local:
3965 case tok::kw___global:
3966 case tok::kw___constant:
3967 case tok::kw___read_only:
3968 case tok::kw___read_write:
3969 case tok::kw___write_only:
3970
Eli Friedman290eeb02009-06-08 23:27:34 +00003971 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003972 }
3973}
3974
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003975bool Parser::isConstructorDeclarator() {
3976 TentativeParsingAction TPA(*this);
3977
3978 // Parse the C++ scope specifier.
3979 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003980 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003981 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003982 TPA.Revert();
3983 return false;
3984 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003985
3986 // Parse the constructor name.
3987 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3988 // We already know that we have a constructor name; just consume
3989 // the token.
3990 ConsumeToken();
3991 } else {
3992 TPA.Revert();
3993 return false;
3994 }
3995
Richard Smith22592862012-03-27 23:05:05 +00003996 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003997 if (Tok.isNot(tok::l_paren)) {
3998 TPA.Revert();
3999 return false;
4000 }
4001 ConsumeParen();
4002
Richard Smith22592862012-03-27 23:05:05 +00004003 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4004 // that we have a constructor.
4005 if (Tok.is(tok::r_paren) ||
4006 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004007 TPA.Revert();
4008 return true;
4009 }
4010
4011 // If we need to, enter the specified scope.
4012 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004013 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004014 DeclScopeObj.EnterDeclaratorScope();
4015
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004016 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004017 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004018 MaybeParseMicrosoftAttributes(Attrs);
4019
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004020 // Check whether the next token(s) are part of a declaration
4021 // specifier, in which case we have the start of a parameter and,
4022 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004023 bool IsConstructor = false;
4024 if (isDeclarationSpecifier())
4025 IsConstructor = true;
4026 else if (Tok.is(tok::identifier) ||
4027 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4028 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4029 // This might be a parenthesized member name, but is more likely to
4030 // be a constructor declaration with an invalid argument type. Keep
4031 // looking.
4032 if (Tok.is(tok::annot_cxxscope))
4033 ConsumeToken();
4034 ConsumeToken();
4035
4036 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004037 // which must have one of the following syntactic forms (see the
4038 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004039 switch (Tok.getKind()) {
4040 case tok::l_paren:
4041 // C(X ( int));
4042 case tok::l_square:
4043 // C(X [ 5]);
4044 // C(X [ [attribute]]);
4045 case tok::coloncolon:
4046 // C(X :: Y);
4047 // C(X :: *p);
4048 case tok::r_paren:
4049 // C(X )
4050 // Assume this isn't a constructor, rather than assuming it's a
4051 // constructor with an unnamed parameter of an ill-formed type.
4052 break;
4053
4054 default:
4055 IsConstructor = true;
4056 break;
4057 }
4058 }
4059
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004060 TPA.Revert();
4061 return IsConstructor;
4062}
Reid Spencer5f016e22007-07-11 17:01:13 +00004063
4064/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004065/// type-qualifier-list: [C99 6.7.5]
4066/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004067/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004068/// [ only if VendorAttributesAllowed=true ]
4069/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004070/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004071/// [ only if VendorAttributesAllowed=true ]
4072/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004073/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004074/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004075///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004076void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4077 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004078 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004079 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004080 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004081 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004082 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004083 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004084 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004085
4086 SourceLocation EndLoc;
4087
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004089 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004091 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004092 SourceLocation Loc = Tok.getLocation();
4093
4094 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004095 case tok::code_completion:
4096 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004097 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004098
Reid Spencer5f016e22007-07-11 17:01:13 +00004099 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004100 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004101 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004102 break;
4103 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004104 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004105 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004106 break;
4107 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004108 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004109 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004110 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004111
4112 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004113 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004114 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004115 goto DoneWithTypeQuals;
4116 case tok::kw___private:
4117 case tok::kw___global:
4118 case tok::kw___local:
4119 case tok::kw___constant:
4120 case tok::kw___read_only:
4121 case tok::kw___write_only:
4122 case tok::kw___read_write:
4123 ParseOpenCLQualifiers(DS);
4124 break;
4125
Eli Friedman290eeb02009-06-08 23:27:34 +00004126 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004127 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004128 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004129 case tok::kw___cdecl:
4130 case tok::kw___stdcall:
4131 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004132 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004133 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004134 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004135 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004136 continue;
4137 }
4138 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004139 case tok::kw___pascal:
4140 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004141 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004142 continue;
4143 }
4144 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004145 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004146 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004147 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004148 continue; // do *not* consume the next token!
4149 }
4150 // otherwise, FALL THROUGH!
4151 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004152 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004153 // If this is not a type-qualifier token, we're done reading type
4154 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004155 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004156 if (EndLoc.isValid())
4157 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004158 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004159 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004160
Reid Spencer5f016e22007-07-11 17:01:13 +00004161 // If the specifier combination wasn't legal, issue a diagnostic.
4162 if (isInvalid) {
4163 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004164 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004165 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004166 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004167 }
4168}
4169
4170
4171/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4172///
4173void Parser::ParseDeclarator(Declarator &D) {
4174 /// This implements the 'declarator' production in the C grammar, then checks
4175 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004176 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004177}
4178
Richard Smith9988f282012-03-29 01:16:42 +00004179static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4180 if (Kind == tok::star || Kind == tok::caret)
4181 return true;
4182
4183 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4184 if (!Lang.CPlusPlus)
4185 return false;
4186
4187 return Kind == tok::amp || Kind == tok::ampamp;
4188}
4189
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004190/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4191/// is parsed by the function passed to it. Pass null, and the direct-declarator
4192/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004193/// ptr-operator production.
4194///
Richard Smith0706df42011-10-19 21:33:05 +00004195/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004196/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4197/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004198///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004199/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4200/// [C] pointer[opt] direct-declarator
4201/// [C++] direct-declarator
4202/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004203///
4204/// pointer: [C99 6.7.5]
4205/// '*' type-qualifier-list[opt]
4206/// '*' type-qualifier-list[opt] pointer
4207///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004208/// ptr-operator:
4209/// '*' cv-qualifier-seq[opt]
4210/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004211/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004212/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004213/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004214/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004215void Parser::ParseDeclaratorInternal(Declarator &D,
4216 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004217 if (Diags.hasAllExtensionsSilenced())
4218 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004219
Sebastian Redlf30208a2009-01-24 21:16:55 +00004220 // C++ member pointers start with a '::' or a nested-name.
4221 // Member pointers get special handling, since there's no place for the
4222 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004223 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004224 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4225 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004226 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4227 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004228 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004229 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004230
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004231 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004232 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004233 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004234 if (D.mayHaveIdentifier())
4235 D.getCXXScopeSpec() = SS;
4236 else
4237 AnnotateScopeToken(SS, true);
4238
Sebastian Redlf30208a2009-01-24 21:16:55 +00004239 if (DirectDeclParser)
4240 (this->*DirectDeclParser)(D);
4241 return;
4242 }
4243
4244 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004245 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004246 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004247 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004248 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004249
4250 // Recurse to parse whatever is left.
4251 ParseDeclaratorInternal(D, DirectDeclParser);
4252
4253 // Sema will have to catch (syntactically invalid) pointers into global
4254 // scope. It has to catch pointers into namespace scope anyway.
4255 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004256 Loc),
4257 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004258 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004259 return;
4260 }
4261 }
4262
4263 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004264 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004265 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004266 if (DirectDeclParser)
4267 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004268 return;
4269 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004270
Sebastian Redl05532f22009-03-15 22:02:01 +00004271 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4272 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004273 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004274 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004275
Chris Lattner9af55002009-03-27 04:18:06 +00004276 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004277 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004278 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004279
Richard Smith6ee326a2012-04-10 01:32:12 +00004280 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004281 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004282 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004283
Reid Spencer5f016e22007-07-11 17:01:13 +00004284 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004285 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004286 if (Kind == tok::star)
4287 // Remember that we parsed a pointer type, and remember the type-quals.
4288 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004289 DS.getConstSpecLoc(),
4290 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004291 DS.getRestrictSpecLoc()),
4292 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004293 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004294 else
4295 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004296 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004297 Loc),
4298 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004299 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004300 } else {
4301 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004302 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004303
Sebastian Redl743de1f2009-03-23 00:00:23 +00004304 // Complain about rvalue references in C++03, but then go on and build
4305 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004306 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004307 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004308 diag::warn_cxx98_compat_rvalue_reference :
4309 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004310
Richard Smith6ee326a2012-04-10 01:32:12 +00004311 // GNU-style and C++11 attributes are allowed here, as is restrict.
4312 ParseTypeQualifierListOpt(DS);
4313 D.ExtendWithDeclSpec(DS);
4314
Reid Spencer5f016e22007-07-11 17:01:13 +00004315 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4316 // cv-qualifiers are introduced through the use of a typedef or of a
4317 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004318 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4319 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4320 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004321 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004322 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4323 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004324 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004325 }
4326
4327 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004328 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004329
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004330 if (D.getNumTypeObjects() > 0) {
4331 // C++ [dcl.ref]p4: There shall be no references to references.
4332 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4333 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004334 if (const IdentifierInfo *II = D.getIdentifier())
4335 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4336 << II;
4337 else
4338 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4339 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004340
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004341 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004342 // can go ahead and build the (technically ill-formed)
4343 // declarator: reference collapsing will take care of it.
4344 }
4345 }
4346
Reid Spencer5f016e22007-07-11 17:01:13 +00004347 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004348 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004349 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004350 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004351 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004352 }
4353}
4354
Richard Smith9988f282012-03-29 01:16:42 +00004355static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4356 SourceLocation EllipsisLoc) {
4357 if (EllipsisLoc.isValid()) {
4358 FixItHint Insertion;
4359 if (!D.getEllipsisLoc().isValid()) {
4360 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4361 D.setEllipsisLoc(EllipsisLoc);
4362 }
4363 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4364 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4365 }
4366}
4367
Reid Spencer5f016e22007-07-11 17:01:13 +00004368/// ParseDirectDeclarator
4369/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004370/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004371/// '(' declarator ')'
4372/// [GNU] '(' attributes declarator ')'
4373/// [C90] direct-declarator '[' constant-expression[opt] ']'
4374/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4375/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4376/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4377/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004378/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4379/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004380/// direct-declarator '(' parameter-type-list ')'
4381/// direct-declarator '(' identifier-list[opt] ')'
4382/// [GNU] direct-declarator '(' parameter-forward-declarations
4383/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004384/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4385/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004386/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4387/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4388/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004389/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004390/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004391///
4392/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004393/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004394/// '::'[opt] nested-name-specifier[opt] type-name
4395///
4396/// id-expression: [C++ 5.1]
4397/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004398/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004399///
4400/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004401/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004402/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004403/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004404/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004405/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004406///
Richard Smith5d8388c2012-03-27 01:42:32 +00004407/// Note, any additional constructs added here may need corresponding changes
4408/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004409void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004410 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004411
David Blaikie4e4d0842012-03-11 07:00:24 +00004412 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004413 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004414 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004415 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4416 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004417 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004418 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004419 }
4420
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004421 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004422 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004423 // Change the declaration context for name lookup, until this function
4424 // is exited (and the declarator has been parsed).
4425 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004426 }
4427
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004428 // C++0x [dcl.fct]p14:
4429 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004430 // of a parameter-declaration-clause without a preceding comma. In
4431 // this case, the ellipsis is parsed as part of the
4432 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004433 // parameter pack that has not been expanded; otherwise, it is parsed
4434 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004435 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004436 !((D.getContext() == Declarator::PrototypeContext ||
4437 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004438 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004439 !Actions.containsUnexpandedParameterPacks(D))) {
4440 SourceLocation EllipsisLoc = ConsumeToken();
4441 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4442 // The ellipsis was put in the wrong place. Recover, and explain to
4443 // the user what they should have done.
4444 ParseDeclarator(D);
4445 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4446 return;
4447 } else
4448 D.setEllipsisLoc(EllipsisLoc);
4449
4450 // The ellipsis can't be followed by a parenthesized declarator. We
4451 // check for that in ParseParenDeclarator, after we have disambiguated
4452 // the l_paren token.
4453 }
4454
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004455 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4456 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4457 // We found something that indicates the start of an unqualified-id.
4458 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004459 bool AllowConstructorName;
4460 if (D.getDeclSpec().hasTypeSpecifier())
4461 AllowConstructorName = false;
4462 else if (D.getCXXScopeSpec().isSet())
4463 AllowConstructorName =
4464 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004465 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004466 else
4467 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4468
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004469 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004470 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4471 /*EnteringContext=*/true,
4472 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004473 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004474 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004475 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004476 D.getName()) ||
4477 // Once we're past the identifier, if the scope was bad, mark the
4478 // whole declarator bad.
4479 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004480 D.SetIdentifier(0, Tok.getLocation());
4481 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004482 } else {
4483 // Parsed the unqualified-id; update range information and move along.
4484 if (D.getSourceRange().getBegin().isInvalid())
4485 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4486 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004487 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004488 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004489 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004490 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004491 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004492 "There's a C++-specific check for tok::identifier above");
4493 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4494 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4495 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004496 goto PastIdentifier;
4497 }
Richard Smith9988f282012-03-29 01:16:42 +00004498
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004499 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004500 // direct-declarator: '(' declarator ')'
4501 // direct-declarator: '(' attributes declarator ')'
4502 // Example: 'char (*X)' or 'int (*XX)(void)'
4503 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004504
4505 // If the declarator was parenthesized, we entered the declarator
4506 // scope when parsing the parenthesized declarator, then exited
4507 // the scope already. Re-enter the scope, if we need to.
4508 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004509 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004510 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004511 if (!D.isInvalidType() &&
4512 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004513 // Change the declaration context for name lookup, until this function
4514 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004515 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004516 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004517 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004518 // This could be something simple like "int" (in which case the declarator
4519 // portion is empty), if an abstract-declarator is allowed.
4520 D.SetIdentifier(0, Tok.getLocation());
4521 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004522 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004523 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004524 if (D.getContext() == Declarator::MemberContext)
4525 Diag(Tok, diag::err_expected_member_name_or_semi)
4526 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004527 else if (getLangOpts().CPlusPlus) {
4528 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4529 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4530 else
4531 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4532 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004533 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004534 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004535 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004536 }
Mike Stump1eb44332009-09-09 15:08:12 +00004537
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004538 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004539 assert(D.isPastIdentifier() &&
4540 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004541
Richard Smith6ee326a2012-04-10 01:32:12 +00004542 // Don't parse attributes unless we have parsed an unparenthesized name.
4543 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004544 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004545
Reid Spencer5f016e22007-07-11 17:01:13 +00004546 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004547 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004548 // Enter function-declaration scope, limiting any declarators to the
4549 // function prototype scope, including parameter declarators.
4550 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004551 Scope::FunctionPrototypeScope|Scope::DeclScope|
4552 (D.isFunctionDeclaratorAFunctionDeclaration()
4553 ? Scope::FunctionDeclarationScope : 0));
4554
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004555 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4556 // In such a case, check if we actually have a function declarator; if it
4557 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004558 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004559 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4560 // The name of the declarator, if any, is tentatively declared within
4561 // a possible direct initializer.
4562 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4563 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4564 TentativelyDeclaredIdentifiers.pop_back();
4565 if (!IsFunctionDecl)
4566 break;
4567 }
John McCall0b7e6782011-03-24 11:26:52 +00004568 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004569 BalancedDelimiterTracker T(*this, tok::l_paren);
4570 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004571 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004572 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004573 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004574 ParseBracketDeclarator(D);
4575 } else {
4576 break;
4577 }
4578 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004579}
Reid Spencer5f016e22007-07-11 17:01:13 +00004580
Chris Lattneref4715c2008-04-06 05:45:57 +00004581/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4582/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004583/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004584/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4585///
4586/// direct-declarator:
4587/// '(' declarator ')'
4588/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004589/// direct-declarator '(' parameter-type-list ')'
4590/// direct-declarator '(' identifier-list[opt] ')'
4591/// [GNU] direct-declarator '(' parameter-forward-declarations
4592/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004593///
4594void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004595 BalancedDelimiterTracker T(*this, tok::l_paren);
4596 T.consumeOpen();
4597
Chris Lattneref4715c2008-04-06 05:45:57 +00004598 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004599
Chris Lattner7399ee02008-10-20 02:05:46 +00004600 // Eat any attributes before we look at whether this is a grouping or function
4601 // declarator paren. If this is a grouping paren, the attribute applies to
4602 // the type being built up, for example:
4603 // int (__attribute__(()) *x)(long y)
4604 // If this ends up not being a grouping paren, the attribute applies to the
4605 // first argument, for example:
4606 // int (__attribute__(()) int x)
4607 // In either case, we need to eat any attributes to be able to determine what
4608 // sort of paren this is.
4609 //
John McCall0b7e6782011-03-24 11:26:52 +00004610 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004611 bool RequiresArg = false;
4612 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004613 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004614
Chris Lattner7399ee02008-10-20 02:05:46 +00004615 // We require that the argument list (if this is a non-grouping paren) be
4616 // present even if the attribute list was empty.
4617 RequiresArg = true;
4618 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004619
Steve Naroff239f0732008-12-25 14:16:32 +00004620 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004621 ParseMicrosoftTypeAttributes(attrs);
4622
Dawn Perchik52fc3142010-09-03 01:29:35 +00004623 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004624 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004625 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004626
Chris Lattneref4715c2008-04-06 05:45:57 +00004627 // If we haven't past the identifier yet (or where the identifier would be
4628 // stored, if this is an abstract declarator), then this is probably just
4629 // grouping parens. However, if this could be an abstract-declarator, then
4630 // this could also be the start of function arguments (consider 'void()').
4631 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004632
Chris Lattneref4715c2008-04-06 05:45:57 +00004633 if (!D.mayOmitIdentifier()) {
4634 // If this can't be an abstract-declarator, this *must* be a grouping
4635 // paren, because we haven't seen the identifier yet.
4636 isGrouping = true;
4637 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004638 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4639 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004640 isDeclarationSpecifier() || // 'int(int)' is a function.
4641 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004642 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4643 // considered to be a type, not a K&R identifier-list.
4644 isGrouping = false;
4645 } else {
4646 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4647 isGrouping = true;
4648 }
Mike Stump1eb44332009-09-09 15:08:12 +00004649
Chris Lattneref4715c2008-04-06 05:45:57 +00004650 // If this is a grouping paren, handle:
4651 // direct-declarator: '(' declarator ')'
4652 // direct-declarator: '(' attributes declarator ')'
4653 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004654 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4655 D.setEllipsisLoc(SourceLocation());
4656
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004657 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004658 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004659 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004660 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004661 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004662 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004663 T.getCloseLocation()),
4664 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004665
4666 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004667
4668 // An ellipsis cannot be placed outside parentheses.
4669 if (EllipsisLoc.isValid())
4670 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4671
Chris Lattneref4715c2008-04-06 05:45:57 +00004672 return;
4673 }
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Chris Lattneref4715c2008-04-06 05:45:57 +00004675 // Okay, if this wasn't a grouping paren, it must be the start of a function
4676 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004677 // identifier (and remember where it would have been), then call into
4678 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004679 D.SetIdentifier(0, Tok.getLocation());
4680
David Blaikie42d6d0c2011-12-04 05:04:18 +00004681 // Enter function-declaration scope, limiting any declarators to the
4682 // function prototype scope, including parameter declarators.
4683 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004684 Scope::FunctionPrototypeScope | Scope::DeclScope |
4685 (D.isFunctionDeclaratorAFunctionDeclaration()
4686 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004687 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004688 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004689}
4690
4691/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4692/// declarator D up to a paren, which indicates that we are parsing function
4693/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004694///
Richard Smith6ee326a2012-04-10 01:32:12 +00004695/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4696/// immediately after the open paren - they should be considered to be the
4697/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004698///
Richard Smith6ee326a2012-04-10 01:32:12 +00004699/// If RequiresArg is true, then the first argument of the function is required
4700/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004701///
Richard Smith6ee326a2012-04-10 01:32:12 +00004702/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4703/// (C++11) ref-qualifier[opt], exception-specification[opt],
4704/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4705///
4706/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004707/// dynamic-exception-specification
4708/// noexcept-specification
4709///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004710void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004711 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004712 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004713 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004714 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004715 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004716 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004717 // lparen is already consumed!
4718 assert(D.isPastIdentifier() && "Should not call before identifier!");
4719
4720 // This should be true when the function has typed arguments.
4721 // Otherwise, it is treated as a K&R-style function.
4722 bool HasProto = false;
4723 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004724 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004725 // Remember where we see an ellipsis, if any.
4726 SourceLocation EllipsisLoc;
4727
4728 DeclSpec DS(AttrFactory);
4729 bool RefQualifierIsLValueRef = true;
4730 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004731 SourceLocation ConstQualifierLoc;
4732 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004733 ExceptionSpecificationType ESpecType = EST_None;
4734 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004735 SmallVector<ParsedType, 2> DynamicExceptions;
4736 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004737 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004738 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004739 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004740
James Molloy16f1f712012-02-29 10:24:19 +00004741 Actions.ActOnStartFunctionDeclarator();
4742
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004743 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4744 EndLoc is the end location for the function declarator.
4745 They differ for trailing return types. */
4746 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004747 SourceLocation LParenLoc, RParenLoc;
4748 LParenLoc = Tracker.getOpenLocation();
4749 StartLoc = LParenLoc;
4750
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004751 if (isFunctionDeclaratorIdentifierList()) {
4752 if (RequiresArg)
4753 Diag(Tok, diag::err_argument_required_after_attribute);
4754
4755 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4756
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004757 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004758 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004759 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004760 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004761 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004762 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004763 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004764 else if (RequiresArg)
4765 Diag(Tok, diag::err_argument_required_after_attribute);
4766
David Blaikie4e4d0842012-03-11 07:00:24 +00004767 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004768
4769 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004770 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004771 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004772 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004773 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004774
David Blaikie4e4d0842012-03-11 07:00:24 +00004775 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004776 // FIXME: Accept these components in any order, and produce fixits to
4777 // correct the order if the user gets it wrong. Ideally we should deal
4778 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004779
4780 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004781 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4782 if (!DS.getSourceRange().getEnd().isInvalid()) {
4783 EndLoc = DS.getSourceRange().getEnd();
4784 ConstQualifierLoc = DS.getConstSpecLoc();
4785 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4786 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004787
4788 // Parse ref-qualifier[opt].
4789 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004790 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004791 diag::warn_cxx98_compat_ref_qualifier :
4792 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004793
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004794 RefQualifierIsLValueRef = Tok.is(tok::amp);
4795 RefQualifierLoc = ConsumeToken();
4796 EndLoc = RefQualifierLoc;
4797 }
4798
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004799 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004800 // If a declaration declares a member function or member function
4801 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004802 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004803 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004804 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004805 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004806 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004807 (D.getContext() == Declarator::MemberContext ||
4808 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004809 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004810 Actions.CurContext->isRecord()));
4811 Sema::CXXThisScopeRAII ThisScope(Actions,
4812 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004813 DS.getTypeQualifiers() |
4814 (D.getDeclSpec().isConstexprSpecified()
4815 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004816 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004817
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004818 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004819 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004820 DynamicExceptions,
4821 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004822 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004823 if (ESpecType != EST_None)
4824 EndLoc = ESpecRange.getEnd();
4825
Richard Smith6ee326a2012-04-10 01:32:12 +00004826 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4827 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004828 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004829
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004830 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004831 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004832 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004833 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004834 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4835 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004836 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004837 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004838 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004839 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004840 }
4841 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004842 }
4843
4844 // Remember that we parsed a function type, and remember the attributes.
4845 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004846 IsAmbiguous,
4847 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004848 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004849 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004850 DS.getTypeQualifiers(),
4851 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004852 RefQualifierLoc, ConstQualifierLoc,
4853 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004854 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004855 ESpecType, ESpecRange.getBegin(),
4856 DynamicExceptions.data(),
4857 DynamicExceptionRanges.data(),
4858 DynamicExceptions.size(),
4859 NoexceptExpr.isUsable() ?
4860 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004861 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004862 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004863 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004864
4865 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004866}
4867
4868/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4869/// identifier list form for a K&R-style function: void foo(a,b,c)
4870///
4871/// Note that identifier-lists are only allowed for normal declarators, not for
4872/// abstract-declarators.
4873bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004874 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004875 && Tok.is(tok::identifier)
4876 && !TryAltiVecVectorToken()
4877 // K&R identifier lists can't have typedefs as identifiers, per C99
4878 // 6.7.5.3p11.
4879 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4880 // Identifier lists follow a really simple grammar: the identifiers can
4881 // be followed *only* by a ", identifier" or ")". However, K&R
4882 // identifier lists are really rare in the brave new modern world, and
4883 // it is very common for someone to typo a type in a non-K&R style
4884 // list. If we are presented with something like: "void foo(intptr x,
4885 // float y)", we don't want to start parsing the function declarator as
4886 // though it is a K&R style declarator just because intptr is an
4887 // invalid type.
4888 //
4889 // To handle this, we check to see if the token after the first
4890 // identifier is a "," or ")". Only then do we parse it as an
4891 // identifier list.
4892 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4893}
4894
4895/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4896/// we found a K&R-style identifier list instead of a typed parameter list.
4897///
4898/// After returning, ParamInfo will hold the parsed parameters.
4899///
4900/// identifier-list: [C99 6.7.5]
4901/// identifier
4902/// identifier-list ',' identifier
4903///
4904void Parser::ParseFunctionDeclaratorIdentifierList(
4905 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004906 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004907 // If there was no identifier specified for the declarator, either we are in
4908 // an abstract-declarator, or we are in a parameter declarator which was found
4909 // to be abstract. In abstract-declarators, identifier lists are not valid:
4910 // diagnose this.
4911 if (!D.getIdentifier())
4912 Diag(Tok, diag::ext_ident_list_in_param);
4913
4914 // Maintain an efficient lookup of params we have seen so far.
4915 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4916
4917 while (1) {
4918 // If this isn't an identifier, report the error and skip until ')'.
4919 if (Tok.isNot(tok::identifier)) {
4920 Diag(Tok, diag::err_expected_ident);
4921 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4922 // Forget we parsed anything.
4923 ParamInfo.clear();
4924 return;
4925 }
4926
4927 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4928
4929 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4930 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4931 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4932
4933 // Verify that the argument identifier has not already been mentioned.
4934 if (!ParamsSoFar.insert(ParmII)) {
4935 Diag(Tok, diag::err_param_redefinition) << ParmII;
4936 } else {
4937 // Remember this identifier in ParamInfo.
4938 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4939 Tok.getLocation(),
4940 0));
4941 }
4942
4943 // Eat the identifier.
4944 ConsumeToken();
4945
4946 // The list continues if we see a comma.
4947 if (Tok.isNot(tok::comma))
4948 break;
4949 ConsumeToken();
4950 }
4951}
4952
4953/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4954/// after the opening parenthesis. This function will not parse a K&R-style
4955/// identifier list.
4956///
Richard Smith6ce48a72012-04-11 04:01:28 +00004957/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4958/// caller parsed those arguments immediately after the open paren - they should
4959/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004960///
4961/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4962/// be the location of the ellipsis, if any was parsed.
4963///
Reid Spencer5f016e22007-07-11 17:01:13 +00004964/// parameter-type-list: [C99 6.7.5]
4965/// parameter-list
4966/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004967/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004968///
4969/// parameter-list: [C99 6.7.5]
4970/// parameter-declaration
4971/// parameter-list ',' parameter-declaration
4972///
4973/// parameter-declaration: [C99 6.7.5]
4974/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004975/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004976/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004977/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004978/// declaration-specifiers abstract-declarator[opt]
4979/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004980/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004981/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004982/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004983///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004984void Parser::ParseParameterDeclarationClause(
4985 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004986 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004987 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004988 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004989
Chris Lattnerf97409f2008-04-06 06:57:35 +00004990 while (1) {
4991 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004992 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4993 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004994 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004995 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004996 }
Mike Stump1eb44332009-09-09 15:08:12 +00004997
Chris Lattnerf97409f2008-04-06 06:57:35 +00004998 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004999 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005000 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005001
Richard Smith6ce48a72012-04-11 04:01:28 +00005002 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005003 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005004
John McCall7f040a92010-12-24 02:08:15 +00005005 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005006 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005007
5008 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005009
5010 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005011 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005012 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005013 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5014 // too much hassle.
5015 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005016
Chris Lattnere64c5492009-02-27 18:38:20 +00005017 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005018
Chris Lattnerf97409f2008-04-06 06:57:35 +00005019 // Parse the declarator. This is "PrototypeContext", because we must
5020 // accept either 'declarator' or 'abstract-declarator' here.
5021 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5022 ParseDeclarator(ParmDecl);
5023
5024 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005025 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005026
Chris Lattnerf97409f2008-04-06 06:57:35 +00005027 // Remember this parsed parameter in ParamInfo.
5028 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005029
Douglas Gregor72b505b2008-12-16 21:30:33 +00005030 // DefArgToks is used when the parsing of default arguments needs
5031 // to be delayed.
5032 CachedTokens *DefArgToks = 0;
5033
Chris Lattnerf97409f2008-04-06 06:57:35 +00005034 // If no parameter was specified, verify that *something* was specified,
5035 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005036 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5037 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005038 // Completely missing, emit error.
5039 Diag(DSStart, diag::err_missing_param);
5040 } else {
5041 // Otherwise, we have something. Add it and let semantic analysis try
5042 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005043
Chris Lattnerf97409f2008-04-06 06:57:35 +00005044 // Inform the actions module about the parameter declarator, so it gets
5045 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005046 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005047
5048 // Parse the default argument, if any. We parse the default
5049 // arguments in all dialects; the semantic analysis in
5050 // ActOnParamDefaultArgument will reject the default argument in
5051 // C.
5052 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005053 SourceLocation EqualLoc = Tok.getLocation();
5054
Chris Lattner04421082008-04-08 04:40:51 +00005055 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005056 if (D.getContext() == Declarator::MemberContext) {
5057 // If we're inside a class definition, cache the tokens
5058 // corresponding to the default argument. We'll actually parse
5059 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005060 // FIXME: Can we use a smart pointer for Toks?
5061 DefArgToks = new CachedTokens;
5062
Mike Stump1eb44332009-09-09 15:08:12 +00005063 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005064 /*StopAtSemi=*/true,
5065 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005066 delete DefArgToks;
5067 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005068 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005069 } else {
5070 // Mark the end of the default argument so that we know when to
5071 // stop when we parse it later on.
5072 Token DefArgEnd;
5073 DefArgEnd.startToken();
5074 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5075 DefArgEnd.setLocation(Tok.getLocation());
5076 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005077 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005078 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005079 }
Chris Lattner04421082008-04-08 04:40:51 +00005080 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005081 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005082 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005083
Chad Rosier8decdee2012-06-26 22:30:43 +00005084 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005085 // used.
5086 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005087 Sema::PotentiallyEvaluatedIfUsed,
5088 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005089
Sebastian Redl84407ba2012-03-14 15:54:00 +00005090 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005091 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005092 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005093 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005094 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005095 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005096 if (DefArgResult.isInvalid()) {
5097 Actions.ActOnParamDefaultArgumentError(Param);
5098 SkipUntil(tok::comma, tok::r_paren, true, true);
5099 } else {
5100 // Inform the actions module about the default argument
5101 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005102 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005103 }
Chris Lattner04421082008-04-08 04:40:51 +00005104 }
5105 }
Mike Stump1eb44332009-09-09 15:08:12 +00005106
5107 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5108 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005109 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005110 }
5111
5112 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005113 if (Tok.isNot(tok::comma)) {
5114 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005115 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005116
David Blaikie4e4d0842012-03-11 07:00:24 +00005117 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005118 // We have ellipsis without a preceding ',', which is ill-formed
5119 // in C. Complain and provide the fix.
5120 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005121 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005122 }
5123 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005124
Douglas Gregored5d6512009-09-22 21:41:40 +00005125 break;
5126 }
Mike Stump1eb44332009-09-09 15:08:12 +00005127
Chris Lattnerf97409f2008-04-06 06:57:35 +00005128 // Consume the comma.
5129 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005130 }
Mike Stump1eb44332009-09-09 15:08:12 +00005131
Chris Lattner66d28652008-04-06 06:34:08 +00005132}
Chris Lattneref4715c2008-04-06 05:45:57 +00005133
Reid Spencer5f016e22007-07-11 17:01:13 +00005134/// [C90] direct-declarator '[' constant-expression[opt] ']'
5135/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5136/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5137/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5138/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005139/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5140/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005141void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005142 if (CheckProhibitedCXX11Attribute())
5143 return;
5144
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005145 BalancedDelimiterTracker T(*this, tok::l_square);
5146 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005147
Chris Lattner378c7e42008-12-18 07:27:21 +00005148 // C array syntax has many features, but by-far the most common is [] and [4].
5149 // This code does a fast path to handle some of the most obvious cases.
5150 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005151 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005152 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005153 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005154
Chris Lattner378c7e42008-12-18 07:27:21 +00005155 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005156 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005157 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005158 T.getOpenLocation(),
5159 T.getCloseLocation()),
5160 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005161 return;
5162 } else if (Tok.getKind() == tok::numeric_constant &&
5163 GetLookAheadToken(1).is(tok::r_square)) {
5164 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005165 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005166 ConsumeToken();
5167
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005168 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005169 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005170 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005171
Chris Lattner378c7e42008-12-18 07:27:21 +00005172 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005173 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005174 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005175 T.getOpenLocation(),
5176 T.getCloseLocation()),
5177 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005178 return;
5179 }
Mike Stump1eb44332009-09-09 15:08:12 +00005180
Reid Spencer5f016e22007-07-11 17:01:13 +00005181 // If valid, this location is the position where we read the 'static' keyword.
5182 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005183 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005184 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005185
Reid Spencer5f016e22007-07-11 17:01:13 +00005186 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005187 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005188 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005189 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005190
Reid Spencer5f016e22007-07-11 17:01:13 +00005191 // If we haven't already read 'static', check to see if there is one after the
5192 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005193 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005194 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005195
Reid Spencer5f016e22007-07-11 17:01:13 +00005196 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5197 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005198 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005199
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005200 // Handle the case where we have '[*]' as the array size. However, a leading
5201 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005202 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005203 // infrequent, use of lookahead is not costly here.
5204 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005205 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005206
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005207 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005208 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005209 StaticLoc = SourceLocation(); // Drop the static.
5210 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005211 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005212 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005213 // Note, in C89, this production uses the constant-expr production instead
5214 // of assignment-expr. The only difference is that assignment-expr allows
5215 // things like '=' and '*='. Sema rejects these in C89 mode because they
5216 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005217
Douglas Gregore0762c92009-06-19 23:52:42 +00005218 // Parse the constant-expression or assignment-expression now (depending
5219 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005220 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005221 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005222 } else {
5223 EnterExpressionEvaluationContext Unevaluated(Actions,
5224 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005225 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005226 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005227 }
Mike Stump1eb44332009-09-09 15:08:12 +00005228
Reid Spencer5f016e22007-07-11 17:01:13 +00005229 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005230 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005231 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005232 // If the expression was invalid, skip it.
5233 SkipUntil(tok::r_square);
5234 return;
5235 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005236
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005237 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005238
John McCall0b7e6782011-03-24 11:26:52 +00005239 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005240 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005241
Chris Lattner378c7e42008-12-18 07:27:21 +00005242 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005243 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005244 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005245 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005246 T.getOpenLocation(),
5247 T.getCloseLocation()),
5248 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005249}
5250
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005251/// [GNU] typeof-specifier:
5252/// typeof ( expressions )
5253/// typeof ( type-name )
5254/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005255///
5256void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005257 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005258 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005259 SourceLocation StartLoc = ConsumeToken();
5260
John McCallcfb708c2010-01-13 20:03:27 +00005261 const bool hasParens = Tok.is(tok::l_paren);
5262
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005263 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5264 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005265
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005266 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005267 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005268 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005269 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5270 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005271 if (hasParens)
5272 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005273
5274 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005275 // FIXME: Not accurate, the range gets one token more than it should.
5276 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005277 else
5278 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005279
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005280 if (isCastExpr) {
5281 if (!CastTy) {
5282 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005283 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005284 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005285
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005286 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005287 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005288 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5289 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005290 DiagID, CastTy))
5291 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005292 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005293 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005294
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005295 // If we get here, the operand to the typeof was an expresion.
5296 if (Operand.isInvalid()) {
5297 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005298 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005299 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005300
Eli Friedman71b8fb52012-01-21 01:01:51 +00005301 // We might need to transform the operand if it is potentially evaluated.
5302 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5303 if (Operand.isInvalid()) {
5304 DS.SetTypeSpecError();
5305 return;
5306 }
5307
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005308 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005309 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005310 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5311 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005312 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005313 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005314}
Chris Lattner1b492422010-02-28 18:33:55 +00005315
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005316/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005317/// _Atomic ( type-name )
5318///
5319void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5320 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5321
5322 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005323 BalancedDelimiterTracker T(*this, tok::l_paren);
5324 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005325 SkipUntil(tok::r_paren);
5326 return;
5327 }
5328
5329 TypeResult Result = ParseTypeName();
5330 if (Result.isInvalid()) {
5331 SkipUntil(tok::r_paren);
5332 return;
5333 }
5334
5335 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005336 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005337
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005338 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005339 return;
5340
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005341 DS.setTypeofParensRange(T.getRange());
5342 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005343
5344 const char *PrevSpec = 0;
5345 unsigned DiagID;
5346 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5347 DiagID, Result.release()))
5348 Diag(StartLoc, DiagID) << PrevSpec;
5349}
5350
Chris Lattner1b492422010-02-28 18:33:55 +00005351
5352/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5353/// from TryAltiVecVectorToken.
5354bool Parser::TryAltiVecVectorTokenOutOfLine() {
5355 Token Next = NextToken();
5356 switch (Next.getKind()) {
5357 default: return false;
5358 case tok::kw_short:
5359 case tok::kw_long:
5360 case tok::kw_signed:
5361 case tok::kw_unsigned:
5362 case tok::kw_void:
5363 case tok::kw_char:
5364 case tok::kw_int:
5365 case tok::kw_float:
5366 case tok::kw_double:
5367 case tok::kw_bool:
5368 case tok::kw___pixel:
5369 Tok.setKind(tok::kw___vector);
5370 return true;
5371 case tok::identifier:
5372 if (Next.getIdentifierInfo() == Ident_pixel) {
5373 Tok.setKind(tok::kw___vector);
5374 return true;
5375 }
5376 return false;
5377 }
5378}
5379
5380bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5381 const char *&PrevSpec, unsigned &DiagID,
5382 bool &isInvalid) {
5383 if (Tok.getIdentifierInfo() == Ident_vector) {
5384 Token Next = NextToken();
5385 switch (Next.getKind()) {
5386 case tok::kw_short:
5387 case tok::kw_long:
5388 case tok::kw_signed:
5389 case tok::kw_unsigned:
5390 case tok::kw_void:
5391 case tok::kw_char:
5392 case tok::kw_int:
5393 case tok::kw_float:
5394 case tok::kw_double:
5395 case tok::kw_bool:
5396 case tok::kw___pixel:
5397 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5398 return true;
5399 case tok::identifier:
5400 if (Next.getIdentifierInfo() == Ident_pixel) {
5401 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5402 return true;
5403 }
5404 break;
5405 default:
5406 break;
5407 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005408 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005409 DS.isTypeAltiVecVector()) {
5410 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5411 return true;
5412 }
5413 return false;
5414}