blob: 0dd1e0a6f4296770dce65877660bbb87058f0d00 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
Chris Lattnerf5fbd792006-08-10 23:56:11 +000033/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000040 AccessSpecifier AS,
41 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000042 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000043 if (DSC == DSC_normal)
44 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000045
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000047 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000048 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000049 if (OwnedType)
50 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000051
Chris Lattnerf5fbd792006-08-10 23:56:11 +000052 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000053 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000054 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000055 if (Range)
56 *Range = DeclaratorInfo.getSourceRange();
57
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000058 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000059 return true;
60
Douglas Gregor0be31a22010-07-02 17:43:08 +000061 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000062}
63
Caitlin Sadowski9385dd72011-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
Alexis Hunt96d5c762009-11-21 08:43:09 +000073/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +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
Steve Naroff0f2fe172007-06-01 17:11:19 +000088/// attrib-name
89/// attrib-name '(' identifier ')'
90/// attrib-name '(' identifier ',' nonempty-expr-list ')'
91/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000092///
Steve Naroff0f2fe172007-06-01 17:11:19 +000093/// [GNU] attrib-name:
94/// identifier
95/// typespec
96/// typequal
97/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000098///
Steve Naroff0f2fe172007-06-01 17:11:19 +000099/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-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
Steve Naroff0f2fe172007-06-01 17:11:19 +0000102/// start with that identifier; otherwise they are an expression list."
103///
Richard Smithb12bf692011-10-17 21:20:17 +0000104/// GCC does not require the ',' between attribs in an attribute-list.
105///
Steve Naroff0f2fe172007-06-01 17:11:19 +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.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000110
John McCall53fa7142010-12-24 02:08:15 +0000111void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000112 SourceLocation *endLoc,
113 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000114 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner76c72282007-10-09 17:33:22 +0000116 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 McCall53fa7142010-12-24 02:08:15 +0000121 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 }
123 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000125 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000126 }
127 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000128 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
129 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000130 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Stump11289f42009-09-09 15:08:12 +0000138
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000139 if (Tok.is(tok::l_paren)) {
140 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000141 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000142 LateParsedAttribute *LA =
143 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
144 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000145
Bill Wendling44426052012-12-20 19:22:21 +0000146 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000147 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000148 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000149 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000150
Caitlin Sadowski9385dd72011-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 Stump11289f42009-09-09 15:08:12 +0000153
Caitlin Sadowski9385dd72011-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 Han23214e52012-10-03 01:56:22 +0000159 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000160 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 }
162 } else {
John McCall084e83d2011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000164 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000165 }
166 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000167 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000168 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000169 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
171 SkipUntil(tok::r_paren, false);
172 }
John McCall53fa7142010-12-24 02:08:15 +0000173 if (endLoc)
174 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000176}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000177
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000178
Michael Han23214e52012-10-03 01:56:22 +0000179/// Parse the arguments to a parameterized GNU attribute or
180/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000181void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
182 SourceLocation AttrNameLoc,
183 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000184 SourceLocation *EndLoc,
185 IdentifierInfo *ScopeName,
186 SourceLocation ScopeLoc,
187 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-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 Gribenkoe4a5a902012-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 Sadowski9385dd72011-09-08 17:42:22 +0000207
208 ConsumeParen(); // ignore the left paren loc for now
209
Richard Smithb12bf692011-10-17 21:20:17 +0000210 IdentifierInfo *ParmName = 0;
211 SourceLocation ParmLoc;
212 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000213
Richard Smithb12bf692011-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 Smithf016bbc2012-04-04 06:24:32 +0000224 case tok::kw___int128:
Richard Smithb12bf692011-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 Kramerf0623432012-08-23 22:51:59 +0000246 ExprVector ArgExprs;
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000252 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000253
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000260 }
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000265 }
Richard Smithb12bf692011-10-17 21:20:17 +0000266 }
Fariborz Jahanian6b708652011-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 Jahanian7f733022011-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 Jahanian6b708652011-10-18 17:11:10 +0000281 SkipUntil(tok::r_paren, false, true); // skip until ')'
282 }
283 }
Richard Smithb12bf692011-10-17 21:20:17 +0000284
285 SourceLocation RParen = Tok.getLocation();
286 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000287 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb12bf692011-10-17 21:20:17 +0000288 AttributeList *attr =
Michael Han360d2252012-10-04 16:42:52 +0000289 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han23214e52012-10-03 01:56:22 +0000290 ScopeName, ScopeLoc, ParmName, ParmLoc,
291 ArgExprs.data(), ArgExprs.size(), Syntax);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000292 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000293 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000294 }
295}
296
Chad Rosierc1183952012-06-26 22:30:43 +0000297/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000298/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000299void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000300 SourceLocation AttrNameLoc,
301 ParsedAttributes &Attrs)
302{
303 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000304 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000305 AttrName->getNameStart(), tok::r_paren))
306 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000307
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000314 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000315 &ExprList, 1, AttributeList::AS_Declspec);
316
317 T.consumeClose();
318}
319
Chad Rosierc1183952012-06-26 22:30:43 +0000320/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000341/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000344void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-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 Rosierc1183952012-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 Ballman478faed2012-06-19 22:09:27 +0000358 // not.
359 if (Tok.getKind() == tok::l_paren)
360 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
361 else
Chad Rosierc1183952012-06-26 22:30:43 +0000362 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000366 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000367 // must be named get or put.
368 //
Chad Rosierc1183952012-06-26 22:30:43 +0000369 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000376 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000385 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-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 Friedman06de2b52009-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 Ballman478faed2012-06-19 22:09:27 +0000403void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000404 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000405
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000406 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000408 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000409 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000410 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000411
Chad Rosierc1183952012-06-26 22:30:43 +0000412 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-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 Rosierc1183952012-06-26 22:30:43 +0000418 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-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 Olesene1c0ae62012-06-19 21:48:43 +0000423 }
Aaron Ballman478faed2012-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 Olesene1c0ae62012-06-19 21:48:43 +0000434 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000435 AttrName = PP.getIdentifierInfo(Str);
436 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000437 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 AttrName = Tok.getIdentifierInfo();
439 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000440 }
Chad Rosierc1183952012-06-26 22:30:43 +0000441
Aaron Ballman478faed2012-06-19 22:09:27 +0000442 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-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 Ballman478faed2012-06-19 22:09:27 +0000445 // (for instance, SAL declspecs in older versions of MSVC).
446 //
Chad Rosierc1183952012-06-26 22:30:43 +0000447 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000448 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000449 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000450 0, 0, AttributeList::AS_Declspec);
451 else
452 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000453 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000454 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000455}
456
John McCall53fa7142010-12-24 02:08:15 +0000457void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000458 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000459 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000460 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000461 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000462 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000463 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
464 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000465 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000466 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000467 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000468}
469
John McCall53fa7142010-12-24 02:08:15 +0000470void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-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 McCall084e83d2011-03-24 11:26:52 +0000475 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000476 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000477 }
John McCall53fa7142010-12-24 02:08:15 +0000478}
479
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000480void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
481 // Treat these like attributes
482 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000483 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000484 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000485 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
486 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000487 }
488}
489
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000490void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000491 // FIXME: The mapping from attribute spelling to semantics should be
492 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000493 SourceLocation Loc = Tok.getLocation();
494 switch(Tok.getKind()) {
495 // OpenCL qualifiers:
496 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000497 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000498 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000499 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000500 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000501 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000502
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000503 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000504 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000505 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000506 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000507 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000508
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000509 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000510 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000511 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000512 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000513 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000514
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000515 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000516 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000517 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000518 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000519 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000520
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000521 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000522 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000523 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000524 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000525 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000526
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000527 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000528 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000529 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000530 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000531 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000532
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000533 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000534 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000535 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000536 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000537 break;
538 default: break;
539 }
540}
541
Douglas Gregor20b2ebd2011-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 Noblesmith2c1dd272012-02-05 02:13:05 +0000561 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-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 Rosea7d03842013-02-08 22:30:41 +0000574 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-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 Rosea7d03842013-02-08 22:30:41 +0000606 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000607 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
608 ++AfterMinor;
609 }
610
611 if (AfterMinor == ActualLength) {
612 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000613
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000620 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000627 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000628 }
629
630 // Parse the subminor version.
631 unsigned AfterSubminor = AfterMinor + 1;
632 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000633 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000650/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-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 Gregorfdd417f2012-03-11 04:53:21 +0000662/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000663/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000664/// opt-message:
665/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000675 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000676
677 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000678 BalancedDelimiterTracker T(*this, tok::l_paren);
679 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000680 Diag(Tok, diag::err_expected_lparen);
681 return;
682 }
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-03-26 03:35:55 +0000703 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000704 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000705 }
706
707 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000708 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-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 Rosierc1183952012-06-26 22:30:43 +0000722 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000723 UnavailableLoc = KeywordLoc;
724
725 if (Tok.isNot(tok::comma))
726 break;
727
728 ConsumeToken();
729 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000730 }
731
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000739 if (Keyword == Ident_message) {
740 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000741 Diag(Tok, diag::err_expected_string_literal)
742 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000743 SkipUntil(tok::r_paren);
744 return;
745 }
746 MessageExpr = ParseStringLiteralExpression();
747 break;
748 }
Chad Rosierc1183952012-06-26 22:30:43 +0000749
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 SourceRange VersionRange;
751 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000752
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000765 else
Douglas Gregor20b2ebd2011-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 Rosierc1183952012-06-26 22:30:43 +0000771 << Keyword
Douglas Gregor20b2ebd2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000791 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792 return;
793
794 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000795 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000796
Douglas Gregor7ab142b2011-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 Gregor20b2ebd2011-03-23 00:50:03 +0000816 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000817 attrs.addNew(&Availability,
818 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000819 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000820 Platform, PlatformLoc,
821 Changes[Introduced],
822 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000823 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000824 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000825 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826}
827
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000828
Bill Wendling44426052012-12-20 19:22:21 +0000829// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-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 Hutchins3fc6e4a2012-02-16 16:50:43 +0000839 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-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 Gregor3024f072012-04-16 07:05:22 +0000853 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000854 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000855 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000856 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
857 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
858
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000859 // Enter the scope of nested classes
860 if (!AlreadyHasClassScope)
861 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
862 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000863 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000864 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
865 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
866 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000867 }
Chad Rosierc1183952012-06-26 22:30:43 +0000868
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000869 if (!AlreadyHasClassScope)
870 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
871 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000872}
873
DeLesley Hutchins3fc6e4a2012-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 Hutchins66e300e2012-11-02 21:44:32 +0000878 assert(LAs.parseSoon() &&
879 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000880 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000881 if (D)
882 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000883 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000884 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000885 }
886 LAs.clear();
887}
888
889
Caitlin Sadowski9385dd72011-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 Rosierc1183952012-06-26 22:30:43 +0000893/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000894/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000895void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
896 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-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 Hutchins3fc6e4a2012-02-16 16:50:43 +0000907 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +0000908 // FIXME: Do not warn on C++11 attributes, once we start supporting
909 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000910 Diag(Tok, diag::warn_attribute_on_function_definition)
911 << LA.AttrName.getName();
912 }
913
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000914 ParsedAttributes Attrs(AttrFactory);
915 SourceLocation endLoc;
916
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000917 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000918 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000919 NamedDecl *ND = dyn_cast<NamedDecl>(D);
920 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000921
DeLesley Hutchinsf1150d32012-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 Sadowski9385dd72011-09-08 17:42:22 +0000926
DeLesley Hutchinsf1150d32012-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 Hutchinsbd2ee132012-03-02 22:12:59 +0000933
DeLesley Hutchinsf1150d32012-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 Hutchinsbd2ee132012-03-02 22:12:59 +0000939
Michael Han23214e52012-10-03 01:56:22 +0000940 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000941 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-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 Han23214e52012-10-03 01:56:22 +0000953 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000954 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000955 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000956 } else {
957 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000958 }
959
DeLesley Hutchinsbd2ee132012-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 Sadowski9385dd72011-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 Gregor0cf55e92012-03-08 01:00:17 +0000972 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000973 }
974}
975
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000976/// \brief Wrapper around a case statement checking if AttrName is
977/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000978bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-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 Sadowski9385dd72011-09-08 17:42:22 +00001017 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001018
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001019 BalancedDelimiterTracker T(*this, tok::l_paren);
1020 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001021
Benjamin Kramerf0623432012-08-23 22:51:59 +00001022 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001023 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001024
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001025 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001026 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001027 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028 ExprResult ArgExpr(ParseAssignmentExpression());
1029 if (ArgExpr.isInvalid()) {
1030 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001031 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032 break;
1033 } else {
1034 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001035 }
Caitlin Sadowski9385dd72011-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 Hutchins30398dd2012-01-20 22:50:54 +00001041 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001042 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001043 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001044 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001045 if (EndLoc)
1046 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001047}
1048
Dmitri Gribenkoe4a5a902012-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 Smith7bdcc4a2012-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 Carruthd8f7d382012-04-10 16:03:08 +00001144 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001145}
1146
John McCall53fa7142010-12-24 02:08:15 +00001147void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1148 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1149 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001150}
1151
Michael Han64536a62012-11-06 19:34:54 +00001152void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1153 AttributeList *AttrList = attrs.getList();
1154 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001155 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001156 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001157 << AttrList->getName();
1158 AttrList->setInvalid();
1159 }
1160 AttrList = AttrList->getNext();
1161 }
1162}
1163
Chris Lattner53361ac2006-08-10 05:19:57 +00001164/// ParseDeclaration - Parse a full 'declaration', which consists of
1165/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001166/// 'Context' should be a Declarator::TheContext value. This returns the
1167/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001168///
1169/// declaration: [C99 6.7]
1170/// block-declaration ->
1171/// simple-declaration
1172/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001173/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001174/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001175/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001176/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001177/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001178/// others... [FIXME]
1179///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001180Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1181 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001182 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001183 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001184 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001185 // Must temporarily exit the objective-c container scope for
1186 // parsing c none objective-c decls.
1187 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001188
John McCall48871652010-08-21 09:40:31 +00001189 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001190 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001191 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001192 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001193 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001194 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001195 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001196 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001197 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001198 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001199 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001200 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001201 SourceLocation InlineLoc = ConsumeToken();
1202 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1203 break;
1204 }
Chad Rosierc1183952012-06-26 22:30:43 +00001205 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001206 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001207 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001208 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001209 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001210 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001211 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001212 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001213 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001214 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001215 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001216 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001217 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001218 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001219 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001220 default:
John McCall53fa7142010-12-24 02:08:15 +00001221 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001222 }
Chad Rosierc1183952012-06-26 22:30:43 +00001223
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001224 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001225 // single decl, convert it now. Alias declarations can also declare a type;
1226 // include that too if it is present.
1227 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001228}
1229
1230/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1231/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001232/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1233/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001234///[C90/C++]init-declarator-list ';' [TODO]
1235/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001236///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001237/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001238/// attribute-specifier-seq[opt] type-specifier-seq declarator
1239///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001240/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001241/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001242///
1243/// If FRI is non-null, we might be parsing a for-range-declaration instead
1244/// of a simple-declaration. If we find that we are, we also parse the
1245/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001246Parser::DeclGroupPtrTy
1247Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1248 SourceLocation &DeclEnd,
1249 ParsedAttributesWithRange &attrs,
1250 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001251 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001252 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001253 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001254
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001255 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001256 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001257
Chris Lattner0e894622006-08-13 19:58:17 +00001258 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1259 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001260 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001261 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001262 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001263 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001264 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001265 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001266 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001267 }
Chad Rosierc1183952012-06-26 22:30:43 +00001268
1269 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001270}
Mike Stump11289f42009-09-09 15:08:12 +00001271
Richard Smith09f76ee2011-10-19 21:33:05 +00001272/// Returns true if this might be the start of a declarator, or a common typo
1273/// for a declarator.
1274bool Parser::MightBeDeclarator(unsigned Context) {
1275 switch (Tok.getKind()) {
1276 case tok::annot_cxxscope:
1277 case tok::annot_template_id:
1278 case tok::caret:
1279 case tok::code_completion:
1280 case tok::coloncolon:
1281 case tok::ellipsis:
1282 case tok::kw___attribute:
1283 case tok::kw_operator:
1284 case tok::l_paren:
1285 case tok::star:
1286 return true;
1287
1288 case tok::amp:
1289 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001290 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001291
Richard Smithc8a79032012-01-09 22:31:44 +00001292 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001293 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001294 NextToken().is(tok::l_square);
1295
1296 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001297 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001298
Richard Smith09f76ee2011-10-19 21:33:05 +00001299 case tok::identifier:
1300 switch (NextToken().getKind()) {
1301 case tok::code_completion:
1302 case tok::coloncolon:
1303 case tok::comma:
1304 case tok::equal:
1305 case tok::equalequal: // Might be a typo for '='.
1306 case tok::kw_alignas:
1307 case tok::kw_asm:
1308 case tok::kw___attribute:
1309 case tok::l_brace:
1310 case tok::l_paren:
1311 case tok::l_square:
1312 case tok::less:
1313 case tok::r_brace:
1314 case tok::r_paren:
1315 case tok::r_square:
1316 case tok::semi:
1317 return true;
1318
1319 case tok::colon:
1320 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001321 // and in block scope it's probably a label. Inside a class definition,
1322 // this is a bit-field.
1323 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001324 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001325
1326 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001327 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001328
1329 default:
1330 return false;
1331 }
1332
1333 default:
1334 return false;
1335 }
1336}
1337
Richard Smithb8caac82012-04-11 20:59:20 +00001338/// Skip until we reach something which seems like a sensible place to pick
1339/// up parsing after a malformed declaration. This will sometimes stop sooner
1340/// than SkipUntil(tok::r_brace) would, but will never stop later.
1341void Parser::SkipMalformedDecl() {
1342 while (true) {
1343 switch (Tok.getKind()) {
1344 case tok::l_brace:
1345 // Skip until matching }, then stop. We've probably skipped over
1346 // a malformed class or function definition or similar.
1347 ConsumeBrace();
1348 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1349 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1350 // This declaration isn't over yet. Keep skipping.
1351 continue;
1352 }
1353 if (Tok.is(tok::semi))
1354 ConsumeToken();
1355 return;
1356
1357 case tok::l_square:
1358 ConsumeBracket();
1359 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1360 continue;
1361
1362 case tok::l_paren:
1363 ConsumeParen();
1364 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1365 continue;
1366
1367 case tok::r_brace:
1368 return;
1369
1370 case tok::semi:
1371 ConsumeToken();
1372 return;
1373
1374 case tok::kw_inline:
1375 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001376 // a good place to pick back up parsing, except in an Objective-C
1377 // @interface context.
1378 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1379 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001380 return;
1381 break;
1382
1383 case tok::kw_namespace:
1384 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001385 // place to pick back up parsing, except in an Objective-C
1386 // @interface context.
1387 if (Tok.isAtStartOfLine() &&
1388 (!ParsingInObjCContainer || CurParsedObjCImpl))
1389 return;
1390 break;
1391
1392 case tok::at:
1393 // @end is very much like } in Objective-C contexts.
1394 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1395 ParsingInObjCContainer)
1396 return;
1397 break;
1398
1399 case tok::minus:
1400 case tok::plus:
1401 // - and + probably start new method declarations in Objective-C contexts.
1402 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001403 return;
1404 break;
1405
1406 case tok::eof:
1407 return;
1408
1409 default:
1410 break;
1411 }
1412
1413 ConsumeAnyToken();
1414 }
1415}
1416
John McCalld5a36322009-11-03 19:26:08 +00001417/// ParseDeclGroup - Having concluded that this is either a function
1418/// definition or a group of object declarations, actually parse the
1419/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001420Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1421 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001422 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001423 SourceLocation *DeclEnd,
1424 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001425 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001426 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001427 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001428
John McCalld5a36322009-11-03 19:26:08 +00001429 // Bail out if the first declarator didn't seem well-formed.
1430 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001431 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001432 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001433 }
Mike Stump11289f42009-09-09 15:08:12 +00001434
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001435 // Save late-parsed attributes for now; they need to be parsed in the
1436 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001437 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1438 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001439 if (D.isFunctionDeclarator())
1440 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1441
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001442 // Check to see if we have a function *definition* which must have a body.
1443 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1444 // Look at the next token to make sure that this isn't a function
1445 // declaration. We have to check this because __attribute__ might be the
1446 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001447 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001448
Chris Lattner13901342010-07-11 22:42:07 +00001449 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001450 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1451 Diag(Tok, diag::err_function_declared_typedef);
1452
1453 // Recover by treating the 'typedef' as spurious.
1454 DS.ClearStorageClassSpecs();
1455 }
1456
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001457 Decl *TheDecl =
1458 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001459 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001460 }
Chad Rosierc1183952012-06-26 22:30:43 +00001461
Chris Lattner13901342010-07-11 22:42:07 +00001462 if (isDeclarationSpecifier()) {
1463 // If there is an invalid declaration specifier right after the function
1464 // prototype, then we must be in a missing semicolon case where this isn't
1465 // actually a body. Just fall through into the code that handles it as a
1466 // prototype, and let the top-level code handle the erroneous declspec
1467 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001468 } else {
1469 Diag(Tok, diag::err_expected_fn_body);
1470 SkipUntil(tok::semi);
1471 return DeclGroupPtrTy();
1472 }
1473 }
1474
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001475 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001476 return DeclGroupPtrTy();
1477
1478 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1479 // must parse and analyze the for-range-initializer before the declaration is
1480 // analyzed.
1481 if (FRI && Tok.is(tok::colon)) {
1482 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001483 if (Tok.is(tok::l_brace))
1484 FRI->RangeExpr = ParseBraceInitializer();
1485 else
1486 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001487 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1488 Actions.ActOnCXXForRangeDecl(ThisDecl);
1489 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001490 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001491 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1492 }
1493
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001494 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001495 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001496 if (LateParsedAttrs.size() > 0)
1497 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001498 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001499 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001500 DeclsInGroup.push_back(FirstDecl);
1501
Richard Smith09f76ee2011-10-19 21:33:05 +00001502 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001503
John McCalld5a36322009-11-03 19:26:08 +00001504 // If we don't have a comma, it is either the end of the list (a ';') or an
1505 // error, bail out.
1506 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001507 SourceLocation CommaLoc = ConsumeToken();
1508
1509 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1510 // This comma was followed by a line-break and something which can't be
1511 // the start of a declarator. The comma was probably a typo for a
1512 // semicolon.
1513 Diag(CommaLoc, diag::err_expected_semi_declaration)
1514 << FixItHint::CreateReplacement(CommaLoc, ";");
1515 ExpectSemi = false;
1516 break;
1517 }
John McCalld5a36322009-11-03 19:26:08 +00001518
1519 // Parse the next declarator.
1520 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001521 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001522
1523 // Accept attributes in an init-declarator. In the first declarator in a
1524 // declaration, these would be part of the declspec. In subsequent
1525 // declarators, they become part of the declarator itself, so that they
1526 // don't apply to declarators after *this* one. Examples:
1527 // short __attribute__((common)) var; -> declspec
1528 // short var __attribute__((common)); -> declarator
1529 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001530 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001531
1532 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001533 if (!D.isInvalidType()) {
1534 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1535 D.complete(ThisDecl);
1536 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001537 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001538 }
John McCalld5a36322009-11-03 19:26:08 +00001539 }
1540
1541 if (DeclEnd)
1542 *DeclEnd = Tok.getLocation();
1543
Richard Smith09f76ee2011-10-19 21:33:05 +00001544 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001545 ExpectAndConsumeSemi(Context == Declarator::FileContext
1546 ? diag::err_invalid_token_after_toplevel_declarator
1547 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001548 // Okay, there was no semicolon and one was expected. If we see a
1549 // declaration specifier, just assume it was missing and continue parsing.
1550 // Otherwise things are very confused and we skip to recover.
1551 if (!isDeclarationSpecifier()) {
1552 SkipUntil(tok::r_brace, true, true);
1553 if (Tok.is(tok::semi))
1554 ConsumeToken();
1555 }
John McCalld5a36322009-11-03 19:26:08 +00001556 }
1557
Douglas Gregor0be31a22010-07-02 17:43:08 +00001558 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001559 DeclsInGroup.data(),
1560 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001561}
1562
Richard Smith02e85f32011-04-14 22:09:26 +00001563/// Parse an optional simple-asm-expr and attributes, and attach them to a
1564/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001565bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001566 // If a simple-asm-expr is present, parse it.
1567 if (Tok.is(tok::kw_asm)) {
1568 SourceLocation Loc;
1569 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1570 if (AsmLabel.isInvalid()) {
1571 SkipUntil(tok::semi, true, true);
1572 return true;
1573 }
1574
1575 D.setAsmLabel(AsmLabel.release());
1576 D.SetRangeEnd(Loc);
1577 }
1578
1579 MaybeParseGNUAttributes(D);
1580 return false;
1581}
1582
Douglas Gregor23996282009-05-12 21:31:51 +00001583/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1584/// declarator'. This method parses the remainder of the declaration
1585/// (including any attributes or initializer, among other things) and
1586/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001587///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001588/// init-declarator: [C99 6.7]
1589/// declarator
1590/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001591/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1592/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001593/// [C++] declarator initializer[opt]
1594///
1595/// [C++] initializer:
1596/// [C++] '=' initializer-clause
1597/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001598/// [C++0x] '=' 'default' [TODO]
1599/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001600/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001601///
1602/// According to the standard grammar, =default and =delete are function
1603/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001604///
John McCall48871652010-08-21 09:40:31 +00001605Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001606 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001607 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001608 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001609
Richard Smith02e85f32011-04-14 22:09:26 +00001610 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1611}
Mike Stump11289f42009-09-09 15:08:12 +00001612
Richard Smith02e85f32011-04-14 22:09:26 +00001613Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1614 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001615 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001616 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001617 switch (TemplateInfo.Kind) {
1618 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001619 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001620 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001621
Douglas Gregor450f00842009-09-25 18:43:00 +00001622 case ParsedTemplateInfo::Template:
1623 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001624 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001625 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001626 D);
1627 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001628
Douglas Gregor450f00842009-09-25 18:43:00 +00001629 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001630 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001631 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001632 TemplateInfo.ExternLoc,
1633 TemplateInfo.TemplateLoc,
1634 D);
1635 if (ThisRes.isInvalid()) {
1636 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001637 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001638 }
Chad Rosierc1183952012-06-26 22:30:43 +00001639
Douglas Gregor450f00842009-09-25 18:43:00 +00001640 ThisDecl = ThisRes.get();
1641 break;
1642 }
1643 }
Mike Stump11289f42009-09-09 15:08:12 +00001644
Richard Smith30482bc2011-02-20 03:19:35 +00001645 bool TypeContainsAuto =
1646 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1647
Douglas Gregor23996282009-05-12 21:31:51 +00001648 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001649 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001650 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001651 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001652 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001653 if (D.isFunctionDeclarator())
1654 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1655 << 1 /* delete */;
1656 else
1657 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001658 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001659 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001660 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1661 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001662 else
1663 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001664 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001665 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001666 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001667 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001668 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001669
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001670 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001671 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001672 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001673 cutOffParsing();
1674 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001675 }
Chad Rosierc1183952012-06-26 22:30:43 +00001676
John McCalldadc5752010-08-24 06:29:42 +00001677 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001678
David Blaikiebbafb8a2012-03-11 07:00:24 +00001679 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001680 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001681 ExitScope();
1682 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001683
Douglas Gregor23996282009-05-12 21:31:51 +00001684 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001685 SkipUntil(tok::comma, true, true);
1686 Actions.ActOnInitializerError(ThisDecl);
1687 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001688 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1689 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001690 }
1691 } else if (Tok.is(tok::l_paren)) {
1692 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001693 BalancedDelimiterTracker T(*this, tok::l_paren);
1694 T.consumeOpen();
1695
Benjamin Kramerf0623432012-08-23 22:51:59 +00001696 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001697 CommaLocsTy CommaLocs;
1698
David Blaikiebbafb8a2012-03-11 07:00:24 +00001699 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001700 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001701 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001702 }
1703
Douglas Gregor23996282009-05-12 21:31:51 +00001704 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001705 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001706 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001707
David Blaikiebbafb8a2012-03-11 07:00:24 +00001708 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001709 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001710 ExitScope();
1711 }
Douglas Gregor23996282009-05-12 21:31:51 +00001712 } else {
1713 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001714 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001715
1716 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1717 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001718
David Blaikiebbafb8a2012-03-11 07:00:24 +00001719 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001720 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001721 ExitScope();
1722 }
1723
Sebastian Redla9351792012-02-11 23:51:47 +00001724 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1725 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001726 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001727 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1728 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001729 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001730 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001731 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001732 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001733 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1734
Sebastian Redl3da34892011-06-05 12:23:16 +00001735 if (D.getCXXScopeSpec().isSet()) {
1736 EnterScope(0);
1737 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1738 }
1739
1740 ExprResult Init(ParseBraceInitializer());
1741
1742 if (D.getCXXScopeSpec().isSet()) {
1743 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1744 ExitScope();
1745 }
1746
1747 if (Init.isInvalid()) {
1748 Actions.ActOnInitializerError(ThisDecl);
1749 } else
1750 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1751 /*DirectInit=*/true, TypeContainsAuto);
1752
Douglas Gregor23996282009-05-12 21:31:51 +00001753 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001754 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001755 }
1756
Richard Smithb2bc2e62011-02-21 20:05:19 +00001757 Actions.FinalizeDeclaration(ThisDecl);
1758
Douglas Gregor23996282009-05-12 21:31:51 +00001759 return ThisDecl;
1760}
1761
Chris Lattner1890ac82006-08-13 01:16:23 +00001762/// ParseSpecifierQualifierList
1763/// specifier-qualifier-list:
1764/// type-specifier specifier-qualifier-list[opt]
1765/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001766/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001767///
Richard Smithc5b05522012-03-12 07:56:15 +00001768void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1769 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001770 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1771 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001772 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001773 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001774
Chris Lattner1890ac82006-08-13 01:16:23 +00001775 // Validate declspec for type-name.
1776 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001777 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1778 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001779 Diag(Tok, diag::err_expected_type);
1780 DS.SetTypeSpecError();
1781 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1782 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001783 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001784 if (!DS.hasTypeSpecifier())
1785 DS.SetTypeSpecError();
1786 }
Mike Stump11289f42009-09-09 15:08:12 +00001787
Chris Lattner1b22eed2006-11-28 05:12:07 +00001788 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001789 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001790 if (DS.getStorageClassSpecLoc().isValid())
1791 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1792 else
1793 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001794 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001795 }
Mike Stump11289f42009-09-09 15:08:12 +00001796
Chris Lattner1b22eed2006-11-28 05:12:07 +00001797 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001798 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001799 if (DS.isInlineSpecified())
1800 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1801 if (DS.isVirtualSpecified())
1802 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1803 if (DS.isExplicitSpecified())
1804 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001805 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001806 }
Richard Smithc5b05522012-03-12 07:56:15 +00001807
1808 // Issue diagnostic and remove constexpr specfier if present.
1809 if (DS.isConstexprSpecified()) {
1810 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1811 DS.ClearConstexprSpec();
1812 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001813}
Chris Lattner53361ac2006-08-10 05:19:57 +00001814
Chris Lattner6cc055a2009-04-12 20:42:31 +00001815/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1816/// specified token is valid after the identifier in a declarator which
1817/// immediately follows the declspec. For example, these things are valid:
1818///
1819/// int x [ 4]; // direct-declarator
1820/// int x ( int y); // direct-declarator
1821/// int(int x ) // direct-declarator
1822/// int x ; // simple-declaration
1823/// int x = 17; // init-declarator-list
1824/// int x , y; // init-declarator-list
1825/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001826/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001827/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001828///
1829/// This is not, because 'x' does not immediately follow the declspec (though
1830/// ')' happens to be valid anyway).
1831/// int (x)
1832///
1833static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1834 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1835 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001836 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001837}
1838
Chris Lattner20a0c612009-04-14 21:34:55 +00001839
1840/// ParseImplicitInt - This method is called when we have an non-typename
1841/// identifier in a declspec (which normally terminates the decl spec) when
1842/// the declspec has no type specifier. In this case, the declspec is either
1843/// malformed or is "implicit int" (in K&R and C89).
1844///
1845/// This method handles diagnosing this prettily and returns false if the
1846/// declspec is done being processed. If it recovers and thinks there may be
1847/// other pieces of declspec after it, it returns true.
1848///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001849bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001850 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00001851 AccessSpecifier AS, DeclSpecContext DSC,
1852 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001853 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001854
Chris Lattner20a0c612009-04-14 21:34:55 +00001855 SourceLocation Loc = Tok.getLocation();
1856 // If we see an identifier that is not a type name, we normally would
1857 // parse it as the identifer being declared. However, when a typename
1858 // is typo'd or the definition is not included, this will incorrectly
1859 // parse the typename as the identifier name and fall over misparsing
1860 // later parts of the diagnostic.
1861 //
1862 // As such, we try to do some look-ahead in cases where this would
1863 // otherwise be an "implicit-int" case to see if this is invalid. For
1864 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1865 // an identifier with implicit int, we'd get a parse error because the
1866 // next token is obviously invalid for a type. Parse these as a case
1867 // with an invalid type specifier.
1868 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001869
Chris Lattner20a0c612009-04-14 21:34:55 +00001870 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001871 // error, do lookahead to try to do better recovery. This never applies
1872 // within a type specifier. Outside of C++, we allow this even if the
1873 // language doesn't "officially" support implicit int -- we support
1874 // implicit int as an extension in C99 and C11. Allegedly, MS also
1875 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001876 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001877 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001878 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001879 // If this token is valid for implicit int, e.g. "static x = 4", then
1880 // we just avoid eating the identifier, so it will be parsed as the
1881 // identifier in the declarator.
1882 return false;
1883 }
Mike Stump11289f42009-09-09 15:08:12 +00001884
Richard Smitha952ebb2012-05-15 21:01:51 +00001885 if (getLangOpts().CPlusPlus &&
1886 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1887 // Don't require a type specifier if we have the 'auto' storage class
1888 // specifier in C++98 -- we'll promote it to a type specifier.
1889 return false;
1890 }
1891
Chris Lattner20a0c612009-04-14 21:34:55 +00001892 // Otherwise, if we don't consume this token, we are going to emit an
1893 // error anyway. Try to recover from various common problems. Check
1894 // to see if this was a reference to a tag name without a tag specified.
1895 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001896 //
1897 // C++ doesn't need this, and isTagName doesn't take SS.
1898 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001899 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001900 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001901
Douglas Gregor0be31a22010-07-02 17:43:08 +00001902 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001903 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001904 case DeclSpec::TST_enum:
1905 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1906 case DeclSpec::TST_union:
1907 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1908 case DeclSpec::TST_struct:
1909 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001910 case DeclSpec::TST_interface:
1911 TagName="__interface"; FixitTagName = "__interface ";
1912 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001913 case DeclSpec::TST_class:
1914 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001915 }
Mike Stump11289f42009-09-09 15:08:12 +00001916
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001917 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001918 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1919 LookupResult R(Actions, TokenName, SourceLocation(),
1920 Sema::LookupOrdinaryName);
1921
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001922 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001923 << TokenName << TagName << getLangOpts().CPlusPlus
1924 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1925
1926 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1927 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1928 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001929 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001930 << TokenName << TagName;
1931 }
Mike Stump11289f42009-09-09 15:08:12 +00001932
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001933 // Parse this as a tag as if the missing tag were present.
1934 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001935 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001936 else
Richard Smithc5b05522012-03-12 07:56:15 +00001937 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00001938 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001939 return true;
1940 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001941 }
Mike Stump11289f42009-09-09 15:08:12 +00001942
Richard Smithfe904f02012-05-15 21:29:55 +00001943 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001944 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001945 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1946 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001947 // Look ahead to the next token to try to figure out what this declaration
1948 // was supposed to be.
1949 switch (NextToken().getKind()) {
1950 case tok::comma:
1951 case tok::equal:
1952 case tok::kw_asm:
1953 case tok::l_brace:
1954 case tok::l_square:
1955 case tok::semi:
1956 // This looks like a variable declaration. The type is probably missing.
1957 // We're done parsing decl-specifiers.
1958 return false;
1959
1960 case tok::l_paren: {
1961 // static x(4); // 'x' is not a type
1962 // x(int n); // 'x' is not a type
1963 // x (*p)[]; // 'x' is a type
1964 //
1965 // Since we're in an error case (or the rare 'implicit int in C++' MS
1966 // extension), we can afford to perform a tentative parse to determine
1967 // which case we're in.
1968 TentativeParsingAction PA(*this);
1969 ConsumeToken();
1970 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1971 PA.Revert();
1972 if (TPR == TPResult::False())
1973 return false;
1974 // The identifier is followed by a parenthesized declarator.
1975 // It's supposed to be a type.
1976 break;
1977 }
1978
1979 default:
1980 // This is probably supposed to be a type. This includes cases like:
1981 // int f(itn);
1982 // struct S { unsinged : 4; };
1983 break;
1984 }
1985 }
1986
Chad Rosierc1183952012-06-26 22:30:43 +00001987 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001988 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001989 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001990 IdentifierInfo *II = Tok.getIdentifierInfo();
1991 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001992 // The action emitted a diagnostic, so we don't have to.
1993 if (T) {
1994 // The action has suggested that the type T could be used. Set that as
1995 // the type in the declaration specifiers, consume the would-be type
1996 // name token, and we're done.
1997 const char *PrevSpec;
1998 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001999 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002000 DS.SetRangeEnd(Tok.getLocation());
2001 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002002 // There may be other declaration specifiers after this.
2003 return true;
2004 } else if (II != Tok.getIdentifierInfo()) {
2005 // If no type was suggested, the correction is to a keyword
2006 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002007 // There may be other declaration specifiers after this.
2008 return true;
2009 }
Chad Rosierc1183952012-06-26 22:30:43 +00002010
Douglas Gregor15e56022009-10-13 23:27:22 +00002011 // Fall through; the action had no suggestion for us.
2012 } else {
2013 // The action did not emit a diagnostic, so emit one now.
2014 SourceRange R;
2015 if (SS) R = SS->getRange();
2016 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2017 }
Mike Stump11289f42009-09-09 15:08:12 +00002018
Douglas Gregor15e56022009-10-13 23:27:22 +00002019 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002020 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002021 DS.SetRangeEnd(Tok.getLocation());
2022 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002023
Chris Lattner20a0c612009-04-14 21:34:55 +00002024 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2025 // avoid rippling error messages on subsequent uses of the same type,
2026 // could be useful if #include was forgotten.
2027 return false;
2028}
2029
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002030/// \brief Determine the declaration specifier context from the declarator
2031/// context.
2032///
2033/// \param Context the declarator context, which is one of the
2034/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002035Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002036Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2037 if (Context == Declarator::MemberContext)
2038 return DSC_class;
2039 if (Context == Declarator::FileContext)
2040 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002041 if (Context == Declarator::TrailingReturnContext)
2042 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002043 return DSC_normal;
2044}
2045
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002046/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2047///
2048/// FIXME: Simply returns an alignof() expression if the argument is a
2049/// type. Ideally, the type should be propagated directly into Sema.
2050///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002051/// [C11] type-id
2052/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002053/// [C++0x] type-id ...[opt]
2054/// [C++0x] assignment-expression ...[opt]
2055ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2056 SourceLocation &EllipsisLoc) {
2057 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002058 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002059 SourceLocation TypeLoc = Tok.getLocation();
2060 ParsedType Ty = ParseTypeName().get();
2061 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002062 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2063 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002064 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002065 ER = ParseConstantExpression();
2066
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002067 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002068 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002069
2070 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002071}
2072
2073/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2074/// attribute to Attrs.
2075///
2076/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002077/// [C11] '_Alignas' '(' type-id ')'
2078/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002079/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2080/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002081void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2082 SourceLocation *endLoc) {
2083 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2084 "Not an alignment-specifier!");
2085
Richard Smithd11c7a12013-01-29 01:48:07 +00002086 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2087 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002088
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002089 BalancedDelimiterTracker T(*this, tok::l_paren);
2090 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002091 return;
2092
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002093 SourceLocation EllipsisLoc;
2094 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002095 if (ArgExpr.isInvalid()) {
2096 SkipUntil(tok::r_paren);
2097 return;
2098 }
2099
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002100 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002101 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002102 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002103
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002104 // FIXME: Handle pack-expansions here.
2105 if (EllipsisLoc.isValid()) {
2106 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2107 return;
2108 }
2109
Benjamin Kramerf0623432012-08-23 22:51:59 +00002110 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002111 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002112 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2113 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002114}
2115
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002116/// ParseDeclarationSpecifiers
2117/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002118/// storage-class-specifier declaration-specifiers[opt]
2119/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002120/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002121/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002122/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002123/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002124///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002125/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002126/// 'typedef'
2127/// 'extern'
2128/// 'static'
2129/// 'auto'
2130/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002131/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002132/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002133/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002134/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002135/// [C++] 'virtual'
2136/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002137/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002138/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002139/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002140
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002141///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002142void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002143 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002144 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002145 DeclSpecContext DSContext,
2146 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002147 if (DS.getSourceRange().isInvalid()) {
2148 DS.SetRangeStart(Tok.getLocation());
2149 DS.SetRangeEnd(Tok.getLocation());
2150 }
Chad Rosierc1183952012-06-26 22:30:43 +00002151
Douglas Gregordf593fb2011-11-07 17:33:42 +00002152 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002153 bool AttrsLastTime = false;
2154 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002155 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002156 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002157 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002158 unsigned DiagID = 0;
2159
Chris Lattner4d8f8732006-11-28 05:05:08 +00002160 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002161
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002162 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002163 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002164 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002165 if (!AttrsLastTime)
2166 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002167 else {
2168 // Reject C++11 attributes that appertain to decl specifiers as
2169 // we don't support any C++11 attributes that appertain to decl
2170 // specifiers. This also conforms to what g++ 4.8 is doing.
2171 ProhibitCXX11Attributes(attrs);
2172
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002173 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002174 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002175
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002176 // If this is not a declaration specifier token, we're done reading decl
2177 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002178 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002179 return;
Mike Stump11289f42009-09-09 15:08:12 +00002180
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002181 case tok::l_square:
2182 case tok::kw_alignas:
2183 if (!isCXX11AttributeSpecifier())
2184 goto DoneWithDeclSpec;
2185
2186 ProhibitAttributes(attrs);
2187 // FIXME: It would be good to recover by accepting the attributes,
2188 // but attempting to do that now would cause serious
2189 // madness in terms of diagnostics.
2190 attrs.clear();
2191 attrs.Range = SourceRange();
2192
2193 ParseCXX11Attributes(attrs);
2194 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002195 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002196
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002197 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002198 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002199 if (DS.hasTypeSpecifier()) {
2200 bool AllowNonIdentifiers
2201 = (getCurScope()->getFlags() & (Scope::ControlScope |
2202 Scope::BlockScope |
2203 Scope::TemplateParamScope |
2204 Scope::FunctionPrototypeScope |
2205 Scope::AtCatchScope)) == 0;
2206 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002207 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002208 (DSContext == DSC_class && DS.isFriendSpecified());
2209
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002210 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002211 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002212 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002213 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002214 }
2215
Douglas Gregor80039242011-02-15 20:33:25 +00002216 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2217 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2218 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002219 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002220 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002221 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002222 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002223 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002224 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002225
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002226 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002227 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002228 }
2229
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002230 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002231 // C++ scope specifier. Annotate and loop, or bail out on error.
2232 if (TryAnnotateCXXScopeToken(true)) {
2233 if (!DS.hasTypeSpecifier())
2234 DS.SetTypeSpecError();
2235 goto DoneWithDeclSpec;
2236 }
John McCall8bc2a702010-03-01 18:20:46 +00002237 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2238 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002239 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002240
2241 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002242 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002243 goto DoneWithDeclSpec;
2244
John McCall9dab4e62009-12-12 11:40:51 +00002245 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002246 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2247 Tok.getAnnotationRange(),
2248 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002249
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002250 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002251 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002252 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002253 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002254 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002255 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256
2257 // C++ [class.qual]p2:
2258 // In a lookup in which the constructor is an acceptable lookup
2259 // result and the nested-name-specifier nominates a class C:
2260 //
2261 // - if the name specified after the
2262 // nested-name-specifier, when looked up in C, is the
2263 // injected-class-name of C (Clause 9), or
2264 //
2265 // - if the name specified after the nested-name-specifier
2266 // is the same as the identifier or the
2267 // simple-template-id's template-name in the last
2268 // component of the nested-name-specifier,
2269 //
2270 // the name is instead considered to name the constructor of
2271 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002272 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002273 // Thus, if the template-name is actually the constructor
2274 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002275 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002276 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002277 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002278 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002279 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002280 if (isConstructorDeclarator()) {
2281 // The user meant this to be an out-of-line constructor
2282 // definition, but template arguments are not allowed
2283 // there. Just allow this as a constructor; we'll
2284 // complain about it later.
2285 goto DoneWithDeclSpec;
2286 }
2287
2288 // The user meant this to name a type, but it actually names
2289 // a constructor with some extraneous template
2290 // arguments. Complain, then parse it as a type as the user
2291 // intended.
2292 Diag(TemplateId->TemplateNameLoc,
2293 diag::err_out_of_line_template_id_names_constructor)
2294 << TemplateId->Name;
2295 }
2296
John McCall9dab4e62009-12-12 11:40:51 +00002297 DS.getTypeSpecScope() = SS;
2298 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002299 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002300 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002301 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002302 continue;
2303 }
2304
Douglas Gregorc5790df2009-09-28 07:26:33 +00002305 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002306 DS.getTypeSpecScope() = SS;
2307 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002308 if (Tok.getAnnotationValue()) {
2309 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002311 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002312 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002313 if (isInvalid)
2314 break;
John McCallba7bf592010-08-24 05:47:05 +00002315 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002316 else
2317 DS.SetTypeSpecError();
2318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2319 ConsumeToken(); // The typename
2320 }
2321
Douglas Gregor167fa622009-03-25 15:40:00 +00002322 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002323 goto DoneWithDeclSpec;
2324
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002325 // If we're in a context where the identifier could be a class name,
2326 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002327 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002328 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002329 &SS)) {
2330 if (isConstructorDeclarator())
2331 goto DoneWithDeclSpec;
2332
2333 // As noted in C++ [class.qual]p2 (cited above), when the name
2334 // of the class is qualified in a context where it could name
2335 // a constructor, its a constructor name. However, we've
2336 // looked at the declarator, and the user probably meant this
2337 // to be a type. Complain that it isn't supposed to be treated
2338 // as a type, then proceed to parse it as a type.
2339 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2340 << Next.getIdentifierInfo();
2341 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002342
John McCallba7bf592010-08-24 05:47:05 +00002343 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2344 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002345 getCurScope(), &SS,
2346 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002347 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002348 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002349
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002350 // If the referenced identifier is not a type, then this declspec is
2351 // erroneous: We already checked about that it has no type specifier, and
2352 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002353 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002354 if (TypeRep == 0) {
2355 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002356 ParsedAttributesWithRange Attrs(AttrFactory);
2357 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2358 if (!Attrs.empty()) {
2359 AttrsLastTime = true;
2360 attrs.takeAllFrom(Attrs);
2361 }
2362 continue;
2363 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002364 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002365 }
Mike Stump11289f42009-09-09 15:08:12 +00002366
John McCall9dab4e62009-12-12 11:40:51 +00002367 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002368 ConsumeToken(); // The C++ scope.
2369
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002371 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002372 if (isInvalid)
2373 break;
Mike Stump11289f42009-09-09 15:08:12 +00002374
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002375 DS.SetRangeEnd(Tok.getLocation());
2376 ConsumeToken(); // The typename.
2377
2378 continue;
2379 }
Mike Stump11289f42009-09-09 15:08:12 +00002380
Chris Lattnere387d9e2009-01-21 19:48:37 +00002381 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002382 if (Tok.getAnnotationValue()) {
2383 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002385 DiagID, T);
2386 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002387 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002388
Chris Lattner005fc1b2010-04-05 18:18:31 +00002389 if (isInvalid)
2390 break;
2391
Chris Lattnere387d9e2009-01-21 19:48:37 +00002392 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2393 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattnere387d9e2009-01-21 19:48:37 +00002395 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2396 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002397 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002398 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002399 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002400
Chris Lattnere387d9e2009-01-21 19:48:37 +00002401 continue;
2402 }
Mike Stump11289f42009-09-09 15:08:12 +00002403
Douglas Gregor06873092011-04-28 15:48:45 +00002404 case tok::kw___is_signed:
2405 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2406 // typically treats it as a trait. If we see __is_signed as it appears
2407 // in libstdc++, e.g.,
2408 //
2409 // static const bool __is_signed;
2410 //
2411 // then treat __is_signed as an identifier rather than as a keyword.
2412 if (DS.getTypeSpecType() == TST_bool &&
2413 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2414 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2415 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2416 Tok.setKind(tok::identifier);
2417 }
2418
2419 // We're done with the declaration-specifiers.
2420 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002421
Chris Lattner16fac4f2008-07-26 01:18:38 +00002422 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002423 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002424 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002425 // In C++, check to see if this is a scope specifier like foo::bar::, if
2426 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002427 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002428 if (TryAnnotateCXXScopeToken(true)) {
2429 if (!DS.hasTypeSpecifier())
2430 DS.SetTypeSpecError();
2431 goto DoneWithDeclSpec;
2432 }
2433 if (!Tok.is(tok::identifier))
2434 continue;
2435 }
Mike Stump11289f42009-09-09 15:08:12 +00002436
Chris Lattner16fac4f2008-07-26 01:18:38 +00002437 // This identifier can only be a typedef name if we haven't already seen
2438 // a type-specifier. Without this check we misparse:
2439 // typedef int X; struct Y { short X; }; as 'short int'.
2440 if (DS.hasTypeSpecifier())
2441 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002442
John Thompson22334602010-02-05 00:12:22 +00002443 // Check for need to substitute AltiVec keyword tokens.
2444 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2445 break;
2446
Richard Smith3092a3b2012-05-09 18:56:43 +00002447 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2448 // allow the use of a typedef name as a type specifier.
2449 if (DS.isTypeAltiVecVector())
2450 goto DoneWithDeclSpec;
2451
John McCallba7bf592010-08-24 05:47:05 +00002452 ParsedType TypeRep =
2453 Actions.getTypeName(*Tok.getIdentifierInfo(),
2454 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002455
Chris Lattner6cc055a2009-04-12 20:42:31 +00002456 // If this is not a typedef name, don't parse it as part of the declspec,
2457 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002458 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002459 ParsedAttributesWithRange Attrs(AttrFactory);
2460 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2461 if (!Attrs.empty()) {
2462 AttrsLastTime = true;
2463 attrs.takeAllFrom(Attrs);
2464 }
2465 continue;
2466 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002467 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002468 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002469
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002470 // If we're in a context where the identifier could be a class name,
2471 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002472 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002474 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002475 goto DoneWithDeclSpec;
2476
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002478 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002479 if (isInvalid)
2480 break;
Mike Stump11289f42009-09-09 15:08:12 +00002481
Chris Lattner16fac4f2008-07-26 01:18:38 +00002482 DS.SetRangeEnd(Tok.getLocation());
2483 ConsumeToken(); // The identifier
2484
2485 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2486 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002487 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002488 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002489 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002490
Steve Naroffcd5e7822008-09-22 10:28:57 +00002491 // Need to support trailing type qualifiers (e.g. "id<p> const").
2492 // If a type specifier follows, it will be diagnosed elsewhere.
2493 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002494 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002495
2496 // type-name
2497 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002498 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002499 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002500 // This template-id does not refer to a type name, so we're
2501 // done with the type-specifiers.
2502 goto DoneWithDeclSpec;
2503 }
2504
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002505 // If we're in a context where the template-id could be a
2506 // constructor name or specialization, check whether this is a
2507 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002508 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002509 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002510 isConstructorDeclarator())
2511 goto DoneWithDeclSpec;
2512
Douglas Gregor7f741122009-02-25 19:37:18 +00002513 // Turn the template-id annotation token into a type annotation
2514 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002515 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002516 continue;
2517 }
2518
Chris Lattnere37e2332006-08-15 04:50:22 +00002519 // GNU attributes support.
2520 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002521 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002522 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002523
2524 // Microsoft declspec support.
2525 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002526 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002527 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002528
Steve Naroff44ac7772008-12-25 14:16:32 +00002529 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002530 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002531 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002532 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002533 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002534 // FIXME: This does not work correctly if it is set to be a declspec
2535 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002536 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002537 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002538 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002539 }
Eli Friedman53339e02009-06-08 23:27:34 +00002540
2541 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002542 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002543 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002544 case tok::kw___cdecl:
2545 case tok::kw___stdcall:
2546 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002547 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002548 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002549 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002550 continue;
2551
Dawn Perchik335e16b2010-09-03 01:29:35 +00002552 // Borland single token adornments.
2553 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002554 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002555 continue;
2556
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002557 // OpenCL single token adornments.
2558 case tok::kw___kernel:
2559 ParseOpenCLAttributes(DS.getAttributes());
2560 continue;
2561
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002562 // storage-class-specifier
2563 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002564 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2565 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002566 break;
2567 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002568 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002569 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002570 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2571 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002572 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002573 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002574 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2575 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002576 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002577 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002578 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002579 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002580 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2581 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002582 break;
2583 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002584 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002585 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002586 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2587 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002588 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002589 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002590 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002591 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2593 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002594 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002595 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2596 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002597 break;
2598 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002599 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2600 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002601 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002602 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002603 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2604 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002605 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002606 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002607 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002608 break;
Mike Stump11289f42009-09-09 15:08:12 +00002609
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002610 // function-specifier
2611 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002612 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002613 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002614 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002615 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002616 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002617 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002618 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002619 break;
Richard Smith0015f092013-01-17 22:16:11 +00002620 case tok::kw__Noreturn:
2621 if (!getLangOpts().C11)
2622 Diag(Loc, diag::ext_c11_noreturn);
2623 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2624 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002625
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002626 // alignment-specifier
2627 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002628 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002629 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002630 ParseAlignmentSpecifier(DS.getAttributes());
2631 continue;
2632
Anders Carlssoncd8db412009-05-06 04:46:28 +00002633 // friend
2634 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002635 if (DSContext == DSC_class)
2636 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2637 else {
2638 PrevSpec = ""; // not actually used by the diagnostic
2639 DiagID = diag::err_friend_invalid_in_context;
2640 isInvalid = true;
2641 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002642 break;
Mike Stump11289f42009-09-09 15:08:12 +00002643
Douglas Gregor26701a42011-09-09 02:06:17 +00002644 // Modules
2645 case tok::kw___module_private__:
2646 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2647 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002648
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002649 // constexpr
2650 case tok::kw_constexpr:
2651 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2652 break;
2653
Chris Lattnere387d9e2009-01-21 19:48:37 +00002654 // type-specifier
2655 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2657 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002658 break;
2659 case tok::kw_long:
2660 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002661 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2662 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002663 else
John McCall49bfce42009-08-03 20:12:06 +00002664 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2665 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002666 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002667 case tok::kw___int64:
2668 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2669 DiagID);
2670 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002671 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002672 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2673 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002674 break;
2675 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002676 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2677 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002678 break;
2679 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2681 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002682 break;
2683 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002684 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2685 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002686 break;
2687 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2689 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002694 break;
2695 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2697 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002698 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002699 case tok::kw___int128:
2700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2701 DiagID);
2702 break;
2703 case tok::kw_half:
2704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2705 DiagID);
2706 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002707 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2709 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002710 break;
2711 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2713 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002714 break;
2715 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2717 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002718 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002719 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2721 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002722 break;
2723 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2725 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002726 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002727 case tok::kw_bool:
2728 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002729 if (Tok.is(tok::kw_bool) &&
2730 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2731 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2732 PrevSpec = ""; // Not used by the diagnostic.
2733 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002734 // For better error recovery.
2735 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002736 isInvalid = true;
2737 } else {
2738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2739 DiagID);
2740 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002741 break;
2742 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2744 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002745 break;
2746 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2748 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002749 break;
2750 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2752 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002753 break;
John Thompson22334602010-02-05 00:12:22 +00002754 case tok::kw___vector:
2755 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2756 break;
2757 case tok::kw___pixel:
2758 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2759 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002760 case tok::kw_image1d_t:
2761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2762 PrevSpec, DiagID);
2763 break;
2764 case tok::kw_image1d_array_t:
2765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2766 PrevSpec, DiagID);
2767 break;
2768 case tok::kw_image1d_buffer_t:
2769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2770 PrevSpec, DiagID);
2771 break;
2772 case tok::kw_image2d_t:
2773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2774 PrevSpec, DiagID);
2775 break;
2776 case tok::kw_image2d_array_t:
2777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2778 PrevSpec, DiagID);
2779 break;
2780 case tok::kw_image3d_t:
2781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2782 PrevSpec, DiagID);
2783 break;
Guy Benyei61054192013-02-07 10:55:47 +00002784 case tok::kw_sampler_t:
2785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2786 PrevSpec, DiagID);
2787 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002788 case tok::kw_event_t:
2789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2790 PrevSpec, DiagID);
2791 break;
John McCall39439732011-04-09 22:50:59 +00002792 case tok::kw___unknown_anytype:
2793 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2794 PrevSpec, DiagID);
2795 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002796
2797 // class-specifier:
2798 case tok::kw_class:
2799 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002800 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002801 case tok::kw_union: {
2802 tok::TokenKind Kind = Tok.getKind();
2803 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002804
2805 // These are attributes following class specifiers.
2806 // To produce better diagnostic, we parse them when
2807 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002808 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002809 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002810 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002811
2812 // If there are attributes following class specifier,
2813 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002814 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002815 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002816 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002817 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002818 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002819 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002820
2821 // enum-specifier:
2822 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002823 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002824 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002825 continue;
2826
2827 // cv-qualifier:
2828 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002829 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002830 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002831 break;
2832 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002833 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002834 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002835 break;
2836 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002837 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002838 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002839 break;
2840
Douglas Gregor333489b2009-03-27 23:10:48 +00002841 // C++ typename-specifier:
2842 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002843 if (TryAnnotateTypeOrScopeToken()) {
2844 DS.SetTypeSpecError();
2845 goto DoneWithDeclSpec;
2846 }
2847 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002848 continue;
2849 break;
2850
Chris Lattnere387d9e2009-01-21 19:48:37 +00002851 // GNU typeof support.
2852 case tok::kw_typeof:
2853 ParseTypeofSpecifier(DS);
2854 continue;
2855
David Blaikie15a430a2011-12-04 05:04:18 +00002856 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002857 ParseDecltypeSpecifier(DS);
2858 continue;
2859
Alexis Hunt4a257072011-05-19 05:37:45 +00002860 case tok::kw___underlying_type:
2861 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002862 continue;
2863
2864 case tok::kw__Atomic:
2865 ParseAtomicSpecifier(DS);
2866 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002867
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002868 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002869 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002870 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002871 goto DoneWithDeclSpec;
2872 case tok::kw___private:
2873 case tok::kw___global:
2874 case tok::kw___local:
2875 case tok::kw___constant:
2876 case tok::kw___read_only:
2877 case tok::kw___write_only:
2878 case tok::kw___read_write:
2879 ParseOpenCLQualifiers(DS);
2880 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002881
Steve Naroffcfdf6162008-06-05 00:02:44 +00002882 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002883 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002884 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2885 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002886 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002887 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002888
Douglas Gregor3a001f42010-11-19 17:10:50 +00002889 if (!ParseObjCProtocolQualifiers(DS))
2890 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2891 << FixItHint::CreateInsertion(Loc, "id")
2892 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002893
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002894 // Need to support trailing type qualifiers (e.g. "id<p> const").
2895 // If a type specifier follows, it will be diagnosed elsewhere.
2896 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002897 }
John McCall49bfce42009-08-03 20:12:06 +00002898 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002899 if (isInvalid) {
2900 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002901 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002902
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002903 if (DiagID == diag::ext_duplicate_declspec)
2904 Diag(Tok, DiagID)
2905 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2906 else
2907 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002908 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002909
Chris Lattner2e232092008-03-13 06:29:04 +00002910 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002911 if (DiagID != diag::err_bool_redeclaration)
2912 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002913
2914 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002915 }
2916}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002917
Chris Lattner70ae4912007-10-29 04:42:53 +00002918/// ParseStructDeclaration - Parse a struct declaration without the terminating
2919/// semicolon.
2920///
Chris Lattner90a26b02007-01-23 04:38:16 +00002921/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002922/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002923/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002924/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002925/// struct-declarator-list:
2926/// struct-declarator
2927/// struct-declarator-list ',' struct-declarator
2928/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2929/// struct-declarator:
2930/// declarator
2931/// [GNU] declarator attributes[opt]
2932/// declarator[opt] ':' constant-expression
2933/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2934///
Chris Lattnera12405b2008-04-10 06:46:29 +00002935void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002936ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002937
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002938 if (Tok.is(tok::kw___extension__)) {
2939 // __extension__ silences extension warnings in the subexpression.
2940 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002941 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002942 return ParseStructDeclaration(DS, Fields);
2943 }
Mike Stump11289f42009-09-09 15:08:12 +00002944
Steve Naroff97170802007-08-20 22:28:22 +00002945 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002946 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002947
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002948 // If there are no declarators, this is a free-standing declaration
2949 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002950 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002951 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2952 DS);
2953 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002954 return;
2955 }
2956
2957 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002958 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002959 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002960 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002961 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002962 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002963
Bill Wendling44426052012-12-20 19:22:21 +00002964 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002965 if (!FirstDeclarator)
2966 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002967
Steve Naroff97170802007-08-20 22:28:22 +00002968 /// struct-declarator: declarator
2969 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002970 if (Tok.isNot(tok::colon)) {
2971 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2972 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002973 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002974 }
Mike Stump11289f42009-09-09 15:08:12 +00002975
Chris Lattner76c72282007-10-09 17:33:22 +00002976 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002977 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002978 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002979 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002980 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002981 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002982 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002983 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002984
Steve Naroff97170802007-08-20 22:28:22 +00002985 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002986 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002987
John McCallcfefb6d2009-11-03 02:38:08 +00002988 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002989 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002990
Steve Naroff97170802007-08-20 22:28:22 +00002991 // If we don't have a comma, it is either the end of the list (a ';')
2992 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002993 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002994 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002995
Steve Naroff97170802007-08-20 22:28:22 +00002996 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002997 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002998
John McCallcfefb6d2009-11-03 02:38:08 +00002999 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003000 }
Steve Naroff97170802007-08-20 22:28:22 +00003001}
3002
3003/// ParseStructUnionBody
3004/// struct-contents:
3005/// struct-declaration-list
3006/// [EXT] empty
3007/// [GNU] "struct-declaration-list" without terminatoring ';'
3008/// struct-declaration-list:
3009/// struct-declaration
3010/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003011/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003012///
Chris Lattner1300fb92007-01-23 23:42:53 +00003013void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003014 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003015 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3016 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00003017
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003018 BalancedDelimiterTracker T(*this, tok::l_brace);
3019 if (T.consumeOpen())
3020 return;
Mike Stump11289f42009-09-09 15:08:12 +00003021
Douglas Gregor658b9552009-01-09 22:42:13 +00003022 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003023 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003024
Chris Lattner7b9ace62007-01-23 20:11:08 +00003025 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3026 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003027 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00003028 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3029 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3030 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003031
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003032 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003033
Chris Lattner7b9ace62007-01-23 20:11:08 +00003034 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003035 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003036 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003037
Chris Lattner736ed5d2007-06-09 05:59:07 +00003038 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003039 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003040 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003041 continue;
3042 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003043
John McCallcfefb6d2009-11-03 02:38:08 +00003044 if (!Tok.is(tok::at)) {
3045 struct CFieldCallback : FieldCallback {
3046 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003047 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003048 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003049
John McCall48871652010-08-21 09:40:31 +00003050 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003051 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003052 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3053
Eli Friedman934dbbf2012-08-08 23:53:27 +00003054 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003055 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003056 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003057 FD.D.getDeclSpec().getSourceRange().getBegin(),
3058 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003059 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003060 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003061 }
John McCallcfefb6d2009-11-03 02:38:08 +00003062 } Callback(*this, TagDecl, FieldDecls);
3063
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003064 // Parse all the comma separated declarators.
3065 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003066 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003067 } else { // Handle @defs
3068 ConsumeToken();
3069 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3070 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003071 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003072 continue;
3073 }
3074 ConsumeToken();
3075 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3076 if (!Tok.is(tok::identifier)) {
3077 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003078 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003079 continue;
3080 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003081 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003082 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003083 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003084 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3085 ConsumeToken();
3086 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003087 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003088
Chris Lattner76c72282007-10-09 17:33:22 +00003089 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003090 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003091 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003092 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003093 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003094 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003095 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3096 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003097 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003098 // If we stopped at a ';', eat it.
3099 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003100 }
3101 }
Mike Stump11289f42009-09-09 15:08:12 +00003102
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003103 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003104
John McCall084e83d2011-03-24 11:26:52 +00003105 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003106 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003107 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003108
Douglas Gregor0be31a22010-07-02 17:43:08 +00003109 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003110 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003111 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003112 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003113 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003114 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3115 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003116}
3117
Chris Lattner3b561a32006-08-13 00:12:11 +00003118/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003119/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003120/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003121///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003122/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3123/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003124/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3125/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003126/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003127/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003128///
Richard Smith7d137e32012-03-23 03:33:32 +00003129/// [C++11] enum-head '{' enumerator-list[opt] '}'
3130/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003131///
Richard Smith7d137e32012-03-23 03:33:32 +00003132/// enum-head: [C++11]
3133/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3134/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3135/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003136///
Richard Smith7d137e32012-03-23 03:33:32 +00003137/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003138/// 'enum'
3139/// 'enum' 'class'
3140/// 'enum' 'struct'
3141///
Richard Smith7d137e32012-03-23 03:33:32 +00003142/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003143/// ':' type-specifier-seq
3144///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003145/// [C++] elaborated-type-specifier:
3146/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3147///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003148void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003149 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003150 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003151 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003152 if (Tok.is(tok::code_completion)) {
3153 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003154 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003155 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003156 }
John McCallcb432fa2011-07-06 05:58:41 +00003157
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003158 // If attributes exist after tag, parse them.
3159 ParsedAttributesWithRange attrs(AttrFactory);
3160 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003161 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003162
3163 // If declspecs exist after tag, parse them.
3164 while (Tok.is(tok::kw___declspec))
3165 ParseMicrosoftDeclSpec(attrs);
3166
Richard Smith0f8ee222012-01-10 01:33:14 +00003167 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003168 bool IsScopedUsingClassTag = false;
3169
John McCallbeae29a2012-06-23 22:30:04 +00003170 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003171 if (getLangOpts().CPlusPlus11 &&
John McCallcb432fa2011-07-06 05:58:41 +00003172 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003173 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003174 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003175 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003176
Bill Wendling44426052012-12-20 19:22:21 +00003177 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003178 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003179 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003180
3181 // They are allowed afterwards, though.
3182 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003183 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003184 while (Tok.is(tok::kw___declspec))
3185 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003186 }
Richard Smith7d137e32012-03-23 03:33:32 +00003187
John McCall6347b682012-05-07 06:16:58 +00003188 // C++11 [temp.explicit]p12:
3189 // The usual access controls do not apply to names used to specify
3190 // explicit instantiations.
3191 // We extend this to also cover explicit specializations. Note that
3192 // we don't suppress if this turns out to be an elaborated type
3193 // specifier.
3194 bool shouldDelayDiagsInTag =
3195 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3196 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3197 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003198
Richard Smithbfdb1082012-03-12 08:56:40 +00003199 // Enum definitions should not be parsed in a trailing-return-type.
3200 bool AllowDeclaration = DSC != DSC_trailing;
3201
3202 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003203 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003204 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003205
Abramo Bagnarad7548482010-05-19 21:37:53 +00003206 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003207 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003208 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3209 // if a fixed underlying type is allowed.
3210 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003211
3212 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003213 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003214 return;
3215
3216 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003217 Diag(Tok, diag::err_expected_ident);
3218 if (Tok.isNot(tok::l_brace)) {
3219 // Has no name and is not a definition.
3220 // Skip the rest of this declarator, up until the comma or semicolon.
3221 SkipUntil(tok::comma, true);
3222 return;
3223 }
3224 }
3225 }
Mike Stump11289f42009-09-09 15:08:12 +00003226
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003227 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003228 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003229 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003230 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003231
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003232 // Skip the rest of this declarator, up until the comma or semicolon.
3233 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003234 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003235 }
Mike Stump11289f42009-09-09 15:08:12 +00003236
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003237 // If an identifier is present, consume and remember it.
3238 IdentifierInfo *Name = 0;
3239 SourceLocation NameLoc;
3240 if (Tok.is(tok::identifier)) {
3241 Name = Tok.getIdentifierInfo();
3242 NameLoc = ConsumeToken();
3243 }
Mike Stump11289f42009-09-09 15:08:12 +00003244
Richard Smith0f8ee222012-01-10 01:33:14 +00003245 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003246 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3247 // declaration of a scoped enumeration.
3248 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003249 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003250 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003251 }
3252
John McCall6347b682012-05-07 06:16:58 +00003253 // Okay, end the suppression area. We'll decide whether to emit the
3254 // diagnostics in a second.
3255 if (shouldDelayDiagsInTag)
3256 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003257
Douglas Gregor0bf31402010-10-08 23:50:27 +00003258 TypeResult BaseType;
3259
Douglas Gregord1f69f62010-12-01 17:42:47 +00003260 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003261 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003262 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003263 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003264 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003265 // If we're in class scope, this can either be an enum declaration with
3266 // an underlying type, or a declaration of a bitfield member. We try to
3267 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003268 // (integer literal, sizeof); if it's still ambiguous, we then consider
3269 // anything that's a simple-type-specifier followed by '(' as an
3270 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003271 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003272 EnterExpressionEvaluationContext Unevaluated(Actions,
3273 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003274 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003275 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003276 // bit-field. This is the common case.
3277 if (TPR == TPResult::True())
3278 PossibleBitfield = true;
3279 // If the next token starts a type-specifier-seq, it may be either a
3280 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003281 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003282 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003283 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003284 GetLookAheadToken(2).getKind() == tok::semi) {
3285 // Consume the ':'.
3286 ConsumeToken();
3287 } else {
3288 // We have the start of a type-specifier-seq, so we have to perform
3289 // tentative parsing to determine whether we have an expression or a
3290 // type.
3291 TentativeParsingAction TPA(*this);
3292
3293 // Consume the ':'.
3294 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003295
3296 // If we see a type specifier followed by an open-brace, we have an
3297 // ambiguity between an underlying type and a C++11 braced
3298 // function-style cast. Resolve this by always treating it as an
3299 // underlying type.
3300 // FIXME: The standard is not entirely clear on how to disambiguate in
3301 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003302 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003303 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003304 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003305 // We'll parse this as a bitfield later.
3306 PossibleBitfield = true;
3307 TPA.Revert();
3308 } else {
3309 // We have a type-specifier-seq.
3310 TPA.Commit();
3311 }
3312 }
3313 } else {
3314 // Consume the ':'.
3315 ConsumeToken();
3316 }
3317
3318 if (!PossibleBitfield) {
3319 SourceRange Range;
3320 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003321
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003322 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003323 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003324 } else if (!getLangOpts().ObjC2) {
3325 if (getLangOpts().CPlusPlus)
3326 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3327 else
3328 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3329 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003330 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003331 }
3332
Richard Smith0f8ee222012-01-10 01:33:14 +00003333 // There are four options here. If we have 'friend enum foo;' then this is a
3334 // friend declaration, and cannot have an accompanying definition. If we have
3335 // 'enum foo;', then this is a forward declaration. If we have
3336 // 'enum foo {...' then this is a definition. Otherwise we have something
3337 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003338 //
3339 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3340 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3341 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3342 //
John McCallfaf5fb42010-08-26 23:41:50 +00003343 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003344 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003345 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003346 } else if (Tok.is(tok::l_brace)) {
3347 if (DS.isFriendSpecified()) {
3348 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3349 << SourceRange(DS.getFriendSpecLoc());
3350 ConsumeBrace();
3351 SkipUntil(tok::r_brace);
3352 TUK = Sema::TUK_Friend;
3353 } else {
3354 TUK = Sema::TUK_Definition;
3355 }
Richard Smith369b9f92012-06-25 21:37:02 +00003356 } else if (DSC != DSC_type_specifier &&
3357 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003358 (Tok.isAtStartOfLine() &&
3359 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003360 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3361 if (Tok.isNot(tok::semi)) {
3362 // A semicolon was missing after this declaration. Diagnose and recover.
3363 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3364 "enum");
3365 PP.EnterToken(Tok);
3366 Tok.setKind(tok::semi);
3367 }
John McCall6347b682012-05-07 06:16:58 +00003368 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003369 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003370 }
3371
3372 // If this is an elaborated type specifier, and we delayed
3373 // diagnostics before, just merge them into the current pool.
3374 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3375 diagsFromTag.redelay();
3376 }
Richard Smith7d137e32012-03-23 03:33:32 +00003377
3378 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003379 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003380 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003381 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003382 // Skip the rest of this declarator, up until the comma or semicolon.
3383 Diag(Tok, diag::err_enum_template);
3384 SkipUntil(tok::comma, true);
3385 return;
3386 }
3387
3388 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3389 // Enumerations can't be explicitly instantiated.
3390 DS.SetTypeSpecError();
3391 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3392 return;
3393 }
3394
3395 assert(TemplateInfo.TemplateParams && "no template parameters");
3396 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3397 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003398 }
Chad Rosierc1183952012-06-26 22:30:43 +00003399
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003400 if (TUK == Sema::TUK_Reference)
3401 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003402
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003403 if (!Name && TUK != Sema::TUK_Definition) {
3404 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003405
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003406 // Skip the rest of this declarator, up until the comma or semicolon.
3407 SkipUntil(tok::comma, true);
3408 return;
3409 }
Richard Smith7d137e32012-03-23 03:33:32 +00003410
Douglas Gregord6ab8742009-05-28 23:31:59 +00003411 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003412 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003413 const char *PrevSpec = 0;
3414 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003415 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003416 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003417 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003418 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003419 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003420
Douglas Gregorba41d012010-04-24 16:38:41 +00003421 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003422 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003423 // dependent tag.
3424 if (!Name) {
3425 DS.SetTypeSpecError();
3426 Diag(Tok, diag::err_expected_type_name_after_typename);
3427 return;
3428 }
Chad Rosierc1183952012-06-26 22:30:43 +00003429
Douglas Gregor0be31a22010-07-02 17:43:08 +00003430 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003431 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003432 NameLoc);
3433 if (Type.isInvalid()) {
3434 DS.SetTypeSpecError();
3435 return;
3436 }
Chad Rosierc1183952012-06-26 22:30:43 +00003437
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003438 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3439 NameLoc.isValid() ? NameLoc : StartLoc,
3440 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003441 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003442
Douglas Gregorba41d012010-04-24 16:38:41 +00003443 return;
3444 }
Mike Stump11289f42009-09-09 15:08:12 +00003445
John McCall48871652010-08-21 09:40:31 +00003446 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003447 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003448 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003449 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003450 ConsumeBrace();
3451 SkipUntil(tok::r_brace);
3452 }
Chad Rosierc1183952012-06-26 22:30:43 +00003453
Douglas Gregorba41d012010-04-24 16:38:41 +00003454 DS.SetTypeSpecError();
3455 return;
3456 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003457
Richard Smith369b9f92012-06-25 21:37:02 +00003458 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003459 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003460
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003461 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3462 NameLoc.isValid() ? NameLoc : StartLoc,
3463 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003464 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003465}
3466
Chris Lattnerc1915e22007-01-25 07:29:02 +00003467/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3468/// enumerator-list:
3469/// enumerator
3470/// enumerator-list ',' enumerator
3471/// enumerator:
3472/// enumeration-constant
3473/// enumeration-constant '=' constant-expression
3474/// enumeration-constant:
3475/// identifier
3476///
John McCall48871652010-08-21 09:40:31 +00003477void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003478 // Enter the scope of the enum body and start the definition.
3479 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003480 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003481
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003482 BalancedDelimiterTracker T(*this, tok::l_brace);
3483 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003484
Chris Lattner37256fb2007-08-27 17:24:30 +00003485 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003486 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003487 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003488
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003489 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003490
John McCall48871652010-08-21 09:40:31 +00003491 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003492
Chris Lattnerc1915e22007-01-25 07:29:02 +00003493 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003494 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003495 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3496 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003497
John McCall811a0f52010-10-22 23:36:17 +00003498 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003499 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003500 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003501 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003502 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003503
Chris Lattnerc1915e22007-01-25 07:29:02 +00003504 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003505 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003506 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003507
Chris Lattner76c72282007-10-09 17:33:22 +00003508 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003509 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003510 AssignedVal = ParseConstantExpression();
3511 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003512 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003513 }
Mike Stump11289f42009-09-09 15:08:12 +00003514
Chris Lattnerc1915e22007-01-25 07:29:02 +00003515 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003516 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3517 LastEnumConstDecl,
3518 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003519 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003520 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003521 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003522
Chris Lattner4ef40012007-06-11 01:28:17 +00003523 EnumConstantDecls.push_back(EnumConstDecl);
3524 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003525
Douglas Gregorce66d022010-09-07 14:51:08 +00003526 if (Tok.is(tok::identifier)) {
3527 // We're missing a comma between enumerators.
3528 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003529 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003530 << FixItHint::CreateInsertion(Loc, ", ");
3531 continue;
3532 }
Chad Rosierc1183952012-06-26 22:30:43 +00003533
Chris Lattner76c72282007-10-09 17:33:22 +00003534 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003535 break;
3536 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003537
Richard Smith5d164bc2011-10-15 05:09:34 +00003538 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003539 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003540 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3541 diag::ext_enumerator_list_comma_cxx :
3542 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003543 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003544 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003545 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3546 << FixItHint::CreateRemoval(CommaLoc);
3547 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003548 }
Mike Stump11289f42009-09-09 15:08:12 +00003549
Chris Lattnerc1915e22007-01-25 07:29:02 +00003550 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003551 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003552
Chris Lattnerc1915e22007-01-25 07:29:02 +00003553 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003554 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003555 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003556
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003557 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3558 EnumDecl, EnumConstantDecls.data(),
3559 EnumConstantDecls.size(), getCurScope(),
3560 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003561
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003562 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003563 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3564 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003565
3566 // The next token must be valid after an enum definition. If not, a ';'
3567 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003568 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3569 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003570 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3571 // Push this token back into the preprocessor and change our current token
3572 // to ';' so that the rest of the code recovers as though there were an
3573 // ';' after the definition.
3574 PP.EnterToken(Tok);
3575 Tok.setKind(tok::semi);
3576 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003577}
Chris Lattner3b561a32006-08-13 00:12:11 +00003578
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003579/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003580/// start of a type-qualifier-list.
3581bool Parser::isTypeQualifier() const {
3582 switch (Tok.getKind()) {
3583 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003584
3585 // type-qualifier only in OpenCL
3586 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003587 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003588
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003589 // type-qualifier
3590 case tok::kw_const:
3591 case tok::kw_volatile:
3592 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003593 case tok::kw___private:
3594 case tok::kw___local:
3595 case tok::kw___global:
3596 case tok::kw___constant:
3597 case tok::kw___read_only:
3598 case tok::kw___read_write:
3599 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003600 return true;
3601 }
3602}
3603
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003604/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3605/// is definitely a type-specifier. Return false if it isn't part of a type
3606/// specifier or if we're not sure.
3607bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3608 switch (Tok.getKind()) {
3609 default: return false;
3610 // type-specifiers
3611 case tok::kw_short:
3612 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003613 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003614 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003615 case tok::kw_signed:
3616 case tok::kw_unsigned:
3617 case tok::kw__Complex:
3618 case tok::kw__Imaginary:
3619 case tok::kw_void:
3620 case tok::kw_char:
3621 case tok::kw_wchar_t:
3622 case tok::kw_char16_t:
3623 case tok::kw_char32_t:
3624 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003625 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003626 case tok::kw_float:
3627 case tok::kw_double:
3628 case tok::kw_bool:
3629 case tok::kw__Bool:
3630 case tok::kw__Decimal32:
3631 case tok::kw__Decimal64:
3632 case tok::kw__Decimal128:
3633 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003634
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003635 // OpenCL specific types:
3636 case tok::kw_image1d_t:
3637 case tok::kw_image1d_array_t:
3638 case tok::kw_image1d_buffer_t:
3639 case tok::kw_image2d_t:
3640 case tok::kw_image2d_array_t:
3641 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003642 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003643 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003644
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003645 // struct-or-union-specifier (C99) or class-specifier (C++)
3646 case tok::kw_class:
3647 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003648 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003649 case tok::kw_union:
3650 // enum-specifier
3651 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003652
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003653 // typedef-name
3654 case tok::annot_typename:
3655 return true;
3656 }
3657}
3658
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003659/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003660/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003661bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003662 switch (Tok.getKind()) {
3663 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003664
Chris Lattner020bab92009-01-04 23:41:41 +00003665 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003666 if (TryAltiVecVectorToken())
3667 return true;
3668 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003669 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003670 // Annotate typenames and C++ scope specifiers. If we get one, just
3671 // recurse to handle whatever we get.
3672 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003673 return true;
3674 if (Tok.is(tok::identifier))
3675 return false;
3676 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003677
Chris Lattner020bab92009-01-04 23:41:41 +00003678 case tok::coloncolon: // ::foo::bar
3679 if (NextToken().is(tok::kw_new) || // ::new
3680 NextToken().is(tok::kw_delete)) // ::delete
3681 return false;
3682
Chris Lattner020bab92009-01-04 23:41:41 +00003683 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003684 return true;
3685 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003686
Chris Lattnere37e2332006-08-15 04:50:22 +00003687 // GNU attributes support.
3688 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003689 // GNU typeof support.
3690 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003691
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003692 // type-specifiers
3693 case tok::kw_short:
3694 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003695 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003696 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003697 case tok::kw_signed:
3698 case tok::kw_unsigned:
3699 case tok::kw__Complex:
3700 case tok::kw__Imaginary:
3701 case tok::kw_void:
3702 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003703 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003704 case tok::kw_char16_t:
3705 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003706 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003707 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003708 case tok::kw_float:
3709 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003710 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003711 case tok::kw__Bool:
3712 case tok::kw__Decimal32:
3713 case tok::kw__Decimal64:
3714 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003715 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003716
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003717 // OpenCL specific types:
3718 case tok::kw_image1d_t:
3719 case tok::kw_image1d_array_t:
3720 case tok::kw_image1d_buffer_t:
3721 case tok::kw_image2d_t:
3722 case tok::kw_image2d_array_t:
3723 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003724 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003725 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003726
Chris Lattner861a2262008-04-13 18:59:07 +00003727 // struct-or-union-specifier (C99) or class-specifier (C++)
3728 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003729 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003730 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003731 case tok::kw_union:
3732 // enum-specifier
3733 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003734
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003735 // type-qualifier
3736 case tok::kw_const:
3737 case tok::kw_volatile:
3738 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003739
John McCallea0a39e2012-11-14 00:49:39 +00003740 // Debugger support.
3741 case tok::kw___unknown_anytype:
3742
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003743 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003744 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003745 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003746
Chris Lattner409bf7d2008-10-20 00:25:30 +00003747 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3748 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003749 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003750
Steve Naroff44ac7772008-12-25 14:16:32 +00003751 case tok::kw___cdecl:
3752 case tok::kw___stdcall:
3753 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003754 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003755 case tok::kw___w64:
3756 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003757 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003758 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003759 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003760
3761 case tok::kw___private:
3762 case tok::kw___local:
3763 case tok::kw___global:
3764 case tok::kw___constant:
3765 case tok::kw___read_only:
3766 case tok::kw___read_write:
3767 case tok::kw___write_only:
3768
Eli Friedman53339e02009-06-08 23:27:34 +00003769 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003770
3771 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003772 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003773
Benjamin Kramere56f3932011-12-23 17:00:35 +00003774 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003775 case tok::kw__Atomic:
3776 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003777 }
3778}
3779
Chris Lattneracd58a32006-08-06 17:24:14 +00003780/// isDeclarationSpecifier() - Return true if the current token is part of a
3781/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003782///
3783/// \param DisambiguatingWithExpression True to indicate that the purpose of
3784/// this check is to disambiguate between an expression and a declaration.
3785bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003786 switch (Tok.getKind()) {
3787 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003788
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003789 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003790 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003791
Chris Lattner020bab92009-01-04 23:41:41 +00003792 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003793 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003794 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003795 return false;
John Thompson22334602010-02-05 00:12:22 +00003796 if (TryAltiVecVectorToken())
3797 return true;
3798 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003799 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003800 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003801 // Annotate typenames and C++ scope specifiers. If we get one, just
3802 // recurse to handle whatever we get.
3803 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003804 return true;
3805 if (Tok.is(tok::identifier))
3806 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003807
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003808 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003809 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003810 // expression is permitted, then this is probably a class message send
3811 // missing the initial '['. In this case, we won't consider this to be
3812 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003813 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003814 isStartOfObjCClassMessageMissingOpenBracket())
3815 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003816
John McCall1f476a12010-02-26 08:45:28 +00003817 return isDeclarationSpecifier();
3818
Chris Lattner020bab92009-01-04 23:41:41 +00003819 case tok::coloncolon: // ::foo::bar
3820 if (NextToken().is(tok::kw_new) || // ::new
3821 NextToken().is(tok::kw_delete)) // ::delete
3822 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003823
Chris Lattner020bab92009-01-04 23:41:41 +00003824 // Annotate typenames and C++ scope specifiers. If we get one, just
3825 // recurse to handle whatever we get.
3826 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003827 return true;
3828 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003829
Chris Lattneracd58a32006-08-06 17:24:14 +00003830 // storage-class-specifier
3831 case tok::kw_typedef:
3832 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003833 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003834 case tok::kw_static:
3835 case tok::kw_auto:
3836 case tok::kw_register:
3837 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003838
Douglas Gregor26701a42011-09-09 02:06:17 +00003839 // Modules
3840 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003841
John McCallea0a39e2012-11-14 00:49:39 +00003842 // Debugger support
3843 case tok::kw___unknown_anytype:
3844
Chris Lattneracd58a32006-08-06 17:24:14 +00003845 // type-specifiers
3846 case tok::kw_short:
3847 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003848 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003849 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003850 case tok::kw_signed:
3851 case tok::kw_unsigned:
3852 case tok::kw__Complex:
3853 case tok::kw__Imaginary:
3854 case tok::kw_void:
3855 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003856 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003857 case tok::kw_char16_t:
3858 case tok::kw_char32_t:
3859
Chris Lattneracd58a32006-08-06 17:24:14 +00003860 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003861 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003862 case tok::kw_float:
3863 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003864 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003865 case tok::kw__Bool:
3866 case tok::kw__Decimal32:
3867 case tok::kw__Decimal64:
3868 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003869 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003870
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003871 // OpenCL specific types:
3872 case tok::kw_image1d_t:
3873 case tok::kw_image1d_array_t:
3874 case tok::kw_image1d_buffer_t:
3875 case tok::kw_image2d_t:
3876 case tok::kw_image2d_array_t:
3877 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003878 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003879 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003880
Chris Lattner861a2262008-04-13 18:59:07 +00003881 // struct-or-union-specifier (C99) or class-specifier (C++)
3882 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003883 case tok::kw_struct:
3884 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003885 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003886 // enum-specifier
3887 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003888
Chris Lattneracd58a32006-08-06 17:24:14 +00003889 // type-qualifier
3890 case tok::kw_const:
3891 case tok::kw_volatile:
3892 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003893
Chris Lattneracd58a32006-08-06 17:24:14 +00003894 // function-specifier
3895 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003896 case tok::kw_virtual:
3897 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00003898 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003899
Richard Smith1dba27c2013-01-29 09:02:09 +00003900 // alignment-specifier
3901 case tok::kw__Alignas:
3902
Richard Smithd16fe122012-10-25 00:00:53 +00003903 // friend keyword.
3904 case tok::kw_friend:
3905
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003906 // static_assert-declaration
3907 case tok::kw__Static_assert:
3908
Chris Lattner599e47e2007-08-09 17:01:07 +00003909 // GNU typeof support.
3910 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003911
Chris Lattner599e47e2007-08-09 17:01:07 +00003912 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003913 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003914
Richard Smithd16fe122012-10-25 00:00:53 +00003915 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003916 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003917 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003918
Benjamin Kramere56f3932011-12-23 17:00:35 +00003919 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003920 case tok::kw__Atomic:
3921 return true;
3922
Chris Lattner8b2ec162008-07-26 03:38:44 +00003923 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3924 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003925 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003926
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003927 // typedef-name
3928 case tok::annot_typename:
3929 return !DisambiguatingWithExpression ||
3930 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003931
Steve Narofff192fab2009-01-06 19:34:12 +00003932 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003933 case tok::kw___cdecl:
3934 case tok::kw___stdcall:
3935 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003936 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003937 case tok::kw___w64:
3938 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003939 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003940 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003941 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003942 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003943
3944 case tok::kw___private:
3945 case tok::kw___local:
3946 case tok::kw___global:
3947 case tok::kw___constant:
3948 case tok::kw___read_only:
3949 case tok::kw___read_write:
3950 case tok::kw___write_only:
3951
Eli Friedman53339e02009-06-08 23:27:34 +00003952 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003953 }
3954}
3955
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003956bool Parser::isConstructorDeclarator() {
3957 TentativeParsingAction TPA(*this);
3958
3959 // Parse the C++ scope specifier.
3960 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003961 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003962 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003963 TPA.Revert();
3964 return false;
3965 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003966
3967 // Parse the constructor name.
3968 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3969 // We already know that we have a constructor name; just consume
3970 // the token.
3971 ConsumeToken();
3972 } else {
3973 TPA.Revert();
3974 return false;
3975 }
3976
Richard Smith43f340f2012-03-27 23:05:05 +00003977 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003978 if (Tok.isNot(tok::l_paren)) {
3979 TPA.Revert();
3980 return false;
3981 }
3982 ConsumeParen();
3983
Richard Smith43f340f2012-03-27 23:05:05 +00003984 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3985 // that we have a constructor.
3986 if (Tok.is(tok::r_paren) ||
3987 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003988 TPA.Revert();
3989 return true;
3990 }
3991
3992 // If we need to, enter the specified scope.
3993 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003994 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003995 DeclScopeObj.EnterDeclaratorScope();
3996
Francois Pichet79f3a872011-01-31 04:54:32 +00003997 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003998 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003999 MaybeParseMicrosoftAttributes(Attrs);
4000
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004001 // Check whether the next token(s) are part of a declaration
4002 // specifier, in which case we have the start of a parameter and,
4003 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004004 bool IsConstructor = false;
4005 if (isDeclarationSpecifier())
4006 IsConstructor = true;
4007 else if (Tok.is(tok::identifier) ||
4008 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4009 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4010 // This might be a parenthesized member name, but is more likely to
4011 // be a constructor declaration with an invalid argument type. Keep
4012 // looking.
4013 if (Tok.is(tok::annot_cxxscope))
4014 ConsumeToken();
4015 ConsumeToken();
4016
4017 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004018 // which must have one of the following syntactic forms (see the
4019 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004020 switch (Tok.getKind()) {
4021 case tok::l_paren:
4022 // C(X ( int));
4023 case tok::l_square:
4024 // C(X [ 5]);
4025 // C(X [ [attribute]]);
4026 case tok::coloncolon:
4027 // C(X :: Y);
4028 // C(X :: *p);
4029 case tok::r_paren:
4030 // C(X )
4031 // Assume this isn't a constructor, rather than assuming it's a
4032 // constructor with an unnamed parameter of an ill-formed type.
4033 break;
4034
4035 default:
4036 IsConstructor = true;
4037 break;
4038 }
4039 }
4040
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004041 TPA.Revert();
4042 return IsConstructor;
4043}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004044
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004045/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004046/// type-qualifier-list: [C99 6.7.5]
4047/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004048/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004049/// [ only if VendorAttributesAllowed=true ]
4050/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004051/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004052/// [ only if VendorAttributesAllowed=true ]
4053/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004054/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004055/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004056///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004057void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4058 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00004059 bool CXX11AttributesAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004060 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004061 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004062 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004063 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004064 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004065 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004066
4067 SourceLocation EndLoc;
4068
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004069 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004070 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004071 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004072 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004073 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004074
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004075 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004076 case tok::code_completion:
4077 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004078 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004079
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004080 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004082 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004083 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004084 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004086 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004087 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004088 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004090 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004091 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004092
4093 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004094 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004095 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004096 goto DoneWithTypeQuals;
4097 case tok::kw___private:
4098 case tok::kw___global:
4099 case tok::kw___local:
4100 case tok::kw___constant:
4101 case tok::kw___read_only:
4102 case tok::kw___write_only:
4103 case tok::kw___read_write:
4104 ParseOpenCLQualifiers(DS);
4105 break;
4106
Eli Friedman53339e02009-06-08 23:27:34 +00004107 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004108 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004109 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004110 case tok::kw___cdecl:
4111 case tok::kw___stdcall:
4112 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004113 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004114 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004115 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004116 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004117 continue;
4118 }
4119 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004120 case tok::kw___pascal:
4121 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004122 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004123 continue;
4124 }
4125 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004126 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004127 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004128 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004129 continue; // do *not* consume the next token!
4130 }
4131 // otherwise, FALL THROUGH!
4132 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004133 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004134 // If this is not a type-qualifier token, we're done reading type
4135 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004136 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004137 if (EndLoc.isValid())
4138 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004139 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004140 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004141
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004142 // If the specifier combination wasn't legal, issue a diagnostic.
4143 if (isInvalid) {
4144 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004145 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004146 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004147 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004148 }
4149}
4150
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004151
4152/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4153///
4154void Parser::ParseDeclarator(Declarator &D) {
4155 /// This implements the 'declarator' production in the C grammar, then checks
4156 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004157 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004158}
4159
Richard Smith0efa75c2012-03-29 01:16:42 +00004160static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4161 if (Kind == tok::star || Kind == tok::caret)
4162 return true;
4163
4164 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4165 if (!Lang.CPlusPlus)
4166 return false;
4167
4168 return Kind == tok::amp || Kind == tok::ampamp;
4169}
4170
Sebastian Redlbd150f42008-11-21 19:14:01 +00004171/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4172/// is parsed by the function passed to it. Pass null, and the direct-declarator
4173/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004174/// ptr-operator production.
4175///
Richard Smith09f76ee2011-10-19 21:33:05 +00004176/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004177/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4178/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004179///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004180/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4181/// [C] pointer[opt] direct-declarator
4182/// [C++] direct-declarator
4183/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004184///
4185/// pointer: [C99 6.7.5]
4186/// '*' type-qualifier-list[opt]
4187/// '*' type-qualifier-list[opt] pointer
4188///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004189/// ptr-operator:
4190/// '*' cv-qualifier-seq[opt]
4191/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004192/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004193/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004194/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004195/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004196void Parser::ParseDeclaratorInternal(Declarator &D,
4197 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004198 if (Diags.hasAllExtensionsSilenced())
4199 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004200
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004201 // C++ member pointers start with a '::' or a nested-name.
4202 // Member pointers get special handling, since there's no place for the
4203 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004204 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004205 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4206 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004207 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4208 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004209 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004210 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004211
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004212 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004213 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004214 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004215 if (D.mayHaveIdentifier())
4216 D.getCXXScopeSpec() = SS;
4217 else
4218 AnnotateScopeToken(SS, true);
4219
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004220 if (DirectDeclParser)
4221 (this->*DirectDeclParser)(D);
4222 return;
4223 }
4224
4225 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004226 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004227 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004228 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004229 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004230
4231 // Recurse to parse whatever is left.
4232 ParseDeclaratorInternal(D, DirectDeclParser);
4233
4234 // Sema will have to catch (syntactically invalid) pointers into global
4235 // scope. It has to catch pointers into namespace scope anyway.
4236 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004237 Loc),
4238 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004239 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004240 return;
4241 }
4242 }
4243
4244 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004245 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004246 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004247 if (DirectDeclParser)
4248 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004249 return;
4250 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004251
Sebastian Redled0f3b02009-03-15 22:02:01 +00004252 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4253 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004254 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004255 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004256
Chris Lattner9eac9312009-03-27 04:18:06 +00004257 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004258 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004259 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004260
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004261 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004262 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004263 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004264
Bill Wendling3708c182007-05-27 10:15:43 +00004265 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004266 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004267 if (Kind == tok::star)
4268 // Remember that we parsed a pointer type, and remember the type-quals.
4269 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004270 DS.getConstSpecLoc(),
4271 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004272 DS.getRestrictSpecLoc()),
4273 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004274 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004275 else
4276 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004277 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004278 Loc),
4279 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004280 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004281 } else {
4282 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004283 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004284
Sebastian Redl3b27be62009-03-23 00:00:23 +00004285 // Complain about rvalue references in C++03, but then go on and build
4286 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004287 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004288 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004289 diag::warn_cxx98_compat_rvalue_reference :
4290 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004291
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004292 // GNU-style and C++11 attributes are allowed here, as is restrict.
4293 ParseTypeQualifierListOpt(DS);
4294 D.ExtendWithDeclSpec(DS);
4295
Bill Wendling93efb222007-06-02 23:28:54 +00004296 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4297 // cv-qualifiers are introduced through the use of a typedef or of a
4298 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004299 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4300 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4301 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004302 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004303 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4304 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004305 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004306 }
Bill Wendling3708c182007-05-27 10:15:43 +00004307
4308 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004309 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004310
Douglas Gregor66583c52008-11-03 15:51:28 +00004311 if (D.getNumTypeObjects() > 0) {
4312 // C++ [dcl.ref]p4: There shall be no references to references.
4313 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4314 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004315 if (const IdentifierInfo *II = D.getIdentifier())
4316 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4317 << II;
4318 else
4319 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4320 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004321
Sebastian Redlbd150f42008-11-21 19:14:01 +00004322 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004323 // can go ahead and build the (technically ill-formed)
4324 // declarator: reference collapsing will take care of it.
4325 }
4326 }
4327
Bill Wendling3708c182007-05-27 10:15:43 +00004328 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004329 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004330 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004331 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004332 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004333 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004334}
4335
Richard Smith0efa75c2012-03-29 01:16:42 +00004336static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4337 SourceLocation EllipsisLoc) {
4338 if (EllipsisLoc.isValid()) {
4339 FixItHint Insertion;
4340 if (!D.getEllipsisLoc().isValid()) {
4341 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4342 D.setEllipsisLoc(EllipsisLoc);
4343 }
4344 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4345 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4346 }
4347}
4348
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004349/// ParseDirectDeclarator
4350/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004351/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004352/// '(' declarator ')'
4353/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004354/// [C90] direct-declarator '[' constant-expression[opt] ']'
4355/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4356/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4357/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4358/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004359/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4360/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004361/// direct-declarator '(' parameter-type-list ')'
4362/// direct-declarator '(' identifier-list[opt] ')'
4363/// [GNU] direct-declarator '(' parameter-forward-declarations
4364/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004365/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4366/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004367/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4368/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4369/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004370/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004371/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004372///
4373/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004374/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004375/// '::'[opt] nested-name-specifier[opt] type-name
4376///
4377/// id-expression: [C++ 5.1]
4378/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004379/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004380///
4381/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004382/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004383/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004384/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004385/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004386/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004387///
Richard Smith1453e312012-03-27 01:42:32 +00004388/// Note, any additional constructs added here may need corresponding changes
4389/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004390void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004391 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004392
David Blaikiebbafb8a2012-03-11 07:00:24 +00004393 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004394 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004395 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004396 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4397 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004398 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004399 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004400 }
4401
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004402 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004403 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004404 // Change the declaration context for name lookup, until this function
4405 // is exited (and the declarator has been parsed).
4406 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004407 }
4408
Douglas Gregor27b4c162010-12-23 22:44:42 +00004409 // C++0x [dcl.fct]p14:
4410 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004411 // of a parameter-declaration-clause without a preceding comma. In
4412 // this case, the ellipsis is parsed as part of the
4413 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004414 // parameter pack that has not been expanded; otherwise, it is parsed
4415 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004416 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004417 !((D.getContext() == Declarator::PrototypeContext ||
4418 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004419 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004420 !Actions.containsUnexpandedParameterPacks(D))) {
4421 SourceLocation EllipsisLoc = ConsumeToken();
4422 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4423 // The ellipsis was put in the wrong place. Recover, and explain to
4424 // the user what they should have done.
4425 ParseDeclarator(D);
4426 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4427 return;
4428 } else
4429 D.setEllipsisLoc(EllipsisLoc);
4430
4431 // The ellipsis can't be followed by a parenthesized declarator. We
4432 // check for that in ParseParenDeclarator, after we have disambiguated
4433 // the l_paren token.
4434 }
4435
Douglas Gregor7861a802009-11-03 01:35:08 +00004436 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4437 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4438 // We found something that indicates the start of an unqualified-id.
4439 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004440 bool AllowConstructorName;
4441 if (D.getDeclSpec().hasTypeSpecifier())
4442 AllowConstructorName = false;
4443 else if (D.getCXXScopeSpec().isSet())
4444 AllowConstructorName =
4445 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004446 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004447 else
4448 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4449
Abramo Bagnara7945c982012-01-27 09:46:47 +00004450 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004451 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4452 /*EnteringContext=*/true,
4453 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004454 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004455 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004456 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004457 D.getName()) ||
4458 // Once we're past the identifier, if the scope was bad, mark the
4459 // whole declarator bad.
4460 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004461 D.SetIdentifier(0, Tok.getLocation());
4462 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004463 } else {
4464 // Parsed the unqualified-id; update range information and move along.
4465 if (D.getSourceRange().getBegin().isInvalid())
4466 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4467 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004468 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004469 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004470 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004471 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004472 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004473 "There's a C++-specific check for tok::identifier above");
4474 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4475 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4476 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004477 goto PastIdentifier;
4478 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004479
Douglas Gregor7861a802009-11-03 01:35:08 +00004480 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004481 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004482 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004483 // Example: 'char (*X)' or 'int (*XX)(void)'
4484 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004485
4486 // If the declarator was parenthesized, we entered the declarator
4487 // scope when parsing the parenthesized declarator, then exited
4488 // the scope already. Re-enter the scope, if we need to.
4489 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004490 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004491 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004492 if (!D.isInvalidType() &&
4493 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004494 // Change the declaration context for name lookup, until this function
4495 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004496 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004497 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004498 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004499 // This could be something simple like "int" (in which case the declarator
4500 // portion is empty), if an abstract-declarator is allowed.
4501 D.SetIdentifier(0, Tok.getLocation());
4502 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004503 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004504 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004505 if (D.getContext() == Declarator::MemberContext)
4506 Diag(Tok, diag::err_expected_member_name_or_semi)
4507 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004508 else if (getLangOpts().CPlusPlus) {
4509 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4510 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4511 else
4512 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4513 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004514 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004515 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004516 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004517 }
Mike Stump11289f42009-09-09 15:08:12 +00004518
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004519 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004520 assert(D.isPastIdentifier() &&
4521 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004522
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004523 // Don't parse attributes unless we have parsed an unparenthesized name.
4524 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004525 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004526
Chris Lattneracd58a32006-08-06 17:24:14 +00004527 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004528 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004529 // Enter function-declaration scope, limiting any declarators to the
4530 // function prototype scope, including parameter declarators.
4531 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004532 Scope::FunctionPrototypeScope|Scope::DeclScope|
4533 (D.isFunctionDeclaratorAFunctionDeclaration()
4534 ? Scope::FunctionDeclarationScope : 0));
4535
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004536 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4537 // In such a case, check if we actually have a function declarator; if it
4538 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004539 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004540 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4541 // The name of the declarator, if any, is tentatively declared within
4542 // a possible direct initializer.
4543 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4544 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4545 TentativelyDeclaredIdentifiers.pop_back();
4546 if (!IsFunctionDecl)
4547 break;
4548 }
John McCall084e83d2011-03-24 11:26:52 +00004549 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004550 BalancedDelimiterTracker T(*this, tok::l_paren);
4551 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004552 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004553 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004554 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004555 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004556 } else {
4557 break;
4558 }
4559 }
Chad Rosierc1183952012-06-26 22:30:43 +00004560}
Chris Lattneracd58a32006-08-06 17:24:14 +00004561
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004562/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4563/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004564/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004565/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4566///
4567/// direct-declarator:
4568/// '(' declarator ')'
4569/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004570/// direct-declarator '(' parameter-type-list ')'
4571/// direct-declarator '(' identifier-list[opt] ')'
4572/// [GNU] direct-declarator '(' parameter-forward-declarations
4573/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004574///
4575void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004576 BalancedDelimiterTracker T(*this, tok::l_paren);
4577 T.consumeOpen();
4578
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004579 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004580
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004581 // Eat any attributes before we look at whether this is a grouping or function
4582 // declarator paren. If this is a grouping paren, the attribute applies to
4583 // the type being built up, for example:
4584 // int (__attribute__(()) *x)(long y)
4585 // If this ends up not being a grouping paren, the attribute applies to the
4586 // first argument, for example:
4587 // int (__attribute__(()) int x)
4588 // In either case, we need to eat any attributes to be able to determine what
4589 // sort of paren this is.
4590 //
John McCall084e83d2011-03-24 11:26:52 +00004591 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004592 bool RequiresArg = false;
4593 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004594 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004595
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004596 // We require that the argument list (if this is a non-grouping paren) be
4597 // present even if the attribute list was empty.
4598 RequiresArg = true;
4599 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004600
Steve Naroff44ac7772008-12-25 14:16:32 +00004601 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004602 ParseMicrosoftTypeAttributes(attrs);
4603
Dawn Perchik335e16b2010-09-03 01:29:35 +00004604 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004605 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004606 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004607
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004608 // If we haven't past the identifier yet (or where the identifier would be
4609 // stored, if this is an abstract declarator), then this is probably just
4610 // grouping parens. However, if this could be an abstract-declarator, then
4611 // this could also be the start of function arguments (consider 'void()').
4612 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004613
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004614 if (!D.mayOmitIdentifier()) {
4615 // If this can't be an abstract-declarator, this *must* be a grouping
4616 // paren, because we haven't seen the identifier yet.
4617 isGrouping = true;
4618 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004619 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4620 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004621 isDeclarationSpecifier() || // 'int(int)' is a function.
4622 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004623 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4624 // considered to be a type, not a K&R identifier-list.
4625 isGrouping = false;
4626 } else {
4627 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4628 isGrouping = true;
4629 }
Mike Stump11289f42009-09-09 15:08:12 +00004630
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004631 // If this is a grouping paren, handle:
4632 // direct-declarator: '(' declarator ')'
4633 // direct-declarator: '(' attributes declarator ')'
4634 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004635 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4636 D.setEllipsisLoc(SourceLocation());
4637
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004638 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004639 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004640 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004641 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004642 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004643 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004644 T.getCloseLocation()),
4645 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004646
4647 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004648
4649 // An ellipsis cannot be placed outside parentheses.
4650 if (EllipsisLoc.isValid())
4651 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4652
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004653 return;
4654 }
Mike Stump11289f42009-09-09 15:08:12 +00004655
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004656 // Okay, if this wasn't a grouping paren, it must be the start of a function
4657 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004658 // identifier (and remember where it would have been), then call into
4659 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004660 D.SetIdentifier(0, Tok.getLocation());
4661
David Blaikie15a430a2011-12-04 05:04:18 +00004662 // Enter function-declaration scope, limiting any declarators to the
4663 // function prototype scope, including parameter declarators.
4664 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004665 Scope::FunctionPrototypeScope | Scope::DeclScope |
4666 (D.isFunctionDeclaratorAFunctionDeclaration()
4667 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004668 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004669 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004670}
4671
4672/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4673/// declarator D up to a paren, which indicates that we are parsing function
4674/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004675///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004676/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4677/// immediately after the open paren - they should be considered to be the
4678/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004679///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004680/// If RequiresArg is true, then the first argument of the function is required
4681/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004682///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004683/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4684/// (C++11) ref-qualifier[opt], exception-specification[opt],
4685/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4686///
4687/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004688/// dynamic-exception-specification
4689/// noexcept-specification
4690///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004691void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004692 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004693 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004694 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004695 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004696 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004697 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004698 // lparen is already consumed!
4699 assert(D.isPastIdentifier() && "Should not call before identifier!");
4700
4701 // This should be true when the function has typed arguments.
4702 // Otherwise, it is treated as a K&R-style function.
4703 bool HasProto = false;
4704 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004705 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004706 // Remember where we see an ellipsis, if any.
4707 SourceLocation EllipsisLoc;
4708
4709 DeclSpec DS(AttrFactory);
4710 bool RefQualifierIsLValueRef = true;
4711 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004712 SourceLocation ConstQualifierLoc;
4713 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004714 ExceptionSpecificationType ESpecType = EST_None;
4715 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004716 SmallVector<ParsedType, 2> DynamicExceptions;
4717 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004718 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004719 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004720 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004721
James Molloy6f8780b2012-02-29 10:24:19 +00004722 Actions.ActOnStartFunctionDeclarator();
4723
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004724 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4725 EndLoc is the end location for the function declarator.
4726 They differ for trailing return types. */
4727 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004728 SourceLocation LParenLoc, RParenLoc;
4729 LParenLoc = Tracker.getOpenLocation();
4730 StartLoc = LParenLoc;
4731
Douglas Gregor9e66af42011-07-05 16:44:18 +00004732 if (isFunctionDeclaratorIdentifierList()) {
4733 if (RequiresArg)
4734 Diag(Tok, diag::err_argument_required_after_attribute);
4735
4736 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4737
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004738 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004739 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004740 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004741 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004742 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004743 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004744 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004745 else if (RequiresArg)
4746 Diag(Tok, diag::err_argument_required_after_attribute);
4747
David Blaikiebbafb8a2012-03-11 07:00:24 +00004748 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004749
4750 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004751 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004752 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004753 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004754 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004755
David Blaikiebbafb8a2012-03-11 07:00:24 +00004756 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004757 // FIXME: Accept these components in any order, and produce fixits to
4758 // correct the order if the user gets it wrong. Ideally we should deal
4759 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004760
4761 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004762 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4763 if (!DS.getSourceRange().getEnd().isInvalid()) {
4764 EndLoc = DS.getSourceRange().getEnd();
4765 ConstQualifierLoc = DS.getConstSpecLoc();
4766 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4767 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004768
4769 // Parse ref-qualifier[opt].
4770 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004771 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004772 diag::warn_cxx98_compat_ref_qualifier :
4773 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004774
Douglas Gregor9e66af42011-07-05 16:44:18 +00004775 RefQualifierIsLValueRef = Tok.is(tok::amp);
4776 RefQualifierLoc = ConsumeToken();
4777 EndLoc = RefQualifierLoc;
4778 }
4779
Douglas Gregor3024f072012-04-16 07:05:22 +00004780 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004781 // If a declaration declares a member function or member function
4782 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004783 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004784 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004785 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004786 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004787 getLangOpts().CPlusPlus11 &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004788 (D.getContext() == Declarator::MemberContext ||
4789 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004790 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004791 Actions.CurContext->isRecord()));
4792 Sema::CXXThisScopeRAII ThisScope(Actions,
4793 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00004794 DS.getTypeQualifiers() |
4795 (D.getDeclSpec().isConstexprSpecified()
4796 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00004797 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004798
Douglas Gregor9e66af42011-07-05 16:44:18 +00004799 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004800 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004801 DynamicExceptions,
4802 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004803 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004804 if (ESpecType != EST_None)
4805 EndLoc = ESpecRange.getEnd();
4806
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004807 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4808 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00004809 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004810
Douglas Gregor9e66af42011-07-05 16:44:18 +00004811 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004812 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004813 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004814 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004815 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4816 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004817 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004818 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004819 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004820 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004821 }
4822 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004823 }
4824
4825 // Remember that we parsed a function type, and remember the attributes.
4826 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004827 IsAmbiguous,
4828 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004829 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004830 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004831 DS.getTypeQualifiers(),
4832 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004833 RefQualifierLoc, ConstQualifierLoc,
4834 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004835 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004836 ESpecType, ESpecRange.getBegin(),
4837 DynamicExceptions.data(),
4838 DynamicExceptionRanges.data(),
4839 DynamicExceptions.size(),
4840 NoexceptExpr.isUsable() ?
4841 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004842 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004843 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004844 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004845
4846 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004847}
4848
4849/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4850/// identifier list form for a K&R-style function: void foo(a,b,c)
4851///
4852/// Note that identifier-lists are only allowed for normal declarators, not for
4853/// abstract-declarators.
4854bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004855 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004856 && Tok.is(tok::identifier)
4857 && !TryAltiVecVectorToken()
4858 // K&R identifier lists can't have typedefs as identifiers, per C99
4859 // 6.7.5.3p11.
4860 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4861 // Identifier lists follow a really simple grammar: the identifiers can
4862 // be followed *only* by a ", identifier" or ")". However, K&R
4863 // identifier lists are really rare in the brave new modern world, and
4864 // it is very common for someone to typo a type in a non-K&R style
4865 // list. If we are presented with something like: "void foo(intptr x,
4866 // float y)", we don't want to start parsing the function declarator as
4867 // though it is a K&R style declarator just because intptr is an
4868 // invalid type.
4869 //
4870 // To handle this, we check to see if the token after the first
4871 // identifier is a "," or ")". Only then do we parse it as an
4872 // identifier list.
4873 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4874}
4875
4876/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4877/// we found a K&R-style identifier list instead of a typed parameter list.
4878///
4879/// After returning, ParamInfo will hold the parsed parameters.
4880///
4881/// identifier-list: [C99 6.7.5]
4882/// identifier
4883/// identifier-list ',' identifier
4884///
4885void Parser::ParseFunctionDeclaratorIdentifierList(
4886 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004887 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004888 // If there was no identifier specified for the declarator, either we are in
4889 // an abstract-declarator, or we are in a parameter declarator which was found
4890 // to be abstract. In abstract-declarators, identifier lists are not valid:
4891 // diagnose this.
4892 if (!D.getIdentifier())
4893 Diag(Tok, diag::ext_ident_list_in_param);
4894
4895 // Maintain an efficient lookup of params we have seen so far.
4896 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4897
4898 while (1) {
4899 // If this isn't an identifier, report the error and skip until ')'.
4900 if (Tok.isNot(tok::identifier)) {
4901 Diag(Tok, diag::err_expected_ident);
4902 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4903 // Forget we parsed anything.
4904 ParamInfo.clear();
4905 return;
4906 }
4907
4908 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4909
4910 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4911 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4912 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4913
4914 // Verify that the argument identifier has not already been mentioned.
4915 if (!ParamsSoFar.insert(ParmII)) {
4916 Diag(Tok, diag::err_param_redefinition) << ParmII;
4917 } else {
4918 // Remember this identifier in ParamInfo.
4919 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4920 Tok.getLocation(),
4921 0));
4922 }
4923
4924 // Eat the identifier.
4925 ConsumeToken();
4926
4927 // The list continues if we see a comma.
4928 if (Tok.isNot(tok::comma))
4929 break;
4930 ConsumeToken();
4931 }
4932}
4933
4934/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4935/// after the opening parenthesis. This function will not parse a K&R-style
4936/// identifier list.
4937///
Richard Smith2620cd92012-04-11 04:01:28 +00004938/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4939/// caller parsed those arguments immediately after the open paren - they should
4940/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004941///
4942/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4943/// be the location of the ellipsis, if any was parsed.
4944///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004945/// parameter-type-list: [C99 6.7.5]
4946/// parameter-list
4947/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004948/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004949///
4950/// parameter-list: [C99 6.7.5]
4951/// parameter-declaration
4952/// parameter-list ',' parameter-declaration
4953///
4954/// parameter-declaration: [C99 6.7.5]
4955/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004956/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004957/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004958/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004959/// declaration-specifiers abstract-declarator[opt]
4960/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004961/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004962/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004963/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004964///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004965void Parser::ParseParameterDeclarationClause(
4966 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004967 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004968 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004969 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004970
Chris Lattner371ed4e2008-04-06 06:57:35 +00004971 while (1) {
4972 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004973 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4974 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004975 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004976 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004977 }
Mike Stump11289f42009-09-09 15:08:12 +00004978
Chris Lattner371ed4e2008-04-06 06:57:35 +00004979 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004980 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004981 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004982
Richard Smith2620cd92012-04-11 04:01:28 +00004983 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00004984 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00004985
John McCall53fa7142010-12-24 02:08:15 +00004986 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00004987 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00004988
4989 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004990
4991 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004992 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004993 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004994 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4995 // too much hassle.
4996 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004997
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004998 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004999
Chris Lattner371ed4e2008-04-06 06:57:35 +00005000 // Parse the declarator. This is "PrototypeContext", because we must
5001 // accept either 'declarator' or 'abstract-declarator' here.
5002 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5003 ParseDeclarator(ParmDecl);
5004
5005 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005006 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005007
Chris Lattner371ed4e2008-04-06 06:57:35 +00005008 // Remember this parsed parameter in ParamInfo.
5009 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005010
Douglas Gregor4d87df52008-12-16 21:30:33 +00005011 // DefArgToks is used when the parsing of default arguments needs
5012 // to be delayed.
5013 CachedTokens *DefArgToks = 0;
5014
Chris Lattner371ed4e2008-04-06 06:57:35 +00005015 // If no parameter was specified, verify that *something* was specified,
5016 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005017 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5018 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005019 // Completely missing, emit error.
5020 Diag(DSStart, diag::err_missing_param);
5021 } else {
5022 // Otherwise, we have something. Add it and let semantic analysis try
5023 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005024
Chris Lattner371ed4e2008-04-06 06:57:35 +00005025 // Inform the actions module about the parameter declarator, so it gets
5026 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005027 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005028
5029 // Parse the default argument, if any. We parse the default
5030 // arguments in all dialects; the semantic analysis in
5031 // ActOnParamDefaultArgument will reject the default argument in
5032 // C.
5033 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005034 SourceLocation EqualLoc = Tok.getLocation();
5035
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005036 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005037 if (D.getContext() == Declarator::MemberContext) {
5038 // If we're inside a class definition, cache the tokens
5039 // corresponding to the default argument. We'll actually parse
5040 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005041 // FIXME: Can we use a smart pointer for Toks?
5042 DefArgToks = new CachedTokens;
5043
Mike Stump11289f42009-09-09 15:08:12 +00005044 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005045 /*StopAtSemi=*/true,
5046 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005047 delete DefArgToks;
5048 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005049 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005050 } else {
5051 // Mark the end of the default argument so that we know when to
5052 // stop when we parse it later on.
5053 Token DefArgEnd;
5054 DefArgEnd.startToken();
5055 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5056 DefArgEnd.setLocation(Tok.getLocation());
5057 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005058 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005059 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005060 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005061 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005062 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005063 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005064
Chad Rosierc1183952012-06-26 22:30:43 +00005065 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005066 // used.
5067 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005068 Sema::PotentiallyEvaluatedIfUsed,
5069 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005070
Sebastian Redldb63af22012-03-14 15:54:00 +00005071 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005072 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005073 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005074 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005075 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005076 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005077 if (DefArgResult.isInvalid()) {
5078 Actions.ActOnParamDefaultArgumentError(Param);
5079 SkipUntil(tok::comma, tok::r_paren, true, true);
5080 } else {
5081 // Inform the actions module about the default argument
5082 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005083 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005084 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005085 }
5086 }
Mike Stump11289f42009-09-09 15:08:12 +00005087
5088 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5089 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005090 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005091 }
5092
5093 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005094 if (Tok.isNot(tok::comma)) {
5095 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005096 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005097
David Blaikiebbafb8a2012-03-11 07:00:24 +00005098 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005099 // We have ellipsis without a preceding ',', which is ill-formed
5100 // in C. Complain and provide the fix.
5101 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005102 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005103 }
5104 }
Chad Rosierc1183952012-06-26 22:30:43 +00005105
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005106 break;
5107 }
Mike Stump11289f42009-09-09 15:08:12 +00005108
Chris Lattner371ed4e2008-04-06 06:57:35 +00005109 // Consume the comma.
5110 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005111 }
Mike Stump11289f42009-09-09 15:08:12 +00005112
Chris Lattner6c940e62008-04-06 06:34:08 +00005113}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005114
Chris Lattnere8074e62006-08-06 18:30:15 +00005115/// [C90] direct-declarator '[' constant-expression[opt] ']'
5116/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5117/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5118/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5119/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005120/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5121/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005122void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005123 if (CheckProhibitedCXX11Attribute())
5124 return;
5125
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005126 BalancedDelimiterTracker T(*this, tok::l_square);
5127 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005128
Chris Lattner84a11622008-12-18 07:27:21 +00005129 // C array syntax has many features, but by-far the most common is [] and [4].
5130 // This code does a fast path to handle some of the most obvious cases.
5131 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005132 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005133 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005134 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005135
Chris Lattner84a11622008-12-18 07:27:21 +00005136 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005137 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005138 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005139 T.getOpenLocation(),
5140 T.getCloseLocation()),
5141 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005142 return;
5143 } else if (Tok.getKind() == tok::numeric_constant &&
5144 GetLookAheadToken(1).is(tok::r_square)) {
5145 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005146 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005147 ConsumeToken();
5148
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005149 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005150 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005151 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005152
Chris Lattner84a11622008-12-18 07:27:21 +00005153 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005154 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005155 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005156 T.getOpenLocation(),
5157 T.getCloseLocation()),
5158 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005159 return;
5160 }
Mike Stump11289f42009-09-09 15:08:12 +00005161
Chris Lattnere8074e62006-08-06 18:30:15 +00005162 // If valid, this location is the position where we read the 'static' keyword.
5163 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005164 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005165 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005166
Chris Lattnere8074e62006-08-06 18:30:15 +00005167 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005168 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005169 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005170 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005171
Chris Lattnere8074e62006-08-06 18:30:15 +00005172 // If we haven't already read 'static', check to see if there is one after the
5173 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005174 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005175 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005176
Chris Lattnere8074e62006-08-06 18:30:15 +00005177 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005178 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005179 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005180
Chris Lattner521ff2b2008-04-06 05:26:30 +00005181 // Handle the case where we have '[*]' as the array size. However, a leading
5182 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005183 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005184 // infrequent, use of lookahead is not costly here.
5185 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005186 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005187
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005188 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005189 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005190 StaticLoc = SourceLocation(); // Drop the static.
5191 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005192 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005193 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005194 // Note, in C89, this production uses the constant-expr production instead
5195 // of assignment-expr. The only difference is that assignment-expr allows
5196 // things like '=' and '*='. Sema rejects these in C89 mode because they
5197 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005198
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005199 // Parse the constant-expression or assignment-expression now (depending
5200 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005201 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005202 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005203 } else {
5204 EnterExpressionEvaluationContext Unevaluated(Actions,
5205 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005206 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005207 }
Chris Lattner62591722006-08-12 18:40:58 +00005208 }
Mike Stump11289f42009-09-09 15:08:12 +00005209
Chris Lattner62591722006-08-12 18:40:58 +00005210 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005211 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005212 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005213 // If the expression was invalid, skip it.
5214 SkipUntil(tok::r_square);
5215 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005216 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005217
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005218 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005219
John McCall084e83d2011-03-24 11:26:52 +00005220 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005221 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005222
Chris Lattner84a11622008-12-18 07:27:21 +00005223 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005224 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005225 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005226 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005227 T.getOpenLocation(),
5228 T.getCloseLocation()),
5229 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005230}
5231
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005232/// [GNU] typeof-specifier:
5233/// typeof ( expressions )
5234/// typeof ( type-name )
5235/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005236///
5237void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005238 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005239 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005240 SourceLocation StartLoc = ConsumeToken();
5241
John McCalle8595032010-01-13 20:03:27 +00005242 const bool hasParens = Tok.is(tok::l_paren);
5243
Eli Friedman15681d62012-09-26 04:34:21 +00005244 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5245 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005246
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005247 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005248 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005249 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005250 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5251 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005252 if (hasParens)
5253 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005254
5255 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005256 // FIXME: Not accurate, the range gets one token more than it should.
5257 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005258 else
5259 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005260
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005261 if (isCastExpr) {
5262 if (!CastTy) {
5263 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005264 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005265 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005266
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005267 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005268 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005269 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5270 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005271 DiagID, CastTy))
5272 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005273 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005274 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005275
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005276 // If we get here, the operand to the typeof was an expresion.
5277 if (Operand.isInvalid()) {
5278 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005279 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005280 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005281
Eli Friedmane0afc982012-01-21 01:01:51 +00005282 // We might need to transform the operand if it is potentially evaluated.
5283 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5284 if (Operand.isInvalid()) {
5285 DS.SetTypeSpecError();
5286 return;
5287 }
5288
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005289 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005290 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005291 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5292 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005293 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005294 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005295}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005296
Benjamin Kramere56f3932011-12-23 17:00:35 +00005297/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005298/// _Atomic ( type-name )
5299///
5300void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5301 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5302
5303 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005304 BalancedDelimiterTracker T(*this, tok::l_paren);
5305 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005306 SkipUntil(tok::r_paren);
5307 return;
5308 }
5309
5310 TypeResult Result = ParseTypeName();
5311 if (Result.isInvalid()) {
5312 SkipUntil(tok::r_paren);
5313 return;
5314 }
5315
5316 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005317 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005318
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005319 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005320 return;
5321
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005322 DS.setTypeofParensRange(T.getRange());
5323 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005324
5325 const char *PrevSpec = 0;
5326 unsigned DiagID;
5327 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5328 DiagID, Result.release()))
5329 Diag(StartLoc, DiagID) << PrevSpec;
5330}
5331
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005332
5333/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5334/// from TryAltiVecVectorToken.
5335bool Parser::TryAltiVecVectorTokenOutOfLine() {
5336 Token Next = NextToken();
5337 switch (Next.getKind()) {
5338 default: return false;
5339 case tok::kw_short:
5340 case tok::kw_long:
5341 case tok::kw_signed:
5342 case tok::kw_unsigned:
5343 case tok::kw_void:
5344 case tok::kw_char:
5345 case tok::kw_int:
5346 case tok::kw_float:
5347 case tok::kw_double:
5348 case tok::kw_bool:
5349 case tok::kw___pixel:
5350 Tok.setKind(tok::kw___vector);
5351 return true;
5352 case tok::identifier:
5353 if (Next.getIdentifierInfo() == Ident_pixel) {
5354 Tok.setKind(tok::kw___vector);
5355 return true;
5356 }
5357 return false;
5358 }
5359}
5360
5361bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5362 const char *&PrevSpec, unsigned &DiagID,
5363 bool &isInvalid) {
5364 if (Tok.getIdentifierInfo() == Ident_vector) {
5365 Token Next = NextToken();
5366 switch (Next.getKind()) {
5367 case tok::kw_short:
5368 case tok::kw_long:
5369 case tok::kw_signed:
5370 case tok::kw_unsigned:
5371 case tok::kw_void:
5372 case tok::kw_char:
5373 case tok::kw_int:
5374 case tok::kw_float:
5375 case tok::kw_double:
5376 case tok::kw_bool:
5377 case tok::kw___pixel:
5378 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5379 return true;
5380 case tok::identifier:
5381 if (Next.getIdentifierInfo() == Ident_pixel) {
5382 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5383 return true;
5384 }
5385 break;
5386 default:
5387 break;
5388 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005389 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005390 DS.isTypeAltiVecVector()) {
5391 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5392 return true;
5393 }
5394 return false;
5395}