blob: e6e010bd9cc07cdd27cb0df2e31f54252ad3a9ad [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"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000017#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000019#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000023#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// C99 6.7: Declarations.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032/// ParseTypeName
33/// type-name: [C99 6.7.6]
34/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000035///
36/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000037TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000038 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000039 AccessSpecifier AS,
40 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000041 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000042 if (DSC == DSC_normal)
43 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000044
Chris Lattnerf5fbd792006-08-10 23:56:11 +000045 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000046 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000047 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000048 if (OwnedType)
49 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000050
Chris Lattnerf5fbd792006-08-10 23:56:11 +000051 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000052 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000053 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000054 if (Range)
55 *Range = DeclaratorInfo.getSourceRange();
56
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000057 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000058 return true;
59
Douglas Gregor0be31a22010-07-02 17:43:08 +000060 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000061}
62
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000063
64/// isAttributeLateParsed - Return true if the attribute has arguments that
65/// require late parsing.
66static bool isAttributeLateParsed(const IdentifierInfo &II) {
67 return llvm::StringSwitch<bool>(II.getName())
68#include "clang/Parse/AttrLateParsed.inc"
69 .Default(false);
70}
71
Alexis Hunt96d5c762009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000097///
Steve Naroff0f2fe172007-06-01 17:11:19 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithb12bf692011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000109
John McCall53fa7142010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000120 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144
Bill Wendling44426052012-12-20 19:22:21 +0000145 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000146 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000147 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
Michael Han23214e52012-10-03 01:56:22 +0000158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000159 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 }
161 } else {
John McCall084e83d2011-03-24 11:26:52 +0000162 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000163 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000164 }
165 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000167 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000168 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000169 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
170 SkipUntil(tok::r_paren, false);
171 }
John McCall53fa7142010-12-24 02:08:15 +0000172 if (endLoc)
173 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000176
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000177
Michael Han23214e52012-10-03 01:56:22 +0000178/// Parse the arguments to a parameterized GNU attribute or
179/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000180void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
181 SourceLocation AttrNameLoc,
182 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000183 SourceLocation *EndLoc,
184 IdentifierInfo *ScopeName,
185 SourceLocation ScopeLoc,
186 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000187
188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
189
190 // Availability attributes have their own grammar.
191 if (AttrName->isStr("availability")) {
192 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
195 // Thread safety attributes fit into the FIXME case above, so we
196 // just parse the arguments as a list of expressions
197 if (IsThreadSafetyAttribute(AttrName->getName())) {
198 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
199 return;
200 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000201 // Type safety attributes have their own grammar.
202 if (AttrName->isStr("type_tag_for_datatype")) {
203 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000206
207 ConsumeParen(); // ignore the left paren loc for now
208
Richard Smithb12bf692011-10-17 21:20:17 +0000209 IdentifierInfo *ParmName = 0;
210 SourceLocation ParmLoc;
211 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000212
Richard Smithb12bf692011-10-17 21:20:17 +0000213 switch (Tok.getKind()) {
214 case tok::kw_char:
215 case tok::kw_wchar_t:
216 case tok::kw_char16_t:
217 case tok::kw_char32_t:
218 case tok::kw_bool:
219 case tok::kw_short:
220 case tok::kw_int:
221 case tok::kw_long:
222 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000223 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000224 case tok::kw_signed:
225 case tok::kw_unsigned:
226 case tok::kw_float:
227 case tok::kw_double:
228 case tok::kw_void:
229 case tok::kw_typeof:
230 // __attribute__(( vec_type_hint(char) ))
231 // FIXME: Don't just discard the builtin type token.
232 ConsumeToken();
233 BuiltinType = true;
234 break;
235
236 case tok::identifier:
237 ParmName = Tok.getIdentifierInfo();
238 ParmLoc = ConsumeToken();
239 break;
240
241 default:
242 break;
243 }
244
Benjamin Kramerf0623432012-08-23 22:51:59 +0000245 ExprVector ArgExprs;
Richard Smithb12bf692011-10-17 21:20:17 +0000246
247 if (!BuiltinType &&
248 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
249 // Eat the comma.
250 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000251 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000252
Richard Smithb12bf692011-10-17 21:20:17 +0000253 // Parse the non-empty comma-separated list of expressions.
254 while (1) {
255 ExprResult ArgExpr(ParseAssignmentExpression());
256 if (ArgExpr.isInvalid()) {
257 SkipUntil(tok::r_paren);
258 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 }
Richard Smithb12bf692011-10-17 21:20:17 +0000260 ArgExprs.push_back(ArgExpr.release());
261 if (Tok.isNot(tok::comma))
262 break;
263 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264 }
Richard Smithb12bf692011-10-17 21:20:17 +0000265 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000266 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
267 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
268 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000269 while (Tok.is(tok::identifier)) {
270 ConsumeToken();
271 if (Tok.is(tok::greater))
272 break;
273 if (Tok.is(tok::comma)) {
274 ConsumeToken();
275 continue;
276 }
277 }
278 if (Tok.isNot(tok::greater))
279 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000280 SkipUntil(tok::r_paren, false, true); // skip until ')'
281 }
282 }
Richard Smithb12bf692011-10-17 21:20:17 +0000283
284 SourceLocation RParen = Tok.getLocation();
285 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000286 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb12bf692011-10-17 21:20:17 +0000287 AttributeList *attr =
Michael Han360d2252012-10-04 16:42:52 +0000288 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han23214e52012-10-03 01:56:22 +0000289 ScopeName, ScopeLoc, ParmName, ParmLoc,
290 ArgExprs.data(), ArgExprs.size(), Syntax);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000291 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000292 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000293 }
294}
295
Chad Rosierc1183952012-06-26 22:30:43 +0000296/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000297/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000298void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000299 SourceLocation AttrNameLoc,
300 ParsedAttributes &Attrs)
301{
302 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000303 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000304 AttrName->getNameStart(), tok::r_paren))
305 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000306
Aaron Ballman478faed2012-06-19 22:09:27 +0000307 ExprResult ArgExpr(ParseConstantExpression());
308 if (ArgExpr.isInvalid()) {
309 T.skipToEnd();
310 return;
311 }
312 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000313 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000314 &ExprList, 1, AttributeList::AS_Declspec);
315
316 T.consumeClose();
317}
318
Chad Rosierc1183952012-06-26 22:30:43 +0000319/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000320/// arguments.
321bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
322 return llvm::StringSwitch<bool>(Ident->getName())
323 .Case("dllimport", true)
324 .Case("dllexport", true)
325 .Case("noreturn", true)
326 .Case("nothrow", true)
327 .Case("noinline", true)
328 .Case("naked", true)
329 .Case("appdomain", true)
330 .Case("process", true)
331 .Case("jitintrinsic", true)
332 .Case("noalias", true)
333 .Case("restrict", true)
334 .Case("novtable", true)
335 .Case("selectany", true)
336 .Case("thread", true)
337 .Default(false);
338}
339
Chad Rosierc1183952012-06-26 22:30:43 +0000340/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000341/// parameters). Will return false if we properly handled the declspec, or
342/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000343void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000344 SourceLocation Loc,
345 ParsedAttributes &Attrs) {
346 // Try to handle the easy case first -- these declspecs all take a single
347 // parameter as their argument.
348 if (llvm::StringSwitch<bool>(Ident->getName())
349 .Case("uuid", true)
350 .Case("align", true)
351 .Case("allocate", true)
352 .Default(false)) {
353 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
354 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000355 // The deprecated declspec has an optional single argument, so we will
356 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000357 // not.
358 if (Tok.getKind() == tok::l_paren)
359 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
360 else
Chad Rosierc1183952012-06-26 22:30:43 +0000361 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000362 AttributeList::AS_Declspec);
363 } else if (Ident->getName() == "property") {
364 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000365 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000366 // must be named get or put.
367 //
Chad Rosierc1183952012-06-26 22:30:43 +0000368 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 // property expression.
370 //
371 // FIXME: we should deal with __declspec(property) at some point because it
372 // is used in the platform SDK headers for the Parallel Patterns Library
373 // and ATL.
374 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000376 Ident->getNameStart(), tok::r_paren))
377 return;
378 T.skipToEnd();
379 } else {
380 // We don't recognize this as a valid declspec, but instead of creating the
381 // attribute and allowing sema to warn about it, we will warn here instead.
382 // This is because some attributes have multiple spellings, but we need to
383 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000384 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000385 // both locations.
386 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
387
388 // If there's an open paren, we should eat the open and close parens under
389 // the assumption that this unknown declspec has parameters.
390 BalancedDelimiterTracker T(*this, tok::l_paren);
391 if (!T.consumeOpen())
392 T.skipToEnd();
393 }
394}
395
Eli Friedman06de2b52009-06-08 07:21:15 +0000396/// [MS] decl-specifier:
397/// __declspec ( extended-decl-modifier-seq )
398///
399/// [MS] extended-decl-modifier-seq:
400/// extended-decl-modifier[opt]
401/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000402void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000403 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000404
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000405 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000407 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000409 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000410
Chad Rosierc1183952012-06-26 22:30:43 +0000411 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 // you can specify multiple attributes per declspec.
413 while (Tok.getKind() != tok::r_paren) {
414 // We expect either a well-known identifier or a generic string. Anything
415 // else is a malformed declspec.
416 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000417 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000418 Tok.getKind() != tok::kw_restrict) {
419 Diag(Tok, diag::err_ms_declspec_type);
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000422 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000423
424 IdentifierInfo *AttrName;
425 SourceLocation AttrNameLoc;
426 if (IsString) {
427 SmallString<8> StrBuffer;
428 bool Invalid = false;
429 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
430 if (Invalid) {
431 T.skipToEnd();
432 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000433 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000434 AttrName = PP.getIdentifierInfo(Str);
435 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000436 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 AttrName = Tok.getIdentifierInfo();
438 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000439 }
Chad Rosierc1183952012-06-26 22:30:43 +0000440
Aaron Ballman478faed2012-06-19 22:09:27 +0000441 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000442 // If we have a generic string, we will allow it because there is no
443 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000444 // (for instance, SAL declspecs in older versions of MSVC).
445 //
Chad Rosierc1183952012-06-26 22:30:43 +0000446 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000447 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000448 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000449 0, 0, AttributeList::AS_Declspec);
450 else
451 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000452 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000453 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000454}
455
John McCall53fa7142010-12-24 02:08:15 +0000456void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000457 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000458 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000459 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000460 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000461 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000465 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000466 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000467}
468
John McCall53fa7142010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000470 // Treat these like attributes
471 while (Tok.is(tok::kw___pascal)) {
472 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
473 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000475 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000476 }
John McCall53fa7142010-12-24 02:08:15 +0000477}
478
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000479void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
480 // Treat these like attributes
481 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000482 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000483 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000484 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
485 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000490 // FIXME: The mapping from attribute spelling to semantics should be
491 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000492 SourceLocation Loc = Tok.getLocation();
493 switch(Tok.getKind()) {
494 // OpenCL qualifiers:
495 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000496 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000498 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000501
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000507
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000510 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000513
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000514 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000516 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000519
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000520 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000522 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000525
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000526 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000527 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000528 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000529 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000530 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000531
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000532 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000533 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000534 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000535 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000536 break;
537 default: break;
538 }
539}
540
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000541/// \brief Parse a version number.
542///
543/// version:
544/// simple-integer
545/// simple-integer ',' simple-integer
546/// simple-integer ',' simple-integer ',' simple-integer
547VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
548 Range = Tok.getLocation();
549
550 if (!Tok.is(tok::numeric_constant)) {
551 Diag(Tok, diag::err_expected_version);
552 SkipUntil(tok::comma, tok::r_paren, true, true, true);
553 return VersionTuple();
554 }
555
556 // Parse the major (and possibly minor and subminor) versions, which
557 // are stored in the numeric constant. We utilize a quirk of the
558 // lexer, which is that it handles something like 1.2.3 as a single
559 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000560 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000561 Buffer.resize(Tok.getLength()+1);
562 const char *ThisTokBegin = &Buffer[0];
563
564 // Get the spelling of the token, which eliminates trigraphs, etc.
565 bool Invalid = false;
566 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
567 if (Invalid)
568 return VersionTuple();
569
570 // Parse the major version.
571 unsigned AfterMajor = 0;
572 unsigned Major = 0;
573 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
574 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
575 ++AfterMajor;
576 }
577
578 if (AfterMajor == 0) {
579 Diag(Tok, diag::err_expected_version);
580 SkipUntil(tok::comma, tok::r_paren, true, true, true);
581 return VersionTuple();
582 }
583
584 if (AfterMajor == ActualLength) {
585 ConsumeToken();
586
587 // We only had a single version component.
588 if (Major == 0) {
589 Diag(Tok, diag::err_zero_version);
590 return VersionTuple();
591 }
592
593 return VersionTuple(Major);
594 }
595
596 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
597 Diag(Tok, diag::err_expected_version);
598 SkipUntil(tok::comma, tok::r_paren, true, true, true);
599 return VersionTuple();
600 }
601
602 // Parse the minor version.
603 unsigned AfterMinor = AfterMajor + 1;
604 unsigned Minor = 0;
605 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
606 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
607 ++AfterMinor;
608 }
609
610 if (AfterMinor == ActualLength) {
611 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000612
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000613 // We had major.minor.
614 if (Major == 0 && Minor == 0) {
615 Diag(Tok, diag::err_zero_version);
616 return VersionTuple();
617 }
618
Chad Rosierc1183952012-06-26 22:30:43 +0000619 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000620 }
621
622 // If what follows is not a '.', we have a problem.
623 if (ThisTokBegin[AfterMinor] != '.') {
624 Diag(Tok, diag::err_expected_version);
625 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000626 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000627 }
628
629 // Parse the subminor version.
630 unsigned AfterSubminor = AfterMinor + 1;
631 unsigned Subminor = 0;
632 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
633 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
634 ++AfterSubminor;
635 }
636
637 if (AfterSubminor != ActualLength) {
638 Diag(Tok, diag::err_expected_version);
639 SkipUntil(tok::comma, tok::r_paren, true, true, true);
640 return VersionTuple();
641 }
642 ConsumeToken();
643 return VersionTuple(Major, Minor, Subminor);
644}
645
646/// \brief Parse the contents of the "availability" attribute.
647///
648/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000649/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000650///
651/// platform:
652/// identifier
653///
654/// version-arg-list:
655/// version-arg
656/// version-arg ',' version-arg-list
657///
658/// version-arg:
659/// 'introduced' '=' version
660/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000661/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000662/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000663/// opt-message:
664/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000665void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
666 SourceLocation AvailabilityLoc,
667 ParsedAttributes &attrs,
668 SourceLocation *endLoc) {
669 SourceLocation PlatformLoc;
670 IdentifierInfo *Platform = 0;
671
672 enum { Introduced, Deprecated, Obsoleted, Unknown };
673 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000674 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000675
676 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000677 BalancedDelimiterTracker T(*this, tok::l_paren);
678 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000679 Diag(Tok, diag::err_expected_lparen);
680 return;
681 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000682
683 // Parse the platform name,
684 if (Tok.isNot(tok::identifier)) {
685 Diag(Tok, diag::err_availability_expected_platform);
686 SkipUntil(tok::r_paren);
687 return;
688 }
689 Platform = Tok.getIdentifierInfo();
690 PlatformLoc = ConsumeToken();
691
692 // Parse the ',' following the platform name.
693 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
694 return;
695
696 // If we haven't grabbed the pointers for the identifiers
697 // "introduced", "deprecated", and "obsoleted", do so now.
698 if (!Ident_introduced) {
699 Ident_introduced = PP.getIdentifierInfo("introduced");
700 Ident_deprecated = PP.getIdentifierInfo("deprecated");
701 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000702 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000703 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000704 }
705
706 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000707 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000708 do {
709 if (Tok.isNot(tok::identifier)) {
710 Diag(Tok, diag::err_availability_expected_change);
711 SkipUntil(tok::r_paren);
712 return;
713 }
714 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
715 SourceLocation KeywordLoc = ConsumeToken();
716
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000717 if (Keyword == Ident_unavailable) {
718 if (UnavailableLoc.isValid()) {
719 Diag(KeywordLoc, diag::err_availability_redundant)
720 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000721 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000722 UnavailableLoc = KeywordLoc;
723
724 if (Tok.isNot(tok::comma))
725 break;
726
727 ConsumeToken();
728 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000729 }
730
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000731 if (Tok.isNot(tok::equal)) {
732 Diag(Tok, diag::err_expected_equal_after)
733 << Keyword;
734 SkipUntil(tok::r_paren);
735 return;
736 }
737 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000738 if (Keyword == Ident_message) {
739 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000740 Diag(Tok, diag::err_expected_string_literal)
741 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000742 SkipUntil(tok::r_paren);
743 return;
744 }
745 MessageExpr = ParseStringLiteralExpression();
746 break;
747 }
Chad Rosierc1183952012-06-26 22:30:43 +0000748
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 SourceRange VersionRange;
750 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000751
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000752 if (Version.empty()) {
753 SkipUntil(tok::r_paren);
754 return;
755 }
756
757 unsigned Index;
758 if (Keyword == Ident_introduced)
759 Index = Introduced;
760 else if (Keyword == Ident_deprecated)
761 Index = Deprecated;
762 else if (Keyword == Ident_obsoleted)
763 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000764 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 Index = Unknown;
766
767 if (Index < Unknown) {
768 if (!Changes[Index].KeywordLoc.isInvalid()) {
769 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000770 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000771 << SourceRange(Changes[Index].KeywordLoc,
772 Changes[Index].VersionRange.getEnd());
773 }
774
775 Changes[Index].KeywordLoc = KeywordLoc;
776 Changes[Index].Version = Version;
777 Changes[Index].VersionRange = VersionRange;
778 } else {
779 Diag(KeywordLoc, diag::err_availability_unknown_change)
780 << Keyword << VersionRange;
781 }
782
783 if (Tok.isNot(tok::comma))
784 break;
785
786 ConsumeToken();
787 } while (true);
788
789 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000790 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000791 return;
792
793 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000794 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000795
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000796 // The 'unavailable' availability cannot be combined with any other
797 // availability changes. Make sure that hasn't happened.
798 if (UnavailableLoc.isValid()) {
799 bool Complained = false;
800 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
801 if (Changes[Index].KeywordLoc.isValid()) {
802 if (!Complained) {
803 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
804 << SourceRange(Changes[Index].KeywordLoc,
805 Changes[Index].VersionRange.getEnd());
806 Complained = true;
807 }
808
809 // Clear out the availability.
810 Changes[Index] = AvailabilityChange();
811 }
812 }
813 }
814
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000815 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000816 attrs.addNew(&Availability,
817 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000818 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000819 Platform, PlatformLoc,
820 Changes[Introduced],
821 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000822 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000823 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000824 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000825}
826
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000827
Bill Wendling44426052012-12-20 19:22:21 +0000828// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000829// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
830
831void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
832
833void Parser::LateParsedClass::ParseLexedAttributes() {
834 Self->ParseLexedAttributes(*Class);
835}
836
837void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000838 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000839}
840
841/// Wrapper class which calls ParseLexedAttribute, after setting up the
842/// scope appropriately.
843void Parser::ParseLexedAttributes(ParsingClass &Class) {
844 // Deal with templates
845 // FIXME: Test cases to make sure this does the right thing for templates.
846 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
847 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
848 HasTemplateScope);
849 if (HasTemplateScope)
850 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
851
Douglas Gregor3024f072012-04-16 07:05:22 +0000852 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000853 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000854 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000855 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
856 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
857
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000858 // Enter the scope of nested classes
859 if (!AlreadyHasClassScope)
860 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000862 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000863 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
864 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
865 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000866 }
Chad Rosierc1183952012-06-26 22:30:43 +0000867
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000868 if (!AlreadyHasClassScope)
869 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
870 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000871}
872
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000873
874/// \brief Parse all attributes in LAs, and attach them to Decl D.
875void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
876 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000877 assert(LAs.parseSoon() &&
878 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000879 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000880 if (D)
881 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000882 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000883 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000884 }
885 LAs.clear();
886}
887
888
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000889/// \brief Finish parsing an attribute for which parsing was delayed.
890/// This will be called at the end of parsing a class declaration
891/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000892/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000893/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000894void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
895 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000896 // Save the current token position.
897 SourceLocation OrigLoc = Tok.getLocation();
898
899 // Append the current token at the end of the new token stream so that it
900 // doesn't get lost.
901 LA.Toks.push_back(Tok);
902 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
903 // Consume the previously pushed token.
904 ConsumeAnyToken();
905
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000906 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +0000907 // FIXME: Do not warn on C++11 attributes, once we start supporting
908 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000909 Diag(Tok, diag::warn_attribute_on_function_definition)
910 << LA.AttrName.getName();
911 }
912
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000913 ParsedAttributes Attrs(AttrFactory);
914 SourceLocation endLoc;
915
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000916 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000917 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000918 NamedDecl *ND = dyn_cast<NamedDecl>(D);
919 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000920
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000921 // Allow 'this' within late-parsed attributes.
922 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
923 /*TypeQuals=*/0,
924 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000925
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000926 if (LA.Decls.size() == 1) {
927 // If the Decl is templatized, add template parameters to scope.
928 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
929 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
930 if (HasTemplateScope)
931 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000932
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000933 // If the Decl is on a function, add function parameters to the scope.
934 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
935 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
936 if (HasFunScope)
937 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000938
Michael Han23214e52012-10-03 01:56:22 +0000939 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000940 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000941
942 if (HasFunScope) {
943 Actions.ActOnExitFunctionContext();
944 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
945 }
946 if (HasTemplateScope) {
947 TempScope.Exit();
948 }
949 } else {
950 // If there are multiple decls, then the decl cannot be within the
951 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000952 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000953 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000954 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000955 } else {
956 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000957 }
958
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000959 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
960 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
961 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000962
963 if (Tok.getLocation() != OrigLoc) {
964 // Due to a parsing error, we either went over the cached tokens or
965 // there are still cached tokens left, so we skip the leftover tokens.
966 // Since this is an uncommon situation that should be avoided, use the
967 // expensive isBeforeInTranslationUnit call.
968 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
969 OrigLoc))
970 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000971 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972 }
973}
974
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000975/// \brief Wrapper around a case statement checking if AttrName is
976/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000977bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000978 return llvm::StringSwitch<bool>(AttrName)
979 .Case("guarded_by", true)
980 .Case("guarded_var", true)
981 .Case("pt_guarded_by", true)
982 .Case("pt_guarded_var", true)
983 .Case("lockable", true)
984 .Case("scoped_lockable", true)
985 .Case("no_thread_safety_analysis", true)
986 .Case("acquired_after", true)
987 .Case("acquired_before", true)
988 .Case("exclusive_lock_function", true)
989 .Case("shared_lock_function", true)
990 .Case("exclusive_trylock_function", true)
991 .Case("shared_trylock_function", true)
992 .Case("unlock_function", true)
993 .Case("lock_returned", true)
994 .Case("locks_excluded", true)
995 .Case("exclusive_locks_required", true)
996 .Case("shared_locks_required", true)
997 .Default(false);
998}
999
1000/// \brief Parse the contents of thread safety attributes. These
1001/// should always be parsed as an expression list.
1002///
1003/// We need to special case the parsing due to the fact that if the first token
1004/// of the first argument is an identifier, the main parse loop will store
1005/// that token as a "parameter" and the rest of
1006/// the arguments will be added to a list of "arguments". However,
1007/// subsequent tokens in the first argument are lost. We instead parse each
1008/// argument as an expression and add all arguments to the list of "arguments".
1009/// In future, we will take advantage of this special case to also
1010/// deal with some argument scoping issues here (for example, referring to a
1011/// function parameter in the attribute on that function).
1012void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1013 SourceLocation AttrNameLoc,
1014 ParsedAttributes &Attrs,
1015 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001016 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001017
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001018 BalancedDelimiterTracker T(*this, tok::l_paren);
1019 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001020
Benjamin Kramerf0623432012-08-23 22:51:59 +00001021 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001022 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001023
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001024 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001025 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001026 ExprResult ArgExpr(ParseAssignmentExpression());
1027 if (ArgExpr.isInvalid()) {
1028 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001029 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001030 break;
1031 } else {
1032 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001033 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001034 if (Tok.isNot(tok::comma))
1035 break;
1036 ConsumeToken(); // Eat the comma, move to the next argument
1037 }
1038 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001039 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001040 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001041 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001042 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001043 if (EndLoc)
1044 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001045}
1046
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001047void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1048 SourceLocation AttrNameLoc,
1049 ParsedAttributes &Attrs,
1050 SourceLocation *EndLoc) {
1051 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1052
1053 BalancedDelimiterTracker T(*this, tok::l_paren);
1054 T.consumeOpen();
1055
1056 if (Tok.isNot(tok::identifier)) {
1057 Diag(Tok, diag::err_expected_ident);
1058 T.skipToEnd();
1059 return;
1060 }
1061 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1062 SourceLocation ArgumentKindLoc = ConsumeToken();
1063
1064 if (Tok.isNot(tok::comma)) {
1065 Diag(Tok, diag::err_expected_comma);
1066 T.skipToEnd();
1067 return;
1068 }
1069 ConsumeToken();
1070
1071 SourceRange MatchingCTypeRange;
1072 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1073 if (MatchingCType.isInvalid()) {
1074 T.skipToEnd();
1075 return;
1076 }
1077
1078 bool LayoutCompatible = false;
1079 bool MustBeNull = false;
1080 while (Tok.is(tok::comma)) {
1081 ConsumeToken();
1082 if (Tok.isNot(tok::identifier)) {
1083 Diag(Tok, diag::err_expected_ident);
1084 T.skipToEnd();
1085 return;
1086 }
1087 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1088 if (Flag->isStr("layout_compatible"))
1089 LayoutCompatible = true;
1090 else if (Flag->isStr("must_be_null"))
1091 MustBeNull = true;
1092 else {
1093 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1094 T.skipToEnd();
1095 return;
1096 }
1097 ConsumeToken(); // consume flag
1098 }
1099
1100 if (!T.consumeClose()) {
1101 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1102 ArgumentKind, ArgumentKindLoc,
1103 MatchingCType.release(), LayoutCompatible,
1104 MustBeNull, AttributeList::AS_GNU);
1105 }
1106
1107 if (EndLoc)
1108 *EndLoc = T.getCloseLocation();
1109}
1110
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001111/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1112/// of a C++11 attribute-specifier in a location where an attribute is not
1113/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1114/// situation.
1115///
1116/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1117/// this doesn't appear to actually be an attribute-specifier, and the caller
1118/// should try to parse it.
1119bool Parser::DiagnoseProhibitedCXX11Attribute() {
1120 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1121
1122 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1123 case CAK_NotAttributeSpecifier:
1124 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1125 return false;
1126
1127 case CAK_InvalidAttributeSpecifier:
1128 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1129 return false;
1130
1131 case CAK_AttributeSpecifier:
1132 // Parse and discard the attributes.
1133 SourceLocation BeginLoc = ConsumeBracket();
1134 ConsumeBracket();
1135 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1136 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1137 SourceLocation EndLoc = ConsumeBracket();
1138 Diag(BeginLoc, diag::err_attributes_not_allowed)
1139 << SourceRange(BeginLoc, EndLoc);
1140 return true;
1141 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001142 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001143}
1144
John McCall53fa7142010-12-24 02:08:15 +00001145void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1146 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1147 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001148}
1149
Michael Han64536a62012-11-06 19:34:54 +00001150void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1151 AttributeList *AttrList = attrs.getList();
1152 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001153 if (AttrList->isCXX11Attribute()) {
Michael Han64536a62012-11-06 19:34:54 +00001154 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1155 << AttrList->getName();
1156 AttrList->setInvalid();
1157 }
1158 AttrList = AttrList->getNext();
1159 }
1160}
1161
Chris Lattner53361ac2006-08-10 05:19:57 +00001162/// ParseDeclaration - Parse a full 'declaration', which consists of
1163/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001164/// 'Context' should be a Declarator::TheContext value. This returns the
1165/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001166///
1167/// declaration: [C99 6.7]
1168/// block-declaration ->
1169/// simple-declaration
1170/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001171/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001172/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001173/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001174/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001175/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001176/// others... [FIXME]
1177///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001178Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1179 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001180 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001181 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001182 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001183 // Must temporarily exit the objective-c container scope for
1184 // parsing c none objective-c decls.
1185 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001186
John McCall48871652010-08-21 09:40:31 +00001187 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001188 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001189 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001190 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001191 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001192 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001193 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001194 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001195 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001196 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001197 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001198 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001199 SourceLocation InlineLoc = ConsumeToken();
1200 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1201 break;
1202 }
Chad Rosierc1183952012-06-26 22:30:43 +00001203 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001204 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001205 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001206 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001207 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001208 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001209 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001210 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001211 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001212 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001213 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001214 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001215 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001216 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001217 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001218 default:
John McCall53fa7142010-12-24 02:08:15 +00001219 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001220 }
Chad Rosierc1183952012-06-26 22:30:43 +00001221
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001222 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001223 // single decl, convert it now. Alias declarations can also declare a type;
1224 // include that too if it is present.
1225 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001226}
1227
1228/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1229/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001230/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1231/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001232///[C90/C++]init-declarator-list ';' [TODO]
1233/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001234///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001235/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001236/// attribute-specifier-seq[opt] type-specifier-seq declarator
1237///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001238/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001239/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001240///
1241/// If FRI is non-null, we might be parsing a for-range-declaration instead
1242/// of a simple-declaration. If we find that we are, we also parse the
1243/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001244Parser::DeclGroupPtrTy
1245Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1246 SourceLocation &DeclEnd,
1247 ParsedAttributesWithRange &attrs,
1248 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001249 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001250 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001251 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001252
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001253 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001254 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001255
Chris Lattner0e894622006-08-13 19:58:17 +00001256 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1257 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001258 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001259 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001260 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001261 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001262 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001263 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001264 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001265 }
Chad Rosierc1183952012-06-26 22:30:43 +00001266
1267 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001268}
Mike Stump11289f42009-09-09 15:08:12 +00001269
Richard Smith09f76ee2011-10-19 21:33:05 +00001270/// Returns true if this might be the start of a declarator, or a common typo
1271/// for a declarator.
1272bool Parser::MightBeDeclarator(unsigned Context) {
1273 switch (Tok.getKind()) {
1274 case tok::annot_cxxscope:
1275 case tok::annot_template_id:
1276 case tok::caret:
1277 case tok::code_completion:
1278 case tok::coloncolon:
1279 case tok::ellipsis:
1280 case tok::kw___attribute:
1281 case tok::kw_operator:
1282 case tok::l_paren:
1283 case tok::star:
1284 return true;
1285
1286 case tok::amp:
1287 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001288 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001289
Richard Smithc8a79032012-01-09 22:31:44 +00001290 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001291 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001292 NextToken().is(tok::l_square);
1293
1294 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001295 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001296
Richard Smith09f76ee2011-10-19 21:33:05 +00001297 case tok::identifier:
1298 switch (NextToken().getKind()) {
1299 case tok::code_completion:
1300 case tok::coloncolon:
1301 case tok::comma:
1302 case tok::equal:
1303 case tok::equalequal: // Might be a typo for '='.
1304 case tok::kw_alignas:
1305 case tok::kw_asm:
1306 case tok::kw___attribute:
1307 case tok::l_brace:
1308 case tok::l_paren:
1309 case tok::l_square:
1310 case tok::less:
1311 case tok::r_brace:
1312 case tok::r_paren:
1313 case tok::r_square:
1314 case tok::semi:
1315 return true;
1316
1317 case tok::colon:
1318 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001319 // and in block scope it's probably a label. Inside a class definition,
1320 // this is a bit-field.
1321 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001322 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001323
1324 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001325 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001326
1327 default:
1328 return false;
1329 }
1330
1331 default:
1332 return false;
1333 }
1334}
1335
Richard Smithb8caac82012-04-11 20:59:20 +00001336/// Skip until we reach something which seems like a sensible place to pick
1337/// up parsing after a malformed declaration. This will sometimes stop sooner
1338/// than SkipUntil(tok::r_brace) would, but will never stop later.
1339void Parser::SkipMalformedDecl() {
1340 while (true) {
1341 switch (Tok.getKind()) {
1342 case tok::l_brace:
1343 // Skip until matching }, then stop. We've probably skipped over
1344 // a malformed class or function definition or similar.
1345 ConsumeBrace();
1346 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1347 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1348 // This declaration isn't over yet. Keep skipping.
1349 continue;
1350 }
1351 if (Tok.is(tok::semi))
1352 ConsumeToken();
1353 return;
1354
1355 case tok::l_square:
1356 ConsumeBracket();
1357 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1358 continue;
1359
1360 case tok::l_paren:
1361 ConsumeParen();
1362 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1363 continue;
1364
1365 case tok::r_brace:
1366 return;
1367
1368 case tok::semi:
1369 ConsumeToken();
1370 return;
1371
1372 case tok::kw_inline:
1373 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001374 // a good place to pick back up parsing, except in an Objective-C
1375 // @interface context.
1376 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1377 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001378 return;
1379 break;
1380
1381 case tok::kw_namespace:
1382 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001383 // place to pick back up parsing, except in an Objective-C
1384 // @interface context.
1385 if (Tok.isAtStartOfLine() &&
1386 (!ParsingInObjCContainer || CurParsedObjCImpl))
1387 return;
1388 break;
1389
1390 case tok::at:
1391 // @end is very much like } in Objective-C contexts.
1392 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1393 ParsingInObjCContainer)
1394 return;
1395 break;
1396
1397 case tok::minus:
1398 case tok::plus:
1399 // - and + probably start new method declarations in Objective-C contexts.
1400 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001401 return;
1402 break;
1403
1404 case tok::eof:
1405 return;
1406
1407 default:
1408 break;
1409 }
1410
1411 ConsumeAnyToken();
1412 }
1413}
1414
John McCalld5a36322009-11-03 19:26:08 +00001415/// ParseDeclGroup - Having concluded that this is either a function
1416/// definition or a group of object declarations, actually parse the
1417/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001418Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1419 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001420 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001421 SourceLocation *DeclEnd,
1422 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001423 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001424 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001425 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001426
John McCalld5a36322009-11-03 19:26:08 +00001427 // Bail out if the first declarator didn't seem well-formed.
1428 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001429 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001430 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001433 // Save late-parsed attributes for now; they need to be parsed in the
1434 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001435 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1436 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001437 if (D.isFunctionDeclarator())
1438 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1439
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001440 // Check to see if we have a function *definition* which must have a body.
1441 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1442 // Look at the next token to make sure that this isn't a function
1443 // declaration. We have to check this because __attribute__ might be the
1444 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001445 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001446
Chris Lattner13901342010-07-11 22:42:07 +00001447 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001448 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1449 Diag(Tok, diag::err_function_declared_typedef);
1450
1451 // Recover by treating the 'typedef' as spurious.
1452 DS.ClearStorageClassSpecs();
1453 }
1454
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001455 Decl *TheDecl =
1456 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001457 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001458 }
Chad Rosierc1183952012-06-26 22:30:43 +00001459
Chris Lattner13901342010-07-11 22:42:07 +00001460 if (isDeclarationSpecifier()) {
1461 // If there is an invalid declaration specifier right after the function
1462 // prototype, then we must be in a missing semicolon case where this isn't
1463 // actually a body. Just fall through into the code that handles it as a
1464 // prototype, and let the top-level code handle the erroneous declspec
1465 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001466 } else {
1467 Diag(Tok, diag::err_expected_fn_body);
1468 SkipUntil(tok::semi);
1469 return DeclGroupPtrTy();
1470 }
1471 }
1472
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001473 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001474 return DeclGroupPtrTy();
1475
1476 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1477 // must parse and analyze the for-range-initializer before the declaration is
1478 // analyzed.
1479 if (FRI && Tok.is(tok::colon)) {
1480 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001481 if (Tok.is(tok::l_brace))
1482 FRI->RangeExpr = ParseBraceInitializer();
1483 else
1484 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001485 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1486 Actions.ActOnCXXForRangeDecl(ThisDecl);
1487 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001488 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001489 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1490 }
1491
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001492 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001493 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001494 if (LateParsedAttrs.size() > 0)
1495 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001496 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001497 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001498 DeclsInGroup.push_back(FirstDecl);
1499
Richard Smith09f76ee2011-10-19 21:33:05 +00001500 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001501
John McCalld5a36322009-11-03 19:26:08 +00001502 // If we don't have a comma, it is either the end of the list (a ';') or an
1503 // error, bail out.
1504 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001505 SourceLocation CommaLoc = ConsumeToken();
1506
1507 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1508 // This comma was followed by a line-break and something which can't be
1509 // the start of a declarator. The comma was probably a typo for a
1510 // semicolon.
1511 Diag(CommaLoc, diag::err_expected_semi_declaration)
1512 << FixItHint::CreateReplacement(CommaLoc, ";");
1513 ExpectSemi = false;
1514 break;
1515 }
John McCalld5a36322009-11-03 19:26:08 +00001516
1517 // Parse the next declarator.
1518 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001519 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001520
1521 // Accept attributes in an init-declarator. In the first declarator in a
1522 // declaration, these would be part of the declspec. In subsequent
1523 // declarators, they become part of the declarator itself, so that they
1524 // don't apply to declarators after *this* one. Examples:
1525 // short __attribute__((common)) var; -> declspec
1526 // short var __attribute__((common)); -> declarator
1527 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001528 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001529
1530 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001531 if (!D.isInvalidType()) {
1532 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1533 D.complete(ThisDecl);
1534 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001535 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001536 }
John McCalld5a36322009-11-03 19:26:08 +00001537 }
1538
1539 if (DeclEnd)
1540 *DeclEnd = Tok.getLocation();
1541
Richard Smith09f76ee2011-10-19 21:33:05 +00001542 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001543 ExpectAndConsumeSemi(Context == Declarator::FileContext
1544 ? diag::err_invalid_token_after_toplevel_declarator
1545 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001546 // Okay, there was no semicolon and one was expected. If we see a
1547 // declaration specifier, just assume it was missing and continue parsing.
1548 // Otherwise things are very confused and we skip to recover.
1549 if (!isDeclarationSpecifier()) {
1550 SkipUntil(tok::r_brace, true, true);
1551 if (Tok.is(tok::semi))
1552 ConsumeToken();
1553 }
John McCalld5a36322009-11-03 19:26:08 +00001554 }
1555
Douglas Gregor0be31a22010-07-02 17:43:08 +00001556 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001557 DeclsInGroup.data(),
1558 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001559}
1560
Richard Smith02e85f32011-04-14 22:09:26 +00001561/// Parse an optional simple-asm-expr and attributes, and attach them to a
1562/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001563bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001564 // If a simple-asm-expr is present, parse it.
1565 if (Tok.is(tok::kw_asm)) {
1566 SourceLocation Loc;
1567 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1568 if (AsmLabel.isInvalid()) {
1569 SkipUntil(tok::semi, true, true);
1570 return true;
1571 }
1572
1573 D.setAsmLabel(AsmLabel.release());
1574 D.SetRangeEnd(Loc);
1575 }
1576
1577 MaybeParseGNUAttributes(D);
1578 return false;
1579}
1580
Douglas Gregor23996282009-05-12 21:31:51 +00001581/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1582/// declarator'. This method parses the remainder of the declaration
1583/// (including any attributes or initializer, among other things) and
1584/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001585///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001586/// init-declarator: [C99 6.7]
1587/// declarator
1588/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001589/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1590/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001591/// [C++] declarator initializer[opt]
1592///
1593/// [C++] initializer:
1594/// [C++] '=' initializer-clause
1595/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001596/// [C++0x] '=' 'default' [TODO]
1597/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001598/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001599///
1600/// According to the standard grammar, =default and =delete are function
1601/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001602///
John McCall48871652010-08-21 09:40:31 +00001603Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001604 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001605 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001606 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001607
Richard Smith02e85f32011-04-14 22:09:26 +00001608 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1609}
Mike Stump11289f42009-09-09 15:08:12 +00001610
Richard Smith02e85f32011-04-14 22:09:26 +00001611Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1612 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001613 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001614 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001615 switch (TemplateInfo.Kind) {
1616 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001617 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001618 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001619
Douglas Gregor450f00842009-09-25 18:43:00 +00001620 case ParsedTemplateInfo::Template:
1621 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001622 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001623 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001624 D);
1625 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001626
Douglas Gregor450f00842009-09-25 18:43:00 +00001627 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001628 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001629 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001630 TemplateInfo.ExternLoc,
1631 TemplateInfo.TemplateLoc,
1632 D);
1633 if (ThisRes.isInvalid()) {
1634 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001635 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001636 }
Chad Rosierc1183952012-06-26 22:30:43 +00001637
Douglas Gregor450f00842009-09-25 18:43:00 +00001638 ThisDecl = ThisRes.get();
1639 break;
1640 }
1641 }
Mike Stump11289f42009-09-09 15:08:12 +00001642
Richard Smith30482bc2011-02-20 03:19:35 +00001643 bool TypeContainsAuto =
1644 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1645
Douglas Gregor23996282009-05-12 21:31:51 +00001646 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001647 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001648 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001649 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001650 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001651 if (D.isFunctionDeclarator())
1652 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1653 << 1 /* delete */;
1654 else
1655 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001656 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001657 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001658 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1659 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001660 else
1661 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001662 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001663 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001664 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001665 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001666 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001667
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001668 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001669 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001670 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001671 cutOffParsing();
1672 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001673 }
Chad Rosierc1183952012-06-26 22:30:43 +00001674
John McCalldadc5752010-08-24 06:29:42 +00001675 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001676
David Blaikiebbafb8a2012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001678 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001679 ExitScope();
1680 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001681
Douglas Gregor23996282009-05-12 21:31:51 +00001682 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001683 SkipUntil(tok::comma, true, true);
1684 Actions.ActOnInitializerError(ThisDecl);
1685 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001686 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1687 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001688 }
1689 } else if (Tok.is(tok::l_paren)) {
1690 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001691 BalancedDelimiterTracker T(*this, tok::l_paren);
1692 T.consumeOpen();
1693
Benjamin Kramerf0623432012-08-23 22:51:59 +00001694 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001695 CommaLocsTy CommaLocs;
1696
David Blaikiebbafb8a2012-03-11 07:00:24 +00001697 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001698 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001699 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001700 }
1701
Douglas Gregor23996282009-05-12 21:31:51 +00001702 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001703 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001704 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001705
David Blaikiebbafb8a2012-03-11 07:00:24 +00001706 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001707 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001708 ExitScope();
1709 }
Douglas Gregor23996282009-05-12 21:31:51 +00001710 } else {
1711 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001712 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001713
1714 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1715 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001716
David Blaikiebbafb8a2012-03-11 07:00:24 +00001717 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001718 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001719 ExitScope();
1720 }
1721
Sebastian Redla9351792012-02-11 23:51:47 +00001722 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1723 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001724 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001725 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1726 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001727 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001728 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001729 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001730 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001731 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1732
Sebastian Redl3da34892011-06-05 12:23:16 +00001733 if (D.getCXXScopeSpec().isSet()) {
1734 EnterScope(0);
1735 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1736 }
1737
1738 ExprResult Init(ParseBraceInitializer());
1739
1740 if (D.getCXXScopeSpec().isSet()) {
1741 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1742 ExitScope();
1743 }
1744
1745 if (Init.isInvalid()) {
1746 Actions.ActOnInitializerError(ThisDecl);
1747 } else
1748 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1749 /*DirectInit=*/true, TypeContainsAuto);
1750
Douglas Gregor23996282009-05-12 21:31:51 +00001751 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001752 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001753 }
1754
Richard Smithb2bc2e62011-02-21 20:05:19 +00001755 Actions.FinalizeDeclaration(ThisDecl);
1756
Douglas Gregor23996282009-05-12 21:31:51 +00001757 return ThisDecl;
1758}
1759
Chris Lattner1890ac82006-08-13 01:16:23 +00001760/// ParseSpecifierQualifierList
1761/// specifier-qualifier-list:
1762/// type-specifier specifier-qualifier-list[opt]
1763/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001764/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001765///
Richard Smithc5b05522012-03-12 07:56:15 +00001766void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1767 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001768 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1769 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001770 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001771 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001772
Chris Lattner1890ac82006-08-13 01:16:23 +00001773 // Validate declspec for type-name.
1774 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001775 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1776 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001777 Diag(Tok, diag::err_expected_type);
1778 DS.SetTypeSpecError();
1779 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1780 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001781 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001782 if (!DS.hasTypeSpecifier())
1783 DS.SetTypeSpecError();
1784 }
Mike Stump11289f42009-09-09 15:08:12 +00001785
Chris Lattner1b22eed2006-11-28 05:12:07 +00001786 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001787 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001788 if (DS.getStorageClassSpecLoc().isValid())
1789 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1790 else
1791 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001792 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001793 }
Mike Stump11289f42009-09-09 15:08:12 +00001794
Chris Lattner1b22eed2006-11-28 05:12:07 +00001795 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001796 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001797 if (DS.isInlineSpecified())
1798 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1799 if (DS.isVirtualSpecified())
1800 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1801 if (DS.isExplicitSpecified())
1802 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001803 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001804 }
Richard Smithc5b05522012-03-12 07:56:15 +00001805
1806 // Issue diagnostic and remove constexpr specfier if present.
1807 if (DS.isConstexprSpecified()) {
1808 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1809 DS.ClearConstexprSpec();
1810 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001811}
Chris Lattner53361ac2006-08-10 05:19:57 +00001812
Chris Lattner6cc055a2009-04-12 20:42:31 +00001813/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1814/// specified token is valid after the identifier in a declarator which
1815/// immediately follows the declspec. For example, these things are valid:
1816///
1817/// int x [ 4]; // direct-declarator
1818/// int x ( int y); // direct-declarator
1819/// int(int x ) // direct-declarator
1820/// int x ; // simple-declaration
1821/// int x = 17; // init-declarator-list
1822/// int x , y; // init-declarator-list
1823/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001824/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001825/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001826///
1827/// This is not, because 'x' does not immediately follow the declspec (though
1828/// ')' happens to be valid anyway).
1829/// int (x)
1830///
1831static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1832 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1833 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001834 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001835}
1836
Chris Lattner20a0c612009-04-14 21:34:55 +00001837
1838/// ParseImplicitInt - This method is called when we have an non-typename
1839/// identifier in a declspec (which normally terminates the decl spec) when
1840/// the declspec has no type specifier. In this case, the declspec is either
1841/// malformed or is "implicit int" (in K&R and C89).
1842///
1843/// This method handles diagnosing this prettily and returns false if the
1844/// declspec is done being processed. If it recovers and thinks there may be
1845/// other pieces of declspec after it, it returns true.
1846///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001847bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001848 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00001849 AccessSpecifier AS, DeclSpecContext DSC,
1850 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001851 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001852
Chris Lattner20a0c612009-04-14 21:34:55 +00001853 SourceLocation Loc = Tok.getLocation();
1854 // If we see an identifier that is not a type name, we normally would
1855 // parse it as the identifer being declared. However, when a typename
1856 // is typo'd or the definition is not included, this will incorrectly
1857 // parse the typename as the identifier name and fall over misparsing
1858 // later parts of the diagnostic.
1859 //
1860 // As such, we try to do some look-ahead in cases where this would
1861 // otherwise be an "implicit-int" case to see if this is invalid. For
1862 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1863 // an identifier with implicit int, we'd get a parse error because the
1864 // next token is obviously invalid for a type. Parse these as a case
1865 // with an invalid type specifier.
1866 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001867
Chris Lattner20a0c612009-04-14 21:34:55 +00001868 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001869 // error, do lookahead to try to do better recovery. This never applies
1870 // within a type specifier. Outside of C++, we allow this even if the
1871 // language doesn't "officially" support implicit int -- we support
1872 // implicit int as an extension in C99 and C11. Allegedly, MS also
1873 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001874 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001875 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001876 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001877 // If this token is valid for implicit int, e.g. "static x = 4", then
1878 // we just avoid eating the identifier, so it will be parsed as the
1879 // identifier in the declarator.
1880 return false;
1881 }
Mike Stump11289f42009-09-09 15:08:12 +00001882
Richard Smitha952ebb2012-05-15 21:01:51 +00001883 if (getLangOpts().CPlusPlus &&
1884 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1885 // Don't require a type specifier if we have the 'auto' storage class
1886 // specifier in C++98 -- we'll promote it to a type specifier.
1887 return false;
1888 }
1889
Chris Lattner20a0c612009-04-14 21:34:55 +00001890 // Otherwise, if we don't consume this token, we are going to emit an
1891 // error anyway. Try to recover from various common problems. Check
1892 // to see if this was a reference to a tag name without a tag specified.
1893 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001894 //
1895 // C++ doesn't need this, and isTagName doesn't take SS.
1896 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001897 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001898 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001899
Douglas Gregor0be31a22010-07-02 17:43:08 +00001900 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001901 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001902 case DeclSpec::TST_enum:
1903 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1904 case DeclSpec::TST_union:
1905 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1906 case DeclSpec::TST_struct:
1907 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001908 case DeclSpec::TST_interface:
1909 TagName="__interface"; FixitTagName = "__interface ";
1910 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001911 case DeclSpec::TST_class:
1912 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001913 }
Mike Stump11289f42009-09-09 15:08:12 +00001914
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001915 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001916 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1917 LookupResult R(Actions, TokenName, SourceLocation(),
1918 Sema::LookupOrdinaryName);
1919
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001920 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001921 << TokenName << TagName << getLangOpts().CPlusPlus
1922 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1923
1924 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1925 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1926 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001927 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001928 << TokenName << TagName;
1929 }
Mike Stump11289f42009-09-09 15:08:12 +00001930
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001931 // Parse this as a tag as if the missing tag were present.
1932 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001933 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001934 else
Richard Smithc5b05522012-03-12 07:56:15 +00001935 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00001936 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001937 return true;
1938 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001939 }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Richard Smithfe904f02012-05-15 21:29:55 +00001941 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001942 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001943 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1944 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001945 // Look ahead to the next token to try to figure out what this declaration
1946 // was supposed to be.
1947 switch (NextToken().getKind()) {
1948 case tok::comma:
1949 case tok::equal:
1950 case tok::kw_asm:
1951 case tok::l_brace:
1952 case tok::l_square:
1953 case tok::semi:
1954 // This looks like a variable declaration. The type is probably missing.
1955 // We're done parsing decl-specifiers.
1956 return false;
1957
1958 case tok::l_paren: {
1959 // static x(4); // 'x' is not a type
1960 // x(int n); // 'x' is not a type
1961 // x (*p)[]; // 'x' is a type
1962 //
1963 // Since we're in an error case (or the rare 'implicit int in C++' MS
1964 // extension), we can afford to perform a tentative parse to determine
1965 // which case we're in.
1966 TentativeParsingAction PA(*this);
1967 ConsumeToken();
1968 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1969 PA.Revert();
1970 if (TPR == TPResult::False())
1971 return false;
1972 // The identifier is followed by a parenthesized declarator.
1973 // It's supposed to be a type.
1974 break;
1975 }
1976
1977 default:
1978 // This is probably supposed to be a type. This includes cases like:
1979 // int f(itn);
1980 // struct S { unsinged : 4; };
1981 break;
1982 }
1983 }
1984
Chad Rosierc1183952012-06-26 22:30:43 +00001985 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001986 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001987 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001988 IdentifierInfo *II = Tok.getIdentifierInfo();
1989 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001990 // The action emitted a diagnostic, so we don't have to.
1991 if (T) {
1992 // The action has suggested that the type T could be used. Set that as
1993 // the type in the declaration specifiers, consume the would-be type
1994 // name token, and we're done.
1995 const char *PrevSpec;
1996 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001997 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001998 DS.SetRangeEnd(Tok.getLocation());
1999 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002000 // There may be other declaration specifiers after this.
2001 return true;
2002 } else if (II != Tok.getIdentifierInfo()) {
2003 // If no type was suggested, the correction is to a keyword
2004 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002005 // There may be other declaration specifiers after this.
2006 return true;
2007 }
Chad Rosierc1183952012-06-26 22:30:43 +00002008
Douglas Gregor15e56022009-10-13 23:27:22 +00002009 // Fall through; the action had no suggestion for us.
2010 } else {
2011 // The action did not emit a diagnostic, so emit one now.
2012 SourceRange R;
2013 if (SS) R = SS->getRange();
2014 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
Douglas Gregor15e56022009-10-13 23:27:22 +00002017 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002018 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002019 DS.SetRangeEnd(Tok.getLocation());
2020 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002021
Chris Lattner20a0c612009-04-14 21:34:55 +00002022 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2023 // avoid rippling error messages on subsequent uses of the same type,
2024 // could be useful if #include was forgotten.
2025 return false;
2026}
2027
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002028/// \brief Determine the declaration specifier context from the declarator
2029/// context.
2030///
2031/// \param Context the declarator context, which is one of the
2032/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002033Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002034Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2035 if (Context == Declarator::MemberContext)
2036 return DSC_class;
2037 if (Context == Declarator::FileContext)
2038 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002039 if (Context == Declarator::TrailingReturnContext)
2040 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002041 return DSC_normal;
2042}
2043
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002044/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2045///
2046/// FIXME: Simply returns an alignof() expression if the argument is a
2047/// type. Ideally, the type should be propagated directly into Sema.
2048///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002049/// [C11] type-id
2050/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002051/// [C++0x] type-id ...[opt]
2052/// [C++0x] assignment-expression ...[opt]
2053ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2054 SourceLocation &EllipsisLoc) {
2055 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002056 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002057 SourceLocation TypeLoc = Tok.getLocation();
2058 ParsedType Ty = ParseTypeName().get();
2059 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002060 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2061 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002062 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002063 ER = ParseConstantExpression();
2064
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002065 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002066 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002067
2068 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002069}
2070
2071/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2072/// attribute to Attrs.
2073///
2074/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002075/// [C11] '_Alignas' '(' type-id ')'
2076/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002077/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2078/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002079void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2080 SourceLocation *endLoc) {
2081 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2082 "Not an alignment-specifier!");
2083
2084 SourceLocation KWLoc = Tok.getLocation();
2085 ConsumeToken();
2086
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002087 BalancedDelimiterTracker T(*this, tok::l_paren);
2088 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002089 return;
2090
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002091 SourceLocation EllipsisLoc;
2092 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002093 if (ArgExpr.isInvalid()) {
2094 SkipUntil(tok::r_paren);
2095 return;
2096 }
2097
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002098 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002099 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002100 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002101
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002102 // FIXME: Handle pack-expansions here.
2103 if (EllipsisLoc.isValid()) {
2104 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2105 return;
2106 }
2107
Benjamin Kramerf0623432012-08-23 22:51:59 +00002108 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002109 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002110 // FIXME: This should not be GNU, but we since the attribute used is
2111 // based on the spelling, and there is no true spelling for
2112 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002113 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002114 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002115 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002116}
2117
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002118/// ParseDeclarationSpecifiers
2119/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002120/// storage-class-specifier declaration-specifiers[opt]
2121/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002122/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002123/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002124/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002125/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002126///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002127/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002128/// 'typedef'
2129/// 'extern'
2130/// 'static'
2131/// 'auto'
2132/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002133/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002134/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002135/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002136/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002137/// [C++] 'virtual'
2138/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002139/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002140/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002141/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002142
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002143///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002144void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002145 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002146 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002147 DeclSpecContext DSContext,
2148 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002149 if (DS.getSourceRange().isInvalid()) {
2150 DS.SetRangeStart(Tok.getLocation());
2151 DS.SetRangeEnd(Tok.getLocation());
2152 }
Chad Rosierc1183952012-06-26 22:30:43 +00002153
Douglas Gregordf593fb2011-11-07 17:33:42 +00002154 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002155 bool AttrsLastTime = false;
2156 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002157 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002158 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002159 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002160 unsigned DiagID = 0;
2161
Chris Lattner4d8f8732006-11-28 05:05:08 +00002162 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002163
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002164 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002165 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002166 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002167 if (!AttrsLastTime)
2168 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002169 else {
2170 // Reject C++11 attributes that appertain to decl specifiers as
2171 // we don't support any C++11 attributes that appertain to decl
2172 // specifiers. This also conforms to what g++ 4.8 is doing.
2173 ProhibitCXX11Attributes(attrs);
2174
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002175 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002176 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002177
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002178 // If this is not a declaration specifier token, we're done reading decl
2179 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002180 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002181 return;
Mike Stump11289f42009-09-09 15:08:12 +00002182
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002183 case tok::l_square:
2184 case tok::kw_alignas:
2185 if (!isCXX11AttributeSpecifier())
2186 goto DoneWithDeclSpec;
2187
2188 ProhibitAttributes(attrs);
2189 // FIXME: It would be good to recover by accepting the attributes,
2190 // but attempting to do that now would cause serious
2191 // madness in terms of diagnostics.
2192 attrs.clear();
2193 attrs.Range = SourceRange();
2194
2195 ParseCXX11Attributes(attrs);
2196 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002197 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002198
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002199 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002200 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002201 if (DS.hasTypeSpecifier()) {
2202 bool AllowNonIdentifiers
2203 = (getCurScope()->getFlags() & (Scope::ControlScope |
2204 Scope::BlockScope |
2205 Scope::TemplateParamScope |
2206 Scope::FunctionPrototypeScope |
2207 Scope::AtCatchScope)) == 0;
2208 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002209 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002210 (DSContext == DSC_class && DS.isFriendSpecified());
2211
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002212 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002213 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002214 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002215 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002216 }
2217
Douglas Gregor80039242011-02-15 20:33:25 +00002218 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2219 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2220 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002221 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002222 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002223 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002224 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002225 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002226 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002227
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002228 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002229 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002230 }
2231
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002232 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002233 // C++ scope specifier. Annotate and loop, or bail out on error.
2234 if (TryAnnotateCXXScopeToken(true)) {
2235 if (!DS.hasTypeSpecifier())
2236 DS.SetTypeSpecError();
2237 goto DoneWithDeclSpec;
2238 }
John McCall8bc2a702010-03-01 18:20:46 +00002239 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2240 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002241 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002242
2243 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002244 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002245 goto DoneWithDeclSpec;
2246
John McCall9dab4e62009-12-12 11:40:51 +00002247 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002248 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2249 Tok.getAnnotationRange(),
2250 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002251
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002252 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002253 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002254 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002255 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002256 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002257 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002258
2259 // C++ [class.qual]p2:
2260 // In a lookup in which the constructor is an acceptable lookup
2261 // result and the nested-name-specifier nominates a class C:
2262 //
2263 // - if the name specified after the
2264 // nested-name-specifier, when looked up in C, is the
2265 // injected-class-name of C (Clause 9), or
2266 //
2267 // - if the name specified after the nested-name-specifier
2268 // is the same as the identifier or the
2269 // simple-template-id's template-name in the last
2270 // component of the nested-name-specifier,
2271 //
2272 // the name is instead considered to name the constructor of
2273 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002274 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002275 // Thus, if the template-name is actually the constructor
2276 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002277 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002278 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002279 if ((DSContext == DSC_top_level ||
2280 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2281 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002282 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002283 if (isConstructorDeclarator()) {
2284 // The user meant this to be an out-of-line constructor
2285 // definition, but template arguments are not allowed
2286 // there. Just allow this as a constructor; we'll
2287 // complain about it later.
2288 goto DoneWithDeclSpec;
2289 }
2290
2291 // The user meant this to name a type, but it actually names
2292 // a constructor with some extraneous template
2293 // arguments. Complain, then parse it as a type as the user
2294 // intended.
2295 Diag(TemplateId->TemplateNameLoc,
2296 diag::err_out_of_line_template_id_names_constructor)
2297 << TemplateId->Name;
2298 }
2299
John McCall9dab4e62009-12-12 11:40:51 +00002300 DS.getTypeSpecScope() = SS;
2301 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002302 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002303 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002304 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002305 continue;
2306 }
2307
Douglas Gregorc5790df2009-09-28 07:26:33 +00002308 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002309 DS.getTypeSpecScope() = SS;
2310 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002311 if (Tok.getAnnotationValue()) {
2312 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002314 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002315 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002316 if (isInvalid)
2317 break;
John McCallba7bf592010-08-24 05:47:05 +00002318 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002319 else
2320 DS.SetTypeSpecError();
2321 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2322 ConsumeToken(); // The typename
2323 }
2324
Douglas Gregor167fa622009-03-25 15:40:00 +00002325 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002326 goto DoneWithDeclSpec;
2327
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002328 // If we're in a context where the identifier could be a class name,
2329 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002330 if ((DSContext == DSC_top_level ||
2331 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002332 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002333 &SS)) {
2334 if (isConstructorDeclarator())
2335 goto DoneWithDeclSpec;
2336
2337 // As noted in C++ [class.qual]p2 (cited above), when the name
2338 // of the class is qualified in a context where it could name
2339 // a constructor, its a constructor name. However, we've
2340 // looked at the declarator, and the user probably meant this
2341 // to be a type. Complain that it isn't supposed to be treated
2342 // as a type, then proceed to parse it as a type.
2343 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2344 << Next.getIdentifierInfo();
2345 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002346
John McCallba7bf592010-08-24 05:47:05 +00002347 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2348 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002349 getCurScope(), &SS,
2350 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002351 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002352 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002353
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002354 // If the referenced identifier is not a type, then this declspec is
2355 // erroneous: We already checked about that it has no type specifier, and
2356 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002357 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002358 if (TypeRep == 0) {
2359 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002360 ParsedAttributesWithRange Attrs(AttrFactory);
2361 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2362 if (!Attrs.empty()) {
2363 AttrsLastTime = true;
2364 attrs.takeAllFrom(Attrs);
2365 }
2366 continue;
2367 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002368 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002369 }
Mike Stump11289f42009-09-09 15:08:12 +00002370
John McCall9dab4e62009-12-12 11:40:51 +00002371 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002372 ConsumeToken(); // The C++ scope.
2373
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002375 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002376 if (isInvalid)
2377 break;
Mike Stump11289f42009-09-09 15:08:12 +00002378
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002379 DS.SetRangeEnd(Tok.getLocation());
2380 ConsumeToken(); // The typename.
2381
2382 continue;
2383 }
Mike Stump11289f42009-09-09 15:08:12 +00002384
Chris Lattnere387d9e2009-01-21 19:48:37 +00002385 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002386 if (Tok.getAnnotationValue()) {
2387 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002389 DiagID, T);
2390 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002391 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002392
Chris Lattner005fc1b2010-04-05 18:18:31 +00002393 if (isInvalid)
2394 break;
2395
Chris Lattnere387d9e2009-01-21 19:48:37 +00002396 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2397 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002398
Chris Lattnere387d9e2009-01-21 19:48:37 +00002399 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2400 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002401 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002402 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002403 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002404
Chris Lattnere387d9e2009-01-21 19:48:37 +00002405 continue;
2406 }
Mike Stump11289f42009-09-09 15:08:12 +00002407
Douglas Gregor06873092011-04-28 15:48:45 +00002408 case tok::kw___is_signed:
2409 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2410 // typically treats it as a trait. If we see __is_signed as it appears
2411 // in libstdc++, e.g.,
2412 //
2413 // static const bool __is_signed;
2414 //
2415 // then treat __is_signed as an identifier rather than as a keyword.
2416 if (DS.getTypeSpecType() == TST_bool &&
2417 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2418 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2419 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2420 Tok.setKind(tok::identifier);
2421 }
2422
2423 // We're done with the declaration-specifiers.
2424 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002425
Chris Lattner16fac4f2008-07-26 01:18:38 +00002426 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002427 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002428 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002429 // In C++, check to see if this is a scope specifier like foo::bar::, if
2430 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002431 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002432 if (TryAnnotateCXXScopeToken(true)) {
2433 if (!DS.hasTypeSpecifier())
2434 DS.SetTypeSpecError();
2435 goto DoneWithDeclSpec;
2436 }
2437 if (!Tok.is(tok::identifier))
2438 continue;
2439 }
Mike Stump11289f42009-09-09 15:08:12 +00002440
Chris Lattner16fac4f2008-07-26 01:18:38 +00002441 // This identifier can only be a typedef name if we haven't already seen
2442 // a type-specifier. Without this check we misparse:
2443 // typedef int X; struct Y { short X; }; as 'short int'.
2444 if (DS.hasTypeSpecifier())
2445 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002446
John Thompson22334602010-02-05 00:12:22 +00002447 // Check for need to substitute AltiVec keyword tokens.
2448 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2449 break;
2450
Richard Smith3092a3b2012-05-09 18:56:43 +00002451 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2452 // allow the use of a typedef name as a type specifier.
2453 if (DS.isTypeAltiVecVector())
2454 goto DoneWithDeclSpec;
2455
John McCallba7bf592010-08-24 05:47:05 +00002456 ParsedType TypeRep =
2457 Actions.getTypeName(*Tok.getIdentifierInfo(),
2458 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002459
Chris Lattner6cc055a2009-04-12 20:42:31 +00002460 // If this is not a typedef name, don't parse it as part of the declspec,
2461 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002462 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002463 ParsedAttributesWithRange Attrs(AttrFactory);
2464 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2465 if (!Attrs.empty()) {
2466 AttrsLastTime = true;
2467 attrs.takeAllFrom(Attrs);
2468 }
2469 continue;
2470 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002471 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002472 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002473
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002474 // If we're in a context where the identifier could be a class name,
2475 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002476 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002477 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002478 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002479 goto DoneWithDeclSpec;
2480
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002482 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002483 if (isInvalid)
2484 break;
Mike Stump11289f42009-09-09 15:08:12 +00002485
Chris Lattner16fac4f2008-07-26 01:18:38 +00002486 DS.SetRangeEnd(Tok.getLocation());
2487 ConsumeToken(); // The identifier
2488
2489 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2490 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002491 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002492 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002493 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002494
Steve Naroffcd5e7822008-09-22 10:28:57 +00002495 // Need to support trailing type qualifiers (e.g. "id<p> const").
2496 // If a type specifier follows, it will be diagnosed elsewhere.
2497 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002498 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002499
2500 // type-name
2501 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002502 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002503 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002504 // This template-id does not refer to a type name, so we're
2505 // done with the type-specifiers.
2506 goto DoneWithDeclSpec;
2507 }
2508
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002509 // If we're in a context where the template-id could be a
2510 // constructor name or specialization, check whether this is a
2511 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002512 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002513 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002514 isConstructorDeclarator())
2515 goto DoneWithDeclSpec;
2516
Douglas Gregor7f741122009-02-25 19:37:18 +00002517 // Turn the template-id annotation token into a type annotation
2518 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002519 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002520 continue;
2521 }
2522
Chris Lattnere37e2332006-08-15 04:50:22 +00002523 // GNU attributes support.
2524 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002525 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002526 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002527
2528 // Microsoft declspec support.
2529 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002530 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002531 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002532
Steve Naroff44ac7772008-12-25 14:16:32 +00002533 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002534 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002535 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002536 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002537 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002538 // FIXME: This does not work correctly if it is set to be a declspec
2539 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002540 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002541 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002542 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002543 }
Eli Friedman53339e02009-06-08 23:27:34 +00002544
2545 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002546 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002547 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002548 case tok::kw___cdecl:
2549 case tok::kw___stdcall:
2550 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002551 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002552 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002553 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002554 continue;
2555
Dawn Perchik335e16b2010-09-03 01:29:35 +00002556 // Borland single token adornments.
2557 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002558 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002559 continue;
2560
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002561 // OpenCL single token adornments.
2562 case tok::kw___kernel:
2563 ParseOpenCLAttributes(DS.getAttributes());
2564 continue;
2565
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002566 // storage-class-specifier
2567 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002568 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2569 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002570 break;
2571 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002572 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002573 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002574 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2575 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002576 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002577 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002578 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2579 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002580 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002581 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002582 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002583 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002584 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2585 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002586 break;
2587 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002588 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002589 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002590 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2591 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002592 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002593 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002594 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002595 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2597 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002598 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002599 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2600 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002601 break;
2602 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002603 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2604 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002605 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002606 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002607 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2608 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002609 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002610 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002611 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002612 break;
Mike Stump11289f42009-09-09 15:08:12 +00002613
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002614 // function-specifier
2615 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002616 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002617 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002618 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002619 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002620 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002621 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002622 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002623 break;
Richard Smith0015f092013-01-17 22:16:11 +00002624 case tok::kw__Noreturn:
2625 if (!getLangOpts().C11)
2626 Diag(Loc, diag::ext_c11_noreturn);
2627 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2628 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002629
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002630 // alignment-specifier
2631 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002632 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002633 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002634 ParseAlignmentSpecifier(DS.getAttributes());
2635 continue;
2636
Anders Carlssoncd8db412009-05-06 04:46:28 +00002637 // friend
2638 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002639 if (DSContext == DSC_class)
2640 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2641 else {
2642 PrevSpec = ""; // not actually used by the diagnostic
2643 DiagID = diag::err_friend_invalid_in_context;
2644 isInvalid = true;
2645 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002646 break;
Mike Stump11289f42009-09-09 15:08:12 +00002647
Douglas Gregor26701a42011-09-09 02:06:17 +00002648 // Modules
2649 case tok::kw___module_private__:
2650 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2651 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002652
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002653 // constexpr
2654 case tok::kw_constexpr:
2655 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2656 break;
2657
Chris Lattnere387d9e2009-01-21 19:48:37 +00002658 // type-specifier
2659 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002660 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2661 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002662 break;
2663 case tok::kw_long:
2664 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2666 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002667 else
John McCall49bfce42009-08-03 20:12:06 +00002668 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2669 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002670 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002671 case tok::kw___int64:
2672 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2673 DiagID);
2674 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002675 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002676 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2677 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002678 break;
2679 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2681 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002682 break;
2683 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002684 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2685 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002686 break;
2687 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2689 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002694 break;
2695 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2697 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002698 break;
2699 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2701 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002702 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002703 case tok::kw___int128:
2704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2705 DiagID);
2706 break;
2707 case tok::kw_half:
2708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2709 DiagID);
2710 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002711 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2713 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002714 break;
2715 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2717 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002718 break;
2719 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2721 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002722 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002723 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2725 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002726 break;
2727 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2729 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002730 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002731 case tok::kw_bool:
2732 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002733 if (Tok.is(tok::kw_bool) &&
2734 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2735 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2736 PrevSpec = ""; // Not used by the diagnostic.
2737 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002738 // For better error recovery.
2739 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002740 isInvalid = true;
2741 } else {
2742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2743 DiagID);
2744 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002745 break;
2746 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2748 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002749 break;
2750 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2752 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002753 break;
2754 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2756 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002757 break;
John Thompson22334602010-02-05 00:12:22 +00002758 case tok::kw___vector:
2759 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2760 break;
2761 case tok::kw___pixel:
2762 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2763 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002764 case tok::kw_image1d_t:
2765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2766 PrevSpec, DiagID);
2767 break;
2768 case tok::kw_image1d_array_t:
2769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2770 PrevSpec, DiagID);
2771 break;
2772 case tok::kw_image1d_buffer_t:
2773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2774 PrevSpec, DiagID);
2775 break;
2776 case tok::kw_image2d_t:
2777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2778 PrevSpec, DiagID);
2779 break;
2780 case tok::kw_image2d_array_t:
2781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2782 PrevSpec, DiagID);
2783 break;
2784 case tok::kw_image3d_t:
2785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_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 Benyei1b4fb3e2013-01-20 12:31:11 +00003642 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003643
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003644 // struct-or-union-specifier (C99) or class-specifier (C++)
3645 case tok::kw_class:
3646 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003647 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003648 case tok::kw_union:
3649 // enum-specifier
3650 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003651
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003652 // typedef-name
3653 case tok::annot_typename:
3654 return true;
3655 }
3656}
3657
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003658/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003659/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003660bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003661 switch (Tok.getKind()) {
3662 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003663
Chris Lattner020bab92009-01-04 23:41:41 +00003664 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003665 if (TryAltiVecVectorToken())
3666 return true;
3667 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003668 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003669 // Annotate typenames and C++ scope specifiers. If we get one, just
3670 // recurse to handle whatever we get.
3671 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003672 return true;
3673 if (Tok.is(tok::identifier))
3674 return false;
3675 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003676
Chris Lattner020bab92009-01-04 23:41:41 +00003677 case tok::coloncolon: // ::foo::bar
3678 if (NextToken().is(tok::kw_new) || // ::new
3679 NextToken().is(tok::kw_delete)) // ::delete
3680 return false;
3681
Chris Lattner020bab92009-01-04 23:41:41 +00003682 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003683 return true;
3684 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003685
Chris Lattnere37e2332006-08-15 04:50:22 +00003686 // GNU attributes support.
3687 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003688 // GNU typeof support.
3689 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003690
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003691 // type-specifiers
3692 case tok::kw_short:
3693 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003694 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003695 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003696 case tok::kw_signed:
3697 case tok::kw_unsigned:
3698 case tok::kw__Complex:
3699 case tok::kw__Imaginary:
3700 case tok::kw_void:
3701 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003702 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003703 case tok::kw_char16_t:
3704 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003705 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003706 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003707 case tok::kw_float:
3708 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003709 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003710 case tok::kw__Bool:
3711 case tok::kw__Decimal32:
3712 case tok::kw__Decimal64:
3713 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003714 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003715
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003716 // OpenCL specific types:
3717 case tok::kw_image1d_t:
3718 case tok::kw_image1d_array_t:
3719 case tok::kw_image1d_buffer_t:
3720 case tok::kw_image2d_t:
3721 case tok::kw_image2d_array_t:
3722 case tok::kw_image3d_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003723 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003724
Chris Lattner861a2262008-04-13 18:59:07 +00003725 // struct-or-union-specifier (C99) or class-specifier (C++)
3726 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003727 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003728 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003729 case tok::kw_union:
3730 // enum-specifier
3731 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003732
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003733 // type-qualifier
3734 case tok::kw_const:
3735 case tok::kw_volatile:
3736 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003737
John McCallea0a39e2012-11-14 00:49:39 +00003738 // Debugger support.
3739 case tok::kw___unknown_anytype:
3740
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003741 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003742 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003743 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003744
Chris Lattner409bf7d2008-10-20 00:25:30 +00003745 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3746 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003747 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003748
Steve Naroff44ac7772008-12-25 14:16:32 +00003749 case tok::kw___cdecl:
3750 case tok::kw___stdcall:
3751 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003752 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003753 case tok::kw___w64:
3754 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003755 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003756 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003757 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003758
3759 case tok::kw___private:
3760 case tok::kw___local:
3761 case tok::kw___global:
3762 case tok::kw___constant:
3763 case tok::kw___read_only:
3764 case tok::kw___read_write:
3765 case tok::kw___write_only:
3766
Eli Friedman53339e02009-06-08 23:27:34 +00003767 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003768
3769 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003770 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003771
Benjamin Kramere56f3932011-12-23 17:00:35 +00003772 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003773 case tok::kw__Atomic:
3774 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003775 }
3776}
3777
Chris Lattneracd58a32006-08-06 17:24:14 +00003778/// isDeclarationSpecifier() - Return true if the current token is part of a
3779/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003780///
3781/// \param DisambiguatingWithExpression True to indicate that the purpose of
3782/// this check is to disambiguate between an expression and a declaration.
3783bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003784 switch (Tok.getKind()) {
3785 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003786
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003787 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003788 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003789
Chris Lattner020bab92009-01-04 23:41:41 +00003790 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003791 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003792 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003793 return false;
John Thompson22334602010-02-05 00:12:22 +00003794 if (TryAltiVecVectorToken())
3795 return true;
3796 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003797 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003798 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003799 // Annotate typenames and C++ scope specifiers. If we get one, just
3800 // recurse to handle whatever we get.
3801 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003802 return true;
3803 if (Tok.is(tok::identifier))
3804 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003805
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003806 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003807 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003808 // expression is permitted, then this is probably a class message send
3809 // missing the initial '['. In this case, we won't consider this to be
3810 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003811 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003812 isStartOfObjCClassMessageMissingOpenBracket())
3813 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003814
John McCall1f476a12010-02-26 08:45:28 +00003815 return isDeclarationSpecifier();
3816
Chris Lattner020bab92009-01-04 23:41:41 +00003817 case tok::coloncolon: // ::foo::bar
3818 if (NextToken().is(tok::kw_new) || // ::new
3819 NextToken().is(tok::kw_delete)) // ::delete
3820 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003821
Chris Lattner020bab92009-01-04 23:41:41 +00003822 // Annotate typenames and C++ scope specifiers. If we get one, just
3823 // recurse to handle whatever we get.
3824 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003825 return true;
3826 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003827
Chris Lattneracd58a32006-08-06 17:24:14 +00003828 // storage-class-specifier
3829 case tok::kw_typedef:
3830 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003831 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003832 case tok::kw_static:
3833 case tok::kw_auto:
3834 case tok::kw_register:
3835 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003836
Douglas Gregor26701a42011-09-09 02:06:17 +00003837 // Modules
3838 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003839
John McCallea0a39e2012-11-14 00:49:39 +00003840 // Debugger support
3841 case tok::kw___unknown_anytype:
3842
Chris Lattneracd58a32006-08-06 17:24:14 +00003843 // type-specifiers
3844 case tok::kw_short:
3845 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003846 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003847 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003848 case tok::kw_signed:
3849 case tok::kw_unsigned:
3850 case tok::kw__Complex:
3851 case tok::kw__Imaginary:
3852 case tok::kw_void:
3853 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003854 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003855 case tok::kw_char16_t:
3856 case tok::kw_char32_t:
3857
Chris Lattneracd58a32006-08-06 17:24:14 +00003858 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003859 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003860 case tok::kw_float:
3861 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003862 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003863 case tok::kw__Bool:
3864 case tok::kw__Decimal32:
3865 case tok::kw__Decimal64:
3866 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003867 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003868
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003869 // OpenCL specific types:
3870 case tok::kw_image1d_t:
3871 case tok::kw_image1d_array_t:
3872 case tok::kw_image1d_buffer_t:
3873 case tok::kw_image2d_t:
3874 case tok::kw_image2d_array_t:
3875 case tok::kw_image3d_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003876 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003877
Chris Lattner861a2262008-04-13 18:59:07 +00003878 // struct-or-union-specifier (C99) or class-specifier (C++)
3879 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003880 case tok::kw_struct:
3881 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003882 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003883 // enum-specifier
3884 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003885
Chris Lattneracd58a32006-08-06 17:24:14 +00003886 // type-qualifier
3887 case tok::kw_const:
3888 case tok::kw_volatile:
3889 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003890
Chris Lattneracd58a32006-08-06 17:24:14 +00003891 // function-specifier
3892 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003893 case tok::kw_virtual:
3894 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00003895 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003896
Richard Smithd16fe122012-10-25 00:00:53 +00003897 // friend keyword.
3898 case tok::kw_friend:
3899
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003900 // static_assert-declaration
3901 case tok::kw__Static_assert:
3902
Chris Lattner599e47e2007-08-09 17:01:07 +00003903 // GNU typeof support.
3904 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003905
Chris Lattner599e47e2007-08-09 17:01:07 +00003906 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003907 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003908
Richard Smithd16fe122012-10-25 00:00:53 +00003909 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003910 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003911 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003912
Benjamin Kramere56f3932011-12-23 17:00:35 +00003913 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003914 case tok::kw__Atomic:
3915 return true;
3916
Chris Lattner8b2ec162008-07-26 03:38:44 +00003917 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3918 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003919 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003920
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003921 // typedef-name
3922 case tok::annot_typename:
3923 return !DisambiguatingWithExpression ||
3924 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003925
Steve Narofff192fab2009-01-06 19:34:12 +00003926 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003927 case tok::kw___cdecl:
3928 case tok::kw___stdcall:
3929 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003930 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003931 case tok::kw___w64:
3932 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003933 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003934 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003935 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003936 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003937
3938 case tok::kw___private:
3939 case tok::kw___local:
3940 case tok::kw___global:
3941 case tok::kw___constant:
3942 case tok::kw___read_only:
3943 case tok::kw___read_write:
3944 case tok::kw___write_only:
3945
Eli Friedman53339e02009-06-08 23:27:34 +00003946 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003947 }
3948}
3949
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003950bool Parser::isConstructorDeclarator() {
3951 TentativeParsingAction TPA(*this);
3952
3953 // Parse the C++ scope specifier.
3954 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003955 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003956 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003957 TPA.Revert();
3958 return false;
3959 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003960
3961 // Parse the constructor name.
3962 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3963 // We already know that we have a constructor name; just consume
3964 // the token.
3965 ConsumeToken();
3966 } else {
3967 TPA.Revert();
3968 return false;
3969 }
3970
Richard Smith43f340f2012-03-27 23:05:05 +00003971 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003972 if (Tok.isNot(tok::l_paren)) {
3973 TPA.Revert();
3974 return false;
3975 }
3976 ConsumeParen();
3977
Richard Smith43f340f2012-03-27 23:05:05 +00003978 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3979 // that we have a constructor.
3980 if (Tok.is(tok::r_paren) ||
3981 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003982 TPA.Revert();
3983 return true;
3984 }
3985
3986 // If we need to, enter the specified scope.
3987 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003988 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003989 DeclScopeObj.EnterDeclaratorScope();
3990
Francois Pichet79f3a872011-01-31 04:54:32 +00003991 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003992 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003993 MaybeParseMicrosoftAttributes(Attrs);
3994
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003995 // Check whether the next token(s) are part of a declaration
3996 // specifier, in which case we have the start of a parameter and,
3997 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003998 bool IsConstructor = false;
3999 if (isDeclarationSpecifier())
4000 IsConstructor = true;
4001 else if (Tok.is(tok::identifier) ||
4002 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4003 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4004 // This might be a parenthesized member name, but is more likely to
4005 // be a constructor declaration with an invalid argument type. Keep
4006 // looking.
4007 if (Tok.is(tok::annot_cxxscope))
4008 ConsumeToken();
4009 ConsumeToken();
4010
4011 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004012 // which must have one of the following syntactic forms (see the
4013 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004014 switch (Tok.getKind()) {
4015 case tok::l_paren:
4016 // C(X ( int));
4017 case tok::l_square:
4018 // C(X [ 5]);
4019 // C(X [ [attribute]]);
4020 case tok::coloncolon:
4021 // C(X :: Y);
4022 // C(X :: *p);
4023 case tok::r_paren:
4024 // C(X )
4025 // Assume this isn't a constructor, rather than assuming it's a
4026 // constructor with an unnamed parameter of an ill-formed type.
4027 break;
4028
4029 default:
4030 IsConstructor = true;
4031 break;
4032 }
4033 }
4034
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004035 TPA.Revert();
4036 return IsConstructor;
4037}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004038
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004039/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004040/// type-qualifier-list: [C99 6.7.5]
4041/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004042/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004043/// [ only if VendorAttributesAllowed=true ]
4044/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004045/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004046/// [ only if VendorAttributesAllowed=true ]
4047/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004048/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004049/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004050///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004051void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4052 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00004053 bool CXX11AttributesAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004054 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004055 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004056 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004057 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004058 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004059 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004060
4061 SourceLocation EndLoc;
4062
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004063 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004064 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004065 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004066 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004067 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004068
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004069 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004070 case tok::code_completion:
4071 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004072 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004073
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004074 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004075 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004076 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004077 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004078 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004079 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004080 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004081 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004082 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004084 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004085 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004086
4087 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004088 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004089 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004090 goto DoneWithTypeQuals;
4091 case tok::kw___private:
4092 case tok::kw___global:
4093 case tok::kw___local:
4094 case tok::kw___constant:
4095 case tok::kw___read_only:
4096 case tok::kw___write_only:
4097 case tok::kw___read_write:
4098 ParseOpenCLQualifiers(DS);
4099 break;
4100
Eli Friedman53339e02009-06-08 23:27:34 +00004101 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004102 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004103 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004104 case tok::kw___cdecl:
4105 case tok::kw___stdcall:
4106 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004107 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004108 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004109 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004110 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004111 continue;
4112 }
4113 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004114 case tok::kw___pascal:
4115 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004116 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004117 continue;
4118 }
4119 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004120 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004121 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004122 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004123 continue; // do *not* consume the next token!
4124 }
4125 // otherwise, FALL THROUGH!
4126 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004127 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004128 // If this is not a type-qualifier token, we're done reading type
4129 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004130 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004131 if (EndLoc.isValid())
4132 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004133 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004134 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004135
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004136 // If the specifier combination wasn't legal, issue a diagnostic.
4137 if (isInvalid) {
4138 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004139 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004140 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004141 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004142 }
4143}
4144
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004145
4146/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4147///
4148void Parser::ParseDeclarator(Declarator &D) {
4149 /// This implements the 'declarator' production in the C grammar, then checks
4150 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004151 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004152}
4153
Richard Smith0efa75c2012-03-29 01:16:42 +00004154static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4155 if (Kind == tok::star || Kind == tok::caret)
4156 return true;
4157
4158 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4159 if (!Lang.CPlusPlus)
4160 return false;
4161
4162 return Kind == tok::amp || Kind == tok::ampamp;
4163}
4164
Sebastian Redlbd150f42008-11-21 19:14:01 +00004165/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4166/// is parsed by the function passed to it. Pass null, and the direct-declarator
4167/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004168/// ptr-operator production.
4169///
Richard Smith09f76ee2011-10-19 21:33:05 +00004170/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004171/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4172/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004173///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004174/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4175/// [C] pointer[opt] direct-declarator
4176/// [C++] direct-declarator
4177/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004178///
4179/// pointer: [C99 6.7.5]
4180/// '*' type-qualifier-list[opt]
4181/// '*' type-qualifier-list[opt] pointer
4182///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004183/// ptr-operator:
4184/// '*' cv-qualifier-seq[opt]
4185/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004186/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004187/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004188/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004189/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004190void Parser::ParseDeclaratorInternal(Declarator &D,
4191 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004192 if (Diags.hasAllExtensionsSilenced())
4193 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004194
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004195 // C++ member pointers start with a '::' or a nested-name.
4196 // Member pointers get special handling, since there's no place for the
4197 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004198 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004199 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4200 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004201 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4202 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004203 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004204 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004205
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004206 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004207 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004208 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004209 if (D.mayHaveIdentifier())
4210 D.getCXXScopeSpec() = SS;
4211 else
4212 AnnotateScopeToken(SS, true);
4213
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004214 if (DirectDeclParser)
4215 (this->*DirectDeclParser)(D);
4216 return;
4217 }
4218
4219 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004220 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004221 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004222 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004223 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004224
4225 // Recurse to parse whatever is left.
4226 ParseDeclaratorInternal(D, DirectDeclParser);
4227
4228 // Sema will have to catch (syntactically invalid) pointers into global
4229 // scope. It has to catch pointers into namespace scope anyway.
4230 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004231 Loc),
4232 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004233 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004234 return;
4235 }
4236 }
4237
4238 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004239 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004240 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004241 if (DirectDeclParser)
4242 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004243 return;
4244 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004245
Sebastian Redled0f3b02009-03-15 22:02:01 +00004246 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4247 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004248 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004249 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004250
Chris Lattner9eac9312009-03-27 04:18:06 +00004251 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004252 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004253 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004254
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004255 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004256 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004257 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004258
Bill Wendling3708c182007-05-27 10:15:43 +00004259 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004260 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004261 if (Kind == tok::star)
4262 // Remember that we parsed a pointer type, and remember the type-quals.
4263 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004264 DS.getConstSpecLoc(),
4265 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004266 DS.getRestrictSpecLoc()),
4267 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004268 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004269 else
4270 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004271 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004272 Loc),
4273 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004274 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004275 } else {
4276 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004277 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004278
Sebastian Redl3b27be62009-03-23 00:00:23 +00004279 // Complain about rvalue references in C++03, but then go on and build
4280 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004281 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004282 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004283 diag::warn_cxx98_compat_rvalue_reference :
4284 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004285
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004286 // GNU-style and C++11 attributes are allowed here, as is restrict.
4287 ParseTypeQualifierListOpt(DS);
4288 D.ExtendWithDeclSpec(DS);
4289
Bill Wendling93efb222007-06-02 23:28:54 +00004290 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4291 // cv-qualifiers are introduced through the use of a typedef or of a
4292 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004293 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4294 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4295 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004296 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004297 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4298 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004299 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004300 }
Bill Wendling3708c182007-05-27 10:15:43 +00004301
4302 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004303 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004304
Douglas Gregor66583c52008-11-03 15:51:28 +00004305 if (D.getNumTypeObjects() > 0) {
4306 // C++ [dcl.ref]p4: There shall be no references to references.
4307 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4308 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004309 if (const IdentifierInfo *II = D.getIdentifier())
4310 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4311 << II;
4312 else
4313 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4314 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004315
Sebastian Redlbd150f42008-11-21 19:14:01 +00004316 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004317 // can go ahead and build the (technically ill-formed)
4318 // declarator: reference collapsing will take care of it.
4319 }
4320 }
4321
Bill Wendling3708c182007-05-27 10:15:43 +00004322 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004323 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004324 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004325 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004326 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004327 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004328}
4329
Richard Smith0efa75c2012-03-29 01:16:42 +00004330static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4331 SourceLocation EllipsisLoc) {
4332 if (EllipsisLoc.isValid()) {
4333 FixItHint Insertion;
4334 if (!D.getEllipsisLoc().isValid()) {
4335 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4336 D.setEllipsisLoc(EllipsisLoc);
4337 }
4338 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4339 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4340 }
4341}
4342
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004343/// ParseDirectDeclarator
4344/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004345/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004346/// '(' declarator ')'
4347/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004348/// [C90] direct-declarator '[' constant-expression[opt] ']'
4349/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4350/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4351/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4352/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004353/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4354/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004355/// direct-declarator '(' parameter-type-list ')'
4356/// direct-declarator '(' identifier-list[opt] ')'
4357/// [GNU] direct-declarator '(' parameter-forward-declarations
4358/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004359/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4360/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004361/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4362/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4363/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004364/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004365/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004366///
4367/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004368/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004369/// '::'[opt] nested-name-specifier[opt] type-name
4370///
4371/// id-expression: [C++ 5.1]
4372/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004373/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004374///
4375/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004376/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004377/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004378/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004379/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004380/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004381///
Richard Smith1453e312012-03-27 01:42:32 +00004382/// Note, any additional constructs added here may need corresponding changes
4383/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004384void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004385 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004386
David Blaikiebbafb8a2012-03-11 07:00:24 +00004387 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004388 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004389 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004390 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4391 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004392 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004393 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004394 }
4395
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004396 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004397 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004398 // Change the declaration context for name lookup, until this function
4399 // is exited (and the declarator has been parsed).
4400 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004401 }
4402
Douglas Gregor27b4c162010-12-23 22:44:42 +00004403 // C++0x [dcl.fct]p14:
4404 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004405 // of a parameter-declaration-clause without a preceding comma. In
4406 // this case, the ellipsis is parsed as part of the
4407 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004408 // parameter pack that has not been expanded; otherwise, it is parsed
4409 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004410 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004411 !((D.getContext() == Declarator::PrototypeContext ||
4412 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004413 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004414 !Actions.containsUnexpandedParameterPacks(D))) {
4415 SourceLocation EllipsisLoc = ConsumeToken();
4416 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4417 // The ellipsis was put in the wrong place. Recover, and explain to
4418 // the user what they should have done.
4419 ParseDeclarator(D);
4420 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4421 return;
4422 } else
4423 D.setEllipsisLoc(EllipsisLoc);
4424
4425 // The ellipsis can't be followed by a parenthesized declarator. We
4426 // check for that in ParseParenDeclarator, after we have disambiguated
4427 // the l_paren token.
4428 }
4429
Douglas Gregor7861a802009-11-03 01:35:08 +00004430 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4431 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4432 // We found something that indicates the start of an unqualified-id.
4433 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004434 bool AllowConstructorName;
4435 if (D.getDeclSpec().hasTypeSpecifier())
4436 AllowConstructorName = false;
4437 else if (D.getCXXScopeSpec().isSet())
4438 AllowConstructorName =
4439 (D.getContext() == Declarator::FileContext ||
4440 (D.getContext() == Declarator::MemberContext &&
4441 D.getDeclSpec().isFriendSpecified()));
4442 else
4443 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4444
Abramo Bagnara7945c982012-01-27 09:46:47 +00004445 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004446 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4447 /*EnteringContext=*/true,
4448 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004449 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004450 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004451 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004452 D.getName()) ||
4453 // Once we're past the identifier, if the scope was bad, mark the
4454 // whole declarator bad.
4455 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004456 D.SetIdentifier(0, Tok.getLocation());
4457 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004458 } else {
4459 // Parsed the unqualified-id; update range information and move along.
4460 if (D.getSourceRange().getBegin().isInvalid())
4461 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4462 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004463 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004464 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004465 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004466 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004467 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004468 "There's a C++-specific check for tok::identifier above");
4469 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4470 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4471 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004472 goto PastIdentifier;
4473 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004474
Douglas Gregor7861a802009-11-03 01:35:08 +00004475 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004476 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004477 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004478 // Example: 'char (*X)' or 'int (*XX)(void)'
4479 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004480
4481 // If the declarator was parenthesized, we entered the declarator
4482 // scope when parsing the parenthesized declarator, then exited
4483 // the scope already. Re-enter the scope, if we need to.
4484 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004485 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004486 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004487 if (!D.isInvalidType() &&
4488 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004489 // Change the declaration context for name lookup, until this function
4490 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004491 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004492 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004493 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004494 // This could be something simple like "int" (in which case the declarator
4495 // portion is empty), if an abstract-declarator is allowed.
4496 D.SetIdentifier(0, Tok.getLocation());
4497 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004498 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004499 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004500 if (D.getContext() == Declarator::MemberContext)
4501 Diag(Tok, diag::err_expected_member_name_or_semi)
4502 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004503 else if (getLangOpts().CPlusPlus) {
4504 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4505 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4506 else
4507 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4508 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004509 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004510 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004511 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004512 }
Mike Stump11289f42009-09-09 15:08:12 +00004513
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004514 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004515 assert(D.isPastIdentifier() &&
4516 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004517
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004518 // Don't parse attributes unless we have parsed an unparenthesized name.
4519 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004520 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004521
Chris Lattneracd58a32006-08-06 17:24:14 +00004522 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004523 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004524 // Enter function-declaration scope, limiting any declarators to the
4525 // function prototype scope, including parameter declarators.
4526 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004527 Scope::FunctionPrototypeScope|Scope::DeclScope|
4528 (D.isFunctionDeclaratorAFunctionDeclaration()
4529 ? Scope::FunctionDeclarationScope : 0));
4530
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004531 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4532 // In such a case, check if we actually have a function declarator; if it
4533 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004534 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004535 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4536 // The name of the declarator, if any, is tentatively declared within
4537 // a possible direct initializer.
4538 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4539 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4540 TentativelyDeclaredIdentifiers.pop_back();
4541 if (!IsFunctionDecl)
4542 break;
4543 }
John McCall084e83d2011-03-24 11:26:52 +00004544 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004545 BalancedDelimiterTracker T(*this, tok::l_paren);
4546 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004547 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004548 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004549 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004550 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004551 } else {
4552 break;
4553 }
4554 }
Chad Rosierc1183952012-06-26 22:30:43 +00004555}
Chris Lattneracd58a32006-08-06 17:24:14 +00004556
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004557/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4558/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004559/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004560/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4561///
4562/// direct-declarator:
4563/// '(' declarator ')'
4564/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004565/// direct-declarator '(' parameter-type-list ')'
4566/// direct-declarator '(' identifier-list[opt] ')'
4567/// [GNU] direct-declarator '(' parameter-forward-declarations
4568/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004569///
4570void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004571 BalancedDelimiterTracker T(*this, tok::l_paren);
4572 T.consumeOpen();
4573
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004574 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004575
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004576 // Eat any attributes before we look at whether this is a grouping or function
4577 // declarator paren. If this is a grouping paren, the attribute applies to
4578 // the type being built up, for example:
4579 // int (__attribute__(()) *x)(long y)
4580 // If this ends up not being a grouping paren, the attribute applies to the
4581 // first argument, for example:
4582 // int (__attribute__(()) int x)
4583 // In either case, we need to eat any attributes to be able to determine what
4584 // sort of paren this is.
4585 //
John McCall084e83d2011-03-24 11:26:52 +00004586 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004587 bool RequiresArg = false;
4588 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004589 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004590
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004591 // We require that the argument list (if this is a non-grouping paren) be
4592 // present even if the attribute list was empty.
4593 RequiresArg = true;
4594 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004595
Steve Naroff44ac7772008-12-25 14:16:32 +00004596 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004597 ParseMicrosoftTypeAttributes(attrs);
4598
Dawn Perchik335e16b2010-09-03 01:29:35 +00004599 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004600 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004601 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004602
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004603 // If we haven't past the identifier yet (or where the identifier would be
4604 // stored, if this is an abstract declarator), then this is probably just
4605 // grouping parens. However, if this could be an abstract-declarator, then
4606 // this could also be the start of function arguments (consider 'void()').
4607 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004608
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004609 if (!D.mayOmitIdentifier()) {
4610 // If this can't be an abstract-declarator, this *must* be a grouping
4611 // paren, because we haven't seen the identifier yet.
4612 isGrouping = true;
4613 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004614 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4615 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004616 isDeclarationSpecifier() || // 'int(int)' is a function.
4617 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004618 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4619 // considered to be a type, not a K&R identifier-list.
4620 isGrouping = false;
4621 } else {
4622 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4623 isGrouping = true;
4624 }
Mike Stump11289f42009-09-09 15:08:12 +00004625
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004626 // If this is a grouping paren, handle:
4627 // direct-declarator: '(' declarator ')'
4628 // direct-declarator: '(' attributes declarator ')'
4629 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004630 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4631 D.setEllipsisLoc(SourceLocation());
4632
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004633 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004634 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004635 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004636 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004637 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004638 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004639 T.getCloseLocation()),
4640 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004641
4642 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004643
4644 // An ellipsis cannot be placed outside parentheses.
4645 if (EllipsisLoc.isValid())
4646 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4647
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004648 return;
4649 }
Mike Stump11289f42009-09-09 15:08:12 +00004650
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004651 // Okay, if this wasn't a grouping paren, it must be the start of a function
4652 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004653 // identifier (and remember where it would have been), then call into
4654 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004655 D.SetIdentifier(0, Tok.getLocation());
4656
David Blaikie15a430a2011-12-04 05:04:18 +00004657 // Enter function-declaration scope, limiting any declarators to the
4658 // function prototype scope, including parameter declarators.
4659 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004660 Scope::FunctionPrototypeScope | Scope::DeclScope |
4661 (D.isFunctionDeclaratorAFunctionDeclaration()
4662 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004663 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004664 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004665}
4666
4667/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4668/// declarator D up to a paren, which indicates that we are parsing function
4669/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004670///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004671/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4672/// immediately after the open paren - they should be considered to be the
4673/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004674///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004675/// If RequiresArg is true, then the first argument of the function is required
4676/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004677///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004678/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4679/// (C++11) ref-qualifier[opt], exception-specification[opt],
4680/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4681///
4682/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004683/// dynamic-exception-specification
4684/// noexcept-specification
4685///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004686void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004687 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004688 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004689 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004690 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004691 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004692 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004693 // lparen is already consumed!
4694 assert(D.isPastIdentifier() && "Should not call before identifier!");
4695
4696 // This should be true when the function has typed arguments.
4697 // Otherwise, it is treated as a K&R-style function.
4698 bool HasProto = false;
4699 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004700 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004701 // Remember where we see an ellipsis, if any.
4702 SourceLocation EllipsisLoc;
4703
4704 DeclSpec DS(AttrFactory);
4705 bool RefQualifierIsLValueRef = true;
4706 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004707 SourceLocation ConstQualifierLoc;
4708 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004709 ExceptionSpecificationType ESpecType = EST_None;
4710 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004711 SmallVector<ParsedType, 2> DynamicExceptions;
4712 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004713 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004714 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004715 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004716
James Molloy6f8780b2012-02-29 10:24:19 +00004717 Actions.ActOnStartFunctionDeclarator();
4718
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004719 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4720 EndLoc is the end location for the function declarator.
4721 They differ for trailing return types. */
4722 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004723 SourceLocation LParenLoc, RParenLoc;
4724 LParenLoc = Tracker.getOpenLocation();
4725 StartLoc = LParenLoc;
4726
Douglas Gregor9e66af42011-07-05 16:44:18 +00004727 if (isFunctionDeclaratorIdentifierList()) {
4728 if (RequiresArg)
4729 Diag(Tok, diag::err_argument_required_after_attribute);
4730
4731 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4732
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004733 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004734 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004735 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004736 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004737 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004738 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004739 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004740 else if (RequiresArg)
4741 Diag(Tok, diag::err_argument_required_after_attribute);
4742
David Blaikiebbafb8a2012-03-11 07:00:24 +00004743 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004744
4745 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004746 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004747 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004748 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004749 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004750
David Blaikiebbafb8a2012-03-11 07:00:24 +00004751 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004752 // FIXME: Accept these components in any order, and produce fixits to
4753 // correct the order if the user gets it wrong. Ideally we should deal
4754 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004755
4756 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004757 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4758 if (!DS.getSourceRange().getEnd().isInvalid()) {
4759 EndLoc = DS.getSourceRange().getEnd();
4760 ConstQualifierLoc = DS.getConstSpecLoc();
4761 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4762 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004763
4764 // Parse ref-qualifier[opt].
4765 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004766 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004767 diag::warn_cxx98_compat_ref_qualifier :
4768 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004769
Douglas Gregor9e66af42011-07-05 16:44:18 +00004770 RefQualifierIsLValueRef = Tok.is(tok::amp);
4771 RefQualifierLoc = ConsumeToken();
4772 EndLoc = RefQualifierLoc;
4773 }
4774
Douglas Gregor3024f072012-04-16 07:05:22 +00004775 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004776 // If a declaration declares a member function or member function
4777 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004778 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004779 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004780 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004781 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004782 getLangOpts().CPlusPlus11 &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004783 (D.getContext() == Declarator::MemberContext ||
4784 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004785 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004786 Actions.CurContext->isRecord()));
4787 Sema::CXXThisScopeRAII ThisScope(Actions,
4788 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00004789 DS.getTypeQualifiers() |
4790 (D.getDeclSpec().isConstexprSpecified()
4791 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00004792 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004793
Douglas Gregor9e66af42011-07-05 16:44:18 +00004794 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004795 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004796 DynamicExceptions,
4797 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004798 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004799 if (ESpecType != EST_None)
4800 EndLoc = ESpecRange.getEnd();
4801
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004802 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4803 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00004804 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004805
Douglas Gregor9e66af42011-07-05 16:44:18 +00004806 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004807 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004808 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004809 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004810 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4811 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004812 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004813 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004814 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004815 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004816 }
4817 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004818 }
4819
4820 // Remember that we parsed a function type, and remember the attributes.
4821 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004822 IsAmbiguous,
4823 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004824 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004825 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004826 DS.getTypeQualifiers(),
4827 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004828 RefQualifierLoc, ConstQualifierLoc,
4829 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004830 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004831 ESpecType, ESpecRange.getBegin(),
4832 DynamicExceptions.data(),
4833 DynamicExceptionRanges.data(),
4834 DynamicExceptions.size(),
4835 NoexceptExpr.isUsable() ?
4836 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004837 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004838 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004839 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004840
4841 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004842}
4843
4844/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4845/// identifier list form for a K&R-style function: void foo(a,b,c)
4846///
4847/// Note that identifier-lists are only allowed for normal declarators, not for
4848/// abstract-declarators.
4849bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004850 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004851 && Tok.is(tok::identifier)
4852 && !TryAltiVecVectorToken()
4853 // K&R identifier lists can't have typedefs as identifiers, per C99
4854 // 6.7.5.3p11.
4855 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4856 // Identifier lists follow a really simple grammar: the identifiers can
4857 // be followed *only* by a ", identifier" or ")". However, K&R
4858 // identifier lists are really rare in the brave new modern world, and
4859 // it is very common for someone to typo a type in a non-K&R style
4860 // list. If we are presented with something like: "void foo(intptr x,
4861 // float y)", we don't want to start parsing the function declarator as
4862 // though it is a K&R style declarator just because intptr is an
4863 // invalid type.
4864 //
4865 // To handle this, we check to see if the token after the first
4866 // identifier is a "," or ")". Only then do we parse it as an
4867 // identifier list.
4868 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4869}
4870
4871/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4872/// we found a K&R-style identifier list instead of a typed parameter list.
4873///
4874/// After returning, ParamInfo will hold the parsed parameters.
4875///
4876/// identifier-list: [C99 6.7.5]
4877/// identifier
4878/// identifier-list ',' identifier
4879///
4880void Parser::ParseFunctionDeclaratorIdentifierList(
4881 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004882 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004883 // If there was no identifier specified for the declarator, either we are in
4884 // an abstract-declarator, or we are in a parameter declarator which was found
4885 // to be abstract. In abstract-declarators, identifier lists are not valid:
4886 // diagnose this.
4887 if (!D.getIdentifier())
4888 Diag(Tok, diag::ext_ident_list_in_param);
4889
4890 // Maintain an efficient lookup of params we have seen so far.
4891 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4892
4893 while (1) {
4894 // If this isn't an identifier, report the error and skip until ')'.
4895 if (Tok.isNot(tok::identifier)) {
4896 Diag(Tok, diag::err_expected_ident);
4897 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4898 // Forget we parsed anything.
4899 ParamInfo.clear();
4900 return;
4901 }
4902
4903 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4904
4905 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4906 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4907 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4908
4909 // Verify that the argument identifier has not already been mentioned.
4910 if (!ParamsSoFar.insert(ParmII)) {
4911 Diag(Tok, diag::err_param_redefinition) << ParmII;
4912 } else {
4913 // Remember this identifier in ParamInfo.
4914 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4915 Tok.getLocation(),
4916 0));
4917 }
4918
4919 // Eat the identifier.
4920 ConsumeToken();
4921
4922 // The list continues if we see a comma.
4923 if (Tok.isNot(tok::comma))
4924 break;
4925 ConsumeToken();
4926 }
4927}
4928
4929/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4930/// after the opening parenthesis. This function will not parse a K&R-style
4931/// identifier list.
4932///
Richard Smith2620cd92012-04-11 04:01:28 +00004933/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4934/// caller parsed those arguments immediately after the open paren - they should
4935/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004936///
4937/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4938/// be the location of the ellipsis, if any was parsed.
4939///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004940/// parameter-type-list: [C99 6.7.5]
4941/// parameter-list
4942/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004943/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004944///
4945/// parameter-list: [C99 6.7.5]
4946/// parameter-declaration
4947/// parameter-list ',' parameter-declaration
4948///
4949/// parameter-declaration: [C99 6.7.5]
4950/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004951/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004952/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004953/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004954/// declaration-specifiers abstract-declarator[opt]
4955/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004956/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004957/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004958/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004959///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004960void Parser::ParseParameterDeclarationClause(
4961 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004962 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004963 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004964 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004965
Chris Lattner371ed4e2008-04-06 06:57:35 +00004966 while (1) {
4967 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004968 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4969 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004970 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004971 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004972 }
Mike Stump11289f42009-09-09 15:08:12 +00004973
Chris Lattner371ed4e2008-04-06 06:57:35 +00004974 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004975 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004976 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004977
Richard Smith2620cd92012-04-11 04:01:28 +00004978 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00004979 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00004980
John McCall53fa7142010-12-24 02:08:15 +00004981 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00004982 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00004983
4984 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004985
4986 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004987 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004988 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004989 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4990 // too much hassle.
4991 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004992
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004993 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004994
Chris Lattner371ed4e2008-04-06 06:57:35 +00004995 // Parse the declarator. This is "PrototypeContext", because we must
4996 // accept either 'declarator' or 'abstract-declarator' here.
4997 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4998 ParseDeclarator(ParmDecl);
4999
5000 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005001 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005002
Chris Lattner371ed4e2008-04-06 06:57:35 +00005003 // Remember this parsed parameter in ParamInfo.
5004 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005005
Douglas Gregor4d87df52008-12-16 21:30:33 +00005006 // DefArgToks is used when the parsing of default arguments needs
5007 // to be delayed.
5008 CachedTokens *DefArgToks = 0;
5009
Chris Lattner371ed4e2008-04-06 06:57:35 +00005010 // If no parameter was specified, verify that *something* was specified,
5011 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005012 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5013 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005014 // Completely missing, emit error.
5015 Diag(DSStart, diag::err_missing_param);
5016 } else {
5017 // Otherwise, we have something. Add it and let semantic analysis try
5018 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005019
Chris Lattner371ed4e2008-04-06 06:57:35 +00005020 // Inform the actions module about the parameter declarator, so it gets
5021 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005022 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005023
5024 // Parse the default argument, if any. We parse the default
5025 // arguments in all dialects; the semantic analysis in
5026 // ActOnParamDefaultArgument will reject the default argument in
5027 // C.
5028 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005029 SourceLocation EqualLoc = Tok.getLocation();
5030
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005031 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005032 if (D.getContext() == Declarator::MemberContext) {
5033 // If we're inside a class definition, cache the tokens
5034 // corresponding to the default argument. We'll actually parse
5035 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005036 // FIXME: Can we use a smart pointer for Toks?
5037 DefArgToks = new CachedTokens;
5038
Mike Stump11289f42009-09-09 15:08:12 +00005039 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005040 /*StopAtSemi=*/true,
5041 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005042 delete DefArgToks;
5043 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005044 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005045 } else {
5046 // Mark the end of the default argument so that we know when to
5047 // stop when we parse it later on.
5048 Token DefArgEnd;
5049 DefArgEnd.startToken();
5050 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5051 DefArgEnd.setLocation(Tok.getLocation());
5052 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005053 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005054 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005055 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005056 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005057 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005058 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005059
Chad Rosierc1183952012-06-26 22:30:43 +00005060 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005061 // used.
5062 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005063 Sema::PotentiallyEvaluatedIfUsed,
5064 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005065
Sebastian Redldb63af22012-03-14 15:54:00 +00005066 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005067 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005068 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005069 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005070 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005071 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005072 if (DefArgResult.isInvalid()) {
5073 Actions.ActOnParamDefaultArgumentError(Param);
5074 SkipUntil(tok::comma, tok::r_paren, true, true);
5075 } else {
5076 // Inform the actions module about the default argument
5077 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005078 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005079 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005080 }
5081 }
Mike Stump11289f42009-09-09 15:08:12 +00005082
5083 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5084 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005085 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005086 }
5087
5088 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005089 if (Tok.isNot(tok::comma)) {
5090 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005091 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005092
David Blaikiebbafb8a2012-03-11 07:00:24 +00005093 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005094 // We have ellipsis without a preceding ',', which is ill-formed
5095 // in C. Complain and provide the fix.
5096 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005097 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005098 }
5099 }
Chad Rosierc1183952012-06-26 22:30:43 +00005100
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005101 break;
5102 }
Mike Stump11289f42009-09-09 15:08:12 +00005103
Chris Lattner371ed4e2008-04-06 06:57:35 +00005104 // Consume the comma.
5105 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005106 }
Mike Stump11289f42009-09-09 15:08:12 +00005107
Chris Lattner6c940e62008-04-06 06:34:08 +00005108}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005109
Chris Lattnere8074e62006-08-06 18:30:15 +00005110/// [C90] direct-declarator '[' constant-expression[opt] ']'
5111/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5112/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5113/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5114/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005115/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5116/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005117void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005118 if (CheckProhibitedCXX11Attribute())
5119 return;
5120
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005121 BalancedDelimiterTracker T(*this, tok::l_square);
5122 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005123
Chris Lattner84a11622008-12-18 07:27:21 +00005124 // C array syntax has many features, but by-far the most common is [] and [4].
5125 // This code does a fast path to handle some of the most obvious cases.
5126 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005127 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005128 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005129 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005130
Chris Lattner84a11622008-12-18 07:27:21 +00005131 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005132 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005133 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005134 T.getOpenLocation(),
5135 T.getCloseLocation()),
5136 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005137 return;
5138 } else if (Tok.getKind() == tok::numeric_constant &&
5139 GetLookAheadToken(1).is(tok::r_square)) {
5140 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005141 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005142 ConsumeToken();
5143
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005144 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005145 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005146 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005147
Chris Lattner84a11622008-12-18 07:27:21 +00005148 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005149 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005150 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005151 T.getOpenLocation(),
5152 T.getCloseLocation()),
5153 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005154 return;
5155 }
Mike Stump11289f42009-09-09 15:08:12 +00005156
Chris Lattnere8074e62006-08-06 18:30:15 +00005157 // If valid, this location is the position where we read the 'static' keyword.
5158 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005159 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005160 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005161
Chris Lattnere8074e62006-08-06 18:30:15 +00005162 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005163 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005164 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005165 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005166
Chris Lattnere8074e62006-08-06 18:30:15 +00005167 // If we haven't already read 'static', check to see if there is one after the
5168 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005169 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005170 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005171
Chris Lattnere8074e62006-08-06 18:30:15 +00005172 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005173 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005174 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005175
Chris Lattner521ff2b2008-04-06 05:26:30 +00005176 // Handle the case where we have '[*]' as the array size. However, a leading
5177 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005178 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005179 // infrequent, use of lookahead is not costly here.
5180 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005181 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005182
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005183 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005184 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005185 StaticLoc = SourceLocation(); // Drop the static.
5186 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005187 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005188 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005189 // Note, in C89, this production uses the constant-expr production instead
5190 // of assignment-expr. The only difference is that assignment-expr allows
5191 // things like '=' and '*='. Sema rejects these in C89 mode because they
5192 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005193
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005194 // Parse the constant-expression or assignment-expression now (depending
5195 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005196 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005197 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005198 } else {
5199 EnterExpressionEvaluationContext Unevaluated(Actions,
5200 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005201 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005202 }
Chris Lattner62591722006-08-12 18:40:58 +00005203 }
Mike Stump11289f42009-09-09 15:08:12 +00005204
Chris Lattner62591722006-08-12 18:40:58 +00005205 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005206 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005207 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005208 // If the expression was invalid, skip it.
5209 SkipUntil(tok::r_square);
5210 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005211 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005212
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005213 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005214
John McCall084e83d2011-03-24 11:26:52 +00005215 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005216 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005217
Chris Lattner84a11622008-12-18 07:27:21 +00005218 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005219 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005220 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005221 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005222 T.getOpenLocation(),
5223 T.getCloseLocation()),
5224 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005225}
5226
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005227/// [GNU] typeof-specifier:
5228/// typeof ( expressions )
5229/// typeof ( type-name )
5230/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005231///
5232void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005233 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005234 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005235 SourceLocation StartLoc = ConsumeToken();
5236
John McCalle8595032010-01-13 20:03:27 +00005237 const bool hasParens = Tok.is(tok::l_paren);
5238
Eli Friedman15681d62012-09-26 04:34:21 +00005239 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5240 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005241
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005242 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005243 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005244 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005245 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5246 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005247 if (hasParens)
5248 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005249
5250 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005251 // FIXME: Not accurate, the range gets one token more than it should.
5252 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005253 else
5254 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005255
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005256 if (isCastExpr) {
5257 if (!CastTy) {
5258 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005259 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005260 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005261
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005262 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005263 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005264 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5265 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005266 DiagID, CastTy))
5267 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005268 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005269 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005270
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005271 // If we get here, the operand to the typeof was an expresion.
5272 if (Operand.isInvalid()) {
5273 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005274 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005275 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005276
Eli Friedmane0afc982012-01-21 01:01:51 +00005277 // We might need to transform the operand if it is potentially evaluated.
5278 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5279 if (Operand.isInvalid()) {
5280 DS.SetTypeSpecError();
5281 return;
5282 }
5283
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005284 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005285 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005286 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5287 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005288 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005289 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005290}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005291
Benjamin Kramere56f3932011-12-23 17:00:35 +00005292/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005293/// _Atomic ( type-name )
5294///
5295void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5296 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5297
5298 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005299 BalancedDelimiterTracker T(*this, tok::l_paren);
5300 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005301 SkipUntil(tok::r_paren);
5302 return;
5303 }
5304
5305 TypeResult Result = ParseTypeName();
5306 if (Result.isInvalid()) {
5307 SkipUntil(tok::r_paren);
5308 return;
5309 }
5310
5311 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005312 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005313
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005314 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005315 return;
5316
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005317 DS.setTypeofParensRange(T.getRange());
5318 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005319
5320 const char *PrevSpec = 0;
5321 unsigned DiagID;
5322 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5323 DiagID, Result.release()))
5324 Diag(StartLoc, DiagID) << PrevSpec;
5325}
5326
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005327
5328/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5329/// from TryAltiVecVectorToken.
5330bool Parser::TryAltiVecVectorTokenOutOfLine() {
5331 Token Next = NextToken();
5332 switch (Next.getKind()) {
5333 default: return false;
5334 case tok::kw_short:
5335 case tok::kw_long:
5336 case tok::kw_signed:
5337 case tok::kw_unsigned:
5338 case tok::kw_void:
5339 case tok::kw_char:
5340 case tok::kw_int:
5341 case tok::kw_float:
5342 case tok::kw_double:
5343 case tok::kw_bool:
5344 case tok::kw___pixel:
5345 Tok.setKind(tok::kw___vector);
5346 return true;
5347 case tok::identifier:
5348 if (Next.getIdentifierInfo() == Ident_pixel) {
5349 Tok.setKind(tok::kw___vector);
5350 return true;
5351 }
5352 return false;
5353 }
5354}
5355
5356bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5357 const char *&PrevSpec, unsigned &DiagID,
5358 bool &isInvalid) {
5359 if (Tok.getIdentifierInfo() == Ident_vector) {
5360 Token Next = NextToken();
5361 switch (Next.getKind()) {
5362 case tok::kw_short:
5363 case tok::kw_long:
5364 case tok::kw_signed:
5365 case tok::kw_unsigned:
5366 case tok::kw_void:
5367 case tok::kw_char:
5368 case tok::kw_int:
5369 case tok::kw_float:
5370 case tok::kw_double:
5371 case tok::kw_bool:
5372 case tok::kw___pixel:
5373 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5374 return true;
5375 case tok::identifier:
5376 if (Next.getIdentifierInfo() == Ident_pixel) {
5377 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5378 return true;
5379 }
5380 break;
5381 default:
5382 break;
5383 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005384 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005385 DS.isTypeAltiVecVector()) {
5386 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5387 return true;
5388 }
5389 return false;
5390}