blob: bf5c078b714e7a490509516aa47bf9c3b8bf8d55 [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,
Richard Smith54ecd982013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000046
Chris Lattnerf5fbd792006-08-10 23:56:11 +000047 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000054
Chris Lattnerf5fbd792006-08-10 23:56:11 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor0be31a22010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000065}
66
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Alexis Hunt96d5c762009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000091/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000095///
Steve Naroff0f2fe172007-06-01 17:11:19 +000096/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000101///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithb12bf692011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000113
John McCall53fa7142010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner76c72282007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000141
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148
Bill Wendling44426052012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000153
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han23214e52012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000164 }
165 } else {
John McCall084e83d2011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000171 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall53fa7142010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000178 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000180
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000181
Michael Han23214e52012-10-03 01:56:22 +0000182/// Parse the arguments to a parameterized GNU attribute or
183/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000184void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
185 SourceLocation AttrNameLoc,
186 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000187 SourceLocation *EndLoc,
188 IdentifierInfo *ScopeName,
189 SourceLocation ScopeLoc,
190 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000191
192 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
193
194 // Availability attributes have their own grammar.
195 if (AttrName->isStr("availability")) {
196 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
197 return;
198 }
199 // Thread safety attributes fit into the FIXME case above, so we
200 // just parse the arguments as a list of expressions
201 if (IsThreadSafetyAttribute(AttrName->getName())) {
202 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000205 // Type safety attributes have their own grammar.
206 if (AttrName->isStr("type_tag_for_datatype")) {
207 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
208 return;
209 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000210
211 ConsumeParen(); // ignore the left paren loc for now
212
Richard Smithb12bf692011-10-17 21:20:17 +0000213 IdentifierInfo *ParmName = 0;
214 SourceLocation ParmLoc;
215 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000216
Joey Goulyaba589c2013-03-08 09:42:32 +0000217 TypeResult T;
218 SourceRange TypeRange;
219 bool TypeParsed = false;
220
Richard Smithb12bf692011-10-17 21:20:17 +0000221 switch (Tok.getKind()) {
222 case tok::kw_char:
223 case tok::kw_wchar_t:
224 case tok::kw_char16_t:
225 case tok::kw_char32_t:
226 case tok::kw_bool:
227 case tok::kw_short:
228 case tok::kw_int:
229 case tok::kw_long:
230 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000231 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000232 case tok::kw_signed:
233 case tok::kw_unsigned:
234 case tok::kw_float:
235 case tok::kw_double:
236 case tok::kw_void:
237 case tok::kw_typeof:
238 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000239 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000240 T = ParseTypeName(&TypeRange);
241 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000242 break;
243
244 case tok::identifier:
Joey Goulyaba589c2013-03-08 09:42:32 +0000245 if (AttrName->isStr("vec_type_hint")) {
246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
248 break;
249 }
Richard Smithb12bf692011-10-17 21:20:17 +0000250 ParmName = Tok.getIdentifierInfo();
251 ParmLoc = ConsumeToken();
252 break;
253
254 default:
255 break;
256 }
257
Benjamin Kramerf0623432012-08-23 22:51:59 +0000258 ExprVector ArgExprs;
Joey Goulyaba589c2013-03-08 09:42:32 +0000259 bool isInvalid = false;
260 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000261
Joey Goulyaba589c2013-03-08 09:42:32 +0000262 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithb12bf692011-10-17 21:20:17 +0000263 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
264 // Eat the comma.
265 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000266 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000267
Richard Smithb12bf692011-10-17 21:20:17 +0000268 // Parse the non-empty comma-separated list of expressions.
269 while (1) {
270 ExprResult ArgExpr(ParseAssignmentExpression());
271 if (ArgExpr.isInvalid()) {
272 SkipUntil(tok::r_paren);
273 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000274 }
Richard Smithb12bf692011-10-17 21:20:17 +0000275 ArgExprs.push_back(ArgExpr.release());
276 if (Tok.isNot(tok::comma))
277 break;
278 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000279 }
Richard Smithb12bf692011-10-17 21:20:17 +0000280 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000281 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
282 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
283 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000284 while (Tok.is(tok::identifier)) {
285 ConsumeToken();
286 if (Tok.is(tok::greater))
287 break;
288 if (Tok.is(tok::comma)) {
289 ConsumeToken();
290 continue;
291 }
292 }
293 if (Tok.isNot(tok::greater))
294 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000295 SkipUntil(tok::r_paren, false, true); // skip until ')'
296 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000297 } else if (AttrName->isStr("vec_type_hint")) {
298 if (T.get() && !T.isInvalid())
299 isParmType = true;
300 else {
301 if (Tok.is(tok::identifier))
302 ConsumeToken();
303 if (TypeParsed)
304 isInvalid = true;
305 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000306 }
Richard Smithb12bf692011-10-17 21:20:17 +0000307
308 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000309 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
310 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000311 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000312 if (isParmType) {
313 QualType ParmType = Sema::GetTypeFromParser(T.get());
314 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
315 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
316 } else {
317 AttributeList *attr = Attrs.addNew(
318 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
319 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
320 if (BuiltinType &&
321 attr->getKind() == AttributeList::AT_IBOutletCollection)
322 Diag(Tok, diag::err_iboutletcollection_builtintype);
323 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000324 }
325}
326
Chad Rosierc1183952012-06-26 22:30:43 +0000327/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000328/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000329void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000330 SourceLocation AttrNameLoc,
331 ParsedAttributes &Attrs)
332{
333 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000334 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000335 AttrName->getNameStart(), tok::r_paren))
336 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000337
Aaron Ballman478faed2012-06-19 22:09:27 +0000338 ExprResult ArgExpr(ParseConstantExpression());
339 if (ArgExpr.isInvalid()) {
340 T.skipToEnd();
341 return;
342 }
343 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000344 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000345 &ExprList, 1, AttributeList::AS_Declspec);
346
347 T.consumeClose();
348}
349
Chad Rosierc1183952012-06-26 22:30:43 +0000350/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000351/// arguments.
352bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
353 return llvm::StringSwitch<bool>(Ident->getName())
354 .Case("dllimport", true)
355 .Case("dllexport", true)
356 .Case("noreturn", true)
357 .Case("nothrow", true)
358 .Case("noinline", true)
359 .Case("naked", true)
360 .Case("appdomain", true)
361 .Case("process", true)
362 .Case("jitintrinsic", true)
363 .Case("noalias", true)
364 .Case("restrict", true)
365 .Case("novtable", true)
366 .Case("selectany", true)
367 .Case("thread", true)
368 .Default(false);
369}
370
Chad Rosierc1183952012-06-26 22:30:43 +0000371/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000372/// parameters). Will return false if we properly handled the declspec, or
373/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000374void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000375 SourceLocation Loc,
376 ParsedAttributes &Attrs) {
377 // Try to handle the easy case first -- these declspecs all take a single
378 // parameter as their argument.
379 if (llvm::StringSwitch<bool>(Ident->getName())
380 .Case("uuid", true)
381 .Case("align", true)
382 .Case("allocate", true)
383 .Default(false)) {
384 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
385 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000386 // The deprecated declspec has an optional single argument, so we will
387 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000388 // not.
389 if (Tok.getKind() == tok::l_paren)
390 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
391 else
Chad Rosierc1183952012-06-26 22:30:43 +0000392 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000393 AttributeList::AS_Declspec);
394 } else if (Ident->getName() == "property") {
395 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000396 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 // must be named get or put.
398 //
Chad Rosierc1183952012-06-26 22:30:43 +0000399 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000400 // property expression.
401 //
402 // FIXME: we should deal with __declspec(property) at some point because it
403 // is used in the platform SDK headers for the Parallel Patterns Library
404 // and ATL.
405 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000406 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 Ident->getNameStart(), tok::r_paren))
408 return;
409 T.skipToEnd();
410 } else {
411 // We don't recognize this as a valid declspec, but instead of creating the
412 // attribute and allowing sema to warn about it, we will warn here instead.
413 // This is because some attributes have multiple spellings, but we need to
414 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000415 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000416 // both locations.
417 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
418
419 // If there's an open paren, we should eat the open and close parens under
420 // the assumption that this unknown declspec has parameters.
421 BalancedDelimiterTracker T(*this, tok::l_paren);
422 if (!T.consumeOpen())
423 T.skipToEnd();
424 }
425}
426
Eli Friedman06de2b52009-06-08 07:21:15 +0000427/// [MS] decl-specifier:
428/// __declspec ( extended-decl-modifier-seq )
429///
430/// [MS] extended-decl-modifier-seq:
431/// extended-decl-modifier[opt]
432/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000433void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000434 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000435
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000436 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000438 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000439 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000440 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000441
Chad Rosierc1183952012-06-26 22:30:43 +0000442 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000443 // you can specify multiple attributes per declspec.
444 while (Tok.getKind() != tok::r_paren) {
445 // We expect either a well-known identifier or a generic string. Anything
446 // else is a malformed declspec.
447 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000448 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000449 Tok.getKind() != tok::kw_restrict) {
450 Diag(Tok, diag::err_ms_declspec_type);
451 T.skipToEnd();
452 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000453 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000454
455 IdentifierInfo *AttrName;
456 SourceLocation AttrNameLoc;
457 if (IsString) {
458 SmallString<8> StrBuffer;
459 bool Invalid = false;
460 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
461 if (Invalid) {
462 T.skipToEnd();
463 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000464 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000465 AttrName = PP.getIdentifierInfo(Str);
466 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000467 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000468 AttrName = Tok.getIdentifierInfo();
469 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000470 }
Chad Rosierc1183952012-06-26 22:30:43 +0000471
Aaron Ballman478faed2012-06-19 22:09:27 +0000472 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000473 // If we have a generic string, we will allow it because there is no
474 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000475 // (for instance, SAL declspecs in older versions of MSVC).
476 //
Chad Rosierc1183952012-06-26 22:30:43 +0000477 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000478 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000479 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000480 0, 0, AttributeList::AS_Declspec);
481 else
482 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000483 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000484 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000485}
486
John McCall53fa7142010-12-24 02:08:15 +0000487void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000488 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000489 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000490 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000491 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000492 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000493 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
494 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000495 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000496 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000497 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000498}
499
John McCall53fa7142010-12-24 02:08:15 +0000500void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000501 // Treat these like attributes
502 while (Tok.is(tok::kw___pascal)) {
503 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
504 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000505 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000506 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000507 }
John McCall53fa7142010-12-24 02:08:15 +0000508}
509
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000510void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
511 // Treat these like attributes
512 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000513 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000514 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000515 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
516 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000517 }
518}
519
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000520void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000521 // FIXME: The mapping from attribute spelling to semantics should be
522 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000523 SourceLocation Loc = Tok.getLocation();
524 switch(Tok.getKind()) {
525 // OpenCL qualifiers:
526 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000527 case tok::kw_private:
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("address_space"), Loc, 0);
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___global:
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("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000537 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000538
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000539 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000540 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000541 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000542 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000543 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000544
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000545 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000546 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000547 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000548 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000549 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000550
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000551 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000552 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000553 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000554 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000555 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000556
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000557 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000558 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000559 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000560 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000561 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000562
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000563 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000564 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000565 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000566 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000567 break;
568 default: break;
569 }
570}
571
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000572/// \brief Parse a version number.
573///
574/// version:
575/// simple-integer
576/// simple-integer ',' simple-integer
577/// simple-integer ',' simple-integer ',' simple-integer
578VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
579 Range = Tok.getLocation();
580
581 if (!Tok.is(tok::numeric_constant)) {
582 Diag(Tok, diag::err_expected_version);
583 SkipUntil(tok::comma, tok::r_paren, true, true, true);
584 return VersionTuple();
585 }
586
587 // Parse the major (and possibly minor and subminor) versions, which
588 // are stored in the numeric constant. We utilize a quirk of the
589 // lexer, which is that it handles something like 1.2.3 as a single
590 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000591 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000592 Buffer.resize(Tok.getLength()+1);
593 const char *ThisTokBegin = &Buffer[0];
594
595 // Get the spelling of the token, which eliminates trigraphs, etc.
596 bool Invalid = false;
597 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
598 if (Invalid)
599 return VersionTuple();
600
601 // Parse the major version.
602 unsigned AfterMajor = 0;
603 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000604 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000605 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
606 ++AfterMajor;
607 }
608
609 if (AfterMajor == 0) {
610 Diag(Tok, diag::err_expected_version);
611 SkipUntil(tok::comma, tok::r_paren, true, true, true);
612 return VersionTuple();
613 }
614
615 if (AfterMajor == ActualLength) {
616 ConsumeToken();
617
618 // We only had a single version component.
619 if (Major == 0) {
620 Diag(Tok, diag::err_zero_version);
621 return VersionTuple();
622 }
623
624 return VersionTuple(Major);
625 }
626
627 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
628 Diag(Tok, diag::err_expected_version);
629 SkipUntil(tok::comma, tok::r_paren, true, true, true);
630 return VersionTuple();
631 }
632
633 // Parse the minor version.
634 unsigned AfterMinor = AfterMajor + 1;
635 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000636 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000637 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
638 ++AfterMinor;
639 }
640
641 if (AfterMinor == ActualLength) {
642 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000643
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000644 // We had major.minor.
645 if (Major == 0 && Minor == 0) {
646 Diag(Tok, diag::err_zero_version);
647 return VersionTuple();
648 }
649
Chad Rosierc1183952012-06-26 22:30:43 +0000650 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000651 }
652
653 // If what follows is not a '.', we have a problem.
654 if (ThisTokBegin[AfterMinor] != '.') {
655 Diag(Tok, diag::err_expected_version);
656 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000657 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000658 }
659
660 // Parse the subminor version.
661 unsigned AfterSubminor = AfterMinor + 1;
662 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000663 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000664 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
665 ++AfterSubminor;
666 }
667
668 if (AfterSubminor != ActualLength) {
669 Diag(Tok, diag::err_expected_version);
670 SkipUntil(tok::comma, tok::r_paren, true, true, true);
671 return VersionTuple();
672 }
673 ConsumeToken();
674 return VersionTuple(Major, Minor, Subminor);
675}
676
677/// \brief Parse the contents of the "availability" attribute.
678///
679/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000680/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000681///
682/// platform:
683/// identifier
684///
685/// version-arg-list:
686/// version-arg
687/// version-arg ',' version-arg-list
688///
689/// version-arg:
690/// 'introduced' '=' version
691/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000692/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000693/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000694/// opt-message:
695/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000696void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
697 SourceLocation AvailabilityLoc,
698 ParsedAttributes &attrs,
699 SourceLocation *endLoc) {
700 SourceLocation PlatformLoc;
701 IdentifierInfo *Platform = 0;
702
703 enum { Introduced, Deprecated, Obsoleted, Unknown };
704 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000705 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000706
707 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000708 BalancedDelimiterTracker T(*this, tok::l_paren);
709 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000710 Diag(Tok, diag::err_expected_lparen);
711 return;
712 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000713
714 // Parse the platform name,
715 if (Tok.isNot(tok::identifier)) {
716 Diag(Tok, diag::err_availability_expected_platform);
717 SkipUntil(tok::r_paren);
718 return;
719 }
720 Platform = Tok.getIdentifierInfo();
721 PlatformLoc = ConsumeToken();
722
723 // Parse the ',' following the platform name.
724 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
725 return;
726
727 // If we haven't grabbed the pointers for the identifiers
728 // "introduced", "deprecated", and "obsoleted", do so now.
729 if (!Ident_introduced) {
730 Ident_introduced = PP.getIdentifierInfo("introduced");
731 Ident_deprecated = PP.getIdentifierInfo("deprecated");
732 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000733 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000734 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000735 }
736
737 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000738 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000739 do {
740 if (Tok.isNot(tok::identifier)) {
741 Diag(Tok, diag::err_availability_expected_change);
742 SkipUntil(tok::r_paren);
743 return;
744 }
745 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
746 SourceLocation KeywordLoc = ConsumeToken();
747
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000748 if (Keyword == Ident_unavailable) {
749 if (UnavailableLoc.isValid()) {
750 Diag(KeywordLoc, diag::err_availability_redundant)
751 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000752 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000753 UnavailableLoc = KeywordLoc;
754
755 if (Tok.isNot(tok::comma))
756 break;
757
758 ConsumeToken();
759 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000760 }
761
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762 if (Tok.isNot(tok::equal)) {
763 Diag(Tok, diag::err_expected_equal_after)
764 << Keyword;
765 SkipUntil(tok::r_paren);
766 return;
767 }
768 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000769 if (Keyword == Ident_message) {
770 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000771 Diag(Tok, diag::err_expected_string_literal)
772 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000773 SkipUntil(tok::r_paren);
774 return;
775 }
776 MessageExpr = ParseStringLiteralExpression();
777 break;
778 }
Chad Rosierc1183952012-06-26 22:30:43 +0000779
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000780 SourceRange VersionRange;
781 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000782
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783 if (Version.empty()) {
784 SkipUntil(tok::r_paren);
785 return;
786 }
787
788 unsigned Index;
789 if (Keyword == Ident_introduced)
790 Index = Introduced;
791 else if (Keyword == Ident_deprecated)
792 Index = Deprecated;
793 else if (Keyword == Ident_obsoleted)
794 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000795 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000796 Index = Unknown;
797
798 if (Index < Unknown) {
799 if (!Changes[Index].KeywordLoc.isInvalid()) {
800 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000801 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802 << SourceRange(Changes[Index].KeywordLoc,
803 Changes[Index].VersionRange.getEnd());
804 }
805
806 Changes[Index].KeywordLoc = KeywordLoc;
807 Changes[Index].Version = Version;
808 Changes[Index].VersionRange = VersionRange;
809 } else {
810 Diag(KeywordLoc, diag::err_availability_unknown_change)
811 << Keyword << VersionRange;
812 }
813
814 if (Tok.isNot(tok::comma))
815 break;
816
817 ConsumeToken();
818 } while (true);
819
820 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000821 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822 return;
823
824 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000825 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000827 // The 'unavailable' availability cannot be combined with any other
828 // availability changes. Make sure that hasn't happened.
829 if (UnavailableLoc.isValid()) {
830 bool Complained = false;
831 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
832 if (Changes[Index].KeywordLoc.isValid()) {
833 if (!Complained) {
834 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
835 << SourceRange(Changes[Index].KeywordLoc,
836 Changes[Index].VersionRange.getEnd());
837 Complained = true;
838 }
839
840 // Clear out the availability.
841 Changes[Index] = AvailabilityChange();
842 }
843 }
844 }
845
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000847 attrs.addNew(&Availability,
848 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000849 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000850 Platform, PlatformLoc,
851 Changes[Introduced],
852 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000853 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000854 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000855 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000856}
857
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000858
Bill Wendling44426052012-12-20 19:22:21 +0000859// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000860// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
861
862void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
863
864void Parser::LateParsedClass::ParseLexedAttributes() {
865 Self->ParseLexedAttributes(*Class);
866}
867
868void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000869 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000870}
871
872/// Wrapper class which calls ParseLexedAttribute, after setting up the
873/// scope appropriately.
874void Parser::ParseLexedAttributes(ParsingClass &Class) {
875 // Deal with templates
876 // FIXME: Test cases to make sure this does the right thing for templates.
877 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
878 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
879 HasTemplateScope);
880 if (HasTemplateScope)
881 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
882
Douglas Gregor3024f072012-04-16 07:05:22 +0000883 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000884 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000885 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000886 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
887 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
888
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000889 // Enter the scope of nested classes
890 if (!AlreadyHasClassScope)
891 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
892 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000893 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000894 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
895 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
896 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000897 }
Chad Rosierc1183952012-06-26 22:30:43 +0000898
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000899 if (!AlreadyHasClassScope)
900 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
901 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000902}
903
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000904
905/// \brief Parse all attributes in LAs, and attach them to Decl D.
906void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
907 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000908 assert(LAs.parseSoon() &&
909 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000910 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000911 if (D)
912 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000913 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000914 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000915 }
916 LAs.clear();
917}
918
919
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000920/// \brief Finish parsing an attribute for which parsing was delayed.
921/// This will be called at the end of parsing a class declaration
922/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000923/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000924/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000925void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
926 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000927 // Save the current token position.
928 SourceLocation OrigLoc = Tok.getLocation();
929
930 // Append the current token at the end of the new token stream so that it
931 // doesn't get lost.
932 LA.Toks.push_back(Tok);
933 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
934 // Consume the previously pushed token.
935 ConsumeAnyToken();
936
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000937 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +0000938 // FIXME: Do not warn on C++11 attributes, once we start supporting
939 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000940 Diag(Tok, diag::warn_attribute_on_function_definition)
941 << LA.AttrName.getName();
942 }
943
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000944 ParsedAttributes Attrs(AttrFactory);
945 SourceLocation endLoc;
946
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000947 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000948 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000949 NamedDecl *ND = dyn_cast<NamedDecl>(D);
950 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000951
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000952 // Allow 'this' within late-parsed attributes.
953 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
954 /*TypeQuals=*/0,
955 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000956
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000957 if (LA.Decls.size() == 1) {
958 // If the Decl is templatized, add template parameters to scope.
959 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
960 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
961 if (HasTemplateScope)
962 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000963
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000964 // If the Decl is on a function, add function parameters to the scope.
965 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
966 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
967 if (HasFunScope)
968 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000969
Michael Han23214e52012-10-03 01:56:22 +0000970 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000971 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000972
973 if (HasFunScope) {
974 Actions.ActOnExitFunctionContext();
975 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
976 }
977 if (HasTemplateScope) {
978 TempScope.Exit();
979 }
980 } else {
981 // If there are multiple decls, then the decl cannot be within the
982 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000983 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000984 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000985 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000986 } else {
987 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000988 }
989
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000990 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
991 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
992 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000993
994 if (Tok.getLocation() != OrigLoc) {
995 // Due to a parsing error, we either went over the cached tokens or
996 // there are still cached tokens left, so we skip the leftover tokens.
997 // Since this is an uncommon situation that should be avoided, use the
998 // expensive isBeforeInTranslationUnit call.
999 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1000 OrigLoc))
1001 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001002 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001003 }
1004}
1005
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001006/// \brief Wrapper around a case statement checking if AttrName is
1007/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001008bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001009 return llvm::StringSwitch<bool>(AttrName)
1010 .Case("guarded_by", true)
1011 .Case("guarded_var", true)
1012 .Case("pt_guarded_by", true)
1013 .Case("pt_guarded_var", true)
1014 .Case("lockable", true)
1015 .Case("scoped_lockable", true)
1016 .Case("no_thread_safety_analysis", true)
1017 .Case("acquired_after", true)
1018 .Case("acquired_before", true)
1019 .Case("exclusive_lock_function", true)
1020 .Case("shared_lock_function", true)
1021 .Case("exclusive_trylock_function", true)
1022 .Case("shared_trylock_function", true)
1023 .Case("unlock_function", true)
1024 .Case("lock_returned", true)
1025 .Case("locks_excluded", true)
1026 .Case("exclusive_locks_required", true)
1027 .Case("shared_locks_required", true)
1028 .Default(false);
1029}
1030
1031/// \brief Parse the contents of thread safety attributes. These
1032/// should always be parsed as an expression list.
1033///
1034/// We need to special case the parsing due to the fact that if the first token
1035/// of the first argument is an identifier, the main parse loop will store
1036/// that token as a "parameter" and the rest of
1037/// the arguments will be added to a list of "arguments". However,
1038/// subsequent tokens in the first argument are lost. We instead parse each
1039/// argument as an expression and add all arguments to the list of "arguments".
1040/// In future, we will take advantage of this special case to also
1041/// deal with some argument scoping issues here (for example, referring to a
1042/// function parameter in the attribute on that function).
1043void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1044 SourceLocation AttrNameLoc,
1045 ParsedAttributes &Attrs,
1046 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001047 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001048
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001049 BalancedDelimiterTracker T(*this, tok::l_paren);
1050 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001051
Benjamin Kramerf0623432012-08-23 22:51:59 +00001052 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001053 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001054
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001055 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001056 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001057 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001058 ExprResult ArgExpr(ParseAssignmentExpression());
1059 if (ArgExpr.isInvalid()) {
1060 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001061 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001062 break;
1063 } else {
1064 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001065 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001066 if (Tok.isNot(tok::comma))
1067 break;
1068 ConsumeToken(); // Eat the comma, move to the next argument
1069 }
1070 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001071 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001072 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001073 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001074 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001075 if (EndLoc)
1076 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001077}
1078
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001079void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1080 SourceLocation AttrNameLoc,
1081 ParsedAttributes &Attrs,
1082 SourceLocation *EndLoc) {
1083 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1084
1085 BalancedDelimiterTracker T(*this, tok::l_paren);
1086 T.consumeOpen();
1087
1088 if (Tok.isNot(tok::identifier)) {
1089 Diag(Tok, diag::err_expected_ident);
1090 T.skipToEnd();
1091 return;
1092 }
1093 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1094 SourceLocation ArgumentKindLoc = ConsumeToken();
1095
1096 if (Tok.isNot(tok::comma)) {
1097 Diag(Tok, diag::err_expected_comma);
1098 T.skipToEnd();
1099 return;
1100 }
1101 ConsumeToken();
1102
1103 SourceRange MatchingCTypeRange;
1104 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1105 if (MatchingCType.isInvalid()) {
1106 T.skipToEnd();
1107 return;
1108 }
1109
1110 bool LayoutCompatible = false;
1111 bool MustBeNull = false;
1112 while (Tok.is(tok::comma)) {
1113 ConsumeToken();
1114 if (Tok.isNot(tok::identifier)) {
1115 Diag(Tok, diag::err_expected_ident);
1116 T.skipToEnd();
1117 return;
1118 }
1119 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1120 if (Flag->isStr("layout_compatible"))
1121 LayoutCompatible = true;
1122 else if (Flag->isStr("must_be_null"))
1123 MustBeNull = true;
1124 else {
1125 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1126 T.skipToEnd();
1127 return;
1128 }
1129 ConsumeToken(); // consume flag
1130 }
1131
1132 if (!T.consumeClose()) {
1133 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1134 ArgumentKind, ArgumentKindLoc,
1135 MatchingCType.release(), LayoutCompatible,
1136 MustBeNull, AttributeList::AS_GNU);
1137 }
1138
1139 if (EndLoc)
1140 *EndLoc = T.getCloseLocation();
1141}
1142
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001143/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1144/// of a C++11 attribute-specifier in a location where an attribute is not
1145/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1146/// situation.
1147///
1148/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1149/// this doesn't appear to actually be an attribute-specifier, and the caller
1150/// should try to parse it.
1151bool Parser::DiagnoseProhibitedCXX11Attribute() {
1152 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1153
1154 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1155 case CAK_NotAttributeSpecifier:
1156 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1157 return false;
1158
1159 case CAK_InvalidAttributeSpecifier:
1160 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1161 return false;
1162
1163 case CAK_AttributeSpecifier:
1164 // Parse and discard the attributes.
1165 SourceLocation BeginLoc = ConsumeBracket();
1166 ConsumeBracket();
1167 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1168 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1169 SourceLocation EndLoc = ConsumeBracket();
1170 Diag(BeginLoc, diag::err_attributes_not_allowed)
1171 << SourceRange(BeginLoc, EndLoc);
1172 return true;
1173 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001174 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001175}
1176
Richard Smith98155ad2013-02-20 01:17:14 +00001177/// \brief We have found the opening square brackets of a C++11
1178/// attribute-specifier in a location where an attribute is not permitted, but
1179/// we know where the attributes ought to be written. Parse them anyway, and
1180/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001181void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1182 SourceLocation CorrectLocation) {
1183 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1184 Tok.is(tok::kw_alignas));
1185
1186 // Consume the attributes.
1187 SourceLocation Loc = Tok.getLocation();
1188 ParseCXX11Attributes(Attrs);
1189 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1190
1191 Diag(Loc, diag::err_attributes_not_allowed)
1192 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1193 << FixItHint::CreateRemoval(AttrRange);
1194}
1195
John McCall53fa7142010-12-24 02:08:15 +00001196void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1197 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1198 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001199}
1200
Michael Han64536a62012-11-06 19:34:54 +00001201void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1202 AttributeList *AttrList = attrs.getList();
1203 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001204 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001205 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001206 << AttrList->getName();
1207 AttrList->setInvalid();
1208 }
1209 AttrList = AttrList->getNext();
1210 }
1211}
1212
Chris Lattner53361ac2006-08-10 05:19:57 +00001213/// ParseDeclaration - Parse a full 'declaration', which consists of
1214/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001215/// 'Context' should be a Declarator::TheContext value. This returns the
1216/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001217///
1218/// declaration: [C99 6.7]
1219/// block-declaration ->
1220/// simple-declaration
1221/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001222/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001223/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001224/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001225/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001226/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001227/// others... [FIXME]
1228///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001229Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1230 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001231 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001232 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001233 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001234 // Must temporarily exit the objective-c container scope for
1235 // parsing c none objective-c decls.
1236 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001237
John McCall48871652010-08-21 09:40:31 +00001238 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001239 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001240 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001241 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001242 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001243 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001244 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001245 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001246 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001247 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001248 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001249 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001250 SourceLocation InlineLoc = ConsumeToken();
1251 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1252 break;
1253 }
Chad Rosierc1183952012-06-26 22:30:43 +00001254 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001255 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001256 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001257 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001258 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001259 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001260 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001261 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001262 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001263 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001264 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001265 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001266 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001267 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001268 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001269 default:
John McCall53fa7142010-12-24 02:08:15 +00001270 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001271 }
Chad Rosierc1183952012-06-26 22:30:43 +00001272
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001273 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001274 // single decl, convert it now. Alias declarations can also declare a type;
1275 // include that too if it is present.
1276 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001277}
1278
1279/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1280/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001281/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1282/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001283///[C90/C++]init-declarator-list ';' [TODO]
1284/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001285///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001286/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001287/// attribute-specifier-seq[opt] type-specifier-seq declarator
1288///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001289/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001290/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001291///
1292/// If FRI is non-null, we might be parsing a for-range-declaration instead
1293/// of a simple-declaration. If we find that we are, we also parse the
1294/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001295Parser::DeclGroupPtrTy
1296Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1297 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001298 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001299 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001300 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001301 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001302
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001303 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001304 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001305
Chris Lattner0e894622006-08-13 19:58:17 +00001306 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1307 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001308 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001309 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001310 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001311 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001312 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001313 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001314 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001315 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001316 }
Chad Rosierc1183952012-06-26 22:30:43 +00001317
Richard Smith2386c8b2013-02-22 09:06:26 +00001318 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001319 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001320}
Mike Stump11289f42009-09-09 15:08:12 +00001321
Richard Smith09f76ee2011-10-19 21:33:05 +00001322/// Returns true if this might be the start of a declarator, or a common typo
1323/// for a declarator.
1324bool Parser::MightBeDeclarator(unsigned Context) {
1325 switch (Tok.getKind()) {
1326 case tok::annot_cxxscope:
1327 case tok::annot_template_id:
1328 case tok::caret:
1329 case tok::code_completion:
1330 case tok::coloncolon:
1331 case tok::ellipsis:
1332 case tok::kw___attribute:
1333 case tok::kw_operator:
1334 case tok::l_paren:
1335 case tok::star:
1336 return true;
1337
1338 case tok::amp:
1339 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001340 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001341
Richard Smithc8a79032012-01-09 22:31:44 +00001342 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001343 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001344 NextToken().is(tok::l_square);
1345
1346 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001347 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001348
Richard Smith09f76ee2011-10-19 21:33:05 +00001349 case tok::identifier:
1350 switch (NextToken().getKind()) {
1351 case tok::code_completion:
1352 case tok::coloncolon:
1353 case tok::comma:
1354 case tok::equal:
1355 case tok::equalequal: // Might be a typo for '='.
1356 case tok::kw_alignas:
1357 case tok::kw_asm:
1358 case tok::kw___attribute:
1359 case tok::l_brace:
1360 case tok::l_paren:
1361 case tok::l_square:
1362 case tok::less:
1363 case tok::r_brace:
1364 case tok::r_paren:
1365 case tok::r_square:
1366 case tok::semi:
1367 return true;
1368
1369 case tok::colon:
1370 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001371 // and in block scope it's probably a label. Inside a class definition,
1372 // this is a bit-field.
1373 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001374 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001375
1376 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001377 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001378
1379 default:
1380 return false;
1381 }
1382
1383 default:
1384 return false;
1385 }
1386}
1387
Richard Smithb8caac82012-04-11 20:59:20 +00001388/// Skip until we reach something which seems like a sensible place to pick
1389/// up parsing after a malformed declaration. This will sometimes stop sooner
1390/// than SkipUntil(tok::r_brace) would, but will never stop later.
1391void Parser::SkipMalformedDecl() {
1392 while (true) {
1393 switch (Tok.getKind()) {
1394 case tok::l_brace:
1395 // Skip until matching }, then stop. We've probably skipped over
1396 // a malformed class or function definition or similar.
1397 ConsumeBrace();
1398 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1399 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1400 // This declaration isn't over yet. Keep skipping.
1401 continue;
1402 }
1403 if (Tok.is(tok::semi))
1404 ConsumeToken();
1405 return;
1406
1407 case tok::l_square:
1408 ConsumeBracket();
1409 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1410 continue;
1411
1412 case tok::l_paren:
1413 ConsumeParen();
1414 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1415 continue;
1416
1417 case tok::r_brace:
1418 return;
1419
1420 case tok::semi:
1421 ConsumeToken();
1422 return;
1423
1424 case tok::kw_inline:
1425 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001426 // a good place to pick back up parsing, except in an Objective-C
1427 // @interface context.
1428 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1429 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001430 return;
1431 break;
1432
1433 case tok::kw_namespace:
1434 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001435 // place to pick back up parsing, except in an Objective-C
1436 // @interface context.
1437 if (Tok.isAtStartOfLine() &&
1438 (!ParsingInObjCContainer || CurParsedObjCImpl))
1439 return;
1440 break;
1441
1442 case tok::at:
1443 // @end is very much like } in Objective-C contexts.
1444 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1445 ParsingInObjCContainer)
1446 return;
1447 break;
1448
1449 case tok::minus:
1450 case tok::plus:
1451 // - and + probably start new method declarations in Objective-C contexts.
1452 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001453 return;
1454 break;
1455
1456 case tok::eof:
1457 return;
1458
1459 default:
1460 break;
1461 }
1462
1463 ConsumeAnyToken();
1464 }
1465}
1466
John McCalld5a36322009-11-03 19:26:08 +00001467/// ParseDeclGroup - Having concluded that this is either a function
1468/// definition or a group of object declarations, actually parse the
1469/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001470Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1471 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001472 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001473 SourceLocation *DeclEnd,
1474 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001475 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001476 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001477 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001478
John McCalld5a36322009-11-03 19:26:08 +00001479 // Bail out if the first declarator didn't seem well-formed.
1480 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001481 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001482 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001483 }
Mike Stump11289f42009-09-09 15:08:12 +00001484
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001485 // Save late-parsed attributes for now; they need to be parsed in the
1486 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001487 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1488 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001489 if (D.isFunctionDeclarator())
1490 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1491
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001492 // Check to see if we have a function *definition* which must have a body.
1493 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1494 // Look at the next token to make sure that this isn't a function
1495 // declaration. We have to check this because __attribute__ might be the
1496 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001497 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001498
Chris Lattner13901342010-07-11 22:42:07 +00001499 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001500 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1501 Diag(Tok, diag::err_function_declared_typedef);
1502
1503 // Recover by treating the 'typedef' as spurious.
1504 DS.ClearStorageClassSpecs();
1505 }
1506
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001507 Decl *TheDecl =
1508 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001509 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001510 }
Chad Rosierc1183952012-06-26 22:30:43 +00001511
Chris Lattner13901342010-07-11 22:42:07 +00001512 if (isDeclarationSpecifier()) {
1513 // If there is an invalid declaration specifier right after the function
1514 // prototype, then we must be in a missing semicolon case where this isn't
1515 // actually a body. Just fall through into the code that handles it as a
1516 // prototype, and let the top-level code handle the erroneous declspec
1517 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001518 } else {
1519 Diag(Tok, diag::err_expected_fn_body);
1520 SkipUntil(tok::semi);
1521 return DeclGroupPtrTy();
1522 }
1523 }
1524
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001525 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001526 return DeclGroupPtrTy();
1527
1528 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1529 // must parse and analyze the for-range-initializer before the declaration is
1530 // analyzed.
1531 if (FRI && Tok.is(tok::colon)) {
1532 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001533 if (Tok.is(tok::l_brace))
1534 FRI->RangeExpr = ParseBraceInitializer();
1535 else
1536 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001537 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1538 Actions.ActOnCXXForRangeDecl(ThisDecl);
1539 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001540 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001541 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1542 }
1543
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001544 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001545 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001546 if (LateParsedAttrs.size() > 0)
1547 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001548 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001549 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001550 DeclsInGroup.push_back(FirstDecl);
1551
Richard Smith09f76ee2011-10-19 21:33:05 +00001552 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001553
John McCalld5a36322009-11-03 19:26:08 +00001554 // If we don't have a comma, it is either the end of the list (a ';') or an
1555 // error, bail out.
1556 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001557 SourceLocation CommaLoc = ConsumeToken();
1558
1559 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1560 // This comma was followed by a line-break and something which can't be
1561 // the start of a declarator. The comma was probably a typo for a
1562 // semicolon.
1563 Diag(CommaLoc, diag::err_expected_semi_declaration)
1564 << FixItHint::CreateReplacement(CommaLoc, ";");
1565 ExpectSemi = false;
1566 break;
1567 }
John McCalld5a36322009-11-03 19:26:08 +00001568
1569 // Parse the next declarator.
1570 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001571 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001572
1573 // Accept attributes in an init-declarator. In the first declarator in a
1574 // declaration, these would be part of the declspec. In subsequent
1575 // declarators, they become part of the declarator itself, so that they
1576 // don't apply to declarators after *this* one. Examples:
1577 // short __attribute__((common)) var; -> declspec
1578 // short var __attribute__((common)); -> declarator
1579 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001580 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001581
1582 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001583 if (!D.isInvalidType()) {
1584 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1585 D.complete(ThisDecl);
1586 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001587 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001588 }
John McCalld5a36322009-11-03 19:26:08 +00001589 }
1590
1591 if (DeclEnd)
1592 *DeclEnd = Tok.getLocation();
1593
Richard Smith09f76ee2011-10-19 21:33:05 +00001594 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001595 ExpectAndConsumeSemi(Context == Declarator::FileContext
1596 ? diag::err_invalid_token_after_toplevel_declarator
1597 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001598 // Okay, there was no semicolon and one was expected. If we see a
1599 // declaration specifier, just assume it was missing and continue parsing.
1600 // Otherwise things are very confused and we skip to recover.
1601 if (!isDeclarationSpecifier()) {
1602 SkipUntil(tok::r_brace, true, true);
1603 if (Tok.is(tok::semi))
1604 ConsumeToken();
1605 }
John McCalld5a36322009-11-03 19:26:08 +00001606 }
1607
Douglas Gregor0be31a22010-07-02 17:43:08 +00001608 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001609 DeclsInGroup.data(),
1610 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001611}
1612
Richard Smith02e85f32011-04-14 22:09:26 +00001613/// Parse an optional simple-asm-expr and attributes, and attach them to a
1614/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001615bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001616 // If a simple-asm-expr is present, parse it.
1617 if (Tok.is(tok::kw_asm)) {
1618 SourceLocation Loc;
1619 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1620 if (AsmLabel.isInvalid()) {
1621 SkipUntil(tok::semi, true, true);
1622 return true;
1623 }
1624
1625 D.setAsmLabel(AsmLabel.release());
1626 D.SetRangeEnd(Loc);
1627 }
1628
1629 MaybeParseGNUAttributes(D);
1630 return false;
1631}
1632
Douglas Gregor23996282009-05-12 21:31:51 +00001633/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1634/// declarator'. This method parses the remainder of the declaration
1635/// (including any attributes or initializer, among other things) and
1636/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001637///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001638/// init-declarator: [C99 6.7]
1639/// declarator
1640/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001641/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1642/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001643/// [C++] declarator initializer[opt]
1644///
1645/// [C++] initializer:
1646/// [C++] '=' initializer-clause
1647/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001648/// [C++0x] '=' 'default' [TODO]
1649/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001650/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001651///
1652/// According to the standard grammar, =default and =delete are function
1653/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001654///
John McCall48871652010-08-21 09:40:31 +00001655Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001656 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001657 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001658 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001659
Richard Smith02e85f32011-04-14 22:09:26 +00001660 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1661}
Mike Stump11289f42009-09-09 15:08:12 +00001662
Richard Smith02e85f32011-04-14 22:09:26 +00001663Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1664 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001665 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001666 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001667 switch (TemplateInfo.Kind) {
1668 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001669 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001670 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001671
Douglas Gregor450f00842009-09-25 18:43:00 +00001672 case ParsedTemplateInfo::Template:
1673 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001674 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001675 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001676 D);
1677 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001678
Douglas Gregor450f00842009-09-25 18:43:00 +00001679 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001680 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001681 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001682 TemplateInfo.ExternLoc,
1683 TemplateInfo.TemplateLoc,
1684 D);
1685 if (ThisRes.isInvalid()) {
1686 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001687 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001688 }
Chad Rosierc1183952012-06-26 22:30:43 +00001689
Douglas Gregor450f00842009-09-25 18:43:00 +00001690 ThisDecl = ThisRes.get();
1691 break;
1692 }
1693 }
Mike Stump11289f42009-09-09 15:08:12 +00001694
Richard Smith30482bc2011-02-20 03:19:35 +00001695 bool TypeContainsAuto =
1696 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1697
Douglas Gregor23996282009-05-12 21:31:51 +00001698 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001699 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001700 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001701 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001702 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001703 if (D.isFunctionDeclarator())
1704 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1705 << 1 /* delete */;
1706 else
1707 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001708 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001709 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001710 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1711 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001712 else
1713 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001714 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001715 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001716 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001717 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001718 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001719
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001720 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001721 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001722 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001723 cutOffParsing();
1724 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001725 }
Chad Rosierc1183952012-06-26 22:30:43 +00001726
John McCalldadc5752010-08-24 06:29:42 +00001727 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001728
David Blaikiebbafb8a2012-03-11 07:00:24 +00001729 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001730 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001731 ExitScope();
1732 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001733
Douglas Gregor23996282009-05-12 21:31:51 +00001734 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001735 SkipUntil(tok::comma, true, true);
1736 Actions.ActOnInitializerError(ThisDecl);
1737 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001738 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1739 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001740 }
1741 } else if (Tok.is(tok::l_paren)) {
1742 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001743 BalancedDelimiterTracker T(*this, tok::l_paren);
1744 T.consumeOpen();
1745
Benjamin Kramerf0623432012-08-23 22:51:59 +00001746 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001747 CommaLocsTy CommaLocs;
1748
David Blaikiebbafb8a2012-03-11 07:00:24 +00001749 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001750 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001751 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001752 }
1753
Douglas Gregor23996282009-05-12 21:31:51 +00001754 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001755 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001756 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001757
David Blaikiebbafb8a2012-03-11 07:00:24 +00001758 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001759 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001760 ExitScope();
1761 }
Douglas Gregor23996282009-05-12 21:31:51 +00001762 } else {
1763 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001764 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001765
1766 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1767 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001768
David Blaikiebbafb8a2012-03-11 07:00:24 +00001769 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001770 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001771 ExitScope();
1772 }
1773
Sebastian Redla9351792012-02-11 23:51:47 +00001774 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1775 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001776 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001777 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1778 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001779 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001780 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001781 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001782 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001783 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1784
Sebastian Redl3da34892011-06-05 12:23:16 +00001785 if (D.getCXXScopeSpec().isSet()) {
1786 EnterScope(0);
1787 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1788 }
1789
1790 ExprResult Init(ParseBraceInitializer());
1791
1792 if (D.getCXXScopeSpec().isSet()) {
1793 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1794 ExitScope();
1795 }
1796
1797 if (Init.isInvalid()) {
1798 Actions.ActOnInitializerError(ThisDecl);
1799 } else
1800 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1801 /*DirectInit=*/true, TypeContainsAuto);
1802
Douglas Gregor23996282009-05-12 21:31:51 +00001803 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001804 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001805 }
1806
Richard Smithb2bc2e62011-02-21 20:05:19 +00001807 Actions.FinalizeDeclaration(ThisDecl);
1808
Douglas Gregor23996282009-05-12 21:31:51 +00001809 return ThisDecl;
1810}
1811
Chris Lattner1890ac82006-08-13 01:16:23 +00001812/// ParseSpecifierQualifierList
1813/// specifier-qualifier-list:
1814/// type-specifier specifier-qualifier-list[opt]
1815/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001816/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001817///
Richard Smithc5b05522012-03-12 07:56:15 +00001818void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1819 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001820 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1821 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001822 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001823 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattner1890ac82006-08-13 01:16:23 +00001825 // Validate declspec for type-name.
1826 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001827 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1828 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001829 Diag(Tok, diag::err_expected_type);
1830 DS.SetTypeSpecError();
1831 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1832 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001833 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001834 if (!DS.hasTypeSpecifier())
1835 DS.SetTypeSpecError();
1836 }
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner1b22eed2006-11-28 05:12:07 +00001838 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001839 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001840 if (DS.getStorageClassSpecLoc().isValid())
1841 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1842 else
1843 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001844 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001845 }
Mike Stump11289f42009-09-09 15:08:12 +00001846
Chris Lattner1b22eed2006-11-28 05:12:07 +00001847 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001848 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001849 if (DS.isInlineSpecified())
1850 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1851 if (DS.isVirtualSpecified())
1852 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1853 if (DS.isExplicitSpecified())
1854 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001855 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001856 }
Richard Smithc5b05522012-03-12 07:56:15 +00001857
1858 // Issue diagnostic and remove constexpr specfier if present.
1859 if (DS.isConstexprSpecified()) {
1860 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1861 DS.ClearConstexprSpec();
1862 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001863}
Chris Lattner53361ac2006-08-10 05:19:57 +00001864
Chris Lattner6cc055a2009-04-12 20:42:31 +00001865/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1866/// specified token is valid after the identifier in a declarator which
1867/// immediately follows the declspec. For example, these things are valid:
1868///
1869/// int x [ 4]; // direct-declarator
1870/// int x ( int y); // direct-declarator
1871/// int(int x ) // direct-declarator
1872/// int x ; // simple-declaration
1873/// int x = 17; // init-declarator-list
1874/// int x , y; // init-declarator-list
1875/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001876/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001877/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001878///
1879/// This is not, because 'x' does not immediately follow the declspec (though
1880/// ')' happens to be valid anyway).
1881/// int (x)
1882///
1883static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1884 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1885 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001886 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001887}
1888
Chris Lattner20a0c612009-04-14 21:34:55 +00001889
1890/// ParseImplicitInt - This method is called when we have an non-typename
1891/// identifier in a declspec (which normally terminates the decl spec) when
1892/// the declspec has no type specifier. In this case, the declspec is either
1893/// malformed or is "implicit int" (in K&R and C89).
1894///
1895/// This method handles diagnosing this prettily and returns false if the
1896/// declspec is done being processed. If it recovers and thinks there may be
1897/// other pieces of declspec after it, it returns true.
1898///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001899bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001900 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00001901 AccessSpecifier AS, DeclSpecContext DSC,
1902 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001903 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattner20a0c612009-04-14 21:34:55 +00001905 SourceLocation Loc = Tok.getLocation();
1906 // If we see an identifier that is not a type name, we normally would
1907 // parse it as the identifer being declared. However, when a typename
1908 // is typo'd or the definition is not included, this will incorrectly
1909 // parse the typename as the identifier name and fall over misparsing
1910 // later parts of the diagnostic.
1911 //
1912 // As such, we try to do some look-ahead in cases where this would
1913 // otherwise be an "implicit-int" case to see if this is invalid. For
1914 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1915 // an identifier with implicit int, we'd get a parse error because the
1916 // next token is obviously invalid for a type. Parse these as a case
1917 // with an invalid type specifier.
1918 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001919
Chris Lattner20a0c612009-04-14 21:34:55 +00001920 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001921 // error, do lookahead to try to do better recovery. This never applies
1922 // within a type specifier. Outside of C++, we allow this even if the
1923 // language doesn't "officially" support implicit int -- we support
1924 // implicit int as an extension in C99 and C11. Allegedly, MS also
1925 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001926 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001927 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001928 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001929 // If this token is valid for implicit int, e.g. "static x = 4", then
1930 // we just avoid eating the identifier, so it will be parsed as the
1931 // identifier in the declarator.
1932 return false;
1933 }
Mike Stump11289f42009-09-09 15:08:12 +00001934
Richard Smitha952ebb2012-05-15 21:01:51 +00001935 if (getLangOpts().CPlusPlus &&
1936 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1937 // Don't require a type specifier if we have the 'auto' storage class
1938 // specifier in C++98 -- we'll promote it to a type specifier.
1939 return false;
1940 }
1941
Chris Lattner20a0c612009-04-14 21:34:55 +00001942 // Otherwise, if we don't consume this token, we are going to emit an
1943 // error anyway. Try to recover from various common problems. Check
1944 // to see if this was a reference to a tag name without a tag specified.
1945 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001946 //
1947 // C++ doesn't need this, and isTagName doesn't take SS.
1948 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001949 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001950 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Douglas Gregor0be31a22010-07-02 17:43:08 +00001952 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001953 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001954 case DeclSpec::TST_enum:
1955 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1956 case DeclSpec::TST_union:
1957 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1958 case DeclSpec::TST_struct:
1959 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001960 case DeclSpec::TST_interface:
1961 TagName="__interface"; FixitTagName = "__interface ";
1962 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001963 case DeclSpec::TST_class:
1964 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001965 }
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001967 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001968 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1969 LookupResult R(Actions, TokenName, SourceLocation(),
1970 Sema::LookupOrdinaryName);
1971
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001972 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001973 << TokenName << TagName << getLangOpts().CPlusPlus
1974 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1975
1976 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1977 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1978 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001979 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001980 << TokenName << TagName;
1981 }
Mike Stump11289f42009-09-09 15:08:12 +00001982
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001983 // Parse this as a tag as if the missing tag were present.
1984 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001985 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001986 else
Richard Smithc5b05522012-03-12 07:56:15 +00001987 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00001988 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001989 return true;
1990 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
Richard Smithfe904f02012-05-15 21:29:55 +00001993 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001994 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001995 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1996 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001997 // Look ahead to the next token to try to figure out what this declaration
1998 // was supposed to be.
1999 switch (NextToken().getKind()) {
2000 case tok::comma:
2001 case tok::equal:
2002 case tok::kw_asm:
2003 case tok::l_brace:
2004 case tok::l_square:
2005 case tok::semi:
2006 // This looks like a variable declaration. The type is probably missing.
2007 // We're done parsing decl-specifiers.
2008 return false;
2009
2010 case tok::l_paren: {
2011 // static x(4); // 'x' is not a type
2012 // x(int n); // 'x' is not a type
2013 // x (*p)[]; // 'x' is a type
2014 //
2015 // Since we're in an error case (or the rare 'implicit int in C++' MS
2016 // extension), we can afford to perform a tentative parse to determine
2017 // which case we're in.
2018 TentativeParsingAction PA(*this);
2019 ConsumeToken();
2020 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2021 PA.Revert();
2022 if (TPR == TPResult::False())
2023 return false;
2024 // The identifier is followed by a parenthesized declarator.
2025 // It's supposed to be a type.
2026 break;
2027 }
2028
2029 default:
2030 // This is probably supposed to be a type. This includes cases like:
2031 // int f(itn);
2032 // struct S { unsinged : 4; };
2033 break;
2034 }
2035 }
2036
Chad Rosierc1183952012-06-26 22:30:43 +00002037 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002038 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002039 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002040 IdentifierInfo *II = Tok.getIdentifierInfo();
2041 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002042 // The action emitted a diagnostic, so we don't have to.
2043 if (T) {
2044 // The action has suggested that the type T could be used. Set that as
2045 // the type in the declaration specifiers, consume the would-be type
2046 // name token, and we're done.
2047 const char *PrevSpec;
2048 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002049 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002050 DS.SetRangeEnd(Tok.getLocation());
2051 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002052 // There may be other declaration specifiers after this.
2053 return true;
2054 } else if (II != Tok.getIdentifierInfo()) {
2055 // If no type was suggested, the correction is to a keyword
2056 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002057 // There may be other declaration specifiers after this.
2058 return true;
2059 }
Chad Rosierc1183952012-06-26 22:30:43 +00002060
Douglas Gregor15e56022009-10-13 23:27:22 +00002061 // Fall through; the action had no suggestion for us.
2062 } else {
2063 // The action did not emit a diagnostic, so emit one now.
2064 SourceRange R;
2065 if (SS) R = SS->getRange();
2066 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2067 }
Mike Stump11289f42009-09-09 15:08:12 +00002068
Douglas Gregor15e56022009-10-13 23:27:22 +00002069 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002070 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002071 DS.SetRangeEnd(Tok.getLocation());
2072 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002073
Chris Lattner20a0c612009-04-14 21:34:55 +00002074 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2075 // avoid rippling error messages on subsequent uses of the same type,
2076 // could be useful if #include was forgotten.
2077 return false;
2078}
2079
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002080/// \brief Determine the declaration specifier context from the declarator
2081/// context.
2082///
2083/// \param Context the declarator context, which is one of the
2084/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002085Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002086Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2087 if (Context == Declarator::MemberContext)
2088 return DSC_class;
2089 if (Context == Declarator::FileContext)
2090 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002091 if (Context == Declarator::TrailingReturnContext)
2092 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002093 return DSC_normal;
2094}
2095
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002096/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2097///
2098/// FIXME: Simply returns an alignof() expression if the argument is a
2099/// type. Ideally, the type should be propagated directly into Sema.
2100///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002101/// [C11] type-id
2102/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002103/// [C++0x] type-id ...[opt]
2104/// [C++0x] assignment-expression ...[opt]
2105ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2106 SourceLocation &EllipsisLoc) {
2107 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002108 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002109 SourceLocation TypeLoc = Tok.getLocation();
2110 ParsedType Ty = ParseTypeName().get();
2111 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002112 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2113 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002114 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002115 ER = ParseConstantExpression();
2116
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002117 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002118 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002119
2120 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002121}
2122
2123/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2124/// attribute to Attrs.
2125///
2126/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002127/// [C11] '_Alignas' '(' type-id ')'
2128/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002129/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2130/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002131void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002132 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002133 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2134 "Not an alignment-specifier!");
2135
Richard Smithd11c7a12013-01-29 01:48:07 +00002136 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2137 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002138
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002139 BalancedDelimiterTracker T(*this, tok::l_paren);
2140 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002141 return;
2142
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002143 SourceLocation EllipsisLoc;
2144 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002145 if (ArgExpr.isInvalid()) {
2146 SkipUntil(tok::r_paren);
2147 return;
2148 }
2149
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002150 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002151 if (EndLoc)
2152 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002153
Benjamin Kramerf0623432012-08-23 22:51:59 +00002154 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002155 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002156 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smith44c247f2013-02-22 08:32:16 +00002157 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002158}
2159
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002160/// ParseDeclarationSpecifiers
2161/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002162/// storage-class-specifier declaration-specifiers[opt]
2163/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002164/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002165/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002166/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002167/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002168///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002169/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002170/// 'typedef'
2171/// 'extern'
2172/// 'static'
2173/// 'auto'
2174/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002175/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002176/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002177/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002178/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002179/// [C++] 'virtual'
2180/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002181/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002182/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002183/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002184
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002185///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002186void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002187 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002188 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002189 DeclSpecContext DSContext,
2190 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002191 if (DS.getSourceRange().isInvalid()) {
2192 DS.SetRangeStart(Tok.getLocation());
2193 DS.SetRangeEnd(Tok.getLocation());
2194 }
Chad Rosierc1183952012-06-26 22:30:43 +00002195
Douglas Gregordf593fb2011-11-07 17:33:42 +00002196 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002197 bool AttrsLastTime = false;
2198 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002199 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002200 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002201 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002202 unsigned DiagID = 0;
2203
Chris Lattner4d8f8732006-11-28 05:05:08 +00002204 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002205
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002206 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002207 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002208 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002209 if (!AttrsLastTime)
2210 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002211 else {
2212 // Reject C++11 attributes that appertain to decl specifiers as
2213 // we don't support any C++11 attributes that appertain to decl
2214 // specifiers. This also conforms to what g++ 4.8 is doing.
2215 ProhibitCXX11Attributes(attrs);
2216
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002217 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002218 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002219
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002220 // If this is not a declaration specifier token, we're done reading decl
2221 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002222 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002223 return;
Mike Stump11289f42009-09-09 15:08:12 +00002224
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002225 case tok::l_square:
2226 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002227 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002228 goto DoneWithDeclSpec;
2229
2230 ProhibitAttributes(attrs);
2231 // FIXME: It would be good to recover by accepting the attributes,
2232 // but attempting to do that now would cause serious
2233 // madness in terms of diagnostics.
2234 attrs.clear();
2235 attrs.Range = SourceRange();
2236
2237 ParseCXX11Attributes(attrs);
2238 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002239 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002240
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002241 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002242 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002243 if (DS.hasTypeSpecifier()) {
2244 bool AllowNonIdentifiers
2245 = (getCurScope()->getFlags() & (Scope::ControlScope |
2246 Scope::BlockScope |
2247 Scope::TemplateParamScope |
2248 Scope::FunctionPrototypeScope |
2249 Scope::AtCatchScope)) == 0;
2250 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002251 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002252 (DSContext == DSC_class && DS.isFriendSpecified());
2253
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002254 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002255 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002256 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002257 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002258 }
2259
Douglas Gregor80039242011-02-15 20:33:25 +00002260 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2261 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2262 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002263 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002264 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002265 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002266 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002267 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002268 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002269
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002270 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002271 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002272 }
2273
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002274 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002275 // C++ scope specifier. Annotate and loop, or bail out on error.
2276 if (TryAnnotateCXXScopeToken(true)) {
2277 if (!DS.hasTypeSpecifier())
2278 DS.SetTypeSpecError();
2279 goto DoneWithDeclSpec;
2280 }
John McCall8bc2a702010-03-01 18:20:46 +00002281 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2282 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002283 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002284
2285 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002286 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002287 goto DoneWithDeclSpec;
2288
John McCall9dab4e62009-12-12 11:40:51 +00002289 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002290 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2291 Tok.getAnnotationRange(),
2292 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002293
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002294 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002295 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002296 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002297 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002298 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002299 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002300
2301 // C++ [class.qual]p2:
2302 // In a lookup in which the constructor is an acceptable lookup
2303 // result and the nested-name-specifier nominates a class C:
2304 //
2305 // - if the name specified after the
2306 // nested-name-specifier, when looked up in C, is the
2307 // injected-class-name of C (Clause 9), or
2308 //
2309 // - if the name specified after the nested-name-specifier
2310 // is the same as the identifier or the
2311 // simple-template-id's template-name in the last
2312 // component of the nested-name-specifier,
2313 //
2314 // the name is instead considered to name the constructor of
2315 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002316 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002317 // Thus, if the template-name is actually the constructor
2318 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002319 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002320 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002321 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002322 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002323 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002324 if (isConstructorDeclarator()) {
2325 // The user meant this to be an out-of-line constructor
2326 // definition, but template arguments are not allowed
2327 // there. Just allow this as a constructor; we'll
2328 // complain about it later.
2329 goto DoneWithDeclSpec;
2330 }
2331
2332 // The user meant this to name a type, but it actually names
2333 // a constructor with some extraneous template
2334 // arguments. Complain, then parse it as a type as the user
2335 // intended.
2336 Diag(TemplateId->TemplateNameLoc,
2337 diag::err_out_of_line_template_id_names_constructor)
2338 << TemplateId->Name;
2339 }
2340
John McCall9dab4e62009-12-12 11:40:51 +00002341 DS.getTypeSpecScope() = SS;
2342 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002343 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002344 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002345 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002346 continue;
2347 }
2348
Douglas Gregorc5790df2009-09-28 07:26:33 +00002349 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002350 DS.getTypeSpecScope() = SS;
2351 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002352 if (Tok.getAnnotationValue()) {
2353 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002355 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002356 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002357 if (isInvalid)
2358 break;
John McCallba7bf592010-08-24 05:47:05 +00002359 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002360 else
2361 DS.SetTypeSpecError();
2362 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2363 ConsumeToken(); // The typename
2364 }
2365
Douglas Gregor167fa622009-03-25 15:40:00 +00002366 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002367 goto DoneWithDeclSpec;
2368
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002369 // If we're in a context where the identifier could be a class name,
2370 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002371 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002372 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002373 &SS)) {
2374 if (isConstructorDeclarator())
2375 goto DoneWithDeclSpec;
2376
2377 // As noted in C++ [class.qual]p2 (cited above), when the name
2378 // of the class is qualified in a context where it could name
2379 // a constructor, its a constructor name. However, we've
2380 // looked at the declarator, and the user probably meant this
2381 // to be a type. Complain that it isn't supposed to be treated
2382 // as a type, then proceed to parse it as a type.
2383 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2384 << Next.getIdentifierInfo();
2385 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002386
John McCallba7bf592010-08-24 05:47:05 +00002387 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2388 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002389 getCurScope(), &SS,
2390 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002391 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002392 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002393
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002394 // If the referenced identifier is not a type, then this declspec is
2395 // erroneous: We already checked about that it has no type specifier, and
2396 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002397 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002398 if (TypeRep == 0) {
2399 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002400 ParsedAttributesWithRange Attrs(AttrFactory);
2401 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2402 if (!Attrs.empty()) {
2403 AttrsLastTime = true;
2404 attrs.takeAllFrom(Attrs);
2405 }
2406 continue;
2407 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002408 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002409 }
Mike Stump11289f42009-09-09 15:08:12 +00002410
John McCall9dab4e62009-12-12 11:40:51 +00002411 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002412 ConsumeToken(); // The C++ scope.
2413
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002415 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002416 if (isInvalid)
2417 break;
Mike Stump11289f42009-09-09 15:08:12 +00002418
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002419 DS.SetRangeEnd(Tok.getLocation());
2420 ConsumeToken(); // The typename.
2421
2422 continue;
2423 }
Mike Stump11289f42009-09-09 15:08:12 +00002424
Chris Lattnere387d9e2009-01-21 19:48:37 +00002425 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002426 if (Tok.getAnnotationValue()) {
2427 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002429 DiagID, T);
2430 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002431 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002432
Chris Lattner005fc1b2010-04-05 18:18:31 +00002433 if (isInvalid)
2434 break;
2435
Chris Lattnere387d9e2009-01-21 19:48:37 +00002436 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2437 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002438
Chris Lattnere387d9e2009-01-21 19:48:37 +00002439 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2440 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002441 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002442 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002443 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002444
Chris Lattnere387d9e2009-01-21 19:48:37 +00002445 continue;
2446 }
Mike Stump11289f42009-09-09 15:08:12 +00002447
Douglas Gregor06873092011-04-28 15:48:45 +00002448 case tok::kw___is_signed:
2449 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2450 // typically treats it as a trait. If we see __is_signed as it appears
2451 // in libstdc++, e.g.,
2452 //
2453 // static const bool __is_signed;
2454 //
2455 // then treat __is_signed as an identifier rather than as a keyword.
2456 if (DS.getTypeSpecType() == TST_bool &&
2457 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2458 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2459 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2460 Tok.setKind(tok::identifier);
2461 }
2462
2463 // We're done with the declaration-specifiers.
2464 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002465
Chris Lattner16fac4f2008-07-26 01:18:38 +00002466 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002467 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002468 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002469 // In C++, check to see if this is a scope specifier like foo::bar::, if
2470 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002471 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002472 if (TryAnnotateCXXScopeToken(true)) {
2473 if (!DS.hasTypeSpecifier())
2474 DS.SetTypeSpecError();
2475 goto DoneWithDeclSpec;
2476 }
2477 if (!Tok.is(tok::identifier))
2478 continue;
2479 }
Mike Stump11289f42009-09-09 15:08:12 +00002480
Chris Lattner16fac4f2008-07-26 01:18:38 +00002481 // This identifier can only be a typedef name if we haven't already seen
2482 // a type-specifier. Without this check we misparse:
2483 // typedef int X; struct Y { short X; }; as 'short int'.
2484 if (DS.hasTypeSpecifier())
2485 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002486
John Thompson22334602010-02-05 00:12:22 +00002487 // Check for need to substitute AltiVec keyword tokens.
2488 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2489 break;
2490
Richard Smith3092a3b2012-05-09 18:56:43 +00002491 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2492 // allow the use of a typedef name as a type specifier.
2493 if (DS.isTypeAltiVecVector())
2494 goto DoneWithDeclSpec;
2495
John McCallba7bf592010-08-24 05:47:05 +00002496 ParsedType TypeRep =
2497 Actions.getTypeName(*Tok.getIdentifierInfo(),
2498 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002499
Chris Lattner6cc055a2009-04-12 20:42:31 +00002500 // If this is not a typedef name, don't parse it as part of the declspec,
2501 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002502 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002503 ParsedAttributesWithRange Attrs(AttrFactory);
2504 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2505 if (!Attrs.empty()) {
2506 AttrsLastTime = true;
2507 attrs.takeAllFrom(Attrs);
2508 }
2509 continue;
2510 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002511 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002512 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002513
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002514 // If we're in a context where the identifier could be a class name,
2515 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002516 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002517 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002518 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002519 goto DoneWithDeclSpec;
2520
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002521 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002522 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002523 if (isInvalid)
2524 break;
Mike Stump11289f42009-09-09 15:08:12 +00002525
Chris Lattner16fac4f2008-07-26 01:18:38 +00002526 DS.SetRangeEnd(Tok.getLocation());
2527 ConsumeToken(); // The identifier
2528
2529 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2530 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002531 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002532 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002533 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002534
Steve Naroffcd5e7822008-09-22 10:28:57 +00002535 // Need to support trailing type qualifiers (e.g. "id<p> const").
2536 // If a type specifier follows, it will be diagnosed elsewhere.
2537 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002538 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002539
2540 // type-name
2541 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002542 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002543 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002544 // This template-id does not refer to a type name, so we're
2545 // done with the type-specifiers.
2546 goto DoneWithDeclSpec;
2547 }
2548
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002549 // If we're in a context where the template-id could be a
2550 // constructor name or specialization, check whether this is a
2551 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002552 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002553 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002554 isConstructorDeclarator())
2555 goto DoneWithDeclSpec;
2556
Douglas Gregor7f741122009-02-25 19:37:18 +00002557 // Turn the template-id annotation token into a type annotation
2558 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002559 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002560 continue;
2561 }
2562
Chris Lattnere37e2332006-08-15 04:50:22 +00002563 // GNU attributes support.
2564 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002565 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002566 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002567
2568 // Microsoft declspec support.
2569 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002570 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002571 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002572
Steve Naroff44ac7772008-12-25 14:16:32 +00002573 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002574 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002575 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002576 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002577 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002578 // FIXME: This does not work correctly if it is set to be a declspec
2579 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002580 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002581 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002582 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002583 }
Eli Friedman53339e02009-06-08 23:27:34 +00002584
2585 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002586 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002587 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002588 case tok::kw___cdecl:
2589 case tok::kw___stdcall:
2590 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002591 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002592 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002593 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002594 continue;
2595
Dawn Perchik335e16b2010-09-03 01:29:35 +00002596 // Borland single token adornments.
2597 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002598 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002599 continue;
2600
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002601 // OpenCL single token adornments.
2602 case tok::kw___kernel:
2603 ParseOpenCLAttributes(DS.getAttributes());
2604 continue;
2605
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002606 // storage-class-specifier
2607 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002608 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2609 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002610 break;
2611 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002612 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002613 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002614 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2615 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002616 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002617 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002618 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2619 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002620 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002621 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002622 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002623 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002624 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2625 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002626 break;
2627 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002628 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002629 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002630 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2631 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002632 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002633 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002634 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002635 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2637 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002638 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002639 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2640 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002641 break;
2642 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002643 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2644 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002645 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002646 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002647 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2648 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002649 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002650 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002651 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002652 break;
Mike Stump11289f42009-09-09 15:08:12 +00002653
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002654 // function-specifier
2655 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002656 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002657 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002658 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002659 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002660 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002661 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002662 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002663 break;
Richard Smith0015f092013-01-17 22:16:11 +00002664 case tok::kw__Noreturn:
2665 if (!getLangOpts().C11)
2666 Diag(Loc, diag::ext_c11_noreturn);
2667 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2668 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002669
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002670 // alignment-specifier
2671 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002672 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002673 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002674 ParseAlignmentSpecifier(DS.getAttributes());
2675 continue;
2676
Anders Carlssoncd8db412009-05-06 04:46:28 +00002677 // friend
2678 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002679 if (DSContext == DSC_class)
2680 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2681 else {
2682 PrevSpec = ""; // not actually used by the diagnostic
2683 DiagID = diag::err_friend_invalid_in_context;
2684 isInvalid = true;
2685 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002686 break;
Mike Stump11289f42009-09-09 15:08:12 +00002687
Douglas Gregor26701a42011-09-09 02:06:17 +00002688 // Modules
2689 case tok::kw___module_private__:
2690 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2691 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002692
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002693 // constexpr
2694 case tok::kw_constexpr:
2695 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2696 break;
2697
Chris Lattnere387d9e2009-01-21 19:48:37 +00002698 // type-specifier
2699 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2701 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002702 break;
2703 case tok::kw_long:
2704 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002705 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2706 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002707 else
John McCall49bfce42009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2709 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002710 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002711 case tok::kw___int64:
2712 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2713 DiagID);
2714 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002715 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2717 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002718 break;
2719 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002720 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2721 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002722 break;
2723 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2725 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002726 break;
2727 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002728 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2729 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002730 break;
2731 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2733 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002734 break;
2735 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2737 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002738 break;
2739 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2741 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002742 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002743 case tok::kw___int128:
2744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2745 DiagID);
2746 break;
2747 case tok::kw_half:
2748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2749 DiagID);
2750 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002751 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2753 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002754 break;
2755 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2757 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002758 break;
2759 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2761 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002762 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002763 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2765 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002766 break;
2767 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2769 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002770 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002771 case tok::kw_bool:
2772 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002773 if (Tok.is(tok::kw_bool) &&
2774 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2775 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2776 PrevSpec = ""; // Not used by the diagnostic.
2777 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002778 // For better error recovery.
2779 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002780 isInvalid = true;
2781 } else {
2782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2783 DiagID);
2784 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002785 break;
2786 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2788 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002789 break;
2790 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2792 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002793 break;
2794 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2796 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002797 break;
John Thompson22334602010-02-05 00:12:22 +00002798 case tok::kw___vector:
2799 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2800 break;
2801 case tok::kw___pixel:
2802 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2803 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002804 case tok::kw_image1d_t:
2805 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2806 PrevSpec, DiagID);
2807 break;
2808 case tok::kw_image1d_array_t:
2809 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2810 PrevSpec, DiagID);
2811 break;
2812 case tok::kw_image1d_buffer_t:
2813 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2814 PrevSpec, DiagID);
2815 break;
2816 case tok::kw_image2d_t:
2817 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2818 PrevSpec, DiagID);
2819 break;
2820 case tok::kw_image2d_array_t:
2821 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2822 PrevSpec, DiagID);
2823 break;
2824 case tok::kw_image3d_t:
2825 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2826 PrevSpec, DiagID);
2827 break;
Guy Benyei61054192013-02-07 10:55:47 +00002828 case tok::kw_sampler_t:
2829 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2830 PrevSpec, DiagID);
2831 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002832 case tok::kw_event_t:
2833 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2834 PrevSpec, DiagID);
2835 break;
John McCall39439732011-04-09 22:50:59 +00002836 case tok::kw___unknown_anytype:
2837 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2838 PrevSpec, DiagID);
2839 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002840
2841 // class-specifier:
2842 case tok::kw_class:
2843 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002844 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002845 case tok::kw_union: {
2846 tok::TokenKind Kind = Tok.getKind();
2847 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002848
2849 // These are attributes following class specifiers.
2850 // To produce better diagnostic, we parse them when
2851 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002852 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002853 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002854 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002855
2856 // If there are attributes following class specifier,
2857 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002858 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002859 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002860 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002861 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002862 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002863 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002864
2865 // enum-specifier:
2866 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002867 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002868 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002869 continue;
2870
2871 // cv-qualifier:
2872 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002873 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002874 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002875 break;
2876 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002877 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002878 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002879 break;
2880 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002881 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002882 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002883 break;
2884
Douglas Gregor333489b2009-03-27 23:10:48 +00002885 // C++ typename-specifier:
2886 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002887 if (TryAnnotateTypeOrScopeToken()) {
2888 DS.SetTypeSpecError();
2889 goto DoneWithDeclSpec;
2890 }
2891 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002892 continue;
2893 break;
2894
Chris Lattnere387d9e2009-01-21 19:48:37 +00002895 // GNU typeof support.
2896 case tok::kw_typeof:
2897 ParseTypeofSpecifier(DS);
2898 continue;
2899
David Blaikie15a430a2011-12-04 05:04:18 +00002900 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002901 ParseDecltypeSpecifier(DS);
2902 continue;
2903
Alexis Hunt4a257072011-05-19 05:37:45 +00002904 case tok::kw___underlying_type:
2905 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002906 continue;
2907
2908 case tok::kw__Atomic:
2909 ParseAtomicSpecifier(DS);
2910 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002911
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002912 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002913 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002914 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002915 goto DoneWithDeclSpec;
2916 case tok::kw___private:
2917 case tok::kw___global:
2918 case tok::kw___local:
2919 case tok::kw___constant:
2920 case tok::kw___read_only:
2921 case tok::kw___write_only:
2922 case tok::kw___read_write:
2923 ParseOpenCLQualifiers(DS);
2924 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002925
Steve Naroffcfdf6162008-06-05 00:02:44 +00002926 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002927 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002928 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2929 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002930 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002931 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002932
Douglas Gregor3a001f42010-11-19 17:10:50 +00002933 if (!ParseObjCProtocolQualifiers(DS))
2934 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2935 << FixItHint::CreateInsertion(Loc, "id")
2936 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002937
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002938 // Need to support trailing type qualifiers (e.g. "id<p> const").
2939 // If a type specifier follows, it will be diagnosed elsewhere.
2940 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002941 }
John McCall49bfce42009-08-03 20:12:06 +00002942 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002943 if (isInvalid) {
2944 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002945 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002946
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002947 if (DiagID == diag::ext_duplicate_declspec)
2948 Diag(Tok, DiagID)
2949 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2950 else
2951 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002952 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002953
Chris Lattner2e232092008-03-13 06:29:04 +00002954 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002955 if (DiagID != diag::err_bool_redeclaration)
2956 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002957
2958 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002959 }
2960}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002961
Chris Lattner70ae4912007-10-29 04:42:53 +00002962/// ParseStructDeclaration - Parse a struct declaration without the terminating
2963/// semicolon.
2964///
Chris Lattner90a26b02007-01-23 04:38:16 +00002965/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002966/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002967/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002968/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002969/// struct-declarator-list:
2970/// struct-declarator
2971/// struct-declarator-list ',' struct-declarator
2972/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2973/// struct-declarator:
2974/// declarator
2975/// [GNU] declarator attributes[opt]
2976/// declarator[opt] ':' constant-expression
2977/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2978///
Chris Lattnera12405b2008-04-10 06:46:29 +00002979void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002980ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002981
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002982 if (Tok.is(tok::kw___extension__)) {
2983 // __extension__ silences extension warnings in the subexpression.
2984 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002985 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002986 return ParseStructDeclaration(DS, Fields);
2987 }
Mike Stump11289f42009-09-09 15:08:12 +00002988
Steve Naroff97170802007-08-20 22:28:22 +00002989 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002990 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002991
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002992 // If there are no declarators, this is a free-standing declaration
2993 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002994 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002995 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2996 DS);
2997 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002998 return;
2999 }
3000
3001 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003002 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003003 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003004 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003005 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003006 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003007
Bill Wendling44426052012-12-20 19:22:21 +00003008 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003009 if (!FirstDeclarator)
3010 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003011
Steve Naroff97170802007-08-20 22:28:22 +00003012 /// struct-declarator: declarator
3013 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003014 if (Tok.isNot(tok::colon)) {
3015 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3016 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003017 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003018 }
Mike Stump11289f42009-09-09 15:08:12 +00003019
Chris Lattner76c72282007-10-09 17:33:22 +00003020 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003021 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003022 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003023 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003024 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003025 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003026 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003027 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003028
Steve Naroff97170802007-08-20 22:28:22 +00003029 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003030 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003031
John McCallcfefb6d2009-11-03 02:38:08 +00003032 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003033 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003034
Steve Naroff97170802007-08-20 22:28:22 +00003035 // If we don't have a comma, it is either the end of the list (a ';')
3036 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003037 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003038 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003039
Steve Naroff97170802007-08-20 22:28:22 +00003040 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003041 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003042
John McCallcfefb6d2009-11-03 02:38:08 +00003043 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003044 }
Steve Naroff97170802007-08-20 22:28:22 +00003045}
3046
3047/// ParseStructUnionBody
3048/// struct-contents:
3049/// struct-declaration-list
3050/// [EXT] empty
3051/// [GNU] "struct-declaration-list" without terminatoring ';'
3052/// struct-declaration-list:
3053/// struct-declaration
3054/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003055/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003056///
Chris Lattner1300fb92007-01-23 23:42:53 +00003057void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003058 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003059 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3060 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00003061
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003062 BalancedDelimiterTracker T(*this, tok::l_brace);
3063 if (T.consumeOpen())
3064 return;
Mike Stump11289f42009-09-09 15:08:12 +00003065
Douglas Gregor658b9552009-01-09 22:42:13 +00003066 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003067 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003068
Chris Lattner7b9ace62007-01-23 20:11:08 +00003069 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3070 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003071 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00003072 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3073 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3074 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003075
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003076 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003077
Chris Lattner7b9ace62007-01-23 20:11:08 +00003078 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003079 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003080 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003081
Chris Lattner736ed5d2007-06-09 05:59:07 +00003082 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003083 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003084 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003085 continue;
3086 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003087
John McCallcfefb6d2009-11-03 02:38:08 +00003088 if (!Tok.is(tok::at)) {
3089 struct CFieldCallback : FieldCallback {
3090 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003091 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003093
John McCall48871652010-08-21 09:40:31 +00003094 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003095 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003096 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3097
Eli Friedman934dbbf2012-08-08 23:53:27 +00003098 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003099 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003100 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003101 FD.D.getDeclSpec().getSourceRange().getBegin(),
3102 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003103 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003104 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003105 }
John McCallcfefb6d2009-11-03 02:38:08 +00003106 } Callback(*this, TagDecl, FieldDecls);
3107
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003108 // Parse all the comma separated declarators.
3109 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003110 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003111 } else { // Handle @defs
3112 ConsumeToken();
3113 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3114 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003115 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003116 continue;
3117 }
3118 ConsumeToken();
3119 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3120 if (!Tok.is(tok::identifier)) {
3121 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003122 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003123 continue;
3124 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003125 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003126 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003127 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003128 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3129 ConsumeToken();
3130 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003131 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003132
Chris Lattner76c72282007-10-09 17:33:22 +00003133 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003134 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003135 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003136 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003137 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003138 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003139 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3140 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003141 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003142 // If we stopped at a ';', eat it.
3143 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003144 }
3145 }
Mike Stump11289f42009-09-09 15:08:12 +00003146
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003147 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003148
John McCall084e83d2011-03-24 11:26:52 +00003149 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003150 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003151 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003152
Douglas Gregor0be31a22010-07-02 17:43:08 +00003153 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003154 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003155 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003156 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003157 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003158 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3159 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003160}
3161
Chris Lattner3b561a32006-08-13 00:12:11 +00003162/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003163/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003164/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003165///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003166/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3167/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003168/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3169/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003170/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003171/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003172///
Richard Smith7d137e32012-03-23 03:33:32 +00003173/// [C++11] enum-head '{' enumerator-list[opt] '}'
3174/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003175///
Richard Smith7d137e32012-03-23 03:33:32 +00003176/// enum-head: [C++11]
3177/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3178/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3179/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003180///
Richard Smith7d137e32012-03-23 03:33:32 +00003181/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003182/// 'enum'
3183/// 'enum' 'class'
3184/// 'enum' 'struct'
3185///
Richard Smith7d137e32012-03-23 03:33:32 +00003186/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003187/// ':' type-specifier-seq
3188///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003189/// [C++] elaborated-type-specifier:
3190/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3191///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003192void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003193 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003194 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003195 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003196 if (Tok.is(tok::code_completion)) {
3197 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003198 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003199 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003200 }
John McCallcb432fa2011-07-06 05:58:41 +00003201
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003202 // If attributes exist after tag, parse them.
3203 ParsedAttributesWithRange attrs(AttrFactory);
3204 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003205 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003206
3207 // If declspecs exist after tag, parse them.
3208 while (Tok.is(tok::kw___declspec))
3209 ParseMicrosoftDeclSpec(attrs);
3210
Richard Smith0f8ee222012-01-10 01:33:14 +00003211 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003212 bool IsScopedUsingClassTag = false;
3213
John McCallbeae29a2012-06-23 22:30:04 +00003214 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003215 if (getLangOpts().CPlusPlus11 &&
John McCallcb432fa2011-07-06 05:58:41 +00003216 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003217 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003218 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003219 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003220
Bill Wendling44426052012-12-20 19:22:21 +00003221 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003222 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003223 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003224
3225 // They are allowed afterwards, though.
3226 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003227 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003228 while (Tok.is(tok::kw___declspec))
3229 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003230 }
Richard Smith7d137e32012-03-23 03:33:32 +00003231
John McCall6347b682012-05-07 06:16:58 +00003232 // C++11 [temp.explicit]p12:
3233 // The usual access controls do not apply to names used to specify
3234 // explicit instantiations.
3235 // We extend this to also cover explicit specializations. Note that
3236 // we don't suppress if this turns out to be an elaborated type
3237 // specifier.
3238 bool shouldDelayDiagsInTag =
3239 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3240 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3241 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003242
Richard Smithbfdb1082012-03-12 08:56:40 +00003243 // Enum definitions should not be parsed in a trailing-return-type.
3244 bool AllowDeclaration = DSC != DSC_trailing;
3245
3246 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003247 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003248 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003249
Abramo Bagnarad7548482010-05-19 21:37:53 +00003250 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003251 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003252 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3253 // if a fixed underlying type is allowed.
3254 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003255
3256 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003257 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003258 return;
3259
3260 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003261 Diag(Tok, diag::err_expected_ident);
3262 if (Tok.isNot(tok::l_brace)) {
3263 // Has no name and is not a definition.
3264 // Skip the rest of this declarator, up until the comma or semicolon.
3265 SkipUntil(tok::comma, true);
3266 return;
3267 }
3268 }
3269 }
Mike Stump11289f42009-09-09 15:08:12 +00003270
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003271 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003272 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003273 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003274 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003275
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003276 // Skip the rest of this declarator, up until the comma or semicolon.
3277 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003278 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003279 }
Mike Stump11289f42009-09-09 15:08:12 +00003280
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003281 // If an identifier is present, consume and remember it.
3282 IdentifierInfo *Name = 0;
3283 SourceLocation NameLoc;
3284 if (Tok.is(tok::identifier)) {
3285 Name = Tok.getIdentifierInfo();
3286 NameLoc = ConsumeToken();
3287 }
Mike Stump11289f42009-09-09 15:08:12 +00003288
Richard Smith0f8ee222012-01-10 01:33:14 +00003289 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003290 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3291 // declaration of a scoped enumeration.
3292 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003293 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003294 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003295 }
3296
John McCall6347b682012-05-07 06:16:58 +00003297 // Okay, end the suppression area. We'll decide whether to emit the
3298 // diagnostics in a second.
3299 if (shouldDelayDiagsInTag)
3300 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003301
Douglas Gregor0bf31402010-10-08 23:50:27 +00003302 TypeResult BaseType;
3303
Douglas Gregord1f69f62010-12-01 17:42:47 +00003304 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003305 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003306 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003307 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003308 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003309 // If we're in class scope, this can either be an enum declaration with
3310 // an underlying type, or a declaration of a bitfield member. We try to
3311 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003312 // (integer literal, sizeof); if it's still ambiguous, we then consider
3313 // anything that's a simple-type-specifier followed by '(' as an
3314 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003315 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003316 EnterExpressionEvaluationContext Unevaluated(Actions,
3317 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003318 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003319 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003320 // bit-field. This is the common case.
3321 if (TPR == TPResult::True())
3322 PossibleBitfield = true;
3323 // If the next token starts a type-specifier-seq, it may be either a
3324 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003325 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003326 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003327 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003328 GetLookAheadToken(2).getKind() == tok::semi) {
3329 // Consume the ':'.
3330 ConsumeToken();
3331 } else {
3332 // We have the start of a type-specifier-seq, so we have to perform
3333 // tentative parsing to determine whether we have an expression or a
3334 // type.
3335 TentativeParsingAction TPA(*this);
3336
3337 // Consume the ':'.
3338 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003339
3340 // If we see a type specifier followed by an open-brace, we have an
3341 // ambiguity between an underlying type and a C++11 braced
3342 // function-style cast. Resolve this by always treating it as an
3343 // underlying type.
3344 // FIXME: The standard is not entirely clear on how to disambiguate in
3345 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003346 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003347 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003348 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003349 // We'll parse this as a bitfield later.
3350 PossibleBitfield = true;
3351 TPA.Revert();
3352 } else {
3353 // We have a type-specifier-seq.
3354 TPA.Commit();
3355 }
3356 }
3357 } else {
3358 // Consume the ':'.
3359 ConsumeToken();
3360 }
3361
3362 if (!PossibleBitfield) {
3363 SourceRange Range;
3364 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003365
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003366 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003367 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003368 } else if (!getLangOpts().ObjC2) {
3369 if (getLangOpts().CPlusPlus)
3370 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3371 else
3372 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3373 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003374 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003375 }
3376
Richard Smith0f8ee222012-01-10 01:33:14 +00003377 // There are four options here. If we have 'friend enum foo;' then this is a
3378 // friend declaration, and cannot have an accompanying definition. If we have
3379 // 'enum foo;', then this is a forward declaration. If we have
3380 // 'enum foo {...' then this is a definition. Otherwise we have something
3381 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003382 //
3383 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3384 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3385 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3386 //
John McCallfaf5fb42010-08-26 23:41:50 +00003387 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003388 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003389 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003390 } else if (Tok.is(tok::l_brace)) {
3391 if (DS.isFriendSpecified()) {
3392 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3393 << SourceRange(DS.getFriendSpecLoc());
3394 ConsumeBrace();
3395 SkipUntil(tok::r_brace);
3396 TUK = Sema::TUK_Friend;
3397 } else {
3398 TUK = Sema::TUK_Definition;
3399 }
Richard Smith369b9f92012-06-25 21:37:02 +00003400 } else if (DSC != DSC_type_specifier &&
3401 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003402 (Tok.isAtStartOfLine() &&
3403 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003404 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3405 if (Tok.isNot(tok::semi)) {
3406 // A semicolon was missing after this declaration. Diagnose and recover.
3407 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3408 "enum");
3409 PP.EnterToken(Tok);
3410 Tok.setKind(tok::semi);
3411 }
John McCall6347b682012-05-07 06:16:58 +00003412 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003413 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003414 }
3415
3416 // If this is an elaborated type specifier, and we delayed
3417 // diagnostics before, just merge them into the current pool.
3418 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3419 diagsFromTag.redelay();
3420 }
Richard Smith7d137e32012-03-23 03:33:32 +00003421
3422 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003423 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003424 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003425 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003426 // Skip the rest of this declarator, up until the comma or semicolon.
3427 Diag(Tok, diag::err_enum_template);
3428 SkipUntil(tok::comma, true);
3429 return;
3430 }
3431
3432 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3433 // Enumerations can't be explicitly instantiated.
3434 DS.SetTypeSpecError();
3435 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3436 return;
3437 }
3438
3439 assert(TemplateInfo.TemplateParams && "no template parameters");
3440 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3441 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003442 }
Chad Rosierc1183952012-06-26 22:30:43 +00003443
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003444 if (TUK == Sema::TUK_Reference)
3445 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003446
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003447 if (!Name && TUK != Sema::TUK_Definition) {
3448 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003449
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003450 // Skip the rest of this declarator, up until the comma or semicolon.
3451 SkipUntil(tok::comma, true);
3452 return;
3453 }
Richard Smith7d137e32012-03-23 03:33:32 +00003454
Douglas Gregord6ab8742009-05-28 23:31:59 +00003455 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003456 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003457 const char *PrevSpec = 0;
3458 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003459 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003460 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003461 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003462 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003463 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003464
Douglas Gregorba41d012010-04-24 16:38:41 +00003465 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003466 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003467 // dependent tag.
3468 if (!Name) {
3469 DS.SetTypeSpecError();
3470 Diag(Tok, diag::err_expected_type_name_after_typename);
3471 return;
3472 }
Chad Rosierc1183952012-06-26 22:30:43 +00003473
Douglas Gregor0be31a22010-07-02 17:43:08 +00003474 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003475 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003476 NameLoc);
3477 if (Type.isInvalid()) {
3478 DS.SetTypeSpecError();
3479 return;
3480 }
Chad Rosierc1183952012-06-26 22:30:43 +00003481
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003482 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3483 NameLoc.isValid() ? NameLoc : StartLoc,
3484 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003485 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003486
Douglas Gregorba41d012010-04-24 16:38:41 +00003487 return;
3488 }
Mike Stump11289f42009-09-09 15:08:12 +00003489
John McCall48871652010-08-21 09:40:31 +00003490 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003491 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003492 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003493 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003494 ConsumeBrace();
3495 SkipUntil(tok::r_brace);
3496 }
Chad Rosierc1183952012-06-26 22:30:43 +00003497
Douglas Gregorba41d012010-04-24 16:38:41 +00003498 DS.SetTypeSpecError();
3499 return;
3500 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003501
Richard Smith369b9f92012-06-25 21:37:02 +00003502 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003503 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003504
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003505 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3506 NameLoc.isValid() ? NameLoc : StartLoc,
3507 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003508 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003509}
3510
Chris Lattnerc1915e22007-01-25 07:29:02 +00003511/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3512/// enumerator-list:
3513/// enumerator
3514/// enumerator-list ',' enumerator
3515/// enumerator:
3516/// enumeration-constant
3517/// enumeration-constant '=' constant-expression
3518/// enumeration-constant:
3519/// identifier
3520///
John McCall48871652010-08-21 09:40:31 +00003521void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003522 // Enter the scope of the enum body and start the definition.
3523 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003524 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003525
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003526 BalancedDelimiterTracker T(*this, tok::l_brace);
3527 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003528
Chris Lattner37256fb2007-08-27 17:24:30 +00003529 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003530 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003531 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003532
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003533 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003534
John McCall48871652010-08-21 09:40:31 +00003535 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003536
Chris Lattnerc1915e22007-01-25 07:29:02 +00003537 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003538 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003539 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3540 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003541
John McCall811a0f52010-10-22 23:36:17 +00003542 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003543 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003544 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003545 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003546 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003547
Chris Lattnerc1915e22007-01-25 07:29:02 +00003548 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003549 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003550 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003551
Chris Lattner76c72282007-10-09 17:33:22 +00003552 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003553 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003554 AssignedVal = ParseConstantExpression();
3555 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003556 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003557 }
Mike Stump11289f42009-09-09 15:08:12 +00003558
Chris Lattnerc1915e22007-01-25 07:29:02 +00003559 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003560 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3561 LastEnumConstDecl,
3562 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003563 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003564 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003565 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003566
Chris Lattner4ef40012007-06-11 01:28:17 +00003567 EnumConstantDecls.push_back(EnumConstDecl);
3568 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003569
Douglas Gregorce66d022010-09-07 14:51:08 +00003570 if (Tok.is(tok::identifier)) {
3571 // We're missing a comma between enumerators.
3572 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003573 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003574 << FixItHint::CreateInsertion(Loc, ", ");
3575 continue;
3576 }
Chad Rosierc1183952012-06-26 22:30:43 +00003577
Chris Lattner76c72282007-10-09 17:33:22 +00003578 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003579 break;
3580 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003581
Richard Smith5d164bc2011-10-15 05:09:34 +00003582 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003583 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003584 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3585 diag::ext_enumerator_list_comma_cxx :
3586 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003587 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003588 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003589 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3590 << FixItHint::CreateRemoval(CommaLoc);
3591 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003592 }
Mike Stump11289f42009-09-09 15:08:12 +00003593
Chris Lattnerc1915e22007-01-25 07:29:02 +00003594 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003595 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003596
Chris Lattnerc1915e22007-01-25 07:29:02 +00003597 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003598 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003599 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003600
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003601 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3602 EnumDecl, EnumConstantDecls.data(),
3603 EnumConstantDecls.size(), getCurScope(),
3604 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003605
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003606 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003607 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3608 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003609
3610 // The next token must be valid after an enum definition. If not, a ';'
3611 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003612 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3613 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003614 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3615 // Push this token back into the preprocessor and change our current token
3616 // to ';' so that the rest of the code recovers as though there were an
3617 // ';' after the definition.
3618 PP.EnterToken(Tok);
3619 Tok.setKind(tok::semi);
3620 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003621}
Chris Lattner3b561a32006-08-13 00:12:11 +00003622
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003623/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003624/// start of a type-qualifier-list.
3625bool Parser::isTypeQualifier() const {
3626 switch (Tok.getKind()) {
3627 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003628
3629 // type-qualifier only in OpenCL
3630 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003631 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003632
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003633 // type-qualifier
3634 case tok::kw_const:
3635 case tok::kw_volatile:
3636 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003637 case tok::kw___private:
3638 case tok::kw___local:
3639 case tok::kw___global:
3640 case tok::kw___constant:
3641 case tok::kw___read_only:
3642 case tok::kw___read_write:
3643 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003644 return true;
3645 }
3646}
3647
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003648/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3649/// is definitely a type-specifier. Return false if it isn't part of a type
3650/// specifier or if we're not sure.
3651bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3652 switch (Tok.getKind()) {
3653 default: return false;
3654 // type-specifiers
3655 case tok::kw_short:
3656 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003657 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003658 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003659 case tok::kw_signed:
3660 case tok::kw_unsigned:
3661 case tok::kw__Complex:
3662 case tok::kw__Imaginary:
3663 case tok::kw_void:
3664 case tok::kw_char:
3665 case tok::kw_wchar_t:
3666 case tok::kw_char16_t:
3667 case tok::kw_char32_t:
3668 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003669 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003670 case tok::kw_float:
3671 case tok::kw_double:
3672 case tok::kw_bool:
3673 case tok::kw__Bool:
3674 case tok::kw__Decimal32:
3675 case tok::kw__Decimal64:
3676 case tok::kw__Decimal128:
3677 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003678
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003679 // OpenCL specific types:
3680 case tok::kw_image1d_t:
3681 case tok::kw_image1d_array_t:
3682 case tok::kw_image1d_buffer_t:
3683 case tok::kw_image2d_t:
3684 case tok::kw_image2d_array_t:
3685 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003686 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003687 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003688
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003689 // struct-or-union-specifier (C99) or class-specifier (C++)
3690 case tok::kw_class:
3691 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003692 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003693 case tok::kw_union:
3694 // enum-specifier
3695 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003696
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003697 // typedef-name
3698 case tok::annot_typename:
3699 return true;
3700 }
3701}
3702
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003703/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003704/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003705bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003706 switch (Tok.getKind()) {
3707 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003708
Chris Lattner020bab92009-01-04 23:41:41 +00003709 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003710 if (TryAltiVecVectorToken())
3711 return true;
3712 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003713 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003714 // Annotate typenames and C++ scope specifiers. If we get one, just
3715 // recurse to handle whatever we get.
3716 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003717 return true;
3718 if (Tok.is(tok::identifier))
3719 return false;
3720 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003721
Chris Lattner020bab92009-01-04 23:41:41 +00003722 case tok::coloncolon: // ::foo::bar
3723 if (NextToken().is(tok::kw_new) || // ::new
3724 NextToken().is(tok::kw_delete)) // ::delete
3725 return false;
3726
Chris Lattner020bab92009-01-04 23:41:41 +00003727 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003728 return true;
3729 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003730
Chris Lattnere37e2332006-08-15 04:50:22 +00003731 // GNU attributes support.
3732 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003733 // GNU typeof support.
3734 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003735
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003736 // type-specifiers
3737 case tok::kw_short:
3738 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003739 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003740 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003741 case tok::kw_signed:
3742 case tok::kw_unsigned:
3743 case tok::kw__Complex:
3744 case tok::kw__Imaginary:
3745 case tok::kw_void:
3746 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003747 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003748 case tok::kw_char16_t:
3749 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003750 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003751 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003752 case tok::kw_float:
3753 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003754 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003755 case tok::kw__Bool:
3756 case tok::kw__Decimal32:
3757 case tok::kw__Decimal64:
3758 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003759 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003760
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003761 // OpenCL specific types:
3762 case tok::kw_image1d_t:
3763 case tok::kw_image1d_array_t:
3764 case tok::kw_image1d_buffer_t:
3765 case tok::kw_image2d_t:
3766 case tok::kw_image2d_array_t:
3767 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003768 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003769 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003770
Chris Lattner861a2262008-04-13 18:59:07 +00003771 // struct-or-union-specifier (C99) or class-specifier (C++)
3772 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003773 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003774 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003775 case tok::kw_union:
3776 // enum-specifier
3777 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003778
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003779 // type-qualifier
3780 case tok::kw_const:
3781 case tok::kw_volatile:
3782 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003783
John McCallea0a39e2012-11-14 00:49:39 +00003784 // Debugger support.
3785 case tok::kw___unknown_anytype:
3786
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003787 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003788 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003789 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003790
Chris Lattner409bf7d2008-10-20 00:25:30 +00003791 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3792 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003793 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003794
Steve Naroff44ac7772008-12-25 14:16:32 +00003795 case tok::kw___cdecl:
3796 case tok::kw___stdcall:
3797 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003798 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003799 case tok::kw___w64:
3800 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003801 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003802 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003803 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003804
3805 case tok::kw___private:
3806 case tok::kw___local:
3807 case tok::kw___global:
3808 case tok::kw___constant:
3809 case tok::kw___read_only:
3810 case tok::kw___read_write:
3811 case tok::kw___write_only:
3812
Eli Friedman53339e02009-06-08 23:27:34 +00003813 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003814
3815 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003816 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003817
Benjamin Kramere56f3932011-12-23 17:00:35 +00003818 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003819 case tok::kw__Atomic:
3820 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003821 }
3822}
3823
Chris Lattneracd58a32006-08-06 17:24:14 +00003824/// isDeclarationSpecifier() - Return true if the current token is part of a
3825/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003826///
3827/// \param DisambiguatingWithExpression True to indicate that the purpose of
3828/// this check is to disambiguate between an expression and a declaration.
3829bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003830 switch (Tok.getKind()) {
3831 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003832
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003833 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003834 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003835
Chris Lattner020bab92009-01-04 23:41:41 +00003836 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003837 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003838 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003839 return false;
John Thompson22334602010-02-05 00:12:22 +00003840 if (TryAltiVecVectorToken())
3841 return true;
3842 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003843 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003844 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003845 // Annotate typenames and C++ scope specifiers. If we get one, just
3846 // recurse to handle whatever we get.
3847 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003848 return true;
3849 if (Tok.is(tok::identifier))
3850 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003851
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003852 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003853 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003854 // expression is permitted, then this is probably a class message send
3855 // missing the initial '['. In this case, we won't consider this to be
3856 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003857 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003858 isStartOfObjCClassMessageMissingOpenBracket())
3859 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003860
John McCall1f476a12010-02-26 08:45:28 +00003861 return isDeclarationSpecifier();
3862
Chris Lattner020bab92009-01-04 23:41:41 +00003863 case tok::coloncolon: // ::foo::bar
3864 if (NextToken().is(tok::kw_new) || // ::new
3865 NextToken().is(tok::kw_delete)) // ::delete
3866 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003867
Chris Lattner020bab92009-01-04 23:41:41 +00003868 // Annotate typenames and C++ scope specifiers. If we get one, just
3869 // recurse to handle whatever we get.
3870 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003871 return true;
3872 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003873
Chris Lattneracd58a32006-08-06 17:24:14 +00003874 // storage-class-specifier
3875 case tok::kw_typedef:
3876 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003877 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003878 case tok::kw_static:
3879 case tok::kw_auto:
3880 case tok::kw_register:
3881 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003882
Douglas Gregor26701a42011-09-09 02:06:17 +00003883 // Modules
3884 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003885
John McCallea0a39e2012-11-14 00:49:39 +00003886 // Debugger support
3887 case tok::kw___unknown_anytype:
3888
Chris Lattneracd58a32006-08-06 17:24:14 +00003889 // type-specifiers
3890 case tok::kw_short:
3891 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003892 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003893 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003894 case tok::kw_signed:
3895 case tok::kw_unsigned:
3896 case tok::kw__Complex:
3897 case tok::kw__Imaginary:
3898 case tok::kw_void:
3899 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003900 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003901 case tok::kw_char16_t:
3902 case tok::kw_char32_t:
3903
Chris Lattneracd58a32006-08-06 17:24:14 +00003904 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003905 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003906 case tok::kw_float:
3907 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003908 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003909 case tok::kw__Bool:
3910 case tok::kw__Decimal32:
3911 case tok::kw__Decimal64:
3912 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003913 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003914
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003915 // OpenCL specific types:
3916 case tok::kw_image1d_t:
3917 case tok::kw_image1d_array_t:
3918 case tok::kw_image1d_buffer_t:
3919 case tok::kw_image2d_t:
3920 case tok::kw_image2d_array_t:
3921 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003922 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003923 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003924
Chris Lattner861a2262008-04-13 18:59:07 +00003925 // struct-or-union-specifier (C99) or class-specifier (C++)
3926 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003927 case tok::kw_struct:
3928 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003929 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003930 // enum-specifier
3931 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003932
Chris Lattneracd58a32006-08-06 17:24:14 +00003933 // type-qualifier
3934 case tok::kw_const:
3935 case tok::kw_volatile:
3936 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003937
Chris Lattneracd58a32006-08-06 17:24:14 +00003938 // function-specifier
3939 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003940 case tok::kw_virtual:
3941 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00003942 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003943
Richard Smith1dba27c2013-01-29 09:02:09 +00003944 // alignment-specifier
3945 case tok::kw__Alignas:
3946
Richard Smithd16fe122012-10-25 00:00:53 +00003947 // friend keyword.
3948 case tok::kw_friend:
3949
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003950 // static_assert-declaration
3951 case tok::kw__Static_assert:
3952
Chris Lattner599e47e2007-08-09 17:01:07 +00003953 // GNU typeof support.
3954 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003955
Chris Lattner599e47e2007-08-09 17:01:07 +00003956 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003957 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003958
Richard Smithd16fe122012-10-25 00:00:53 +00003959 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003960 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003961 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003962
Benjamin Kramere56f3932011-12-23 17:00:35 +00003963 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003964 case tok::kw__Atomic:
3965 return true;
3966
Chris Lattner8b2ec162008-07-26 03:38:44 +00003967 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3968 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003969 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003970
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003971 // typedef-name
3972 case tok::annot_typename:
3973 return !DisambiguatingWithExpression ||
3974 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003975
Steve Narofff192fab2009-01-06 19:34:12 +00003976 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003977 case tok::kw___cdecl:
3978 case tok::kw___stdcall:
3979 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003980 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003981 case tok::kw___w64:
3982 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003983 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003984 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003985 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003986 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003987
3988 case tok::kw___private:
3989 case tok::kw___local:
3990 case tok::kw___global:
3991 case tok::kw___constant:
3992 case tok::kw___read_only:
3993 case tok::kw___read_write:
3994 case tok::kw___write_only:
3995
Eli Friedman53339e02009-06-08 23:27:34 +00003996 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003997 }
3998}
3999
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004000bool Parser::isConstructorDeclarator() {
4001 TentativeParsingAction TPA(*this);
4002
4003 // Parse the C++ scope specifier.
4004 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004005 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004006 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004007 TPA.Revert();
4008 return false;
4009 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004010
4011 // Parse the constructor name.
4012 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4013 // We already know that we have a constructor name; just consume
4014 // the token.
4015 ConsumeToken();
4016 } else {
4017 TPA.Revert();
4018 return false;
4019 }
4020
Richard Smith43f340f2012-03-27 23:05:05 +00004021 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004022 if (Tok.isNot(tok::l_paren)) {
4023 TPA.Revert();
4024 return false;
4025 }
4026 ConsumeParen();
4027
Richard Smith43f340f2012-03-27 23:05:05 +00004028 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4029 // that we have a constructor.
4030 if (Tok.is(tok::r_paren) ||
4031 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004032 TPA.Revert();
4033 return true;
4034 }
4035
4036 // If we need to, enter the specified scope.
4037 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004038 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004039 DeclScopeObj.EnterDeclaratorScope();
4040
Francois Pichet79f3a872011-01-31 04:54:32 +00004041 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004042 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004043 MaybeParseMicrosoftAttributes(Attrs);
4044
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004045 // Check whether the next token(s) are part of a declaration
4046 // specifier, in which case we have the start of a parameter and,
4047 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004048 bool IsConstructor = false;
4049 if (isDeclarationSpecifier())
4050 IsConstructor = true;
4051 else if (Tok.is(tok::identifier) ||
4052 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4053 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4054 // This might be a parenthesized member name, but is more likely to
4055 // be a constructor declaration with an invalid argument type. Keep
4056 // looking.
4057 if (Tok.is(tok::annot_cxxscope))
4058 ConsumeToken();
4059 ConsumeToken();
4060
4061 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004062 // which must have one of the following syntactic forms (see the
4063 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004064 switch (Tok.getKind()) {
4065 case tok::l_paren:
4066 // C(X ( int));
4067 case tok::l_square:
4068 // C(X [ 5]);
4069 // C(X [ [attribute]]);
4070 case tok::coloncolon:
4071 // C(X :: Y);
4072 // C(X :: *p);
4073 case tok::r_paren:
4074 // C(X )
4075 // Assume this isn't a constructor, rather than assuming it's a
4076 // constructor with an unnamed parameter of an ill-formed type.
4077 break;
4078
4079 default:
4080 IsConstructor = true;
4081 break;
4082 }
4083 }
4084
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004085 TPA.Revert();
4086 return IsConstructor;
4087}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004088
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004089/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004090/// type-qualifier-list: [C99 6.7.5]
4091/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004092/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004093/// [ only if VendorAttributesAllowed=true ]
4094/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004095/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004096/// [ only if VendorAttributesAllowed=true ]
4097/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004098/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004099/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004100///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004101void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4102 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00004103 bool CXX11AttributesAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004104 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004105 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004106 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004107 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004108 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004109 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004110
4111 SourceLocation EndLoc;
4112
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004113 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004114 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004115 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004116 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004117 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004118
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004119 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004120 case tok::code_completion:
4121 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004122 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004123
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004124 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004125 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004126 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004127 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004128 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004129 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004130 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004131 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004132 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004133 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004134 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004135 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004136
4137 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004138 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004139 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004140 goto DoneWithTypeQuals;
4141 case tok::kw___private:
4142 case tok::kw___global:
4143 case tok::kw___local:
4144 case tok::kw___constant:
4145 case tok::kw___read_only:
4146 case tok::kw___write_only:
4147 case tok::kw___read_write:
4148 ParseOpenCLQualifiers(DS);
4149 break;
4150
Eli Friedman53339e02009-06-08 23:27:34 +00004151 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004152 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004153 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004154 case tok::kw___cdecl:
4155 case tok::kw___stdcall:
4156 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004157 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004158 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004159 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004160 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004161 continue;
4162 }
4163 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004164 case tok::kw___pascal:
4165 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004166 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004167 continue;
4168 }
4169 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004170 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004171 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004172 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004173 continue; // do *not* consume the next token!
4174 }
4175 // otherwise, FALL THROUGH!
4176 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004177 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004178 // If this is not a type-qualifier token, we're done reading type
4179 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004180 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004181 if (EndLoc.isValid())
4182 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004183 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004184 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004185
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004186 // If the specifier combination wasn't legal, issue a diagnostic.
4187 if (isInvalid) {
4188 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004189 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004190 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004191 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004192 }
4193}
4194
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004195
4196/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4197///
4198void Parser::ParseDeclarator(Declarator &D) {
4199 /// This implements the 'declarator' production in the C grammar, then checks
4200 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004201 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004202}
4203
Richard Smith0efa75c2012-03-29 01:16:42 +00004204static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4205 if (Kind == tok::star || Kind == tok::caret)
4206 return true;
4207
4208 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4209 if (!Lang.CPlusPlus)
4210 return false;
4211
4212 return Kind == tok::amp || Kind == tok::ampamp;
4213}
4214
Sebastian Redlbd150f42008-11-21 19:14:01 +00004215/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4216/// is parsed by the function passed to it. Pass null, and the direct-declarator
4217/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004218/// ptr-operator production.
4219///
Richard Smith09f76ee2011-10-19 21:33:05 +00004220/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004221/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4222/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004223///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004224/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4225/// [C] pointer[opt] direct-declarator
4226/// [C++] direct-declarator
4227/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004228///
4229/// pointer: [C99 6.7.5]
4230/// '*' type-qualifier-list[opt]
4231/// '*' type-qualifier-list[opt] pointer
4232///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004233/// ptr-operator:
4234/// '*' cv-qualifier-seq[opt]
4235/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004236/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004237/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004238/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004239/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004240void Parser::ParseDeclaratorInternal(Declarator &D,
4241 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004242 if (Diags.hasAllExtensionsSilenced())
4243 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004244
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004245 // C++ member pointers start with a '::' or a nested-name.
4246 // Member pointers get special handling, since there's no place for the
4247 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004248 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004249 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4250 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004251 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4252 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004253 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004254 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004255
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004256 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004257 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004258 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004259 if (D.mayHaveIdentifier())
4260 D.getCXXScopeSpec() = SS;
4261 else
4262 AnnotateScopeToken(SS, true);
4263
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004264 if (DirectDeclParser)
4265 (this->*DirectDeclParser)(D);
4266 return;
4267 }
4268
4269 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004270 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004271 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004272 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004273 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004274
4275 // Recurse to parse whatever is left.
4276 ParseDeclaratorInternal(D, DirectDeclParser);
4277
4278 // Sema will have to catch (syntactically invalid) pointers into global
4279 // scope. It has to catch pointers into namespace scope anyway.
4280 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004281 Loc),
4282 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004283 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004284 return;
4285 }
4286 }
4287
4288 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004289 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004290 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004291 if (DirectDeclParser)
4292 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004293 return;
4294 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004295
Sebastian Redled0f3b02009-03-15 22:02:01 +00004296 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4297 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004298 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004299 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004300
Chris Lattner9eac9312009-03-27 04:18:06 +00004301 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004302 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004303 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004304
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004305 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004306 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004307 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004308
Bill Wendling3708c182007-05-27 10:15:43 +00004309 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004310 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004311 if (Kind == tok::star)
4312 // Remember that we parsed a pointer type, and remember the type-quals.
4313 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004314 DS.getConstSpecLoc(),
4315 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004316 DS.getRestrictSpecLoc()),
4317 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004318 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004319 else
4320 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004321 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004322 Loc),
4323 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004324 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004325 } else {
4326 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004327 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004328
Sebastian Redl3b27be62009-03-23 00:00:23 +00004329 // Complain about rvalue references in C++03, but then go on and build
4330 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004331 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004332 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004333 diag::warn_cxx98_compat_rvalue_reference :
4334 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004335
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004336 // GNU-style and C++11 attributes are allowed here, as is restrict.
4337 ParseTypeQualifierListOpt(DS);
4338 D.ExtendWithDeclSpec(DS);
4339
Bill Wendling93efb222007-06-02 23:28:54 +00004340 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4341 // cv-qualifiers are introduced through the use of a typedef or of a
4342 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004343 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4344 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4345 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004346 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004347 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4348 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004349 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004350 }
Bill Wendling3708c182007-05-27 10:15:43 +00004351
4352 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004353 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004354
Douglas Gregor66583c52008-11-03 15:51:28 +00004355 if (D.getNumTypeObjects() > 0) {
4356 // C++ [dcl.ref]p4: There shall be no references to references.
4357 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4358 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004359 if (const IdentifierInfo *II = D.getIdentifier())
4360 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4361 << II;
4362 else
4363 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4364 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004365
Sebastian Redlbd150f42008-11-21 19:14:01 +00004366 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004367 // can go ahead and build the (technically ill-formed)
4368 // declarator: reference collapsing will take care of it.
4369 }
4370 }
4371
Bill Wendling3708c182007-05-27 10:15:43 +00004372 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004373 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004374 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004375 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004376 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004377 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004378}
4379
Richard Smith0efa75c2012-03-29 01:16:42 +00004380static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4381 SourceLocation EllipsisLoc) {
4382 if (EllipsisLoc.isValid()) {
4383 FixItHint Insertion;
4384 if (!D.getEllipsisLoc().isValid()) {
4385 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4386 D.setEllipsisLoc(EllipsisLoc);
4387 }
4388 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4389 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4390 }
4391}
4392
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004393/// ParseDirectDeclarator
4394/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004395/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004396/// '(' declarator ')'
4397/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004398/// [C90] direct-declarator '[' constant-expression[opt] ']'
4399/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4400/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4401/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4402/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004403/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4404/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004405/// direct-declarator '(' parameter-type-list ')'
4406/// direct-declarator '(' identifier-list[opt] ')'
4407/// [GNU] direct-declarator '(' parameter-forward-declarations
4408/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004409/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4410/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004411/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4412/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4413/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004414/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004415/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004416///
4417/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004418/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004419/// '::'[opt] nested-name-specifier[opt] type-name
4420///
4421/// id-expression: [C++ 5.1]
4422/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004423/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004424///
4425/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004426/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004427/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004428/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004429/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004430/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004431///
Richard Smith1453e312012-03-27 01:42:32 +00004432/// Note, any additional constructs added here may need corresponding changes
4433/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004434void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004435 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004436
David Blaikiebbafb8a2012-03-11 07:00:24 +00004437 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004438 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004439 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004440 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4441 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004442 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004443 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004444 }
4445
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004446 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004447 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004448 // Change the declaration context for name lookup, until this function
4449 // is exited (and the declarator has been parsed).
4450 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004451 }
4452
Douglas Gregor27b4c162010-12-23 22:44:42 +00004453 // C++0x [dcl.fct]p14:
4454 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004455 // of a parameter-declaration-clause without a preceding comma. In
4456 // this case, the ellipsis is parsed as part of the
4457 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004458 // parameter pack that has not been expanded; otherwise, it is parsed
4459 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004460 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004461 !((D.getContext() == Declarator::PrototypeContext ||
4462 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004463 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004464 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004465 !Actions.containsUnexpandedParameterPacks(D))) {
4466 SourceLocation EllipsisLoc = ConsumeToken();
4467 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4468 // The ellipsis was put in the wrong place. Recover, and explain to
4469 // the user what they should have done.
4470 ParseDeclarator(D);
4471 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4472 return;
4473 } else
4474 D.setEllipsisLoc(EllipsisLoc);
4475
4476 // The ellipsis can't be followed by a parenthesized declarator. We
4477 // check for that in ParseParenDeclarator, after we have disambiguated
4478 // the l_paren token.
4479 }
4480
Douglas Gregor7861a802009-11-03 01:35:08 +00004481 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4482 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4483 // We found something that indicates the start of an unqualified-id.
4484 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004485 bool AllowConstructorName;
4486 if (D.getDeclSpec().hasTypeSpecifier())
4487 AllowConstructorName = false;
4488 else if (D.getCXXScopeSpec().isSet())
4489 AllowConstructorName =
4490 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004491 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004492 else
4493 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4494
Abramo Bagnara7945c982012-01-27 09:46:47 +00004495 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004496 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4497 /*EnteringContext=*/true,
4498 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004499 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004500 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004501 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004502 D.getName()) ||
4503 // Once we're past the identifier, if the scope was bad, mark the
4504 // whole declarator bad.
4505 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004506 D.SetIdentifier(0, Tok.getLocation());
4507 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004508 } else {
4509 // Parsed the unqualified-id; update range information and move along.
4510 if (D.getSourceRange().getBegin().isInvalid())
4511 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4512 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004513 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004514 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004515 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004516 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004517 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004518 "There's a C++-specific check for tok::identifier above");
4519 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4520 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4521 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004522 goto PastIdentifier;
4523 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004524
Douglas Gregor7861a802009-11-03 01:35:08 +00004525 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004526 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004527 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004528 // Example: 'char (*X)' or 'int (*XX)(void)'
4529 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004530
4531 // If the declarator was parenthesized, we entered the declarator
4532 // scope when parsing the parenthesized declarator, then exited
4533 // the scope already. Re-enter the scope, if we need to.
4534 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004535 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004536 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004537 if (!D.isInvalidType() &&
4538 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004539 // Change the declaration context for name lookup, until this function
4540 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004541 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004542 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004543 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004544 // This could be something simple like "int" (in which case the declarator
4545 // portion is empty), if an abstract-declarator is allowed.
4546 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004547
4548 // The grammar for abstract-pack-declarator does not allow grouping parens.
4549 // FIXME: Revisit this once core issue 1488 is resolved.
4550 if (D.hasEllipsis() && D.hasGroupingParens())
4551 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4552 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004553 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004554 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004555 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004556 if (D.getContext() == Declarator::MemberContext)
4557 Diag(Tok, diag::err_expected_member_name_or_semi)
4558 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004559 else if (getLangOpts().CPlusPlus) {
4560 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4561 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4562 else
4563 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4564 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004565 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004566 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004567 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004568 }
Mike Stump11289f42009-09-09 15:08:12 +00004569
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004570 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004571 assert(D.isPastIdentifier() &&
4572 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004573
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004574 // Don't parse attributes unless we have parsed an unparenthesized name.
4575 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004576 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004577
Chris Lattneracd58a32006-08-06 17:24:14 +00004578 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004579 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004580 // Enter function-declaration scope, limiting any declarators to the
4581 // function prototype scope, including parameter declarators.
4582 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004583 Scope::FunctionPrototypeScope|Scope::DeclScope|
4584 (D.isFunctionDeclaratorAFunctionDeclaration()
4585 ? Scope::FunctionDeclarationScope : 0));
4586
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004587 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4588 // In such a case, check if we actually have a function declarator; if it
4589 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004590 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004591 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4592 // The name of the declarator, if any, is tentatively declared within
4593 // a possible direct initializer.
4594 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4595 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4596 TentativelyDeclaredIdentifiers.pop_back();
4597 if (!IsFunctionDecl)
4598 break;
4599 }
John McCall084e83d2011-03-24 11:26:52 +00004600 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004601 BalancedDelimiterTracker T(*this, tok::l_paren);
4602 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004603 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004604 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004605 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004606 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004607 } else {
4608 break;
4609 }
4610 }
Chad Rosierc1183952012-06-26 22:30:43 +00004611}
Chris Lattneracd58a32006-08-06 17:24:14 +00004612
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004613/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4614/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004615/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004616/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4617///
4618/// direct-declarator:
4619/// '(' declarator ')'
4620/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004621/// direct-declarator '(' parameter-type-list ')'
4622/// direct-declarator '(' identifier-list[opt] ')'
4623/// [GNU] direct-declarator '(' parameter-forward-declarations
4624/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004625///
4626void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004627 BalancedDelimiterTracker T(*this, tok::l_paren);
4628 T.consumeOpen();
4629
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004630 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004631
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004632 // Eat any attributes before we look at whether this is a grouping or function
4633 // declarator paren. If this is a grouping paren, the attribute applies to
4634 // the type being built up, for example:
4635 // int (__attribute__(()) *x)(long y)
4636 // If this ends up not being a grouping paren, the attribute applies to the
4637 // first argument, for example:
4638 // int (__attribute__(()) int x)
4639 // In either case, we need to eat any attributes to be able to determine what
4640 // sort of paren this is.
4641 //
John McCall084e83d2011-03-24 11:26:52 +00004642 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004643 bool RequiresArg = false;
4644 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004645 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004646
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004647 // We require that the argument list (if this is a non-grouping paren) be
4648 // present even if the attribute list was empty.
4649 RequiresArg = true;
4650 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004651
Steve Naroff44ac7772008-12-25 14:16:32 +00004652 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004653 ParseMicrosoftTypeAttributes(attrs);
4654
Dawn Perchik335e16b2010-09-03 01:29:35 +00004655 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004656 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004657 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004658
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004659 // If we haven't past the identifier yet (or where the identifier would be
4660 // stored, if this is an abstract declarator), then this is probably just
4661 // grouping parens. However, if this could be an abstract-declarator, then
4662 // this could also be the start of function arguments (consider 'void()').
4663 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004664
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004665 if (!D.mayOmitIdentifier()) {
4666 // If this can't be an abstract-declarator, this *must* be a grouping
4667 // paren, because we haven't seen the identifier yet.
4668 isGrouping = true;
4669 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004670 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4671 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004672 isDeclarationSpecifier() || // 'int(int)' is a function.
4673 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004674 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4675 // considered to be a type, not a K&R identifier-list.
4676 isGrouping = false;
4677 } else {
4678 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4679 isGrouping = true;
4680 }
Mike Stump11289f42009-09-09 15:08:12 +00004681
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004682 // If this is a grouping paren, handle:
4683 // direct-declarator: '(' declarator ')'
4684 // direct-declarator: '(' attributes declarator ')'
4685 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004686 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4687 D.setEllipsisLoc(SourceLocation());
4688
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004689 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004690 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004691 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004692 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004693 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004694 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004695 T.getCloseLocation()),
4696 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004697
4698 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004699
4700 // An ellipsis cannot be placed outside parentheses.
4701 if (EllipsisLoc.isValid())
4702 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4703
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004704 return;
4705 }
Mike Stump11289f42009-09-09 15:08:12 +00004706
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004707 // Okay, if this wasn't a grouping paren, it must be the start of a function
4708 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004709 // identifier (and remember where it would have been), then call into
4710 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004711 D.SetIdentifier(0, Tok.getLocation());
4712
David Blaikie15a430a2011-12-04 05:04:18 +00004713 // Enter function-declaration scope, limiting any declarators to the
4714 // function prototype scope, including parameter declarators.
4715 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004716 Scope::FunctionPrototypeScope | Scope::DeclScope |
4717 (D.isFunctionDeclaratorAFunctionDeclaration()
4718 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004719 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004720 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004721}
4722
4723/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4724/// declarator D up to a paren, which indicates that we are parsing function
4725/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004726///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004727/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4728/// immediately after the open paren - they should be considered to be the
4729/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004730///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004731/// If RequiresArg is true, then the first argument of the function is required
4732/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004733///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004734/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4735/// (C++11) ref-qualifier[opt], exception-specification[opt],
4736/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4737///
4738/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004739/// dynamic-exception-specification
4740/// noexcept-specification
4741///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004742void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004743 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004744 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004745 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004746 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004747 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004748 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004749 // lparen is already consumed!
4750 assert(D.isPastIdentifier() && "Should not call before identifier!");
4751
4752 // This should be true when the function has typed arguments.
4753 // Otherwise, it is treated as a K&R-style function.
4754 bool HasProto = false;
4755 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004756 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004757 // Remember where we see an ellipsis, if any.
4758 SourceLocation EllipsisLoc;
4759
4760 DeclSpec DS(AttrFactory);
4761 bool RefQualifierIsLValueRef = true;
4762 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004763 SourceLocation ConstQualifierLoc;
4764 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004765 ExceptionSpecificationType ESpecType = EST_None;
4766 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004767 SmallVector<ParsedType, 2> DynamicExceptions;
4768 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004769 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004770 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004771 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004772
James Molloy6f8780b2012-02-29 10:24:19 +00004773 Actions.ActOnStartFunctionDeclarator();
4774
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004775 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4776 EndLoc is the end location for the function declarator.
4777 They differ for trailing return types. */
4778 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004779 SourceLocation LParenLoc, RParenLoc;
4780 LParenLoc = Tracker.getOpenLocation();
4781 StartLoc = LParenLoc;
4782
Douglas Gregor9e66af42011-07-05 16:44:18 +00004783 if (isFunctionDeclaratorIdentifierList()) {
4784 if (RequiresArg)
4785 Diag(Tok, diag::err_argument_required_after_attribute);
4786
4787 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4788
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004789 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004790 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004791 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004792 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004793 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004794 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004795 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004796 else if (RequiresArg)
4797 Diag(Tok, diag::err_argument_required_after_attribute);
4798
David Blaikiebbafb8a2012-03-11 07:00:24 +00004799 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004800
4801 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004802 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004803 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004804 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004805 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004806
David Blaikiebbafb8a2012-03-11 07:00:24 +00004807 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004808 // FIXME: Accept these components in any order, and produce fixits to
4809 // correct the order if the user gets it wrong. Ideally we should deal
4810 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004811
4812 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004813 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4814 if (!DS.getSourceRange().getEnd().isInvalid()) {
4815 EndLoc = DS.getSourceRange().getEnd();
4816 ConstQualifierLoc = DS.getConstSpecLoc();
4817 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4818 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004819
4820 // Parse ref-qualifier[opt].
4821 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004822 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004823 diag::warn_cxx98_compat_ref_qualifier :
4824 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004825
Douglas Gregor9e66af42011-07-05 16:44:18 +00004826 RefQualifierIsLValueRef = Tok.is(tok::amp);
4827 RefQualifierLoc = ConsumeToken();
4828 EndLoc = RefQualifierLoc;
4829 }
4830
Douglas Gregor3024f072012-04-16 07:05:22 +00004831 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004832 // If a declaration declares a member function or member function
4833 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004834 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004835 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004836 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004837 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004838 getLangOpts().CPlusPlus11 &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004839 (D.getContext() == Declarator::MemberContext ||
4840 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004841 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004842 Actions.CurContext->isRecord()));
4843 Sema::CXXThisScopeRAII ThisScope(Actions,
4844 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00004845 DS.getTypeQualifiers() |
4846 (D.getDeclSpec().isConstexprSpecified()
4847 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00004848 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004849
Douglas Gregor9e66af42011-07-05 16:44:18 +00004850 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004851 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004852 DynamicExceptions,
4853 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004854 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004855 if (ESpecType != EST_None)
4856 EndLoc = ESpecRange.getEnd();
4857
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004858 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4859 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00004860 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004861
Douglas Gregor9e66af42011-07-05 16:44:18 +00004862 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004863 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004864 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004865 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004866 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4867 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004868 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004869 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004870 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004871 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004872 }
4873 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004874 }
4875
4876 // Remember that we parsed a function type, and remember the attributes.
4877 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004878 IsAmbiguous,
4879 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004880 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004881 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004882 DS.getTypeQualifiers(),
4883 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004884 RefQualifierLoc, ConstQualifierLoc,
4885 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004886 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004887 ESpecType, ESpecRange.getBegin(),
4888 DynamicExceptions.data(),
4889 DynamicExceptionRanges.data(),
4890 DynamicExceptions.size(),
4891 NoexceptExpr.isUsable() ?
4892 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004893 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004894 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004895 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004896
4897 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004898}
4899
4900/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4901/// identifier list form for a K&R-style function: void foo(a,b,c)
4902///
4903/// Note that identifier-lists are only allowed for normal declarators, not for
4904/// abstract-declarators.
4905bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004906 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004907 && Tok.is(tok::identifier)
4908 && !TryAltiVecVectorToken()
4909 // K&R identifier lists can't have typedefs as identifiers, per C99
4910 // 6.7.5.3p11.
4911 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4912 // Identifier lists follow a really simple grammar: the identifiers can
4913 // be followed *only* by a ", identifier" or ")". However, K&R
4914 // identifier lists are really rare in the brave new modern world, and
4915 // it is very common for someone to typo a type in a non-K&R style
4916 // list. If we are presented with something like: "void foo(intptr x,
4917 // float y)", we don't want to start parsing the function declarator as
4918 // though it is a K&R style declarator just because intptr is an
4919 // invalid type.
4920 //
4921 // To handle this, we check to see if the token after the first
4922 // identifier is a "," or ")". Only then do we parse it as an
4923 // identifier list.
4924 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4925}
4926
4927/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4928/// we found a K&R-style identifier list instead of a typed parameter list.
4929///
4930/// After returning, ParamInfo will hold the parsed parameters.
4931///
4932/// identifier-list: [C99 6.7.5]
4933/// identifier
4934/// identifier-list ',' identifier
4935///
4936void Parser::ParseFunctionDeclaratorIdentifierList(
4937 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004938 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004939 // If there was no identifier specified for the declarator, either we are in
4940 // an abstract-declarator, or we are in a parameter declarator which was found
4941 // to be abstract. In abstract-declarators, identifier lists are not valid:
4942 // diagnose this.
4943 if (!D.getIdentifier())
4944 Diag(Tok, diag::ext_ident_list_in_param);
4945
4946 // Maintain an efficient lookup of params we have seen so far.
4947 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4948
4949 while (1) {
4950 // If this isn't an identifier, report the error and skip until ')'.
4951 if (Tok.isNot(tok::identifier)) {
4952 Diag(Tok, diag::err_expected_ident);
4953 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4954 // Forget we parsed anything.
4955 ParamInfo.clear();
4956 return;
4957 }
4958
4959 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4960
4961 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4962 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4963 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4964
4965 // Verify that the argument identifier has not already been mentioned.
4966 if (!ParamsSoFar.insert(ParmII)) {
4967 Diag(Tok, diag::err_param_redefinition) << ParmII;
4968 } else {
4969 // Remember this identifier in ParamInfo.
4970 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4971 Tok.getLocation(),
4972 0));
4973 }
4974
4975 // Eat the identifier.
4976 ConsumeToken();
4977
4978 // The list continues if we see a comma.
4979 if (Tok.isNot(tok::comma))
4980 break;
4981 ConsumeToken();
4982 }
4983}
4984
4985/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4986/// after the opening parenthesis. This function will not parse a K&R-style
4987/// identifier list.
4988///
Richard Smith2620cd92012-04-11 04:01:28 +00004989/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4990/// caller parsed those arguments immediately after the open paren - they should
4991/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004992///
4993/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4994/// be the location of the ellipsis, if any was parsed.
4995///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004996/// parameter-type-list: [C99 6.7.5]
4997/// parameter-list
4998/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004999/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005000///
5001/// parameter-list: [C99 6.7.5]
5002/// parameter-declaration
5003/// parameter-list ',' parameter-declaration
5004///
5005/// parameter-declaration: [C99 6.7.5]
5006/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005007/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005008/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005009/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005010/// declaration-specifiers abstract-declarator[opt]
5011/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005012/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005013/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005014/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005015///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005016void Parser::ParseParameterDeclarationClause(
5017 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005018 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005019 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005020 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00005021
Chris Lattner371ed4e2008-04-06 06:57:35 +00005022 while (1) {
5023 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005024 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5025 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005026 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005027 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005028 }
Mike Stump11289f42009-09-09 15:08:12 +00005029
Chris Lattner371ed4e2008-04-06 06:57:35 +00005030 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005031 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005032 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005033
Richard Smith2620cd92012-04-11 04:01:28 +00005034 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005035 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005036
John McCall53fa7142010-12-24 02:08:15 +00005037 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005038 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005039
5040 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005041
5042 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005043 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005044 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005045 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5046 // too much hassle.
5047 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005048
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005049 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005050
Chris Lattner371ed4e2008-04-06 06:57:35 +00005051 // Parse the declarator. This is "PrototypeContext", because we must
5052 // accept either 'declarator' or 'abstract-declarator' here.
5053 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5054 ParseDeclarator(ParmDecl);
5055
5056 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005057 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005058
Chris Lattner371ed4e2008-04-06 06:57:35 +00005059 // Remember this parsed parameter in ParamInfo.
5060 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005061
Douglas Gregor4d87df52008-12-16 21:30:33 +00005062 // DefArgToks is used when the parsing of default arguments needs
5063 // to be delayed.
5064 CachedTokens *DefArgToks = 0;
5065
Chris Lattner371ed4e2008-04-06 06:57:35 +00005066 // If no parameter was specified, verify that *something* was specified,
5067 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005068 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5069 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005070 // Completely missing, emit error.
5071 Diag(DSStart, diag::err_missing_param);
5072 } else {
5073 // Otherwise, we have something. Add it and let semantic analysis try
5074 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005075
Chris Lattner371ed4e2008-04-06 06:57:35 +00005076 // Inform the actions module about the parameter declarator, so it gets
5077 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005078 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005079
5080 // Parse the default argument, if any. We parse the default
5081 // arguments in all dialects; the semantic analysis in
5082 // ActOnParamDefaultArgument will reject the default argument in
5083 // C.
5084 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005085 SourceLocation EqualLoc = Tok.getLocation();
5086
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005087 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005088 if (D.getContext() == Declarator::MemberContext) {
5089 // If we're inside a class definition, cache the tokens
5090 // corresponding to the default argument. We'll actually parse
5091 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005092 // FIXME: Can we use a smart pointer for Toks?
5093 DefArgToks = new CachedTokens;
5094
Mike Stump11289f42009-09-09 15:08:12 +00005095 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005096 /*StopAtSemi=*/true,
5097 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005098 delete DefArgToks;
5099 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005100 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005101 } else {
5102 // Mark the end of the default argument so that we know when to
5103 // stop when we parse it later on.
5104 Token DefArgEnd;
5105 DefArgEnd.startToken();
5106 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5107 DefArgEnd.setLocation(Tok.getLocation());
5108 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005109 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005110 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005111 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005112 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005113 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005114 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005115
Chad Rosierc1183952012-06-26 22:30:43 +00005116 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005117 // used.
5118 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005119 Sema::PotentiallyEvaluatedIfUsed,
5120 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005121
Sebastian Redldb63af22012-03-14 15:54:00 +00005122 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005123 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005124 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005125 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005126 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005127 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005128 if (DefArgResult.isInvalid()) {
5129 Actions.ActOnParamDefaultArgumentError(Param);
5130 SkipUntil(tok::comma, tok::r_paren, true, true);
5131 } else {
5132 // Inform the actions module about the default argument
5133 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005134 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005135 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005136 }
5137 }
Mike Stump11289f42009-09-09 15:08:12 +00005138
5139 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5140 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005141 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005142 }
5143
5144 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005145 if (Tok.isNot(tok::comma)) {
5146 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005147 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005148
David Blaikiebbafb8a2012-03-11 07:00:24 +00005149 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005150 // We have ellipsis without a preceding ',', which is ill-formed
5151 // in C. Complain and provide the fix.
5152 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005153 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005154 }
5155 }
Chad Rosierc1183952012-06-26 22:30:43 +00005156
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005157 break;
5158 }
Mike Stump11289f42009-09-09 15:08:12 +00005159
Chris Lattner371ed4e2008-04-06 06:57:35 +00005160 // Consume the comma.
5161 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005162 }
Mike Stump11289f42009-09-09 15:08:12 +00005163
Chris Lattner6c940e62008-04-06 06:34:08 +00005164}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005165
Chris Lattnere8074e62006-08-06 18:30:15 +00005166/// [C90] direct-declarator '[' constant-expression[opt] ']'
5167/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5168/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5169/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5170/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005171/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5172/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005173void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005174 if (CheckProhibitedCXX11Attribute())
5175 return;
5176
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005177 BalancedDelimiterTracker T(*this, tok::l_square);
5178 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005179
Chris Lattner84a11622008-12-18 07:27:21 +00005180 // C array syntax has many features, but by-far the most common is [] and [4].
5181 // This code does a fast path to handle some of the most obvious cases.
5182 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005183 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005184 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005185 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005186
Chris Lattner84a11622008-12-18 07:27:21 +00005187 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005188 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005189 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005190 T.getOpenLocation(),
5191 T.getCloseLocation()),
5192 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005193 return;
5194 } else if (Tok.getKind() == tok::numeric_constant &&
5195 GetLookAheadToken(1).is(tok::r_square)) {
5196 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005197 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005198 ConsumeToken();
5199
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005200 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005201 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005202 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005203
Chris Lattner84a11622008-12-18 07:27:21 +00005204 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005205 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005206 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005207 T.getOpenLocation(),
5208 T.getCloseLocation()),
5209 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005210 return;
5211 }
Mike Stump11289f42009-09-09 15:08:12 +00005212
Chris Lattnere8074e62006-08-06 18:30:15 +00005213 // If valid, this location is the position where we read the 'static' keyword.
5214 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005215 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005216 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005217
Chris Lattnere8074e62006-08-06 18:30:15 +00005218 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005219 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005220 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005221 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005222
Chris Lattnere8074e62006-08-06 18:30:15 +00005223 // If we haven't already read 'static', check to see if there is one after the
5224 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005225 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005226 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005227
Chris Lattnere8074e62006-08-06 18:30:15 +00005228 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005229 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005230 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005231
Chris Lattner521ff2b2008-04-06 05:26:30 +00005232 // Handle the case where we have '[*]' as the array size. However, a leading
5233 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005234 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005235 // infrequent, use of lookahead is not costly here.
5236 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005237 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005238
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005239 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005240 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005241 StaticLoc = SourceLocation(); // Drop the static.
5242 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005243 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005244 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005245 // Note, in C89, this production uses the constant-expr production instead
5246 // of assignment-expr. The only difference is that assignment-expr allows
5247 // things like '=' and '*='. Sema rejects these in C89 mode because they
5248 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005249
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005250 // Parse the constant-expression or assignment-expression now (depending
5251 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005252 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005253 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005254 } else {
5255 EnterExpressionEvaluationContext Unevaluated(Actions,
5256 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005257 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005258 }
Chris Lattner62591722006-08-12 18:40:58 +00005259 }
Mike Stump11289f42009-09-09 15:08:12 +00005260
Chris Lattner62591722006-08-12 18:40:58 +00005261 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005262 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005263 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005264 // If the expression was invalid, skip it.
5265 SkipUntil(tok::r_square);
5266 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005267 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005268
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005269 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005270
John McCall084e83d2011-03-24 11:26:52 +00005271 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005272 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005273
Chris Lattner84a11622008-12-18 07:27:21 +00005274 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005275 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005276 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005277 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005278 T.getOpenLocation(),
5279 T.getCloseLocation()),
5280 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005281}
5282
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005283/// [GNU] typeof-specifier:
5284/// typeof ( expressions )
5285/// typeof ( type-name )
5286/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005287///
5288void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005289 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005290 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005291 SourceLocation StartLoc = ConsumeToken();
5292
John McCalle8595032010-01-13 20:03:27 +00005293 const bool hasParens = Tok.is(tok::l_paren);
5294
Eli Friedman15681d62012-09-26 04:34:21 +00005295 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5296 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005297
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005298 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005299 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005300 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005301 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5302 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005303 if (hasParens)
5304 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005305
5306 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005307 // FIXME: Not accurate, the range gets one token more than it should.
5308 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005309 else
5310 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005311
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005312 if (isCastExpr) {
5313 if (!CastTy) {
5314 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005315 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005316 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005317
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005318 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005319 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005320 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5321 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005322 DiagID, CastTy))
5323 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005324 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005325 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005326
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005327 // If we get here, the operand to the typeof was an expresion.
5328 if (Operand.isInvalid()) {
5329 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005330 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005331 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005332
Eli Friedmane0afc982012-01-21 01:01:51 +00005333 // We might need to transform the operand if it is potentially evaluated.
5334 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5335 if (Operand.isInvalid()) {
5336 DS.SetTypeSpecError();
5337 return;
5338 }
5339
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005340 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005341 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005342 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5343 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005344 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005345 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005346}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005347
Benjamin Kramere56f3932011-12-23 17:00:35 +00005348/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005349/// _Atomic ( type-name )
5350///
5351void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5352 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5353
5354 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005355 BalancedDelimiterTracker T(*this, tok::l_paren);
5356 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005357 SkipUntil(tok::r_paren);
5358 return;
5359 }
5360
5361 TypeResult Result = ParseTypeName();
5362 if (Result.isInvalid()) {
5363 SkipUntil(tok::r_paren);
5364 return;
5365 }
5366
5367 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005368 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005369
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005370 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005371 return;
5372
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005373 DS.setTypeofParensRange(T.getRange());
5374 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005375
5376 const char *PrevSpec = 0;
5377 unsigned DiagID;
5378 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5379 DiagID, Result.release()))
5380 Diag(StartLoc, DiagID) << PrevSpec;
5381}
5382
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005383
5384/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5385/// from TryAltiVecVectorToken.
5386bool Parser::TryAltiVecVectorTokenOutOfLine() {
5387 Token Next = NextToken();
5388 switch (Next.getKind()) {
5389 default: return false;
5390 case tok::kw_short:
5391 case tok::kw_long:
5392 case tok::kw_signed:
5393 case tok::kw_unsigned:
5394 case tok::kw_void:
5395 case tok::kw_char:
5396 case tok::kw_int:
5397 case tok::kw_float:
5398 case tok::kw_double:
5399 case tok::kw_bool:
5400 case tok::kw___pixel:
5401 Tok.setKind(tok::kw___vector);
5402 return true;
5403 case tok::identifier:
5404 if (Next.getIdentifierInfo() == Ident_pixel) {
5405 Tok.setKind(tok::kw___vector);
5406 return true;
5407 }
5408 return false;
5409 }
5410}
5411
5412bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5413 const char *&PrevSpec, unsigned &DiagID,
5414 bool &isInvalid) {
5415 if (Tok.getIdentifierInfo() == Ident_vector) {
5416 Token Next = NextToken();
5417 switch (Next.getKind()) {
5418 case tok::kw_short:
5419 case tok::kw_long:
5420 case tok::kw_signed:
5421 case tok::kw_unsigned:
5422 case tok::kw_void:
5423 case tok::kw_char:
5424 case tok::kw_int:
5425 case tok::kw_float:
5426 case tok::kw_double:
5427 case tok::kw_bool:
5428 case tok::kw___pixel:
5429 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5430 return true;
5431 case tok::identifier:
5432 if (Next.getIdentifierInfo() == Ident_pixel) {
5433 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5434 return true;
5435 }
5436 break;
5437 default:
5438 break;
5439 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005440 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005441 DS.isTypeAltiVecVector()) {
5442 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5443 return true;
5444 }
5445 return false;
5446}