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