blob: a4cec8c2575c172c27f21a42e55db1d5d7c946f6 [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
Douglas Gregord2472d42013-05-02 23:25:32 +0000181/// \brief Determine whether the given attribute has all expression arguments.
182static bool attributeHasExprArgs(const IdentifierInfo &II) {
183 return llvm::StringSwitch<bool>(II.getName())
184#include "clang/Parse/AttrExprArgs.inc"
185 .Default(false);
186}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000187
Michael Han23214e52012-10-03 01:56:22 +0000188/// Parse the arguments to a parameterized GNU attribute or
189/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000190void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
191 SourceLocation AttrNameLoc,
192 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000193 SourceLocation *EndLoc,
194 IdentifierInfo *ScopeName,
195 SourceLocation ScopeLoc,
196 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000197
198 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
199
200 // Availability attributes have their own grammar.
201 if (AttrName->isStr("availability")) {
202 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
205 // Thread safety attributes fit into the FIXME case above, so we
206 // just parse the arguments as a list of expressions
207 if (IsThreadSafetyAttribute(AttrName->getName())) {
208 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
209 return;
210 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000211 // Type safety attributes have their own grammar.
212 if (AttrName->isStr("type_tag_for_datatype")) {
213 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
214 return;
215 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000216
217 ConsumeParen(); // ignore the left paren loc for now
218
Richard Smithb12bf692011-10-17 21:20:17 +0000219 IdentifierInfo *ParmName = 0;
220 SourceLocation ParmLoc;
221 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000222
Joey Goulyaba589c2013-03-08 09:42:32 +0000223 TypeResult T;
224 SourceRange TypeRange;
225 bool TypeParsed = false;
226
Richard Smithb12bf692011-10-17 21:20:17 +0000227 switch (Tok.getKind()) {
228 case tok::kw_char:
229 case tok::kw_wchar_t:
230 case tok::kw_char16_t:
231 case tok::kw_char32_t:
232 case tok::kw_bool:
233 case tok::kw_short:
234 case tok::kw_int:
235 case tok::kw_long:
236 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000237 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000238 case tok::kw_signed:
239 case tok::kw_unsigned:
240 case tok::kw_float:
241 case tok::kw_double:
242 case tok::kw_void:
243 case tok::kw_typeof:
244 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000245 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000248 break;
249
250 case tok::identifier:
Joey Goulyaba589c2013-03-08 09:42:32 +0000251 if (AttrName->isStr("vec_type_hint")) {
252 T = ParseTypeName(&TypeRange);
253 TypeParsed = true;
254 break;
255 }
Douglas Gregord2472d42013-05-02 23:25:32 +0000256 // If the attribute has all expression arguments, and not a "parameter",
257 // break out to handle it below.
258 if (attributeHasExprArgs(*AttrName))
259 break;
Richard Smithb12bf692011-10-17 21:20:17 +0000260 ParmName = Tok.getIdentifierInfo();
261 ParmLoc = ConsumeToken();
262 break;
263
264 default:
265 break;
266 }
267
Benjamin Kramerf0623432012-08-23 22:51:59 +0000268 ExprVector ArgExprs;
Joey Goulyaba589c2013-03-08 09:42:32 +0000269 bool isInvalid = false;
270 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000271
Joey Goulyaba589c2013-03-08 09:42:32 +0000272 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithb12bf692011-10-17 21:20:17 +0000273 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
274 // Eat the comma.
275 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000276 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000277
Richard Smithb12bf692011-10-17 21:20:17 +0000278 // Parse the non-empty comma-separated list of expressions.
279 while (1) {
280 ExprResult ArgExpr(ParseAssignmentExpression());
281 if (ArgExpr.isInvalid()) {
282 SkipUntil(tok::r_paren);
283 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000284 }
Richard Smithb12bf692011-10-17 21:20:17 +0000285 ArgExprs.push_back(ArgExpr.release());
286 if (Tok.isNot(tok::comma))
287 break;
288 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000289 }
Richard Smithb12bf692011-10-17 21:20:17 +0000290 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000291 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
292 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
293 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000294 while (Tok.is(tok::identifier)) {
295 ConsumeToken();
296 if (Tok.is(tok::greater))
297 break;
298 if (Tok.is(tok::comma)) {
299 ConsumeToken();
300 continue;
301 }
302 }
303 if (Tok.isNot(tok::greater))
304 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000305 SkipUntil(tok::r_paren, false, true); // skip until ')'
306 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000307 } else if (AttrName->isStr("vec_type_hint")) {
308 if (T.get() && !T.isInvalid())
309 isParmType = true;
310 else {
311 if (Tok.is(tok::identifier))
312 ConsumeToken();
313 if (TypeParsed)
314 isInvalid = true;
315 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000316 }
Richard Smithb12bf692011-10-17 21:20:17 +0000317
318 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000319 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
320 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000321 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000322 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000323 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
324 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
325 } else {
326 AttributeList *attr = Attrs.addNew(
327 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
328 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
329 if (BuiltinType &&
330 attr->getKind() == AttributeList::AT_IBOutletCollection)
331 Diag(Tok, diag::err_iboutletcollection_builtintype);
332 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000333 }
334}
335
Chad Rosierc1183952012-06-26 22:30:43 +0000336/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000337/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000338void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000339 SourceLocation AttrNameLoc,
340 ParsedAttributes &Attrs)
341{
342 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000343 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000344 AttrName->getNameStart(), tok::r_paren))
345 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000346
Aaron Ballman478faed2012-06-19 22:09:27 +0000347 ExprResult ArgExpr(ParseConstantExpression());
348 if (ArgExpr.isInvalid()) {
349 T.skipToEnd();
350 return;
351 }
352 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000353 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000354 &ExprList, 1, AttributeList::AS_Declspec);
355
356 T.consumeClose();
357}
358
Chad Rosierc1183952012-06-26 22:30:43 +0000359/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000360/// arguments.
361bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
362 return llvm::StringSwitch<bool>(Ident->getName())
363 .Case("dllimport", true)
364 .Case("dllexport", true)
365 .Case("noreturn", true)
366 .Case("nothrow", true)
367 .Case("noinline", true)
368 .Case("naked", true)
369 .Case("appdomain", true)
370 .Case("process", true)
371 .Case("jitintrinsic", true)
372 .Case("noalias", true)
373 .Case("restrict", true)
374 .Case("novtable", true)
375 .Case("selectany", true)
376 .Case("thread", true)
377 .Default(false);
378}
379
Chad Rosierc1183952012-06-26 22:30:43 +0000380/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000381/// parameters). Will return false if we properly handled the declspec, or
382/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000383void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000384 SourceLocation Loc,
385 ParsedAttributes &Attrs) {
386 // Try to handle the easy case first -- these declspecs all take a single
387 // parameter as their argument.
388 if (llvm::StringSwitch<bool>(Ident->getName())
389 .Case("uuid", true)
390 .Case("align", true)
391 .Case("allocate", true)
392 .Default(false)) {
393 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
394 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000395 // The deprecated declspec has an optional single argument, so we will
396 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 // not.
398 if (Tok.getKind() == tok::l_paren)
399 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
400 else
Chad Rosierc1183952012-06-26 22:30:43 +0000401 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000402 AttributeList::AS_Declspec);
403 } else if (Ident->getName() == "property") {
404 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000405 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000407 if (Tok.isNot(tok::l_paren)) {
408 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
409 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000410 return;
John McCall5e77d762013-04-16 07:28:30 +0000411 }
412 BalancedDelimiterTracker T(*this, tok::l_paren);
413 T.expectAndConsume(diag::err_expected_lparen_after,
414 Ident->getNameStart(), tok::r_paren);
415
416 enum AccessorKind {
417 AK_Invalid = -1,
418 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
419 };
420 IdentifierInfo *AccessorNames[] = { 0, 0 };
421 bool HasInvalidAccessor = false;
422
423 // Parse the accessor specifications.
424 while (true) {
425 // Stop if this doesn't look like an accessor spec.
426 if (!Tok.is(tok::identifier)) {
427 // If the user wrote a completely empty list, use a special diagnostic.
428 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
429 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
430 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
431 break;
432 }
433
434 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
435 break;
436 }
437
438 AccessorKind Kind;
439 SourceLocation KindLoc = Tok.getLocation();
440 StringRef KindStr = Tok.getIdentifierInfo()->getName();
441 if (KindStr == "get") {
442 Kind = AK_Get;
443 } else if (KindStr == "put") {
444 Kind = AK_Put;
445
446 // Recover from the common mistake of using 'set' instead of 'put'.
447 } else if (KindStr == "set") {
448 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
449 << FixItHint::CreateReplacement(KindLoc, "put");
450 Kind = AK_Put;
451
452 // Handle the mistake of forgetting the accessor kind by skipping
453 // this accessor.
454 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
455 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
456 ConsumeToken();
457 HasInvalidAccessor = true;
458 goto next_property_accessor;
459
460 // Otherwise, complain about the unknown accessor kind.
461 } else {
462 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
463 HasInvalidAccessor = true;
464 Kind = AK_Invalid;
465
466 // Try to keep parsing unless it doesn't look like an accessor spec.
467 if (!NextToken().is(tok::equal)) break;
468 }
469
470 // Consume the identifier.
471 ConsumeToken();
472
473 // Consume the '='.
474 if (Tok.is(tok::equal)) {
475 ConsumeToken();
476 } else {
477 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
478 << KindStr;
479 break;
480 }
481
482 // Expect the method name.
483 if (!Tok.is(tok::identifier)) {
484 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
485 break;
486 }
487
488 if (Kind == AK_Invalid) {
489 // Just drop invalid accessors.
490 } else if (AccessorNames[Kind] != NULL) {
491 // Complain about the repeated accessor, ignore it, and keep parsing.
492 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
493 } else {
494 AccessorNames[Kind] = Tok.getIdentifierInfo();
495 }
496 ConsumeToken();
497
498 next_property_accessor:
499 // Keep processing accessors until we run out.
500 if (Tok.is(tok::comma)) {
501 ConsumeAnyToken();
502 continue;
503
504 // If we run into the ')', stop without consuming it.
505 } else if (Tok.is(tok::r_paren)) {
506 break;
507 } else {
508 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
509 break;
510 }
511 }
512
513 // Only add the property attribute if it was well-formed.
514 if (!HasInvalidAccessor) {
515 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
516 SourceLocation(),
517 AccessorNames[AK_Get], AccessorNames[AK_Put],
518 AttributeList::AS_Declspec);
519 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000520 T.skipToEnd();
521 } else {
522 // We don't recognize this as a valid declspec, but instead of creating the
523 // attribute and allowing sema to warn about it, we will warn here instead.
524 // This is because some attributes have multiple spellings, but we need to
525 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000526 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000527 // both locations.
528 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
529
530 // If there's an open paren, we should eat the open and close parens under
531 // the assumption that this unknown declspec has parameters.
532 BalancedDelimiterTracker T(*this, tok::l_paren);
533 if (!T.consumeOpen())
534 T.skipToEnd();
535 }
536}
537
Eli Friedman06de2b52009-06-08 07:21:15 +0000538/// [MS] decl-specifier:
539/// __declspec ( extended-decl-modifier-seq )
540///
541/// [MS] extended-decl-modifier-seq:
542/// extended-decl-modifier[opt]
543/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000544void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000545 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000546
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000547 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000548 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000549 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000551 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000552
Chad Rosierc1183952012-06-26 22:30:43 +0000553 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000554 // you can specify multiple attributes per declspec.
555 while (Tok.getKind() != tok::r_paren) {
556 // We expect either a well-known identifier or a generic string. Anything
557 // else is a malformed declspec.
558 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000559 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000560 Tok.getKind() != tok::kw_restrict) {
561 Diag(Tok, diag::err_ms_declspec_type);
562 T.skipToEnd();
563 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000564 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000565
566 IdentifierInfo *AttrName;
567 SourceLocation AttrNameLoc;
568 if (IsString) {
569 SmallString<8> StrBuffer;
570 bool Invalid = false;
571 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
572 if (Invalid) {
573 T.skipToEnd();
574 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000575 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 AttrName = PP.getIdentifierInfo(Str);
577 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000578 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 AttrName = Tok.getIdentifierInfo();
580 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000581 }
Chad Rosierc1183952012-06-26 22:30:43 +0000582
Aaron Ballman478faed2012-06-19 22:09:27 +0000583 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000584 // If we have a generic string, we will allow it because there is no
585 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000586 // (for instance, SAL declspecs in older versions of MSVC).
587 //
Chad Rosierc1183952012-06-26 22:30:43 +0000588 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000589 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000590 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000591 0, 0, AttributeList::AS_Declspec);
592 else
593 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000594 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000595 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000596}
597
John McCall53fa7142010-12-24 02:08:15 +0000598void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000599 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000600 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000601 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000602 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000603 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000604 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
605 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000606 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000607 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000608 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000609}
610
John McCall53fa7142010-12-24 02:08:15 +0000611void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000612 // Treat these like attributes
613 while (Tok.is(tok::kw___pascal)) {
614 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
615 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000616 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000617 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000618 }
John McCall53fa7142010-12-24 02:08:15 +0000619}
620
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000621void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
622 // Treat these like attributes
623 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000624 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000625 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000626 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
627 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000628 }
629}
630
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000631void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000632 // FIXME: The mapping from attribute spelling to semantics should be
633 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000634 SourceLocation Loc = Tok.getLocation();
635 switch(Tok.getKind()) {
636 // OpenCL qualifiers:
637 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000638 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000639 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000640 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000641 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000642 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000643
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000644 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000645 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000646 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000647 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000648 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000649
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000650 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000651 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000652 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000653 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000654 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000655
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000656 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000657 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000658 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000659 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000660 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000661
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000662 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000663 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000664 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000665 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000666 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000667
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000669 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000670 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000671 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000672 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000673
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000674 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000675 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000676 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000677 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000678 break;
679 default: break;
680 }
681}
682
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000683/// \brief Parse a version number.
684///
685/// version:
686/// simple-integer
687/// simple-integer ',' simple-integer
688/// simple-integer ',' simple-integer ',' simple-integer
689VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
690 Range = Tok.getLocation();
691
692 if (!Tok.is(tok::numeric_constant)) {
693 Diag(Tok, diag::err_expected_version);
694 SkipUntil(tok::comma, tok::r_paren, true, true, true);
695 return VersionTuple();
696 }
697
698 // Parse the major (and possibly minor and subminor) versions, which
699 // are stored in the numeric constant. We utilize a quirk of the
700 // lexer, which is that it handles something like 1.2.3 as a single
701 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000702 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000703 Buffer.resize(Tok.getLength()+1);
704 const char *ThisTokBegin = &Buffer[0];
705
706 // Get the spelling of the token, which eliminates trigraphs, etc.
707 bool Invalid = false;
708 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
709 if (Invalid)
710 return VersionTuple();
711
712 // Parse the major version.
713 unsigned AfterMajor = 0;
714 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000715 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000716 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
717 ++AfterMajor;
718 }
719
720 if (AfterMajor == 0) {
721 Diag(Tok, diag::err_expected_version);
722 SkipUntil(tok::comma, tok::r_paren, true, true, true);
723 return VersionTuple();
724 }
725
726 if (AfterMajor == ActualLength) {
727 ConsumeToken();
728
729 // We only had a single version component.
730 if (Major == 0) {
731 Diag(Tok, diag::err_zero_version);
732 return VersionTuple();
733 }
734
735 return VersionTuple(Major);
736 }
737
738 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
739 Diag(Tok, diag::err_expected_version);
740 SkipUntil(tok::comma, tok::r_paren, true, true, true);
741 return VersionTuple();
742 }
743
744 // Parse the minor version.
745 unsigned AfterMinor = AfterMajor + 1;
746 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000747 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000748 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
749 ++AfterMinor;
750 }
751
752 if (AfterMinor == ActualLength) {
753 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000754
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000755 // We had major.minor.
756 if (Major == 0 && Minor == 0) {
757 Diag(Tok, diag::err_zero_version);
758 return VersionTuple();
759 }
760
Chad Rosierc1183952012-06-26 22:30:43 +0000761 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762 }
763
764 // If what follows is not a '.', we have a problem.
765 if (ThisTokBegin[AfterMinor] != '.') {
766 Diag(Tok, diag::err_expected_version);
767 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000768 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000769 }
770
771 // Parse the subminor version.
772 unsigned AfterSubminor = AfterMinor + 1;
773 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000774 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000775 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
776 ++AfterSubminor;
777 }
778
779 if (AfterSubminor != ActualLength) {
780 Diag(Tok, diag::err_expected_version);
781 SkipUntil(tok::comma, tok::r_paren, true, true, true);
782 return VersionTuple();
783 }
784 ConsumeToken();
785 return VersionTuple(Major, Minor, Subminor);
786}
787
788/// \brief Parse the contents of the "availability" attribute.
789///
790/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000791/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792///
793/// platform:
794/// identifier
795///
796/// version-arg-list:
797/// version-arg
798/// version-arg ',' version-arg-list
799///
800/// version-arg:
801/// 'introduced' '=' version
802/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000803/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000804/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000805/// opt-message:
806/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000807void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
808 SourceLocation AvailabilityLoc,
809 ParsedAttributes &attrs,
810 SourceLocation *endLoc) {
811 SourceLocation PlatformLoc;
812 IdentifierInfo *Platform = 0;
813
814 enum { Introduced, Deprecated, Obsoleted, Unknown };
815 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000816 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817
818 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000819 BalancedDelimiterTracker T(*this, tok::l_paren);
820 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000821 Diag(Tok, diag::err_expected_lparen);
822 return;
823 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824
825 // Parse the platform name,
826 if (Tok.isNot(tok::identifier)) {
827 Diag(Tok, diag::err_availability_expected_platform);
828 SkipUntil(tok::r_paren);
829 return;
830 }
831 Platform = Tok.getIdentifierInfo();
832 PlatformLoc = ConsumeToken();
833
834 // Parse the ',' following the platform name.
835 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
836 return;
837
838 // If we haven't grabbed the pointers for the identifiers
839 // "introduced", "deprecated", and "obsoleted", do so now.
840 if (!Ident_introduced) {
841 Ident_introduced = PP.getIdentifierInfo("introduced");
842 Ident_deprecated = PP.getIdentifierInfo("deprecated");
843 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000844 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000845 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 }
847
848 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000849 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000850 do {
851 if (Tok.isNot(tok::identifier)) {
852 Diag(Tok, diag::err_availability_expected_change);
853 SkipUntil(tok::r_paren);
854 return;
855 }
856 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
857 SourceLocation KeywordLoc = ConsumeToken();
858
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000859 if (Keyword == Ident_unavailable) {
860 if (UnavailableLoc.isValid()) {
861 Diag(KeywordLoc, diag::err_availability_redundant)
862 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000863 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000864 UnavailableLoc = KeywordLoc;
865
866 if (Tok.isNot(tok::comma))
867 break;
868
869 ConsumeToken();
870 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000871 }
872
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000873 if (Tok.isNot(tok::equal)) {
874 Diag(Tok, diag::err_expected_equal_after)
875 << Keyword;
876 SkipUntil(tok::r_paren);
877 return;
878 }
879 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000880 if (Keyword == Ident_message) {
881 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000882 Diag(Tok, diag::err_expected_string_literal)
883 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000884 SkipUntil(tok::r_paren);
885 return;
886 }
887 MessageExpr = ParseStringLiteralExpression();
888 break;
889 }
Chad Rosierc1183952012-06-26 22:30:43 +0000890
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000891 SourceRange VersionRange;
892 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000893
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000894 if (Version.empty()) {
895 SkipUntil(tok::r_paren);
896 return;
897 }
898
899 unsigned Index;
900 if (Keyword == Ident_introduced)
901 Index = Introduced;
902 else if (Keyword == Ident_deprecated)
903 Index = Deprecated;
904 else if (Keyword == Ident_obsoleted)
905 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000906 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000907 Index = Unknown;
908
909 if (Index < Unknown) {
910 if (!Changes[Index].KeywordLoc.isInvalid()) {
911 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000912 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000913 << SourceRange(Changes[Index].KeywordLoc,
914 Changes[Index].VersionRange.getEnd());
915 }
916
917 Changes[Index].KeywordLoc = KeywordLoc;
918 Changes[Index].Version = Version;
919 Changes[Index].VersionRange = VersionRange;
920 } else {
921 Diag(KeywordLoc, diag::err_availability_unknown_change)
922 << Keyword << VersionRange;
923 }
924
925 if (Tok.isNot(tok::comma))
926 break;
927
928 ConsumeToken();
929 } while (true);
930
931 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000932 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000933 return;
934
935 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000936 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000937
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000938 // The 'unavailable' availability cannot be combined with any other
939 // availability changes. Make sure that hasn't happened.
940 if (UnavailableLoc.isValid()) {
941 bool Complained = false;
942 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
943 if (Changes[Index].KeywordLoc.isValid()) {
944 if (!Complained) {
945 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
946 << SourceRange(Changes[Index].KeywordLoc,
947 Changes[Index].VersionRange.getEnd());
948 Complained = true;
949 }
950
951 // Clear out the availability.
952 Changes[Index] = AvailabilityChange();
953 }
954 }
955 }
956
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000957 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000958 attrs.addNew(&Availability,
959 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000960 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000961 Platform, PlatformLoc,
962 Changes[Introduced],
963 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000964 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000965 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000966 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000967}
968
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000969
Bill Wendling44426052012-12-20 19:22:21 +0000970// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000971// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
972
973void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
974
975void Parser::LateParsedClass::ParseLexedAttributes() {
976 Self->ParseLexedAttributes(*Class);
977}
978
979void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000980 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000981}
982
983/// Wrapper class which calls ParseLexedAttribute, after setting up the
984/// scope appropriately.
985void Parser::ParseLexedAttributes(ParsingClass &Class) {
986 // Deal with templates
987 // FIXME: Test cases to make sure this does the right thing for templates.
988 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
989 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
990 HasTemplateScope);
991 if (HasTemplateScope)
992 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
993
Douglas Gregor3024f072012-04-16 07:05:22 +0000994 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000995 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000996 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000997 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
998 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
999
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001000 // Enter the scope of nested classes
1001 if (!AlreadyHasClassScope)
1002 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1003 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001004 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001005 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1006 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1007 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001008 }
Chad Rosierc1183952012-06-26 22:30:43 +00001009
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001010 if (!AlreadyHasClassScope)
1011 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1012 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001013}
1014
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001015
1016/// \brief Parse all attributes in LAs, and attach them to Decl D.
1017void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1018 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001019 assert(LAs.parseSoon() &&
1020 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001021 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001022 if (D)
1023 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001024 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001025 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001026 }
1027 LAs.clear();
1028}
1029
1030
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001031/// \brief Finish parsing an attribute for which parsing was delayed.
1032/// This will be called at the end of parsing a class declaration
1033/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001034/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001035/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001036void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1037 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001038 // Save the current token position.
1039 SourceLocation OrigLoc = Tok.getLocation();
1040
1041 // Append the current token at the end of the new token stream so that it
1042 // doesn't get lost.
1043 LA.Toks.push_back(Tok);
1044 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1045 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001046 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001047
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001048 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001049 // FIXME: Do not warn on C++11 attributes, once we start supporting
1050 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001051 Diag(Tok, diag::warn_attribute_on_function_definition)
1052 << LA.AttrName.getName();
1053 }
1054
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001055 ParsedAttributes Attrs(AttrFactory);
1056 SourceLocation endLoc;
1057
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001058 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001059 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001060 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1061 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001062
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001063 // Allow 'this' within late-parsed attributes.
1064 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
1065 /*TypeQuals=*/0,
1066 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001067
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001068 if (LA.Decls.size() == 1) {
1069 // If the Decl is templatized, add template parameters to scope.
1070 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1071 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1072 if (HasTemplateScope)
1073 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001074
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001075 // If the Decl is on a function, add function parameters to the scope.
1076 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1077 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1078 if (HasFunScope)
1079 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001080
Michael Han23214e52012-10-03 01:56:22 +00001081 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001082 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001083
1084 if (HasFunScope) {
1085 Actions.ActOnExitFunctionContext();
1086 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1087 }
1088 if (HasTemplateScope) {
1089 TempScope.Exit();
1090 }
1091 } else {
1092 // If there are multiple decls, then the decl cannot be within the
1093 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001094 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001095 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001096 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001097 } else {
1098 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001099 }
1100
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001101 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1102 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1103 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001104
1105 if (Tok.getLocation() != OrigLoc) {
1106 // Due to a parsing error, we either went over the cached tokens or
1107 // there are still cached tokens left, so we skip the leftover tokens.
1108 // Since this is an uncommon situation that should be avoided, use the
1109 // expensive isBeforeInTranslationUnit call.
1110 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1111 OrigLoc))
1112 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001113 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001114 }
1115}
1116
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001117/// \brief Wrapper around a case statement checking if AttrName is
1118/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001119bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001120 return llvm::StringSwitch<bool>(AttrName)
1121 .Case("guarded_by", true)
1122 .Case("guarded_var", true)
1123 .Case("pt_guarded_by", true)
1124 .Case("pt_guarded_var", true)
1125 .Case("lockable", true)
1126 .Case("scoped_lockable", true)
1127 .Case("no_thread_safety_analysis", true)
1128 .Case("acquired_after", true)
1129 .Case("acquired_before", true)
1130 .Case("exclusive_lock_function", true)
1131 .Case("shared_lock_function", true)
1132 .Case("exclusive_trylock_function", true)
1133 .Case("shared_trylock_function", true)
1134 .Case("unlock_function", true)
1135 .Case("lock_returned", true)
1136 .Case("locks_excluded", true)
1137 .Case("exclusive_locks_required", true)
1138 .Case("shared_locks_required", true)
1139 .Default(false);
1140}
1141
1142/// \brief Parse the contents of thread safety attributes. These
1143/// should always be parsed as an expression list.
1144///
1145/// We need to special case the parsing due to the fact that if the first token
1146/// of the first argument is an identifier, the main parse loop will store
1147/// that token as a "parameter" and the rest of
1148/// the arguments will be added to a list of "arguments". However,
1149/// subsequent tokens in the first argument are lost. We instead parse each
1150/// argument as an expression and add all arguments to the list of "arguments".
1151/// In future, we will take advantage of this special case to also
1152/// deal with some argument scoping issues here (for example, referring to a
1153/// function parameter in the attribute on that function).
1154void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1155 SourceLocation AttrNameLoc,
1156 ParsedAttributes &Attrs,
1157 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001158 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001159
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001160 BalancedDelimiterTracker T(*this, tok::l_paren);
1161 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001162
Benjamin Kramerf0623432012-08-23 22:51:59 +00001163 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001164 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001165
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001166 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001167 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001168 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001169 ExprResult ArgExpr(ParseAssignmentExpression());
1170 if (ArgExpr.isInvalid()) {
1171 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001172 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001173 break;
1174 } else {
1175 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001176 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001177 if (Tok.isNot(tok::comma))
1178 break;
1179 ConsumeToken(); // Eat the comma, move to the next argument
1180 }
1181 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001182 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001183 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001184 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001185 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001186 if (EndLoc)
1187 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001188}
1189
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001190void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1191 SourceLocation AttrNameLoc,
1192 ParsedAttributes &Attrs,
1193 SourceLocation *EndLoc) {
1194 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1195
1196 BalancedDelimiterTracker T(*this, tok::l_paren);
1197 T.consumeOpen();
1198
1199 if (Tok.isNot(tok::identifier)) {
1200 Diag(Tok, diag::err_expected_ident);
1201 T.skipToEnd();
1202 return;
1203 }
1204 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1205 SourceLocation ArgumentKindLoc = ConsumeToken();
1206
1207 if (Tok.isNot(tok::comma)) {
1208 Diag(Tok, diag::err_expected_comma);
1209 T.skipToEnd();
1210 return;
1211 }
1212 ConsumeToken();
1213
1214 SourceRange MatchingCTypeRange;
1215 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1216 if (MatchingCType.isInvalid()) {
1217 T.skipToEnd();
1218 return;
1219 }
1220
1221 bool LayoutCompatible = false;
1222 bool MustBeNull = false;
1223 while (Tok.is(tok::comma)) {
1224 ConsumeToken();
1225 if (Tok.isNot(tok::identifier)) {
1226 Diag(Tok, diag::err_expected_ident);
1227 T.skipToEnd();
1228 return;
1229 }
1230 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1231 if (Flag->isStr("layout_compatible"))
1232 LayoutCompatible = true;
1233 else if (Flag->isStr("must_be_null"))
1234 MustBeNull = true;
1235 else {
1236 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1237 T.skipToEnd();
1238 return;
1239 }
1240 ConsumeToken(); // consume flag
1241 }
1242
1243 if (!T.consumeClose()) {
1244 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1245 ArgumentKind, ArgumentKindLoc,
1246 MatchingCType.release(), LayoutCompatible,
1247 MustBeNull, AttributeList::AS_GNU);
1248 }
1249
1250 if (EndLoc)
1251 *EndLoc = T.getCloseLocation();
1252}
1253
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001254/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1255/// of a C++11 attribute-specifier in a location where an attribute is not
1256/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1257/// situation.
1258///
1259/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1260/// this doesn't appear to actually be an attribute-specifier, and the caller
1261/// should try to parse it.
1262bool Parser::DiagnoseProhibitedCXX11Attribute() {
1263 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1264
1265 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1266 case CAK_NotAttributeSpecifier:
1267 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1268 return false;
1269
1270 case CAK_InvalidAttributeSpecifier:
1271 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1272 return false;
1273
1274 case CAK_AttributeSpecifier:
1275 // Parse and discard the attributes.
1276 SourceLocation BeginLoc = ConsumeBracket();
1277 ConsumeBracket();
1278 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1279 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1280 SourceLocation EndLoc = ConsumeBracket();
1281 Diag(BeginLoc, diag::err_attributes_not_allowed)
1282 << SourceRange(BeginLoc, EndLoc);
1283 return true;
1284 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001285 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001286}
1287
Richard Smith98155ad2013-02-20 01:17:14 +00001288/// \brief We have found the opening square brackets of a C++11
1289/// attribute-specifier in a location where an attribute is not permitted, but
1290/// we know where the attributes ought to be written. Parse them anyway, and
1291/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001292void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1293 SourceLocation CorrectLocation) {
1294 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1295 Tok.is(tok::kw_alignas));
1296
1297 // Consume the attributes.
1298 SourceLocation Loc = Tok.getLocation();
1299 ParseCXX11Attributes(Attrs);
1300 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1301
1302 Diag(Loc, diag::err_attributes_not_allowed)
1303 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1304 << FixItHint::CreateRemoval(AttrRange);
1305}
1306
John McCall53fa7142010-12-24 02:08:15 +00001307void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1308 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1309 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001310}
1311
Michael Han64536a62012-11-06 19:34:54 +00001312void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1313 AttributeList *AttrList = attrs.getList();
1314 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001315 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001316 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001317 << AttrList->getName();
1318 AttrList->setInvalid();
1319 }
1320 AttrList = AttrList->getNext();
1321 }
1322}
1323
Chris Lattner53361ac2006-08-10 05:19:57 +00001324/// ParseDeclaration - Parse a full 'declaration', which consists of
1325/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001326/// 'Context' should be a Declarator::TheContext value. This returns the
1327/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001328///
1329/// declaration: [C99 6.7]
1330/// block-declaration ->
1331/// simple-declaration
1332/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001333/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001334/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001335/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001336/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001337/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001338/// others... [FIXME]
1339///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001340Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1341 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001342 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001343 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001344 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001345 // Must temporarily exit the objective-c container scope for
1346 // parsing c none objective-c decls.
1347 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001348
John McCall48871652010-08-21 09:40:31 +00001349 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001350 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001351 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001352 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001353 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001354 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001355 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001356 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001357 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001358 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001359 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001360 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001361 SourceLocation InlineLoc = ConsumeToken();
1362 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1363 break;
1364 }
Chad Rosierc1183952012-06-26 22:30:43 +00001365 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001366 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001367 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001368 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001369 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001370 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001371 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001372 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001373 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001374 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001375 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001376 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001377 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001378 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001379 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001380 default:
John McCall53fa7142010-12-24 02:08:15 +00001381 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001382 }
Chad Rosierc1183952012-06-26 22:30:43 +00001383
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001384 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001385 // single decl, convert it now. Alias declarations can also declare a type;
1386 // include that too if it is present.
1387 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001388}
1389
1390/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1391/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001392/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1393/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001394///[C90/C++]init-declarator-list ';' [TODO]
1395/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001396///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001397/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001398/// attribute-specifier-seq[opt] type-specifier-seq declarator
1399///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001400/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001401/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001402///
1403/// If FRI is non-null, we might be parsing a for-range-declaration instead
1404/// of a simple-declaration. If we find that we are, we also parse the
1405/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001406Parser::DeclGroupPtrTy
1407Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1408 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001409 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001410 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001411 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001412 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001413
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001414 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001415 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001416
Chris Lattner0e894622006-08-13 19:58:17 +00001417 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1418 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001419 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001420 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001421 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001422 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001423 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001424 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001425 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001426 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001427 }
Chad Rosierc1183952012-06-26 22:30:43 +00001428
Richard Smith2386c8b2013-02-22 09:06:26 +00001429 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001430 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001431}
Mike Stump11289f42009-09-09 15:08:12 +00001432
Richard Smith09f76ee2011-10-19 21:33:05 +00001433/// Returns true if this might be the start of a declarator, or a common typo
1434/// for a declarator.
1435bool Parser::MightBeDeclarator(unsigned Context) {
1436 switch (Tok.getKind()) {
1437 case tok::annot_cxxscope:
1438 case tok::annot_template_id:
1439 case tok::caret:
1440 case tok::code_completion:
1441 case tok::coloncolon:
1442 case tok::ellipsis:
1443 case tok::kw___attribute:
1444 case tok::kw_operator:
1445 case tok::l_paren:
1446 case tok::star:
1447 return true;
1448
1449 case tok::amp:
1450 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001451 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001452
Richard Smithc8a79032012-01-09 22:31:44 +00001453 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001454 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001455 NextToken().is(tok::l_square);
1456
1457 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001458 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001459
Richard Smith09f76ee2011-10-19 21:33:05 +00001460 case tok::identifier:
1461 switch (NextToken().getKind()) {
1462 case tok::code_completion:
1463 case tok::coloncolon:
1464 case tok::comma:
1465 case tok::equal:
1466 case tok::equalequal: // Might be a typo for '='.
1467 case tok::kw_alignas:
1468 case tok::kw_asm:
1469 case tok::kw___attribute:
1470 case tok::l_brace:
1471 case tok::l_paren:
1472 case tok::l_square:
1473 case tok::less:
1474 case tok::r_brace:
1475 case tok::r_paren:
1476 case tok::r_square:
1477 case tok::semi:
1478 return true;
1479
1480 case tok::colon:
1481 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001482 // and in block scope it's probably a label. Inside a class definition,
1483 // this is a bit-field.
1484 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001485 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001486
1487 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001488 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001489
1490 default:
1491 return false;
1492 }
1493
1494 default:
1495 return false;
1496 }
1497}
1498
Richard Smithb8caac82012-04-11 20:59:20 +00001499/// Skip until we reach something which seems like a sensible place to pick
1500/// up parsing after a malformed declaration. This will sometimes stop sooner
1501/// than SkipUntil(tok::r_brace) would, but will never stop later.
1502void Parser::SkipMalformedDecl() {
1503 while (true) {
1504 switch (Tok.getKind()) {
1505 case tok::l_brace:
1506 // Skip until matching }, then stop. We've probably skipped over
1507 // a malformed class or function definition or similar.
1508 ConsumeBrace();
1509 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1510 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1511 // This declaration isn't over yet. Keep skipping.
1512 continue;
1513 }
1514 if (Tok.is(tok::semi))
1515 ConsumeToken();
1516 return;
1517
1518 case tok::l_square:
1519 ConsumeBracket();
1520 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1521 continue;
1522
1523 case tok::l_paren:
1524 ConsumeParen();
1525 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1526 continue;
1527
1528 case tok::r_brace:
1529 return;
1530
1531 case tok::semi:
1532 ConsumeToken();
1533 return;
1534
1535 case tok::kw_inline:
1536 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001537 // a good place to pick back up parsing, except in an Objective-C
1538 // @interface context.
1539 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1540 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001541 return;
1542 break;
1543
1544 case tok::kw_namespace:
1545 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001546 // place to pick back up parsing, except in an Objective-C
1547 // @interface context.
1548 if (Tok.isAtStartOfLine() &&
1549 (!ParsingInObjCContainer || CurParsedObjCImpl))
1550 return;
1551 break;
1552
1553 case tok::at:
1554 // @end is very much like } in Objective-C contexts.
1555 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1556 ParsingInObjCContainer)
1557 return;
1558 break;
1559
1560 case tok::minus:
1561 case tok::plus:
1562 // - and + probably start new method declarations in Objective-C contexts.
1563 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001564 return;
1565 break;
1566
1567 case tok::eof:
1568 return;
1569
1570 default:
1571 break;
1572 }
1573
1574 ConsumeAnyToken();
1575 }
1576}
1577
John McCalld5a36322009-11-03 19:26:08 +00001578/// ParseDeclGroup - Having concluded that this is either a function
1579/// definition or a group of object declarations, actually parse the
1580/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001581Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1582 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001583 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001584 SourceLocation *DeclEnd,
1585 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001586 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001587 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001588 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001589
John McCalld5a36322009-11-03 19:26:08 +00001590 // Bail out if the first declarator didn't seem well-formed.
1591 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001592 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001593 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001596 // Save late-parsed attributes for now; they need to be parsed in the
1597 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001598 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1599 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001600 if (D.isFunctionDeclarator())
1601 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1602
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001603 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001604 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001605 // Look at the next token to make sure that this isn't a function
1606 // declaration. We have to check this because __attribute__ might be the
1607 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001608 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001609
Douglas Gregor012efe22013-04-16 16:01:32 +00001610 if (AllowFunctionDefinitions) {
1611 if (isStartOfFunctionDefinition(D)) {
1612 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1613 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001614
Douglas Gregor012efe22013-04-16 16:01:32 +00001615 // Recover by treating the 'typedef' as spurious.
1616 DS.ClearStorageClassSpecs();
1617 }
1618
1619 Decl *TheDecl =
1620 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1621 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001622 }
1623
Douglas Gregor012efe22013-04-16 16:01:32 +00001624 if (isDeclarationSpecifier()) {
1625 // If there is an invalid declaration specifier right after the function
1626 // prototype, then we must be in a missing semicolon case where this isn't
1627 // actually a body. Just fall through into the code that handles it as a
1628 // prototype, and let the top-level code handle the erroneous declspec
1629 // where it would otherwise expect a comma or semicolon.
1630 } else {
1631 Diag(Tok, diag::err_expected_fn_body);
1632 SkipUntil(tok::semi);
1633 return DeclGroupPtrTy();
1634 }
John McCalld5a36322009-11-03 19:26:08 +00001635 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001636 if (Tok.is(tok::l_brace)) {
1637 Diag(Tok, diag::err_function_definition_not_allowed);
1638 SkipUntil(tok::r_brace, true, true);
1639 }
John McCalld5a36322009-11-03 19:26:08 +00001640 }
1641 }
1642
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001643 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001644 return DeclGroupPtrTy();
1645
1646 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1647 // must parse and analyze the for-range-initializer before the declaration is
1648 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001649 //
1650 // Handle the Objective-C for-in loop variable similarly, although we
1651 // don't need to parse the container in advance.
1652 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1653 bool IsForRangeLoop = false;
1654 if (Tok.is(tok::colon)) {
1655 IsForRangeLoop = true;
1656 FRI->ColonLoc = ConsumeToken();
1657 if (Tok.is(tok::l_brace))
1658 FRI->RangeExpr = ParseBraceInitializer();
1659 else
1660 FRI->RangeExpr = ParseExpression();
1661 }
1662
Richard Smith02e85f32011-04-14 22:09:26 +00001663 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001664 if (IsForRangeLoop)
1665 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001666 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001667 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001668 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1669 }
1670
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001671 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001672 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001673 if (LateParsedAttrs.size() > 0)
1674 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001675 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001676 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001677 DeclsInGroup.push_back(FirstDecl);
1678
Richard Smith09f76ee2011-10-19 21:33:05 +00001679 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001680
John McCalld5a36322009-11-03 19:26:08 +00001681 // If we don't have a comma, it is either the end of the list (a ';') or an
1682 // error, bail out.
1683 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001684 SourceLocation CommaLoc = ConsumeToken();
1685
1686 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1687 // This comma was followed by a line-break and something which can't be
1688 // the start of a declarator. The comma was probably a typo for a
1689 // semicolon.
1690 Diag(CommaLoc, diag::err_expected_semi_declaration)
1691 << FixItHint::CreateReplacement(CommaLoc, ";");
1692 ExpectSemi = false;
1693 break;
1694 }
John McCalld5a36322009-11-03 19:26:08 +00001695
1696 // Parse the next declarator.
1697 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001698 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001699
1700 // Accept attributes in an init-declarator. In the first declarator in a
1701 // declaration, these would be part of the declspec. In subsequent
1702 // declarators, they become part of the declarator itself, so that they
1703 // don't apply to declarators after *this* one. Examples:
1704 // short __attribute__((common)) var; -> declspec
1705 // short var __attribute__((common)); -> declarator
1706 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001707 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001708
1709 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001710 if (!D.isInvalidType()) {
1711 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1712 D.complete(ThisDecl);
1713 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001714 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001715 }
John McCalld5a36322009-11-03 19:26:08 +00001716 }
1717
1718 if (DeclEnd)
1719 *DeclEnd = Tok.getLocation();
1720
Richard Smith09f76ee2011-10-19 21:33:05 +00001721 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001722 ExpectAndConsumeSemi(Context == Declarator::FileContext
1723 ? diag::err_invalid_token_after_toplevel_declarator
1724 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001725 // Okay, there was no semicolon and one was expected. If we see a
1726 // declaration specifier, just assume it was missing and continue parsing.
1727 // Otherwise things are very confused and we skip to recover.
1728 if (!isDeclarationSpecifier()) {
1729 SkipUntil(tok::r_brace, true, true);
1730 if (Tok.is(tok::semi))
1731 ConsumeToken();
1732 }
John McCalld5a36322009-11-03 19:26:08 +00001733 }
1734
Douglas Gregor0be31a22010-07-02 17:43:08 +00001735 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001736 DeclsInGroup.data(),
1737 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001738}
1739
Richard Smith02e85f32011-04-14 22:09:26 +00001740/// Parse an optional simple-asm-expr and attributes, and attach them to a
1741/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001742bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001743 // If a simple-asm-expr is present, parse it.
1744 if (Tok.is(tok::kw_asm)) {
1745 SourceLocation Loc;
1746 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1747 if (AsmLabel.isInvalid()) {
1748 SkipUntil(tok::semi, true, true);
1749 return true;
1750 }
1751
1752 D.setAsmLabel(AsmLabel.release());
1753 D.SetRangeEnd(Loc);
1754 }
1755
1756 MaybeParseGNUAttributes(D);
1757 return false;
1758}
1759
Douglas Gregor23996282009-05-12 21:31:51 +00001760/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1761/// declarator'. This method parses the remainder of the declaration
1762/// (including any attributes or initializer, among other things) and
1763/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001764///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001765/// init-declarator: [C99 6.7]
1766/// declarator
1767/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001768/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1769/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001770/// [C++] declarator initializer[opt]
1771///
1772/// [C++] initializer:
1773/// [C++] '=' initializer-clause
1774/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001775/// [C++0x] '=' 'default' [TODO]
1776/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001777/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001778///
1779/// According to the standard grammar, =default and =delete are function
1780/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001781///
John McCall48871652010-08-21 09:40:31 +00001782Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001783 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001784 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001785 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001786
Richard Smith02e85f32011-04-14 22:09:26 +00001787 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1788}
Mike Stump11289f42009-09-09 15:08:12 +00001789
Richard Smith02e85f32011-04-14 22:09:26 +00001790Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1791 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001792 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001793 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001794 switch (TemplateInfo.Kind) {
1795 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001796 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001797 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001798
Douglas Gregor450f00842009-09-25 18:43:00 +00001799 case ParsedTemplateInfo::Template:
1800 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001801 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001802 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001803 D);
1804 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001805
Douglas Gregor450f00842009-09-25 18:43:00 +00001806 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001807 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001808 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001809 TemplateInfo.ExternLoc,
1810 TemplateInfo.TemplateLoc,
1811 D);
1812 if (ThisRes.isInvalid()) {
1813 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001814 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001815 }
Chad Rosierc1183952012-06-26 22:30:43 +00001816
Douglas Gregor450f00842009-09-25 18:43:00 +00001817 ThisDecl = ThisRes.get();
1818 break;
1819 }
1820 }
Mike Stump11289f42009-09-09 15:08:12 +00001821
Richard Smith74aeef52013-04-26 16:15:35 +00001822 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001823
Douglas Gregor23996282009-05-12 21:31:51 +00001824 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001825 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001826 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001827 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001828 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001829 if (D.isFunctionDeclarator())
1830 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1831 << 1 /* delete */;
1832 else
1833 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001834 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001835 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001836 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1837 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001838 else
1839 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001840 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001841 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001842 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001843 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001844 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001845
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001846 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001847 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001848 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001849 cutOffParsing();
1850 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001851 }
Chad Rosierc1183952012-06-26 22:30:43 +00001852
John McCalldadc5752010-08-24 06:29:42 +00001853 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001854
David Blaikiebbafb8a2012-03-11 07:00:24 +00001855 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001856 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001857 ExitScope();
1858 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001859
Douglas Gregor23996282009-05-12 21:31:51 +00001860 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001861 SkipUntil(tok::comma, true, true);
1862 Actions.ActOnInitializerError(ThisDecl);
1863 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001864 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1865 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001866 }
1867 } else if (Tok.is(tok::l_paren)) {
1868 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001869 BalancedDelimiterTracker T(*this, tok::l_paren);
1870 T.consumeOpen();
1871
Benjamin Kramerf0623432012-08-23 22:51:59 +00001872 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001873 CommaLocsTy CommaLocs;
1874
David Blaikiebbafb8a2012-03-11 07:00:24 +00001875 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001876 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001877 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001878 }
1879
Douglas Gregor23996282009-05-12 21:31:51 +00001880 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001881 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001882 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001883
David Blaikiebbafb8a2012-03-11 07:00:24 +00001884 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001885 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001886 ExitScope();
1887 }
Douglas Gregor23996282009-05-12 21:31:51 +00001888 } else {
1889 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001890 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001891
1892 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1893 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001894
David Blaikiebbafb8a2012-03-11 07:00:24 +00001895 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001896 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001897 ExitScope();
1898 }
1899
Sebastian Redla9351792012-02-11 23:51:47 +00001900 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1901 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001902 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001903 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1904 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001905 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001906 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001907 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001908 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001909 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1910
Sebastian Redl3da34892011-06-05 12:23:16 +00001911 if (D.getCXXScopeSpec().isSet()) {
1912 EnterScope(0);
1913 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1914 }
1915
1916 ExprResult Init(ParseBraceInitializer());
1917
1918 if (D.getCXXScopeSpec().isSet()) {
1919 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1920 ExitScope();
1921 }
1922
1923 if (Init.isInvalid()) {
1924 Actions.ActOnInitializerError(ThisDecl);
1925 } else
1926 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1927 /*DirectInit=*/true, TypeContainsAuto);
1928
Douglas Gregor23996282009-05-12 21:31:51 +00001929 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001930 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001931 }
1932
Richard Smithb2bc2e62011-02-21 20:05:19 +00001933 Actions.FinalizeDeclaration(ThisDecl);
1934
Douglas Gregor23996282009-05-12 21:31:51 +00001935 return ThisDecl;
1936}
1937
Chris Lattner1890ac82006-08-13 01:16:23 +00001938/// ParseSpecifierQualifierList
1939/// specifier-qualifier-list:
1940/// type-specifier specifier-qualifier-list[opt]
1941/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001942/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001943///
Richard Smithc5b05522012-03-12 07:56:15 +00001944void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1945 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001946 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1947 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001948 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001949 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001950
Chris Lattner1890ac82006-08-13 01:16:23 +00001951 // Validate declspec for type-name.
1952 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001953 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1954 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001955 Diag(Tok, diag::err_expected_type);
1956 DS.SetTypeSpecError();
1957 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1958 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001959 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001960 if (!DS.hasTypeSpecifier())
1961 DS.SetTypeSpecError();
1962 }
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner1b22eed2006-11-28 05:12:07 +00001964 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001965 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001966 if (DS.getStorageClassSpecLoc().isValid())
1967 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1968 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001969 Diag(DS.getThreadStorageClassSpecLoc(),
1970 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001971 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001972 }
Mike Stump11289f42009-09-09 15:08:12 +00001973
Chris Lattner1b22eed2006-11-28 05:12:07 +00001974 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001975 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001976 if (DS.isInlineSpecified())
1977 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1978 if (DS.isVirtualSpecified())
1979 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1980 if (DS.isExplicitSpecified())
1981 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001982 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001983 }
Richard Smithc5b05522012-03-12 07:56:15 +00001984
1985 // Issue diagnostic and remove constexpr specfier if present.
1986 if (DS.isConstexprSpecified()) {
1987 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1988 DS.ClearConstexprSpec();
1989 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001990}
Chris Lattner53361ac2006-08-10 05:19:57 +00001991
Chris Lattner6cc055a2009-04-12 20:42:31 +00001992/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1993/// specified token is valid after the identifier in a declarator which
1994/// immediately follows the declspec. For example, these things are valid:
1995///
1996/// int x [ 4]; // direct-declarator
1997/// int x ( int y); // direct-declarator
1998/// int(int x ) // direct-declarator
1999/// int x ; // simple-declaration
2000/// int x = 17; // init-declarator-list
2001/// int x , y; // init-declarator-list
2002/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002003/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002004/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002005///
2006/// This is not, because 'x' does not immediately follow the declspec (though
2007/// ')' happens to be valid anyway).
2008/// int (x)
2009///
2010static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2011 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2012 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002013 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002014}
2015
Chris Lattner20a0c612009-04-14 21:34:55 +00002016
2017/// ParseImplicitInt - This method is called when we have an non-typename
2018/// identifier in a declspec (which normally terminates the decl spec) when
2019/// the declspec has no type specifier. In this case, the declspec is either
2020/// malformed or is "implicit int" (in K&R and C89).
2021///
2022/// This method handles diagnosing this prettily and returns false if the
2023/// declspec is done being processed. If it recovers and thinks there may be
2024/// other pieces of declspec after it, it returns true.
2025///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002026bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002027 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002028 AccessSpecifier AS, DeclSpecContext DSC,
2029 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002030 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002031
Chris Lattner20a0c612009-04-14 21:34:55 +00002032 SourceLocation Loc = Tok.getLocation();
2033 // If we see an identifier that is not a type name, we normally would
2034 // parse it as the identifer being declared. However, when a typename
2035 // is typo'd or the definition is not included, this will incorrectly
2036 // parse the typename as the identifier name and fall over misparsing
2037 // later parts of the diagnostic.
2038 //
2039 // As such, we try to do some look-ahead in cases where this would
2040 // otherwise be an "implicit-int" case to see if this is invalid. For
2041 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2042 // an identifier with implicit int, we'd get a parse error because the
2043 // next token is obviously invalid for a type. Parse these as a case
2044 // with an invalid type specifier.
2045 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002046
Chris Lattner20a0c612009-04-14 21:34:55 +00002047 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002048 // error, do lookahead to try to do better recovery. This never applies
2049 // within a type specifier. Outside of C++, we allow this even if the
2050 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002051 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002052 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002053 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002054 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002055 // If this token is valid for implicit int, e.g. "static x = 4", then
2056 // we just avoid eating the identifier, so it will be parsed as the
2057 // identifier in the declarator.
2058 return false;
2059 }
Mike Stump11289f42009-09-09 15:08:12 +00002060
Richard Smitha952ebb2012-05-15 21:01:51 +00002061 if (getLangOpts().CPlusPlus &&
2062 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2063 // Don't require a type specifier if we have the 'auto' storage class
2064 // specifier in C++98 -- we'll promote it to a type specifier.
2065 return false;
2066 }
2067
Chris Lattner20a0c612009-04-14 21:34:55 +00002068 // Otherwise, if we don't consume this token, we are going to emit an
2069 // error anyway. Try to recover from various common problems. Check
2070 // to see if this was a reference to a tag name without a tag specified.
2071 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002072 //
2073 // C++ doesn't need this, and isTagName doesn't take SS.
2074 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002075 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002076 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002077
Douglas Gregor0be31a22010-07-02 17:43:08 +00002078 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002079 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002080 case DeclSpec::TST_enum:
2081 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2082 case DeclSpec::TST_union:
2083 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2084 case DeclSpec::TST_struct:
2085 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002086 case DeclSpec::TST_interface:
2087 TagName="__interface"; FixitTagName = "__interface ";
2088 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002089 case DeclSpec::TST_class:
2090 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002091 }
Mike Stump11289f42009-09-09 15:08:12 +00002092
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002093 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002094 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2095 LookupResult R(Actions, TokenName, SourceLocation(),
2096 Sema::LookupOrdinaryName);
2097
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002098 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002099 << TokenName << TagName << getLangOpts().CPlusPlus
2100 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2101
2102 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2103 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2104 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002105 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002106 << TokenName << TagName;
2107 }
Mike Stump11289f42009-09-09 15:08:12 +00002108
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002109 // Parse this as a tag as if the missing tag were present.
2110 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002111 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002112 else
Richard Smithc5b05522012-03-12 07:56:15 +00002113 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002114 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002115 return true;
2116 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002117 }
Mike Stump11289f42009-09-09 15:08:12 +00002118
Richard Smithfe904f02012-05-15 21:29:55 +00002119 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002120 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002121 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2122 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002123 // Look ahead to the next token to try to figure out what this declaration
2124 // was supposed to be.
2125 switch (NextToken().getKind()) {
2126 case tok::comma:
2127 case tok::equal:
2128 case tok::kw_asm:
2129 case tok::l_brace:
2130 case tok::l_square:
2131 case tok::semi:
2132 // This looks like a variable declaration. The type is probably missing.
2133 // We're done parsing decl-specifiers.
2134 return false;
2135
2136 case tok::l_paren: {
2137 // static x(4); // 'x' is not a type
2138 // x(int n); // 'x' is not a type
2139 // x (*p)[]; // 'x' is a type
2140 //
2141 // Since we're in an error case (or the rare 'implicit int in C++' MS
2142 // extension), we can afford to perform a tentative parse to determine
2143 // which case we're in.
2144 TentativeParsingAction PA(*this);
2145 ConsumeToken();
2146 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2147 PA.Revert();
2148 if (TPR == TPResult::False())
2149 return false;
2150 // The identifier is followed by a parenthesized declarator.
2151 // It's supposed to be a type.
2152 break;
2153 }
2154
2155 default:
2156 // This is probably supposed to be a type. This includes cases like:
2157 // int f(itn);
2158 // struct S { unsinged : 4; };
2159 break;
2160 }
2161 }
2162
Chad Rosierc1183952012-06-26 22:30:43 +00002163 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002164 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002165 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002166 IdentifierInfo *II = Tok.getIdentifierInfo();
2167 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002168 // The action emitted a diagnostic, so we don't have to.
2169 if (T) {
2170 // The action has suggested that the type T could be used. Set that as
2171 // the type in the declaration specifiers, consume the would-be type
2172 // name token, and we're done.
2173 const char *PrevSpec;
2174 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002175 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002176 DS.SetRangeEnd(Tok.getLocation());
2177 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002178 // There may be other declaration specifiers after this.
2179 return true;
2180 } else if (II != Tok.getIdentifierInfo()) {
2181 // If no type was suggested, the correction is to a keyword
2182 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002183 // There may be other declaration specifiers after this.
2184 return true;
2185 }
Chad Rosierc1183952012-06-26 22:30:43 +00002186
Douglas Gregor15e56022009-10-13 23:27:22 +00002187 // Fall through; the action had no suggestion for us.
2188 } else {
2189 // The action did not emit a diagnostic, so emit one now.
2190 SourceRange R;
2191 if (SS) R = SS->getRange();
2192 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2193 }
Mike Stump11289f42009-09-09 15:08:12 +00002194
Douglas Gregor15e56022009-10-13 23:27:22 +00002195 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002196 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002197 DS.SetRangeEnd(Tok.getLocation());
2198 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002199
Chris Lattner20a0c612009-04-14 21:34:55 +00002200 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2201 // avoid rippling error messages on subsequent uses of the same type,
2202 // could be useful if #include was forgotten.
2203 return false;
2204}
2205
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002206/// \brief Determine the declaration specifier context from the declarator
2207/// context.
2208///
2209/// \param Context the declarator context, which is one of the
2210/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002211Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002212Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2213 if (Context == Declarator::MemberContext)
2214 return DSC_class;
2215 if (Context == Declarator::FileContext)
2216 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002217 if (Context == Declarator::TrailingReturnContext)
2218 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002219 return DSC_normal;
2220}
2221
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002222/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2223///
2224/// FIXME: Simply returns an alignof() expression if the argument is a
2225/// type. Ideally, the type should be propagated directly into Sema.
2226///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002227/// [C11] type-id
2228/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002229/// [C++0x] type-id ...[opt]
2230/// [C++0x] assignment-expression ...[opt]
2231ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2232 SourceLocation &EllipsisLoc) {
2233 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002234 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002235 SourceLocation TypeLoc = Tok.getLocation();
2236 ParsedType Ty = ParseTypeName().get();
2237 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002238 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2239 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002240 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002241 ER = ParseConstantExpression();
2242
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002243 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002244 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002245
2246 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002247}
2248
2249/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2250/// attribute to Attrs.
2251///
2252/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002253/// [C11] '_Alignas' '(' type-id ')'
2254/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002255/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2256/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002257void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002258 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002259 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2260 "Not an alignment-specifier!");
2261
Richard Smithd11c7a12013-01-29 01:48:07 +00002262 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2263 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002264
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002265 BalancedDelimiterTracker T(*this, tok::l_paren);
2266 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002267 return;
2268
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002269 SourceLocation EllipsisLoc;
2270 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002271 if (ArgExpr.isInvalid()) {
2272 SkipUntil(tok::r_paren);
2273 return;
2274 }
2275
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002276 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002277 if (EndLoc)
2278 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002279
Benjamin Kramerf0623432012-08-23 22:51:59 +00002280 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002281 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002282 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smith44c247f2013-02-22 08:32:16 +00002283 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002284}
2285
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002286/// ParseDeclarationSpecifiers
2287/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002288/// storage-class-specifier declaration-specifiers[opt]
2289/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002290/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002291/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002292/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002293/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002294///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002295/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002296/// 'typedef'
2297/// 'extern'
2298/// 'static'
2299/// 'auto'
2300/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002301/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002302/// [C++11] 'thread_local'
2303/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002304/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002305/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002306/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002307/// [C++] 'virtual'
2308/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002309/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002310/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002311/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002312
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002313///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002314void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002315 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002316 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002317 DeclSpecContext DSContext,
2318 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002319 if (DS.getSourceRange().isInvalid()) {
2320 DS.SetRangeStart(Tok.getLocation());
2321 DS.SetRangeEnd(Tok.getLocation());
2322 }
Chad Rosierc1183952012-06-26 22:30:43 +00002323
Douglas Gregordf593fb2011-11-07 17:33:42 +00002324 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002325 bool AttrsLastTime = false;
2326 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002327 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002328 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002329 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002330 unsigned DiagID = 0;
2331
Chris Lattner4d8f8732006-11-28 05:05:08 +00002332 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002333
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002334 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002335 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002336 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002337 if (!AttrsLastTime)
2338 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002339 else {
2340 // Reject C++11 attributes that appertain to decl specifiers as
2341 // we don't support any C++11 attributes that appertain to decl
2342 // specifiers. This also conforms to what g++ 4.8 is doing.
2343 ProhibitCXX11Attributes(attrs);
2344
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002345 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002346 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002347
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002348 // If this is not a declaration specifier token, we're done reading decl
2349 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002350 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002351 return;
Mike Stump11289f42009-09-09 15:08:12 +00002352
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002353 case tok::l_square:
2354 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002355 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002356 goto DoneWithDeclSpec;
2357
2358 ProhibitAttributes(attrs);
2359 // FIXME: It would be good to recover by accepting the attributes,
2360 // but attempting to do that now would cause serious
2361 // madness in terms of diagnostics.
2362 attrs.clear();
2363 attrs.Range = SourceRange();
2364
2365 ParseCXX11Attributes(attrs);
2366 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002367 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002368
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002369 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002370 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002371 if (DS.hasTypeSpecifier()) {
2372 bool AllowNonIdentifiers
2373 = (getCurScope()->getFlags() & (Scope::ControlScope |
2374 Scope::BlockScope |
2375 Scope::TemplateParamScope |
2376 Scope::FunctionPrototypeScope |
2377 Scope::AtCatchScope)) == 0;
2378 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002379 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002380 (DSContext == DSC_class && DS.isFriendSpecified());
2381
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002382 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002383 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002384 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002385 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002386 }
2387
Douglas Gregor80039242011-02-15 20:33:25 +00002388 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2389 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2390 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002391 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002392 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002393 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002394 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002395 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002396 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002397
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002398 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002399 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002400 }
2401
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002402 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002403 // C++ scope specifier. Annotate and loop, or bail out on error.
2404 if (TryAnnotateCXXScopeToken(true)) {
2405 if (!DS.hasTypeSpecifier())
2406 DS.SetTypeSpecError();
2407 goto DoneWithDeclSpec;
2408 }
John McCall8bc2a702010-03-01 18:20:46 +00002409 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2410 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002411 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002412
2413 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002414 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002415 goto DoneWithDeclSpec;
2416
John McCall9dab4e62009-12-12 11:40:51 +00002417 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002418 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2419 Tok.getAnnotationRange(),
2420 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002421
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002422 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002423 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002424 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002425 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002426 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002427 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002428
2429 // C++ [class.qual]p2:
2430 // In a lookup in which the constructor is an acceptable lookup
2431 // result and the nested-name-specifier nominates a class C:
2432 //
2433 // - if the name specified after the
2434 // nested-name-specifier, when looked up in C, is the
2435 // injected-class-name of C (Clause 9), or
2436 //
2437 // - if the name specified after the nested-name-specifier
2438 // is the same as the identifier or the
2439 // simple-template-id's template-name in the last
2440 // component of the nested-name-specifier,
2441 //
2442 // the name is instead considered to name the constructor of
2443 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002444 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002445 // Thus, if the template-name is actually the constructor
2446 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002447 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002448 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002449 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002450 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002451 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002452 if (isConstructorDeclarator()) {
2453 // The user meant this to be an out-of-line constructor
2454 // definition, but template arguments are not allowed
2455 // there. Just allow this as a constructor; we'll
2456 // complain about it later.
2457 goto DoneWithDeclSpec;
2458 }
2459
2460 // The user meant this to name a type, but it actually names
2461 // a constructor with some extraneous template
2462 // arguments. Complain, then parse it as a type as the user
2463 // intended.
2464 Diag(TemplateId->TemplateNameLoc,
2465 diag::err_out_of_line_template_id_names_constructor)
2466 << TemplateId->Name;
2467 }
2468
John McCall9dab4e62009-12-12 11:40:51 +00002469 DS.getTypeSpecScope() = SS;
2470 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002471 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002472 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002473 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002474 continue;
2475 }
2476
Douglas Gregorc5790df2009-09-28 07:26:33 +00002477 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002478 DS.getTypeSpecScope() = SS;
2479 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002480 if (Tok.getAnnotationValue()) {
2481 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002483 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002484 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002485 if (isInvalid)
2486 break;
John McCallba7bf592010-08-24 05:47:05 +00002487 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002488 else
2489 DS.SetTypeSpecError();
2490 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2491 ConsumeToken(); // The typename
2492 }
2493
Douglas Gregor167fa622009-03-25 15:40:00 +00002494 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002495 goto DoneWithDeclSpec;
2496
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002497 // If we're in a context where the identifier could be a class name,
2498 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002499 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002500 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002501 &SS)) {
2502 if (isConstructorDeclarator())
2503 goto DoneWithDeclSpec;
2504
2505 // As noted in C++ [class.qual]p2 (cited above), when the name
2506 // of the class is qualified in a context where it could name
2507 // a constructor, its a constructor name. However, we've
2508 // looked at the declarator, and the user probably meant this
2509 // to be a type. Complain that it isn't supposed to be treated
2510 // as a type, then proceed to parse it as a type.
2511 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2512 << Next.getIdentifierInfo();
2513 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002514
John McCallba7bf592010-08-24 05:47:05 +00002515 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2516 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002517 getCurScope(), &SS,
2518 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002519 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002520 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002521
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002522 // If the referenced identifier is not a type, then this declspec is
2523 // erroneous: We already checked about that it has no type specifier, and
2524 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002525 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002526 if (TypeRep == 0) {
2527 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002528 ParsedAttributesWithRange Attrs(AttrFactory);
2529 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2530 if (!Attrs.empty()) {
2531 AttrsLastTime = true;
2532 attrs.takeAllFrom(Attrs);
2533 }
2534 continue;
2535 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002536 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002537 }
Mike Stump11289f42009-09-09 15:08:12 +00002538
John McCall9dab4e62009-12-12 11:40:51 +00002539 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002540 ConsumeToken(); // The C++ scope.
2541
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002543 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002544 if (isInvalid)
2545 break;
Mike Stump11289f42009-09-09 15:08:12 +00002546
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002547 DS.SetRangeEnd(Tok.getLocation());
2548 ConsumeToken(); // The typename.
2549
2550 continue;
2551 }
Mike Stump11289f42009-09-09 15:08:12 +00002552
Chris Lattnere387d9e2009-01-21 19:48:37 +00002553 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002554 if (Tok.getAnnotationValue()) {
2555 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002557 DiagID, T);
2558 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002559 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002560
Chris Lattner005fc1b2010-04-05 18:18:31 +00002561 if (isInvalid)
2562 break;
2563
Chris Lattnere387d9e2009-01-21 19:48:37 +00002564 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2565 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002566
Chris Lattnere387d9e2009-01-21 19:48:37 +00002567 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2568 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002569 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002570 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002571 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002572
Chris Lattnere387d9e2009-01-21 19:48:37 +00002573 continue;
2574 }
Mike Stump11289f42009-09-09 15:08:12 +00002575
Douglas Gregor06873092011-04-28 15:48:45 +00002576 case tok::kw___is_signed:
2577 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2578 // typically treats it as a trait. If we see __is_signed as it appears
2579 // in libstdc++, e.g.,
2580 //
2581 // static const bool __is_signed;
2582 //
2583 // then treat __is_signed as an identifier rather than as a keyword.
2584 if (DS.getTypeSpecType() == TST_bool &&
2585 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2586 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2587 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2588 Tok.setKind(tok::identifier);
2589 }
2590
2591 // We're done with the declaration-specifiers.
2592 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002593
Chris Lattner16fac4f2008-07-26 01:18:38 +00002594 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002595 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002596 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002597 // In C++, check to see if this is a scope specifier like foo::bar::, if
2598 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002599 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002600 if (TryAnnotateCXXScopeToken(true)) {
2601 if (!DS.hasTypeSpecifier())
2602 DS.SetTypeSpecError();
2603 goto DoneWithDeclSpec;
2604 }
2605 if (!Tok.is(tok::identifier))
2606 continue;
2607 }
Mike Stump11289f42009-09-09 15:08:12 +00002608
Chris Lattner16fac4f2008-07-26 01:18:38 +00002609 // This identifier can only be a typedef name if we haven't already seen
2610 // a type-specifier. Without this check we misparse:
2611 // typedef int X; struct Y { short X; }; as 'short int'.
2612 if (DS.hasTypeSpecifier())
2613 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002614
John Thompson22334602010-02-05 00:12:22 +00002615 // Check for need to substitute AltiVec keyword tokens.
2616 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2617 break;
2618
Richard Smith3092a3b2012-05-09 18:56:43 +00002619 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2620 // allow the use of a typedef name as a type specifier.
2621 if (DS.isTypeAltiVecVector())
2622 goto DoneWithDeclSpec;
2623
John McCallba7bf592010-08-24 05:47:05 +00002624 ParsedType TypeRep =
2625 Actions.getTypeName(*Tok.getIdentifierInfo(),
2626 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002627
Chris Lattner6cc055a2009-04-12 20:42:31 +00002628 // If this is not a typedef name, don't parse it as part of the declspec,
2629 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002630 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002631 ParsedAttributesWithRange Attrs(AttrFactory);
2632 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2633 if (!Attrs.empty()) {
2634 AttrsLastTime = true;
2635 attrs.takeAllFrom(Attrs);
2636 }
2637 continue;
2638 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002639 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002640 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002641
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002642 // If we're in a context where the identifier could be a class name,
2643 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002644 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002645 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002646 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002647 goto DoneWithDeclSpec;
2648
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002650 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002651 if (isInvalid)
2652 break;
Mike Stump11289f42009-09-09 15:08:12 +00002653
Chris Lattner16fac4f2008-07-26 01:18:38 +00002654 DS.SetRangeEnd(Tok.getLocation());
2655 ConsumeToken(); // The identifier
2656
2657 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2658 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002659 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002660 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002661 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002662
Steve Naroffcd5e7822008-09-22 10:28:57 +00002663 // Need to support trailing type qualifiers (e.g. "id<p> const").
2664 // If a type specifier follows, it will be diagnosed elsewhere.
2665 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002666 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002667
2668 // type-name
2669 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002670 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002671 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002672 // This template-id does not refer to a type name, so we're
2673 // done with the type-specifiers.
2674 goto DoneWithDeclSpec;
2675 }
2676
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002677 // If we're in a context where the template-id could be a
2678 // constructor name or specialization, check whether this is a
2679 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002680 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002681 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002682 isConstructorDeclarator())
2683 goto DoneWithDeclSpec;
2684
Douglas Gregor7f741122009-02-25 19:37:18 +00002685 // Turn the template-id annotation token into a type annotation
2686 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002687 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002688 continue;
2689 }
2690
Chris Lattnere37e2332006-08-15 04:50:22 +00002691 // GNU attributes support.
2692 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002693 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002694 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002695
2696 // Microsoft declspec support.
2697 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002698 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002699 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002700
Steve Naroff44ac7772008-12-25 14:16:32 +00002701 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002702 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002703 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002704 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002705 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002706 // FIXME: This does not work correctly if it is set to be a declspec
2707 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002708 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002709 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002710 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002711 }
Eli Friedman53339e02009-06-08 23:27:34 +00002712
2713 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002714 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002715 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002716 case tok::kw___cdecl:
2717 case tok::kw___stdcall:
2718 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002719 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002720 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002721 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002722 continue;
2723
Dawn Perchik335e16b2010-09-03 01:29:35 +00002724 // Borland single token adornments.
2725 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002726 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002727 continue;
2728
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002729 // OpenCL single token adornments.
2730 case tok::kw___kernel:
2731 ParseOpenCLAttributes(DS.getAttributes());
2732 continue;
2733
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002734 // storage-class-specifier
2735 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002736 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2737 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002738 break;
2739 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002740 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002741 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002742 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2743 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002744 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002745 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002746 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2747 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002748 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002749 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002750 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002751 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002752 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2753 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002754 break;
2755 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002756 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002757 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002758 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2759 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002760 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002761 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002762 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002763 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2765 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002766 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002767 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2768 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002769 break;
2770 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002771 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2772 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002773 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002774 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002775 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2776 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002777 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002778 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002779 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2780 PrevSpec, DiagID);
2781 break;
2782 case tok::kw_thread_local:
2783 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2784 PrevSpec, DiagID);
2785 break;
2786 case tok::kw__Thread_local:
2787 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2788 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002789 break;
Mike Stump11289f42009-09-09 15:08:12 +00002790
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002791 // function-specifier
2792 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002793 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002794 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002795 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002796 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002797 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002798 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002799 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002800 break;
Richard Smith0015f092013-01-17 22:16:11 +00002801 case tok::kw__Noreturn:
2802 if (!getLangOpts().C11)
2803 Diag(Loc, diag::ext_c11_noreturn);
2804 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2805 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002806
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002807 // alignment-specifier
2808 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002809 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002810 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002811 ParseAlignmentSpecifier(DS.getAttributes());
2812 continue;
2813
Anders Carlssoncd8db412009-05-06 04:46:28 +00002814 // friend
2815 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002816 if (DSContext == DSC_class)
2817 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2818 else {
2819 PrevSpec = ""; // not actually used by the diagnostic
2820 DiagID = diag::err_friend_invalid_in_context;
2821 isInvalid = true;
2822 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002823 break;
Mike Stump11289f42009-09-09 15:08:12 +00002824
Douglas Gregor26701a42011-09-09 02:06:17 +00002825 // Modules
2826 case tok::kw___module_private__:
2827 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2828 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002829
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002830 // constexpr
2831 case tok::kw_constexpr:
2832 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2833 break;
2834
Chris Lattnere387d9e2009-01-21 19:48:37 +00002835 // type-specifier
2836 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002837 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2838 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002839 break;
2840 case tok::kw_long:
2841 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002842 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2843 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002844 else
John McCall49bfce42009-08-03 20:12:06 +00002845 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2846 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002847 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002848 case tok::kw___int64:
2849 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2850 DiagID);
2851 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002852 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002853 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2854 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002855 break;
2856 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002857 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2858 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002859 break;
2860 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002861 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2862 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002863 break;
2864 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002865 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2866 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002867 break;
2868 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2870 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002871 break;
2872 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2874 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002875 break;
2876 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2878 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002879 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002880 case tok::kw___int128:
2881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2882 DiagID);
2883 break;
2884 case tok::kw_half:
2885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2886 DiagID);
2887 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002888 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2890 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002891 break;
2892 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2894 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002895 break;
2896 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2898 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002899 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002900 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002901 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2902 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002903 break;
2904 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2906 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002907 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002908 case tok::kw_bool:
2909 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002910 if (Tok.is(tok::kw_bool) &&
2911 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2912 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2913 PrevSpec = ""; // Not used by the diagnostic.
2914 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002915 // For better error recovery.
2916 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002917 isInvalid = true;
2918 } else {
2919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2920 DiagID);
2921 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002922 break;
2923 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002924 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2925 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002926 break;
2927 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2929 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002930 break;
2931 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2933 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002934 break;
John Thompson22334602010-02-05 00:12:22 +00002935 case tok::kw___vector:
2936 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2937 break;
2938 case tok::kw___pixel:
2939 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2940 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002941 case tok::kw_image1d_t:
2942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2943 PrevSpec, DiagID);
2944 break;
2945 case tok::kw_image1d_array_t:
2946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2947 PrevSpec, DiagID);
2948 break;
2949 case tok::kw_image1d_buffer_t:
2950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2951 PrevSpec, DiagID);
2952 break;
2953 case tok::kw_image2d_t:
2954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2955 PrevSpec, DiagID);
2956 break;
2957 case tok::kw_image2d_array_t:
2958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2959 PrevSpec, DiagID);
2960 break;
2961 case tok::kw_image3d_t:
2962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2963 PrevSpec, DiagID);
2964 break;
Guy Benyei61054192013-02-07 10:55:47 +00002965 case tok::kw_sampler_t:
2966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2967 PrevSpec, DiagID);
2968 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002969 case tok::kw_event_t:
2970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2971 PrevSpec, DiagID);
2972 break;
John McCall39439732011-04-09 22:50:59 +00002973 case tok::kw___unknown_anytype:
2974 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2975 PrevSpec, DiagID);
2976 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002977
2978 // class-specifier:
2979 case tok::kw_class:
2980 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002981 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002982 case tok::kw_union: {
2983 tok::TokenKind Kind = Tok.getKind();
2984 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002985
2986 // These are attributes following class specifiers.
2987 // To produce better diagnostic, we parse them when
2988 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002989 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002990 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002991 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002992
2993 // If there are attributes following class specifier,
2994 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002995 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002996 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002997 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002998 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003000 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003001
3002 // enum-specifier:
3003 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003004 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003005 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003006 continue;
3007
3008 // cv-qualifier:
3009 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003011 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003012 break;
3013 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003015 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003016 break;
3017 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003019 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003020 break;
3021
Douglas Gregor333489b2009-03-27 23:10:48 +00003022 // C++ typename-specifier:
3023 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003024 if (TryAnnotateTypeOrScopeToken()) {
3025 DS.SetTypeSpecError();
3026 goto DoneWithDeclSpec;
3027 }
3028 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003029 continue;
3030 break;
3031
Chris Lattnere387d9e2009-01-21 19:48:37 +00003032 // GNU typeof support.
3033 case tok::kw_typeof:
3034 ParseTypeofSpecifier(DS);
3035 continue;
3036
David Blaikie15a430a2011-12-04 05:04:18 +00003037 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003038 ParseDecltypeSpecifier(DS);
3039 continue;
3040
Alexis Hunt4a257072011-05-19 05:37:45 +00003041 case tok::kw___underlying_type:
3042 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003043 continue;
3044
3045 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003046 // C11 6.7.2.4/4:
3047 // If the _Atomic keyword is immediately followed by a left parenthesis,
3048 // it is interpreted as a type specifier (with a type name), not as a
3049 // type qualifier.
3050 if (NextToken().is(tok::l_paren)) {
3051 ParseAtomicSpecifier(DS);
3052 continue;
3053 }
3054 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3055 getLangOpts());
3056 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003057
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003058 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003059 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003060 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003061 goto DoneWithDeclSpec;
3062 case tok::kw___private:
3063 case tok::kw___global:
3064 case tok::kw___local:
3065 case tok::kw___constant:
3066 case tok::kw___read_only:
3067 case tok::kw___write_only:
3068 case tok::kw___read_write:
3069 ParseOpenCLQualifiers(DS);
3070 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003071
Steve Naroffcfdf6162008-06-05 00:02:44 +00003072 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003073 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003074 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3075 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003076 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003077 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003078
Douglas Gregor3a001f42010-11-19 17:10:50 +00003079 if (!ParseObjCProtocolQualifiers(DS))
3080 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3081 << FixItHint::CreateInsertion(Loc, "id")
3082 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003083
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003084 // Need to support trailing type qualifiers (e.g. "id<p> const").
3085 // If a type specifier follows, it will be diagnosed elsewhere.
3086 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003087 }
John McCall49bfce42009-08-03 20:12:06 +00003088 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003089 if (isInvalid) {
3090 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003091 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003092
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003093 if (DiagID == diag::ext_duplicate_declspec)
3094 Diag(Tok, DiagID)
3095 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3096 else
3097 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003098 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003099
Chris Lattner2e232092008-03-13 06:29:04 +00003100 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003101 if (DiagID != diag::err_bool_redeclaration)
3102 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003103
3104 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003105 }
3106}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003107
Chris Lattner70ae4912007-10-29 04:42:53 +00003108/// ParseStructDeclaration - Parse a struct declaration without the terminating
3109/// semicolon.
3110///
Chris Lattner90a26b02007-01-23 04:38:16 +00003111/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003112/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003113/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003114/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003115/// struct-declarator-list:
3116/// struct-declarator
3117/// struct-declarator-list ',' struct-declarator
3118/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3119/// struct-declarator:
3120/// declarator
3121/// [GNU] declarator attributes[opt]
3122/// declarator[opt] ':' constant-expression
3123/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3124///
Chris Lattnera12405b2008-04-10 06:46:29 +00003125void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003126ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003127
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003128 if (Tok.is(tok::kw___extension__)) {
3129 // __extension__ silences extension warnings in the subexpression.
3130 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003131 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003132 return ParseStructDeclaration(DS, Fields);
3133 }
Mike Stump11289f42009-09-09 15:08:12 +00003134
Steve Naroff97170802007-08-20 22:28:22 +00003135 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003136 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003137
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003138 // If there are no declarators, this is a free-standing declaration
3139 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003140 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003141 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3142 DS);
3143 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003144 return;
3145 }
3146
3147 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003148 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003149 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003150 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003151 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003152 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003153
Bill Wendling44426052012-12-20 19:22:21 +00003154 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003155 if (!FirstDeclarator)
3156 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003157
Steve Naroff97170802007-08-20 22:28:22 +00003158 /// struct-declarator: declarator
3159 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003160 if (Tok.isNot(tok::colon)) {
3161 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3162 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003163 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003164 }
Mike Stump11289f42009-09-09 15:08:12 +00003165
Chris Lattner76c72282007-10-09 17:33:22 +00003166 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003167 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003168 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003169 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003170 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003171 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003172 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003173 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003174
Steve Naroff97170802007-08-20 22:28:22 +00003175 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003176 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003177
John McCallcfefb6d2009-11-03 02:38:08 +00003178 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003179 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003180
Steve Naroff97170802007-08-20 22:28:22 +00003181 // If we don't have a comma, it is either the end of the list (a ';')
3182 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003183 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003184 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003185
Steve Naroff97170802007-08-20 22:28:22 +00003186 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003187 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003188
John McCallcfefb6d2009-11-03 02:38:08 +00003189 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003190 }
Steve Naroff97170802007-08-20 22:28:22 +00003191}
3192
3193/// ParseStructUnionBody
3194/// struct-contents:
3195/// struct-declaration-list
3196/// [EXT] empty
3197/// [GNU] "struct-declaration-list" without terminatoring ';'
3198/// struct-declaration-list:
3199/// struct-declaration
3200/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003201/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003202///
Chris Lattner1300fb92007-01-23 23:42:53 +00003203void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003204 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003205 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3206 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003207 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003208
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003209 BalancedDelimiterTracker T(*this, tok::l_brace);
3210 if (T.consumeOpen())
3211 return;
Mike Stump11289f42009-09-09 15:08:12 +00003212
Douglas Gregor658b9552009-01-09 22:42:13 +00003213 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003214 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003215
Andy Gibbs22e140b2013-04-03 09:31:19 +00003216 // Empty structs are an extension in C (C99 6.7.2.1p7).
3217 if (Tok.is(tok::r_brace)) {
Richard Smithe4345902011-12-29 21:57:33 +00003218 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3219 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3220 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003221
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003222 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003223
Chris Lattner7b9ace62007-01-23 20:11:08 +00003224 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003225 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003226 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003227
Chris Lattner736ed5d2007-06-09 05:59:07 +00003228 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003229 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003230 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003231 continue;
3232 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003233
Andy Gibbsc804e082013-04-03 09:46:04 +00003234 // Parse _Static_assert declaration.
3235 if (Tok.is(tok::kw__Static_assert)) {
3236 SourceLocation DeclEnd;
3237 ParseStaticAssertDeclaration(DeclEnd);
3238 continue;
3239 }
3240
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003241 if (Tok.is(tok::annot_pragma_pack)) {
3242 HandlePragmaPack();
3243 continue;
3244 }
3245
3246 if (Tok.is(tok::annot_pragma_align)) {
3247 HandlePragmaAlign();
3248 continue;
3249 }
3250
John McCallcfefb6d2009-11-03 02:38:08 +00003251 if (!Tok.is(tok::at)) {
3252 struct CFieldCallback : FieldCallback {
3253 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003254 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003255 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003256
John McCall48871652010-08-21 09:40:31 +00003257 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003258 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003259 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3260
Eli Friedman934dbbf2012-08-08 23:53:27 +00003261 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003262 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003263 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003264 FD.D.getDeclSpec().getSourceRange().getBegin(),
3265 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003266 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003267 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003268 }
John McCallcfefb6d2009-11-03 02:38:08 +00003269 } Callback(*this, TagDecl, FieldDecls);
3270
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003271 // Parse all the comma separated declarators.
3272 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003273 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003274 } else { // Handle @defs
3275 ConsumeToken();
3276 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3277 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003278 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003279 continue;
3280 }
3281 ConsumeToken();
3282 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3283 if (!Tok.is(tok::identifier)) {
3284 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003285 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003286 continue;
3287 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003288 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003289 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003290 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003291 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3292 ConsumeToken();
3293 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003294 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003295
Chris Lattner76c72282007-10-09 17:33:22 +00003296 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003297 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003298 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003299 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003300 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003301 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003302 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3303 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003304 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003305 // If we stopped at a ';', eat it.
3306 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003307 }
3308 }
Mike Stump11289f42009-09-09 15:08:12 +00003309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003310 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003311
John McCall084e83d2011-03-24 11:26:52 +00003312 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003313 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003314 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003315
Douglas Gregor0be31a22010-07-02 17:43:08 +00003316 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003317 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003318 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003319 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003320 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003321 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3322 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003323}
3324
Chris Lattner3b561a32006-08-13 00:12:11 +00003325/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003326/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003327/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003328///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003329/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3330/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003331/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3332/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003333/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003334/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003335///
Richard Smith7d137e32012-03-23 03:33:32 +00003336/// [C++11] enum-head '{' enumerator-list[opt] '}'
3337/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003338///
Richard Smith7d137e32012-03-23 03:33:32 +00003339/// enum-head: [C++11]
3340/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3341/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3342/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003343///
Richard Smith7d137e32012-03-23 03:33:32 +00003344/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003345/// 'enum'
3346/// 'enum' 'class'
3347/// 'enum' 'struct'
3348///
Richard Smith7d137e32012-03-23 03:33:32 +00003349/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003350/// ':' type-specifier-seq
3351///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003352/// [C++] elaborated-type-specifier:
3353/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3354///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003355void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003356 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003357 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003358 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003359 if (Tok.is(tok::code_completion)) {
3360 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003361 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003362 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003363 }
John McCallcb432fa2011-07-06 05:58:41 +00003364
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003365 // If attributes exist after tag, parse them.
3366 ParsedAttributesWithRange attrs(AttrFactory);
3367 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003368 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003369
3370 // If declspecs exist after tag, parse them.
3371 while (Tok.is(tok::kw___declspec))
3372 ParseMicrosoftDeclSpec(attrs);
3373
Richard Smith0f8ee222012-01-10 01:33:14 +00003374 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003375 bool IsScopedUsingClassTag = false;
3376
John McCallbeae29a2012-06-23 22:30:04 +00003377 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003378 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3379 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3380 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003381 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003382 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003383
Bill Wendling44426052012-12-20 19:22:21 +00003384 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003385 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003386 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003387
3388 // They are allowed afterwards, though.
3389 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003390 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003391 while (Tok.is(tok::kw___declspec))
3392 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003393 }
Richard Smith7d137e32012-03-23 03:33:32 +00003394
John McCall6347b682012-05-07 06:16:58 +00003395 // C++11 [temp.explicit]p12:
3396 // The usual access controls do not apply to names used to specify
3397 // explicit instantiations.
3398 // We extend this to also cover explicit specializations. Note that
3399 // we don't suppress if this turns out to be an elaborated type
3400 // specifier.
3401 bool shouldDelayDiagsInTag =
3402 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3403 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3404 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003405
Richard Smithbfdb1082012-03-12 08:56:40 +00003406 // Enum definitions should not be parsed in a trailing-return-type.
3407 bool AllowDeclaration = DSC != DSC_trailing;
3408
3409 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003410 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003411 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003412
Abramo Bagnarad7548482010-05-19 21:37:53 +00003413 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003414 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003415 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3416 // if a fixed underlying type is allowed.
3417 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003418
3419 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003420 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003421 return;
3422
3423 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003424 Diag(Tok, diag::err_expected_ident);
3425 if (Tok.isNot(tok::l_brace)) {
3426 // Has no name and is not a definition.
3427 // Skip the rest of this declarator, up until the comma or semicolon.
3428 SkipUntil(tok::comma, true);
3429 return;
3430 }
3431 }
3432 }
Mike Stump11289f42009-09-09 15:08:12 +00003433
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003434 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003435 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003436 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003437 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003438
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003439 // Skip the rest of this declarator, up until the comma or semicolon.
3440 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003441 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003442 }
Mike Stump11289f42009-09-09 15:08:12 +00003443
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003444 // If an identifier is present, consume and remember it.
3445 IdentifierInfo *Name = 0;
3446 SourceLocation NameLoc;
3447 if (Tok.is(tok::identifier)) {
3448 Name = Tok.getIdentifierInfo();
3449 NameLoc = ConsumeToken();
3450 }
Mike Stump11289f42009-09-09 15:08:12 +00003451
Richard Smith0f8ee222012-01-10 01:33:14 +00003452 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003453 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3454 // declaration of a scoped enumeration.
3455 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003456 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003457 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003458 }
3459
John McCall6347b682012-05-07 06:16:58 +00003460 // Okay, end the suppression area. We'll decide whether to emit the
3461 // diagnostics in a second.
3462 if (shouldDelayDiagsInTag)
3463 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003464
Douglas Gregor0bf31402010-10-08 23:50:27 +00003465 TypeResult BaseType;
3466
Douglas Gregord1f69f62010-12-01 17:42:47 +00003467 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003468 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003469 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003470 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003471 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003472 // If we're in class scope, this can either be an enum declaration with
3473 // an underlying type, or a declaration of a bitfield member. We try to
3474 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003475 // (integer literal, sizeof); if it's still ambiguous, we then consider
3476 // anything that's a simple-type-specifier followed by '(' as an
3477 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003478 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003479 EnterExpressionEvaluationContext Unevaluated(Actions,
3480 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003481 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003482 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003483 // bit-field. This is the common case.
3484 if (TPR == TPResult::True())
3485 PossibleBitfield = true;
3486 // If the next token starts a type-specifier-seq, it may be either a
3487 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003488 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003489 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003490 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003491 GetLookAheadToken(2).getKind() == tok::semi) {
3492 // Consume the ':'.
3493 ConsumeToken();
3494 } else {
3495 // We have the start of a type-specifier-seq, so we have to perform
3496 // tentative parsing to determine whether we have an expression or a
3497 // type.
3498 TentativeParsingAction TPA(*this);
3499
3500 // Consume the ':'.
3501 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003502
3503 // If we see a type specifier followed by an open-brace, we have an
3504 // ambiguity between an underlying type and a C++11 braced
3505 // function-style cast. Resolve this by always treating it as an
3506 // underlying type.
3507 // FIXME: The standard is not entirely clear on how to disambiguate in
3508 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003509 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003510 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003511 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003512 // We'll parse this as a bitfield later.
3513 PossibleBitfield = true;
3514 TPA.Revert();
3515 } else {
3516 // We have a type-specifier-seq.
3517 TPA.Commit();
3518 }
3519 }
3520 } else {
3521 // Consume the ':'.
3522 ConsumeToken();
3523 }
3524
3525 if (!PossibleBitfield) {
3526 SourceRange Range;
3527 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003528
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003529 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003530 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003531 } else if (!getLangOpts().ObjC2) {
3532 if (getLangOpts().CPlusPlus)
3533 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3534 else
3535 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3536 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003537 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003538 }
3539
Richard Smith0f8ee222012-01-10 01:33:14 +00003540 // There are four options here. If we have 'friend enum foo;' then this is a
3541 // friend declaration, and cannot have an accompanying definition. If we have
3542 // 'enum foo;', then this is a forward declaration. If we have
3543 // 'enum foo {...' then this is a definition. Otherwise we have something
3544 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003545 //
3546 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3547 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3548 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3549 //
John McCallfaf5fb42010-08-26 23:41:50 +00003550 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003551 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003552 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003553 } else if (Tok.is(tok::l_brace)) {
3554 if (DS.isFriendSpecified()) {
3555 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3556 << SourceRange(DS.getFriendSpecLoc());
3557 ConsumeBrace();
3558 SkipUntil(tok::r_brace);
3559 TUK = Sema::TUK_Friend;
3560 } else {
3561 TUK = Sema::TUK_Definition;
3562 }
Richard Smith369b9f92012-06-25 21:37:02 +00003563 } else if (DSC != DSC_type_specifier &&
3564 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003565 (Tok.isAtStartOfLine() &&
3566 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003567 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3568 if (Tok.isNot(tok::semi)) {
3569 // A semicolon was missing after this declaration. Diagnose and recover.
3570 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3571 "enum");
3572 PP.EnterToken(Tok);
3573 Tok.setKind(tok::semi);
3574 }
John McCall6347b682012-05-07 06:16:58 +00003575 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003576 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003577 }
3578
3579 // If this is an elaborated type specifier, and we delayed
3580 // diagnostics before, just merge them into the current pool.
3581 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3582 diagsFromTag.redelay();
3583 }
Richard Smith7d137e32012-03-23 03:33:32 +00003584
3585 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003586 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003587 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003588 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003589 // Skip the rest of this declarator, up until the comma or semicolon.
3590 Diag(Tok, diag::err_enum_template);
3591 SkipUntil(tok::comma, true);
3592 return;
3593 }
3594
3595 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3596 // Enumerations can't be explicitly instantiated.
3597 DS.SetTypeSpecError();
3598 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3599 return;
3600 }
3601
3602 assert(TemplateInfo.TemplateParams && "no template parameters");
3603 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3604 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003605 }
Chad Rosierc1183952012-06-26 22:30:43 +00003606
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003607 if (TUK == Sema::TUK_Reference)
3608 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003609
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003610 if (!Name && TUK != Sema::TUK_Definition) {
3611 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003612
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003613 // Skip the rest of this declarator, up until the comma or semicolon.
3614 SkipUntil(tok::comma, true);
3615 return;
3616 }
Richard Smith7d137e32012-03-23 03:33:32 +00003617
Douglas Gregord6ab8742009-05-28 23:31:59 +00003618 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003619 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003620 const char *PrevSpec = 0;
3621 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003622 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003623 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003624 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003625 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003626 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003627
Douglas Gregorba41d012010-04-24 16:38:41 +00003628 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003629 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003630 // dependent tag.
3631 if (!Name) {
3632 DS.SetTypeSpecError();
3633 Diag(Tok, diag::err_expected_type_name_after_typename);
3634 return;
3635 }
Chad Rosierc1183952012-06-26 22:30:43 +00003636
Douglas Gregor0be31a22010-07-02 17:43:08 +00003637 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003638 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003639 NameLoc);
3640 if (Type.isInvalid()) {
3641 DS.SetTypeSpecError();
3642 return;
3643 }
Chad Rosierc1183952012-06-26 22:30:43 +00003644
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003645 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3646 NameLoc.isValid() ? NameLoc : StartLoc,
3647 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003648 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003649
Douglas Gregorba41d012010-04-24 16:38:41 +00003650 return;
3651 }
Mike Stump11289f42009-09-09 15:08:12 +00003652
John McCall48871652010-08-21 09:40:31 +00003653 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003654 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003655 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003656 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003657 ConsumeBrace();
3658 SkipUntil(tok::r_brace);
3659 }
Chad Rosierc1183952012-06-26 22:30:43 +00003660
Douglas Gregorba41d012010-04-24 16:38:41 +00003661 DS.SetTypeSpecError();
3662 return;
3663 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003664
Richard Smith369b9f92012-06-25 21:37:02 +00003665 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003666 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003667
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003668 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3669 NameLoc.isValid() ? NameLoc : StartLoc,
3670 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003671 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003672}
3673
Chris Lattnerc1915e22007-01-25 07:29:02 +00003674/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3675/// enumerator-list:
3676/// enumerator
3677/// enumerator-list ',' enumerator
3678/// enumerator:
3679/// enumeration-constant
3680/// enumeration-constant '=' constant-expression
3681/// enumeration-constant:
3682/// identifier
3683///
John McCall48871652010-08-21 09:40:31 +00003684void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003685 // Enter the scope of the enum body and start the definition.
3686 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003687 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003688
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003689 BalancedDelimiterTracker T(*this, tok::l_brace);
3690 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003691
Chris Lattner37256fb2007-08-27 17:24:30 +00003692 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003693 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003694 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003695
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003696 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003697
John McCall48871652010-08-21 09:40:31 +00003698 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003699
Chris Lattnerc1915e22007-01-25 07:29:02 +00003700 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003701 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003702 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3703 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003704
John McCall811a0f52010-10-22 23:36:17 +00003705 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003706 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003707 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003708 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003709 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003710
Chris Lattnerc1915e22007-01-25 07:29:02 +00003711 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003712 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003713 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003714
Chris Lattner76c72282007-10-09 17:33:22 +00003715 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003716 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003717 AssignedVal = ParseConstantExpression();
3718 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003719 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003720 }
Mike Stump11289f42009-09-09 15:08:12 +00003721
Chris Lattnerc1915e22007-01-25 07:29:02 +00003722 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003723 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3724 LastEnumConstDecl,
3725 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003726 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003727 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003728 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003729
Chris Lattner4ef40012007-06-11 01:28:17 +00003730 EnumConstantDecls.push_back(EnumConstDecl);
3731 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003732
Douglas Gregorce66d022010-09-07 14:51:08 +00003733 if (Tok.is(tok::identifier)) {
3734 // We're missing a comma between enumerators.
3735 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003736 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003737 << FixItHint::CreateInsertion(Loc, ", ");
3738 continue;
3739 }
Chad Rosierc1183952012-06-26 22:30:43 +00003740
Chris Lattner76c72282007-10-09 17:33:22 +00003741 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003742 break;
3743 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003744
Richard Smith5d164bc2011-10-15 05:09:34 +00003745 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003746 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003747 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3748 diag::ext_enumerator_list_comma_cxx :
3749 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003750 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003751 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003752 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3753 << FixItHint::CreateRemoval(CommaLoc);
3754 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003755 }
Mike Stump11289f42009-09-09 15:08:12 +00003756
Chris Lattnerc1915e22007-01-25 07:29:02 +00003757 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003758 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003759
Chris Lattnerc1915e22007-01-25 07:29:02 +00003760 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003761 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003762 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003763
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003764 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003765 EnumDecl, EnumConstantDecls,
3766 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003767 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003768
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003769 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003770 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3771 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003772
3773 // The next token must be valid after an enum definition. If not, a ';'
3774 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003775 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3776 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003777 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3778 // Push this token back into the preprocessor and change our current token
3779 // to ';' so that the rest of the code recovers as though there were an
3780 // ';' after the definition.
3781 PP.EnterToken(Tok);
3782 Tok.setKind(tok::semi);
3783 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003784}
Chris Lattner3b561a32006-08-13 00:12:11 +00003785
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003786/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003787/// start of a type-qualifier-list.
3788bool Parser::isTypeQualifier() const {
3789 switch (Tok.getKind()) {
3790 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003791
3792 // type-qualifier only in OpenCL
3793 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003794 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003795
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003796 // type-qualifier
3797 case tok::kw_const:
3798 case tok::kw_volatile:
3799 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003800 case tok::kw___private:
3801 case tok::kw___local:
3802 case tok::kw___global:
3803 case tok::kw___constant:
3804 case tok::kw___read_only:
3805 case tok::kw___read_write:
3806 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003807 return true;
3808 }
3809}
3810
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003811/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3812/// is definitely a type-specifier. Return false if it isn't part of a type
3813/// specifier or if we're not sure.
3814bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3815 switch (Tok.getKind()) {
3816 default: return false;
3817 // type-specifiers
3818 case tok::kw_short:
3819 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003820 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003821 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003822 case tok::kw_signed:
3823 case tok::kw_unsigned:
3824 case tok::kw__Complex:
3825 case tok::kw__Imaginary:
3826 case tok::kw_void:
3827 case tok::kw_char:
3828 case tok::kw_wchar_t:
3829 case tok::kw_char16_t:
3830 case tok::kw_char32_t:
3831 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003832 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003833 case tok::kw_float:
3834 case tok::kw_double:
3835 case tok::kw_bool:
3836 case tok::kw__Bool:
3837 case tok::kw__Decimal32:
3838 case tok::kw__Decimal64:
3839 case tok::kw__Decimal128:
3840 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003841
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003842 // OpenCL specific types:
3843 case tok::kw_image1d_t:
3844 case tok::kw_image1d_array_t:
3845 case tok::kw_image1d_buffer_t:
3846 case tok::kw_image2d_t:
3847 case tok::kw_image2d_array_t:
3848 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003849 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003850 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003851
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003852 // struct-or-union-specifier (C99) or class-specifier (C++)
3853 case tok::kw_class:
3854 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003855 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003856 case tok::kw_union:
3857 // enum-specifier
3858 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003859
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003860 // typedef-name
3861 case tok::annot_typename:
3862 return true;
3863 }
3864}
3865
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003866/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003867/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003868bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003869 switch (Tok.getKind()) {
3870 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattner020bab92009-01-04 23:41:41 +00003872 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003873 if (TryAltiVecVectorToken())
3874 return true;
3875 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003876 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003877 // Annotate typenames and C++ scope specifiers. If we get one, just
3878 // recurse to handle whatever we get.
3879 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003880 return true;
3881 if (Tok.is(tok::identifier))
3882 return false;
3883 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003884
Chris Lattner020bab92009-01-04 23:41:41 +00003885 case tok::coloncolon: // ::foo::bar
3886 if (NextToken().is(tok::kw_new) || // ::new
3887 NextToken().is(tok::kw_delete)) // ::delete
3888 return false;
3889
Chris Lattner020bab92009-01-04 23:41:41 +00003890 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003891 return true;
3892 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003893
Chris Lattnere37e2332006-08-15 04:50:22 +00003894 // GNU attributes support.
3895 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003896 // GNU typeof support.
3897 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003898
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003899 // type-specifiers
3900 case tok::kw_short:
3901 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003902 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003903 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003904 case tok::kw_signed:
3905 case tok::kw_unsigned:
3906 case tok::kw__Complex:
3907 case tok::kw__Imaginary:
3908 case tok::kw_void:
3909 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003910 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003911 case tok::kw_char16_t:
3912 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003913 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003914 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003915 case tok::kw_float:
3916 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003917 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003918 case tok::kw__Bool:
3919 case tok::kw__Decimal32:
3920 case tok::kw__Decimal64:
3921 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003922 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003923
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003924 // OpenCL specific types:
3925 case tok::kw_image1d_t:
3926 case tok::kw_image1d_array_t:
3927 case tok::kw_image1d_buffer_t:
3928 case tok::kw_image2d_t:
3929 case tok::kw_image2d_array_t:
3930 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003931 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003932 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003933
Chris Lattner861a2262008-04-13 18:59:07 +00003934 // struct-or-union-specifier (C99) or class-specifier (C++)
3935 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003936 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003937 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003938 case tok::kw_union:
3939 // enum-specifier
3940 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003941
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003942 // type-qualifier
3943 case tok::kw_const:
3944 case tok::kw_volatile:
3945 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003946
John McCallea0a39e2012-11-14 00:49:39 +00003947 // Debugger support.
3948 case tok::kw___unknown_anytype:
3949
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003950 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003951 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003952 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003953
Chris Lattner409bf7d2008-10-20 00:25:30 +00003954 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3955 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003956 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003957
Steve Naroff44ac7772008-12-25 14:16:32 +00003958 case tok::kw___cdecl:
3959 case tok::kw___stdcall:
3960 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003961 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003962 case tok::kw___w64:
3963 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003964 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003965 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003966 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003967
3968 case tok::kw___private:
3969 case tok::kw___local:
3970 case tok::kw___global:
3971 case tok::kw___constant:
3972 case tok::kw___read_only:
3973 case tok::kw___read_write:
3974 case tok::kw___write_only:
3975
Eli Friedman53339e02009-06-08 23:27:34 +00003976 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003977
3978 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003979 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003980
Richard Smith8e1ac332013-03-28 01:55:44 +00003981 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00003982 case tok::kw__Atomic:
3983 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003984 }
3985}
3986
Chris Lattneracd58a32006-08-06 17:24:14 +00003987/// isDeclarationSpecifier() - Return true if the current token is part of a
3988/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003989///
3990/// \param DisambiguatingWithExpression True to indicate that the purpose of
3991/// this check is to disambiguate between an expression and a declaration.
3992bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003993 switch (Tok.getKind()) {
3994 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003995
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003996 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003997 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003998
Chris Lattner020bab92009-01-04 23:41:41 +00003999 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004000 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004001 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004002 return false;
John Thompson22334602010-02-05 00:12:22 +00004003 if (TryAltiVecVectorToken())
4004 return true;
4005 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004006 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004007 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004008 // Annotate typenames and C++ scope specifiers. If we get one, just
4009 // recurse to handle whatever we get.
4010 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004011 return true;
4012 if (Tok.is(tok::identifier))
4013 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004014
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004015 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004016 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004017 // expression is permitted, then this is probably a class message send
4018 // missing the initial '['. In this case, we won't consider this to be
4019 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004020 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004021 isStartOfObjCClassMessageMissingOpenBracket())
4022 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004023
John McCall1f476a12010-02-26 08:45:28 +00004024 return isDeclarationSpecifier();
4025
Chris Lattner020bab92009-01-04 23:41:41 +00004026 case tok::coloncolon: // ::foo::bar
4027 if (NextToken().is(tok::kw_new) || // ::new
4028 NextToken().is(tok::kw_delete)) // ::delete
4029 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004030
Chris Lattner020bab92009-01-04 23:41:41 +00004031 // Annotate typenames and C++ scope specifiers. If we get one, just
4032 // recurse to handle whatever we get.
4033 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004034 return true;
4035 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004036
Chris Lattneracd58a32006-08-06 17:24:14 +00004037 // storage-class-specifier
4038 case tok::kw_typedef:
4039 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004040 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004041 case tok::kw_static:
4042 case tok::kw_auto:
4043 case tok::kw_register:
4044 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004045 case tok::kw_thread_local:
4046 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004047
Douglas Gregor26701a42011-09-09 02:06:17 +00004048 // Modules
4049 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004050
John McCallea0a39e2012-11-14 00:49:39 +00004051 // Debugger support
4052 case tok::kw___unknown_anytype:
4053
Chris Lattneracd58a32006-08-06 17:24:14 +00004054 // type-specifiers
4055 case tok::kw_short:
4056 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004057 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004058 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004059 case tok::kw_signed:
4060 case tok::kw_unsigned:
4061 case tok::kw__Complex:
4062 case tok::kw__Imaginary:
4063 case tok::kw_void:
4064 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004065 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004066 case tok::kw_char16_t:
4067 case tok::kw_char32_t:
4068
Chris Lattneracd58a32006-08-06 17:24:14 +00004069 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004070 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004071 case tok::kw_float:
4072 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004073 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004074 case tok::kw__Bool:
4075 case tok::kw__Decimal32:
4076 case tok::kw__Decimal64:
4077 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004078 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004079
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004080 // OpenCL specific types:
4081 case tok::kw_image1d_t:
4082 case tok::kw_image1d_array_t:
4083 case tok::kw_image1d_buffer_t:
4084 case tok::kw_image2d_t:
4085 case tok::kw_image2d_array_t:
4086 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004087 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004088 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004089
Chris Lattner861a2262008-04-13 18:59:07 +00004090 // struct-or-union-specifier (C99) or class-specifier (C++)
4091 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004092 case tok::kw_struct:
4093 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004094 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004095 // enum-specifier
4096 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004097
Chris Lattneracd58a32006-08-06 17:24:14 +00004098 // type-qualifier
4099 case tok::kw_const:
4100 case tok::kw_volatile:
4101 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004102
Chris Lattneracd58a32006-08-06 17:24:14 +00004103 // function-specifier
4104 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004105 case tok::kw_virtual:
4106 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004107 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004108
Richard Smith1dba27c2013-01-29 09:02:09 +00004109 // alignment-specifier
4110 case tok::kw__Alignas:
4111
Richard Smithd16fe122012-10-25 00:00:53 +00004112 // friend keyword.
4113 case tok::kw_friend:
4114
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004115 // static_assert-declaration
4116 case tok::kw__Static_assert:
4117
Chris Lattner599e47e2007-08-09 17:01:07 +00004118 // GNU typeof support.
4119 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004120
Chris Lattner599e47e2007-08-09 17:01:07 +00004121 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004122 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004123
Richard Smithd16fe122012-10-25 00:00:53 +00004124 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004125 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004126 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004127
Richard Smith8e1ac332013-03-28 01:55:44 +00004128 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004129 case tok::kw__Atomic:
4130 return true;
4131
Chris Lattner8b2ec162008-07-26 03:38:44 +00004132 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4133 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004134 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004135
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004136 // typedef-name
4137 case tok::annot_typename:
4138 return !DisambiguatingWithExpression ||
4139 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004140
Steve Narofff192fab2009-01-06 19:34:12 +00004141 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004142 case tok::kw___cdecl:
4143 case tok::kw___stdcall:
4144 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004145 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004146 case tok::kw___w64:
4147 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004148 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004149 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004150 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004151 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004152
4153 case tok::kw___private:
4154 case tok::kw___local:
4155 case tok::kw___global:
4156 case tok::kw___constant:
4157 case tok::kw___read_only:
4158 case tok::kw___read_write:
4159 case tok::kw___write_only:
4160
Eli Friedman53339e02009-06-08 23:27:34 +00004161 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004162 }
4163}
4164
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004165bool Parser::isConstructorDeclarator() {
4166 TentativeParsingAction TPA(*this);
4167
4168 // Parse the C++ scope specifier.
4169 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004170 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004171 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004172 TPA.Revert();
4173 return false;
4174 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004175
4176 // Parse the constructor name.
4177 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4178 // We already know that we have a constructor name; just consume
4179 // the token.
4180 ConsumeToken();
4181 } else {
4182 TPA.Revert();
4183 return false;
4184 }
4185
Richard Smith43f340f2012-03-27 23:05:05 +00004186 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004187 if (Tok.isNot(tok::l_paren)) {
4188 TPA.Revert();
4189 return false;
4190 }
4191 ConsumeParen();
4192
Richard Smith43f340f2012-03-27 23:05:05 +00004193 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4194 // that we have a constructor.
4195 if (Tok.is(tok::r_paren) ||
4196 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004197 TPA.Revert();
4198 return true;
4199 }
4200
4201 // If we need to, enter the specified scope.
4202 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004203 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004204 DeclScopeObj.EnterDeclaratorScope();
4205
Francois Pichet79f3a872011-01-31 04:54:32 +00004206 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004207 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004208 MaybeParseMicrosoftAttributes(Attrs);
4209
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004210 // Check whether the next token(s) are part of a declaration
4211 // specifier, in which case we have the start of a parameter and,
4212 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004213 bool IsConstructor = false;
4214 if (isDeclarationSpecifier())
4215 IsConstructor = true;
4216 else if (Tok.is(tok::identifier) ||
4217 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4218 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4219 // This might be a parenthesized member name, but is more likely to
4220 // be a constructor declaration with an invalid argument type. Keep
4221 // looking.
4222 if (Tok.is(tok::annot_cxxscope))
4223 ConsumeToken();
4224 ConsumeToken();
4225
4226 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004227 // which must have one of the following syntactic forms (see the
4228 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004229 switch (Tok.getKind()) {
4230 case tok::l_paren:
4231 // C(X ( int));
4232 case tok::l_square:
4233 // C(X [ 5]);
4234 // C(X [ [attribute]]);
4235 case tok::coloncolon:
4236 // C(X :: Y);
4237 // C(X :: *p);
4238 case tok::r_paren:
4239 // C(X )
4240 // Assume this isn't a constructor, rather than assuming it's a
4241 // constructor with an unnamed parameter of an ill-formed type.
4242 break;
4243
4244 default:
4245 IsConstructor = true;
4246 break;
4247 }
4248 }
4249
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004250 TPA.Revert();
4251 return IsConstructor;
4252}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004253
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004254/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004255/// type-qualifier-list: [C99 6.7.5]
4256/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004257/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004258/// [ only if VendorAttributesAllowed=true ]
4259/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004260/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004261/// [ only if VendorAttributesAllowed=true ]
4262/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004263/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004264/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004265///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004266void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4267 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004268 bool CXX11AttributesAllowed,
4269 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004270 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004271 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004272 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004273 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004274 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004275 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004276
4277 SourceLocation EndLoc;
4278
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004279 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004280 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004281 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004282 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004283 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004284
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004285 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004286 case tok::code_completion:
4287 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004288 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004289
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004290 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004291 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004292 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004293 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004294 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004295 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004296 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004297 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004298 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004299 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004300 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004301 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004302 case tok::kw__Atomic:
4303 if (!AtomicAllowed)
4304 goto DoneWithTypeQuals;
4305 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4306 getLangOpts());
4307 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004308
4309 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004310 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004311 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004312 goto DoneWithTypeQuals;
4313 case tok::kw___private:
4314 case tok::kw___global:
4315 case tok::kw___local:
4316 case tok::kw___constant:
4317 case tok::kw___read_only:
4318 case tok::kw___write_only:
4319 case tok::kw___read_write:
4320 ParseOpenCLQualifiers(DS);
4321 break;
4322
Eli Friedman53339e02009-06-08 23:27:34 +00004323 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004324 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004325 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004326 case tok::kw___cdecl:
4327 case tok::kw___stdcall:
4328 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004329 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004330 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004331 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004332 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004333 continue;
4334 }
4335 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004336 case tok::kw___pascal:
4337 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004338 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004339 continue;
4340 }
4341 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004342 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004343 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004344 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004345 continue; // do *not* consume the next token!
4346 }
4347 // otherwise, FALL THROUGH!
4348 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004349 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004350 // If this is not a type-qualifier token, we're done reading type
4351 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004352 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004353 if (EndLoc.isValid())
4354 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004355 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004356 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004357
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004358 // If the specifier combination wasn't legal, issue a diagnostic.
4359 if (isInvalid) {
4360 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004361 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004362 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004363 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004364 }
4365}
4366
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004367
4368/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4369///
4370void Parser::ParseDeclarator(Declarator &D) {
4371 /// This implements the 'declarator' production in the C grammar, then checks
4372 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004373 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004374}
4375
Richard Smith0efa75c2012-03-29 01:16:42 +00004376static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4377 if (Kind == tok::star || Kind == tok::caret)
4378 return true;
4379
4380 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4381 if (!Lang.CPlusPlus)
4382 return false;
4383
4384 return Kind == tok::amp || Kind == tok::ampamp;
4385}
4386
Sebastian Redlbd150f42008-11-21 19:14:01 +00004387/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4388/// is parsed by the function passed to it. Pass null, and the direct-declarator
4389/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004390/// ptr-operator production.
4391///
Richard Smith09f76ee2011-10-19 21:33:05 +00004392/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004393/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4394/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004395///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004396/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4397/// [C] pointer[opt] direct-declarator
4398/// [C++] direct-declarator
4399/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004400///
4401/// pointer: [C99 6.7.5]
4402/// '*' type-qualifier-list[opt]
4403/// '*' type-qualifier-list[opt] pointer
4404///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004405/// ptr-operator:
4406/// '*' cv-qualifier-seq[opt]
4407/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004408/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004409/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004410/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004411/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004412void Parser::ParseDeclaratorInternal(Declarator &D,
4413 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004414 if (Diags.hasAllExtensionsSilenced())
4415 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004416
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004417 // C++ member pointers start with a '::' or a nested-name.
4418 // Member pointers get special handling, since there's no place for the
4419 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004420 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004421 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4422 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004423 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4424 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004425 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004426 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004427
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004428 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004429 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004430 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004431 if (D.mayHaveIdentifier())
4432 D.getCXXScopeSpec() = SS;
4433 else
4434 AnnotateScopeToken(SS, true);
4435
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004436 if (DirectDeclParser)
4437 (this->*DirectDeclParser)(D);
4438 return;
4439 }
4440
4441 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004442 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004443 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004444 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004445 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004446
4447 // Recurse to parse whatever is left.
4448 ParseDeclaratorInternal(D, DirectDeclParser);
4449
4450 // Sema will have to catch (syntactically invalid) pointers into global
4451 // scope. It has to catch pointers into namespace scope anyway.
4452 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004453 Loc),
4454 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004455 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004456 return;
4457 }
4458 }
4459
4460 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004461 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004462 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004463 if (DirectDeclParser)
4464 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004465 return;
4466 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004467
Sebastian Redled0f3b02009-03-15 22:02:01 +00004468 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4469 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004470 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004471 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004472
Chris Lattner9eac9312009-03-27 04:18:06 +00004473 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004474 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004475 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004476
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004477 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004478 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004479 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004480
Bill Wendling3708c182007-05-27 10:15:43 +00004481 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004482 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004483 if (Kind == tok::star)
4484 // Remember that we parsed a pointer type, and remember the type-quals.
4485 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004486 DS.getConstSpecLoc(),
4487 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004488 DS.getRestrictSpecLoc()),
4489 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004490 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004491 else
4492 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004493 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004494 Loc),
4495 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004496 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004497 } else {
4498 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004499 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004500
Sebastian Redl3b27be62009-03-23 00:00:23 +00004501 // Complain about rvalue references in C++03, but then go on and build
4502 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004503 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004504 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004505 diag::warn_cxx98_compat_rvalue_reference :
4506 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004507
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004508 // GNU-style and C++11 attributes are allowed here, as is restrict.
4509 ParseTypeQualifierListOpt(DS);
4510 D.ExtendWithDeclSpec(DS);
4511
Bill Wendling93efb222007-06-02 23:28:54 +00004512 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4513 // cv-qualifiers are introduced through the use of a typedef or of a
4514 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004515 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4516 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4517 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004518 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004519 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4520 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004521 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004522 // 'restrict' is permitted as an extension.
4523 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4524 Diag(DS.getAtomicSpecLoc(),
4525 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004526 }
Bill Wendling3708c182007-05-27 10:15:43 +00004527
4528 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004529 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004530
Douglas Gregor66583c52008-11-03 15:51:28 +00004531 if (D.getNumTypeObjects() > 0) {
4532 // C++ [dcl.ref]p4: There shall be no references to references.
4533 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4534 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004535 if (const IdentifierInfo *II = D.getIdentifier())
4536 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4537 << II;
4538 else
4539 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4540 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004541
Sebastian Redlbd150f42008-11-21 19:14:01 +00004542 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004543 // can go ahead and build the (technically ill-formed)
4544 // declarator: reference collapsing will take care of it.
4545 }
4546 }
4547
Richard Smith8e1ac332013-03-28 01:55:44 +00004548 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004549 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004550 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004551 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004552 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004553 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004554}
4555
Richard Smith0efa75c2012-03-29 01:16:42 +00004556static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4557 SourceLocation EllipsisLoc) {
4558 if (EllipsisLoc.isValid()) {
4559 FixItHint Insertion;
4560 if (!D.getEllipsisLoc().isValid()) {
4561 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4562 D.setEllipsisLoc(EllipsisLoc);
4563 }
4564 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4565 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4566 }
4567}
4568
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004569/// ParseDirectDeclarator
4570/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004571/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004572/// '(' declarator ')'
4573/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004574/// [C90] direct-declarator '[' constant-expression[opt] ']'
4575/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4576/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4577/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4578/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004579/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4580/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004581/// direct-declarator '(' parameter-type-list ')'
4582/// direct-declarator '(' identifier-list[opt] ')'
4583/// [GNU] direct-declarator '(' parameter-forward-declarations
4584/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004585/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4586/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004587/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4588/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4589/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004590/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004591/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004592///
4593/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004594/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004595/// '::'[opt] nested-name-specifier[opt] type-name
4596///
4597/// id-expression: [C++ 5.1]
4598/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004599/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004600///
4601/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004602/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004603/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004604/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004605/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004606/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004607///
Richard Smith1453e312012-03-27 01:42:32 +00004608/// Note, any additional constructs added here may need corresponding changes
4609/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004610void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004611 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004612
David Blaikiebbafb8a2012-03-11 07:00:24 +00004613 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004614 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004615 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004616 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4617 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004618 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004619 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004620 }
4621
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004622 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004623 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004624 // Change the declaration context for name lookup, until this function
4625 // is exited (and the declarator has been parsed).
4626 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004627 }
4628
Douglas Gregor27b4c162010-12-23 22:44:42 +00004629 // C++0x [dcl.fct]p14:
4630 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004631 // of a parameter-declaration-clause without a preceding comma. In
4632 // this case, the ellipsis is parsed as part of the
4633 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004634 // parameter pack that has not been expanded; otherwise, it is parsed
4635 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004636 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004637 !((D.getContext() == Declarator::PrototypeContext ||
4638 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004639 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004640 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004641 !Actions.containsUnexpandedParameterPacks(D))) {
4642 SourceLocation EllipsisLoc = ConsumeToken();
4643 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4644 // The ellipsis was put in the wrong place. Recover, and explain to
4645 // the user what they should have done.
4646 ParseDeclarator(D);
4647 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4648 return;
4649 } else
4650 D.setEllipsisLoc(EllipsisLoc);
4651
4652 // The ellipsis can't be followed by a parenthesized declarator. We
4653 // check for that in ParseParenDeclarator, after we have disambiguated
4654 // the l_paren token.
4655 }
4656
Douglas Gregor7861a802009-11-03 01:35:08 +00004657 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4658 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4659 // We found something that indicates the start of an unqualified-id.
4660 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004661 bool AllowConstructorName;
4662 if (D.getDeclSpec().hasTypeSpecifier())
4663 AllowConstructorName = false;
4664 else if (D.getCXXScopeSpec().isSet())
4665 AllowConstructorName =
4666 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004667 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004668 else
4669 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4670
Abramo Bagnara7945c982012-01-27 09:46:47 +00004671 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004672 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4673 /*EnteringContext=*/true,
4674 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004675 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004676 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004677 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004678 D.getName()) ||
4679 // Once we're past the identifier, if the scope was bad, mark the
4680 // whole declarator bad.
4681 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004682 D.SetIdentifier(0, Tok.getLocation());
4683 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004684 } else {
4685 // Parsed the unqualified-id; update range information and move along.
4686 if (D.getSourceRange().getBegin().isInvalid())
4687 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4688 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004689 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004690 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004691 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004692 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004693 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004694 "There's a C++-specific check for tok::identifier above");
4695 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4696 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4697 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004698 goto PastIdentifier;
4699 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004700
Douglas Gregor7861a802009-11-03 01:35:08 +00004701 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004702 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004703 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004704 // Example: 'char (*X)' or 'int (*XX)(void)'
4705 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004706
4707 // If the declarator was parenthesized, we entered the declarator
4708 // scope when parsing the parenthesized declarator, then exited
4709 // the scope already. Re-enter the scope, if we need to.
4710 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004711 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004712 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004713 if (!D.isInvalidType() &&
4714 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004715 // Change the declaration context for name lookup, until this function
4716 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004717 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004718 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004719 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004720 // This could be something simple like "int" (in which case the declarator
4721 // portion is empty), if an abstract-declarator is allowed.
4722 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004723
4724 // The grammar for abstract-pack-declarator does not allow grouping parens.
4725 // FIXME: Revisit this once core issue 1488 is resolved.
4726 if (D.hasEllipsis() && D.hasGroupingParens())
4727 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4728 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004729 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004730 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004731 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004732 if (D.getContext() == Declarator::MemberContext)
4733 Diag(Tok, diag::err_expected_member_name_or_semi)
4734 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004735 else if (getLangOpts().CPlusPlus) {
4736 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4737 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4738 else
4739 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4740 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004741 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004742 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004743 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004744 }
Mike Stump11289f42009-09-09 15:08:12 +00004745
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004746 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004747 assert(D.isPastIdentifier() &&
4748 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004749
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004750 // Don't parse attributes unless we have parsed an unparenthesized name.
4751 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004752 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004753
Chris Lattneracd58a32006-08-06 17:24:14 +00004754 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004755 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004756 // Enter function-declaration scope, limiting any declarators to the
4757 // function prototype scope, including parameter declarators.
4758 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004759 Scope::FunctionPrototypeScope|Scope::DeclScope|
4760 (D.isFunctionDeclaratorAFunctionDeclaration()
4761 ? Scope::FunctionDeclarationScope : 0));
4762
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004763 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4764 // In such a case, check if we actually have a function declarator; if it
4765 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004766 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004767 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4768 // The name of the declarator, if any, is tentatively declared within
4769 // a possible direct initializer.
4770 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4771 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4772 TentativelyDeclaredIdentifiers.pop_back();
4773 if (!IsFunctionDecl)
4774 break;
4775 }
John McCall084e83d2011-03-24 11:26:52 +00004776 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004777 BalancedDelimiterTracker T(*this, tok::l_paren);
4778 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004779 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004780 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004781 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004782 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004783 } else {
4784 break;
4785 }
4786 }
Chad Rosierc1183952012-06-26 22:30:43 +00004787}
Chris Lattneracd58a32006-08-06 17:24:14 +00004788
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004789/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4790/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004791/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004792/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4793///
4794/// direct-declarator:
4795/// '(' declarator ')'
4796/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004797/// direct-declarator '(' parameter-type-list ')'
4798/// direct-declarator '(' identifier-list[opt] ')'
4799/// [GNU] direct-declarator '(' parameter-forward-declarations
4800/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004801///
4802void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004803 BalancedDelimiterTracker T(*this, tok::l_paren);
4804 T.consumeOpen();
4805
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004806 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004807
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004808 // Eat any attributes before we look at whether this is a grouping or function
4809 // declarator paren. If this is a grouping paren, the attribute applies to
4810 // the type being built up, for example:
4811 // int (__attribute__(()) *x)(long y)
4812 // If this ends up not being a grouping paren, the attribute applies to the
4813 // first argument, for example:
4814 // int (__attribute__(()) int x)
4815 // In either case, we need to eat any attributes to be able to determine what
4816 // sort of paren this is.
4817 //
John McCall084e83d2011-03-24 11:26:52 +00004818 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004819 bool RequiresArg = false;
4820 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004821 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004822
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004823 // We require that the argument list (if this is a non-grouping paren) be
4824 // present even if the attribute list was empty.
4825 RequiresArg = true;
4826 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004827
Steve Naroff44ac7772008-12-25 14:16:32 +00004828 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004829 ParseMicrosoftTypeAttributes(attrs);
4830
Dawn Perchik335e16b2010-09-03 01:29:35 +00004831 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004832 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004833 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004834
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004835 // If we haven't past the identifier yet (or where the identifier would be
4836 // stored, if this is an abstract declarator), then this is probably just
4837 // grouping parens. However, if this could be an abstract-declarator, then
4838 // this could also be the start of function arguments (consider 'void()').
4839 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004840
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004841 if (!D.mayOmitIdentifier()) {
4842 // If this can't be an abstract-declarator, this *must* be a grouping
4843 // paren, because we haven't seen the identifier yet.
4844 isGrouping = true;
4845 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004846 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4847 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004848 isDeclarationSpecifier() || // 'int(int)' is a function.
4849 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004850 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4851 // considered to be a type, not a K&R identifier-list.
4852 isGrouping = false;
4853 } else {
4854 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4855 isGrouping = true;
4856 }
Mike Stump11289f42009-09-09 15:08:12 +00004857
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004858 // If this is a grouping paren, handle:
4859 // direct-declarator: '(' declarator ')'
4860 // direct-declarator: '(' attributes declarator ')'
4861 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004862 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4863 D.setEllipsisLoc(SourceLocation());
4864
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004865 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004866 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004867 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004868 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004869 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004870 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004871 T.getCloseLocation()),
4872 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004873
4874 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004875
4876 // An ellipsis cannot be placed outside parentheses.
4877 if (EllipsisLoc.isValid())
4878 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4879
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004880 return;
4881 }
Mike Stump11289f42009-09-09 15:08:12 +00004882
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004883 // Okay, if this wasn't a grouping paren, it must be the start of a function
4884 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004885 // identifier (and remember where it would have been), then call into
4886 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004887 D.SetIdentifier(0, Tok.getLocation());
4888
David Blaikie15a430a2011-12-04 05:04:18 +00004889 // Enter function-declaration scope, limiting any declarators to the
4890 // function prototype scope, including parameter declarators.
4891 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004892 Scope::FunctionPrototypeScope | Scope::DeclScope |
4893 (D.isFunctionDeclaratorAFunctionDeclaration()
4894 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004895 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004896 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004897}
4898
4899/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4900/// declarator D up to a paren, which indicates that we are parsing function
4901/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004902///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004903/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4904/// immediately after the open paren - they should be considered to be the
4905/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004906///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004907/// If RequiresArg is true, then the first argument of the function is required
4908/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004909///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004910/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4911/// (C++11) ref-qualifier[opt], exception-specification[opt],
4912/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4913///
4914/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004915/// dynamic-exception-specification
4916/// noexcept-specification
4917///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004918void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004919 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004920 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004921 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004922 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004923 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004924 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004925 // lparen is already consumed!
4926 assert(D.isPastIdentifier() && "Should not call before identifier!");
4927
4928 // This should be true when the function has typed arguments.
4929 // Otherwise, it is treated as a K&R-style function.
4930 bool HasProto = false;
4931 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004932 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004933 // Remember where we see an ellipsis, if any.
4934 SourceLocation EllipsisLoc;
4935
4936 DeclSpec DS(AttrFactory);
4937 bool RefQualifierIsLValueRef = true;
4938 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004939 SourceLocation ConstQualifierLoc;
4940 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004941 ExceptionSpecificationType ESpecType = EST_None;
4942 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004943 SmallVector<ParsedType, 2> DynamicExceptions;
4944 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004945 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004946 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004947 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004948
James Molloy6f8780b2012-02-29 10:24:19 +00004949 Actions.ActOnStartFunctionDeclarator();
4950
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004951 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4952 EndLoc is the end location for the function declarator.
4953 They differ for trailing return types. */
4954 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004955 SourceLocation LParenLoc, RParenLoc;
4956 LParenLoc = Tracker.getOpenLocation();
4957 StartLoc = LParenLoc;
4958
Douglas Gregor9e66af42011-07-05 16:44:18 +00004959 if (isFunctionDeclaratorIdentifierList()) {
4960 if (RequiresArg)
4961 Diag(Tok, diag::err_argument_required_after_attribute);
4962
4963 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4964
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004965 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004966 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004967 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004968 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004969 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004970 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004971 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004972 else if (RequiresArg)
4973 Diag(Tok, diag::err_argument_required_after_attribute);
4974
David Blaikiebbafb8a2012-03-11 07:00:24 +00004975 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004976
4977 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004978 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004979 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004980 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004981 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004982
David Blaikiebbafb8a2012-03-11 07:00:24 +00004983 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004984 // FIXME: Accept these components in any order, and produce fixits to
4985 // correct the order if the user gets it wrong. Ideally we should deal
4986 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004987
4988 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00004989 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4990 /*CXX11AttributesAllowed*/ false,
4991 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004992 if (!DS.getSourceRange().getEnd().isInvalid()) {
4993 EndLoc = DS.getSourceRange().getEnd();
4994 ConstQualifierLoc = DS.getConstSpecLoc();
4995 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4996 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004997
4998 // Parse ref-qualifier[opt].
4999 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005000 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005001 diag::warn_cxx98_compat_ref_qualifier :
5002 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005003
Douglas Gregor9e66af42011-07-05 16:44:18 +00005004 RefQualifierIsLValueRef = Tok.is(tok::amp);
5005 RefQualifierLoc = ConsumeToken();
5006 EndLoc = RefQualifierLoc;
5007 }
5008
Douglas Gregor3024f072012-04-16 07:05:22 +00005009 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005010 // If a declaration declares a member function or member function
5011 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005012 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005013 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005014 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005015 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005016 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005017 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005018 (D.getContext() == Declarator::MemberContext
5019 ? !D.getDeclSpec().isFriendSpecified()
5020 : D.getContext() == Declarator::FileContext &&
5021 D.getCXXScopeSpec().isValid() &&
5022 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005023 Sema::CXXThisScopeRAII ThisScope(Actions,
5024 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005025 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005026 (D.getDeclSpec().isConstexprSpecified() &&
5027 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005028 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005029 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005030
Douglas Gregor9e66af42011-07-05 16:44:18 +00005031 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005032 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005033 DynamicExceptions,
5034 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005035 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005036 if (ESpecType != EST_None)
5037 EndLoc = ESpecRange.getEnd();
5038
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005039 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5040 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005041 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005042
Douglas Gregor9e66af42011-07-05 16:44:18 +00005043 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005044 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005045 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005046 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005047 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5048 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005049 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005050 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005051 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005052 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005053 }
5054 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005055 }
5056
5057 // Remember that we parsed a function type, and remember the attributes.
5058 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005059 IsAmbiguous,
5060 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005061 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005062 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005063 DS.getTypeQualifiers(),
5064 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005065 RefQualifierLoc, ConstQualifierLoc,
5066 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005067 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005068 ESpecType, ESpecRange.getBegin(),
5069 DynamicExceptions.data(),
5070 DynamicExceptionRanges.data(),
5071 DynamicExceptions.size(),
5072 NoexceptExpr.isUsable() ?
5073 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005074 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005075 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005076 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005077
5078 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005079}
5080
5081/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5082/// identifier list form for a K&R-style function: void foo(a,b,c)
5083///
5084/// Note that identifier-lists are only allowed for normal declarators, not for
5085/// abstract-declarators.
5086bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005087 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005088 && Tok.is(tok::identifier)
5089 && !TryAltiVecVectorToken()
5090 // K&R identifier lists can't have typedefs as identifiers, per C99
5091 // 6.7.5.3p11.
5092 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5093 // Identifier lists follow a really simple grammar: the identifiers can
5094 // be followed *only* by a ", identifier" or ")". However, K&R
5095 // identifier lists are really rare in the brave new modern world, and
5096 // it is very common for someone to typo a type in a non-K&R style
5097 // list. If we are presented with something like: "void foo(intptr x,
5098 // float y)", we don't want to start parsing the function declarator as
5099 // though it is a K&R style declarator just because intptr is an
5100 // invalid type.
5101 //
5102 // To handle this, we check to see if the token after the first
5103 // identifier is a "," or ")". Only then do we parse it as an
5104 // identifier list.
5105 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5106}
5107
5108/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5109/// we found a K&R-style identifier list instead of a typed parameter list.
5110///
5111/// After returning, ParamInfo will hold the parsed parameters.
5112///
5113/// identifier-list: [C99 6.7.5]
5114/// identifier
5115/// identifier-list ',' identifier
5116///
5117void Parser::ParseFunctionDeclaratorIdentifierList(
5118 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005119 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005120 // If there was no identifier specified for the declarator, either we are in
5121 // an abstract-declarator, or we are in a parameter declarator which was found
5122 // to be abstract. In abstract-declarators, identifier lists are not valid:
5123 // diagnose this.
5124 if (!D.getIdentifier())
5125 Diag(Tok, diag::ext_ident_list_in_param);
5126
5127 // Maintain an efficient lookup of params we have seen so far.
5128 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5129
5130 while (1) {
5131 // If this isn't an identifier, report the error and skip until ')'.
5132 if (Tok.isNot(tok::identifier)) {
5133 Diag(Tok, diag::err_expected_ident);
5134 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5135 // Forget we parsed anything.
5136 ParamInfo.clear();
5137 return;
5138 }
5139
5140 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5141
5142 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5143 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5144 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5145
5146 // Verify that the argument identifier has not already been mentioned.
5147 if (!ParamsSoFar.insert(ParmII)) {
5148 Diag(Tok, diag::err_param_redefinition) << ParmII;
5149 } else {
5150 // Remember this identifier in ParamInfo.
5151 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5152 Tok.getLocation(),
5153 0));
5154 }
5155
5156 // Eat the identifier.
5157 ConsumeToken();
5158
5159 // The list continues if we see a comma.
5160 if (Tok.isNot(tok::comma))
5161 break;
5162 ConsumeToken();
5163 }
5164}
5165
5166/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5167/// after the opening parenthesis. This function will not parse a K&R-style
5168/// identifier list.
5169///
Richard Smith2620cd92012-04-11 04:01:28 +00005170/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5171/// caller parsed those arguments immediately after the open paren - they should
5172/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005173///
5174/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5175/// be the location of the ellipsis, if any was parsed.
5176///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005177/// parameter-type-list: [C99 6.7.5]
5178/// parameter-list
5179/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005180/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005181///
5182/// parameter-list: [C99 6.7.5]
5183/// parameter-declaration
5184/// parameter-list ',' parameter-declaration
5185///
5186/// parameter-declaration: [C99 6.7.5]
5187/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005188/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005189/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005190/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005191/// declaration-specifiers abstract-declarator[opt]
5192/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005193/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005194/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005195/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005196///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005197void Parser::ParseParameterDeclarationClause(
5198 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005199 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005200 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005201 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00005202
Chris Lattner371ed4e2008-04-06 06:57:35 +00005203 while (1) {
5204 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005205 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5206 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005207 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005208 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005209 }
Mike Stump11289f42009-09-09 15:08:12 +00005210
Chris Lattner371ed4e2008-04-06 06:57:35 +00005211 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005212 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005213 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005214
Richard Smith2620cd92012-04-11 04:01:28 +00005215 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005216 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005217
John McCall53fa7142010-12-24 02:08:15 +00005218 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005219 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005220
5221 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005222
5223 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005224 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005225 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005226 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5227 // too much hassle.
5228 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005229
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005230 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005231
Chris Lattner371ed4e2008-04-06 06:57:35 +00005232 // Parse the declarator. This is "PrototypeContext", because we must
5233 // accept either 'declarator' or 'abstract-declarator' here.
5234 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5235 ParseDeclarator(ParmDecl);
5236
5237 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005238 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005239
Chris Lattner371ed4e2008-04-06 06:57:35 +00005240 // Remember this parsed parameter in ParamInfo.
5241 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005242
Douglas Gregor4d87df52008-12-16 21:30:33 +00005243 // DefArgToks is used when the parsing of default arguments needs
5244 // to be delayed.
5245 CachedTokens *DefArgToks = 0;
5246
Chris Lattner371ed4e2008-04-06 06:57:35 +00005247 // If no parameter was specified, verify that *something* was specified,
5248 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005249 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5250 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005251 // Completely missing, emit error.
5252 Diag(DSStart, diag::err_missing_param);
5253 } else {
5254 // Otherwise, we have something. Add it and let semantic analysis try
5255 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005256
Chris Lattner371ed4e2008-04-06 06:57:35 +00005257 // Inform the actions module about the parameter declarator, so it gets
5258 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005259 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005260
5261 // Parse the default argument, if any. We parse the default
5262 // arguments in all dialects; the semantic analysis in
5263 // ActOnParamDefaultArgument will reject the default argument in
5264 // C.
5265 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005266 SourceLocation EqualLoc = Tok.getLocation();
5267
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005268 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005269 if (D.getContext() == Declarator::MemberContext) {
5270 // If we're inside a class definition, cache the tokens
5271 // corresponding to the default argument. We'll actually parse
5272 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005273 // FIXME: Can we use a smart pointer for Toks?
5274 DefArgToks = new CachedTokens;
5275
Mike Stump11289f42009-09-09 15:08:12 +00005276 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005277 /*StopAtSemi=*/true,
5278 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005279 delete DefArgToks;
5280 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005281 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005282 } else {
5283 // Mark the end of the default argument so that we know when to
5284 // stop when we parse it later on.
5285 Token DefArgEnd;
5286 DefArgEnd.startToken();
5287 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5288 DefArgEnd.setLocation(Tok.getLocation());
5289 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005290 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005291 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005292 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005293 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005294 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005295 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005296
Chad Rosierc1183952012-06-26 22:30:43 +00005297 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005298 // used.
5299 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005300 Sema::PotentiallyEvaluatedIfUsed,
5301 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005302
Sebastian Redldb63af22012-03-14 15:54:00 +00005303 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005304 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005305 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005306 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005307 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005308 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005309 if (DefArgResult.isInvalid()) {
5310 Actions.ActOnParamDefaultArgumentError(Param);
5311 SkipUntil(tok::comma, tok::r_paren, true, true);
5312 } else {
5313 // Inform the actions module about the default argument
5314 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005315 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005316 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005317 }
5318 }
Mike Stump11289f42009-09-09 15:08:12 +00005319
5320 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5321 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005322 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005323 }
5324
5325 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005326 if (Tok.isNot(tok::comma)) {
5327 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005328 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005329
David Blaikiebbafb8a2012-03-11 07:00:24 +00005330 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005331 // We have ellipsis without a preceding ',', which is ill-formed
5332 // in C. Complain and provide the fix.
5333 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005334 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005335 }
5336 }
Chad Rosierc1183952012-06-26 22:30:43 +00005337
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005338 break;
5339 }
Mike Stump11289f42009-09-09 15:08:12 +00005340
Chris Lattner371ed4e2008-04-06 06:57:35 +00005341 // Consume the comma.
5342 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005343 }
Mike Stump11289f42009-09-09 15:08:12 +00005344
Chris Lattner6c940e62008-04-06 06:34:08 +00005345}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005346
Chris Lattnere8074e62006-08-06 18:30:15 +00005347/// [C90] direct-declarator '[' constant-expression[opt] ']'
5348/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5349/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5350/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5351/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005352/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5353/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005354void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005355 if (CheckProhibitedCXX11Attribute())
5356 return;
5357
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005358 BalancedDelimiterTracker T(*this, tok::l_square);
5359 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005360
Chris Lattner84a11622008-12-18 07:27:21 +00005361 // C array syntax has many features, but by-far the most common is [] and [4].
5362 // This code does a fast path to handle some of the most obvious cases.
5363 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005364 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005365 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005366 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005367
Chris Lattner84a11622008-12-18 07:27:21 +00005368 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005369 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005370 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005371 T.getOpenLocation(),
5372 T.getCloseLocation()),
5373 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005374 return;
5375 } else if (Tok.getKind() == tok::numeric_constant &&
5376 GetLookAheadToken(1).is(tok::r_square)) {
5377 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005378 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005379 ConsumeToken();
5380
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005381 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005382 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005383 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005384
Chris Lattner84a11622008-12-18 07:27:21 +00005385 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005386 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005387 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005388 T.getOpenLocation(),
5389 T.getCloseLocation()),
5390 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005391 return;
5392 }
Mike Stump11289f42009-09-09 15:08:12 +00005393
Chris Lattnere8074e62006-08-06 18:30:15 +00005394 // If valid, this location is the position where we read the 'static' keyword.
5395 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005396 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005397 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005398
Chris Lattnere8074e62006-08-06 18:30:15 +00005399 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005400 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005401 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005402 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005403
Chris Lattnere8074e62006-08-06 18:30:15 +00005404 // If we haven't already read 'static', check to see if there is one after the
5405 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005406 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005407 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005408
Chris Lattnere8074e62006-08-06 18:30:15 +00005409 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005410 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005411 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005412
Chris Lattner521ff2b2008-04-06 05:26:30 +00005413 // Handle the case where we have '[*]' as the array size. However, a leading
5414 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005415 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005416 // infrequent, use of lookahead is not costly here.
5417 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005418 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005419
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005420 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005421 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005422 StaticLoc = SourceLocation(); // Drop the static.
5423 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005424 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005425 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005426 // Note, in C89, this production uses the constant-expr production instead
5427 // of assignment-expr. The only difference is that assignment-expr allows
5428 // things like '=' and '*='. Sema rejects these in C89 mode because they
5429 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005430
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005431 // Parse the constant-expression or assignment-expression now (depending
5432 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005433 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005434 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005435 } else {
5436 EnterExpressionEvaluationContext Unevaluated(Actions,
5437 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005438 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005439 }
Chris Lattner62591722006-08-12 18:40:58 +00005440 }
Mike Stump11289f42009-09-09 15:08:12 +00005441
Chris Lattner62591722006-08-12 18:40:58 +00005442 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005443 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005444 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005445 // If the expression was invalid, skip it.
5446 SkipUntil(tok::r_square);
5447 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005448 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005449
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005450 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005451
John McCall084e83d2011-03-24 11:26:52 +00005452 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005453 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005454
Chris Lattner84a11622008-12-18 07:27:21 +00005455 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005456 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005457 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005458 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005459 T.getOpenLocation(),
5460 T.getCloseLocation()),
5461 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005462}
5463
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005464/// [GNU] typeof-specifier:
5465/// typeof ( expressions )
5466/// typeof ( type-name )
5467/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005468///
5469void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005470 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005471 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005472 SourceLocation StartLoc = ConsumeToken();
5473
John McCalle8595032010-01-13 20:03:27 +00005474 const bool hasParens = Tok.is(tok::l_paren);
5475
Eli Friedman15681d62012-09-26 04:34:21 +00005476 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5477 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005478
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005479 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005480 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005481 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005482 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5483 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005484 if (hasParens)
5485 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005486
5487 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005488 // FIXME: Not accurate, the range gets one token more than it should.
5489 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005490 else
5491 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005492
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005493 if (isCastExpr) {
5494 if (!CastTy) {
5495 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005496 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005497 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005498
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005499 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005500 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005501 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5502 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005503 DiagID, CastTy))
5504 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005505 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005506 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005507
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005508 // If we get here, the operand to the typeof was an expresion.
5509 if (Operand.isInvalid()) {
5510 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005511 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005512 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005513
Eli Friedmane0afc982012-01-21 01:01:51 +00005514 // We might need to transform the operand if it is potentially evaluated.
5515 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5516 if (Operand.isInvalid()) {
5517 DS.SetTypeSpecError();
5518 return;
5519 }
5520
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005521 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005522 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005523 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5524 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005525 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005526 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005527}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005528
Benjamin Kramere56f3932011-12-23 17:00:35 +00005529/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005530/// _Atomic ( type-name )
5531///
5532void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005533 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5534 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005535
5536 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005537 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005538 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005539 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005540
5541 TypeResult Result = ParseTypeName();
5542 if (Result.isInvalid()) {
5543 SkipUntil(tok::r_paren);
5544 return;
5545 }
5546
5547 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005548 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005549
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005550 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005551 return;
5552
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005553 DS.setTypeofParensRange(T.getRange());
5554 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005555
5556 const char *PrevSpec = 0;
5557 unsigned DiagID;
5558 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5559 DiagID, Result.release()))
5560 Diag(StartLoc, DiagID) << PrevSpec;
5561}
5562
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005563
5564/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5565/// from TryAltiVecVectorToken.
5566bool Parser::TryAltiVecVectorTokenOutOfLine() {
5567 Token Next = NextToken();
5568 switch (Next.getKind()) {
5569 default: return false;
5570 case tok::kw_short:
5571 case tok::kw_long:
5572 case tok::kw_signed:
5573 case tok::kw_unsigned:
5574 case tok::kw_void:
5575 case tok::kw_char:
5576 case tok::kw_int:
5577 case tok::kw_float:
5578 case tok::kw_double:
5579 case tok::kw_bool:
5580 case tok::kw___pixel:
5581 Tok.setKind(tok::kw___vector);
5582 return true;
5583 case tok::identifier:
5584 if (Next.getIdentifierInfo() == Ident_pixel) {
5585 Tok.setKind(tok::kw___vector);
5586 return true;
5587 }
5588 return false;
5589 }
5590}
5591
5592bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5593 const char *&PrevSpec, unsigned &DiagID,
5594 bool &isInvalid) {
5595 if (Tok.getIdentifierInfo() == Ident_vector) {
5596 Token Next = NextToken();
5597 switch (Next.getKind()) {
5598 case tok::kw_short:
5599 case tok::kw_long:
5600 case tok::kw_signed:
5601 case tok::kw_unsigned:
5602 case tok::kw_void:
5603 case tok::kw_char:
5604 case tok::kw_int:
5605 case tok::kw_float:
5606 case tok::kw_double:
5607 case tok::kw_bool:
5608 case tok::kw___pixel:
5609 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5610 return true;
5611 case tok::identifier:
5612 if (Next.getIdentifierInfo() == Ident_pixel) {
5613 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5614 return true;
5615 }
5616 break;
5617 default:
5618 break;
5619 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005620 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005621 DS.isTypeAltiVecVector()) {
5622 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5623 return true;
5624 }
5625 return false;
5626}