blob: 6a87b78879e860933a3a62519e9ff8427678b6cd [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)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000377 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000378 .Default(false);
379}
380
Chad Rosierc1183952012-06-26 22:30:43 +0000381/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000382/// parameters). Will return false if we properly handled the declspec, or
383/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000384void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000385 SourceLocation Loc,
386 ParsedAttributes &Attrs) {
387 // Try to handle the easy case first -- these declspecs all take a single
388 // parameter as their argument.
389 if (llvm::StringSwitch<bool>(Ident->getName())
390 .Case("uuid", true)
391 .Case("align", true)
392 .Case("allocate", true)
393 .Default(false)) {
394 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
395 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000396 // The deprecated declspec has an optional single argument, so we will
397 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000398 // not.
399 if (Tok.getKind() == tok::l_paren)
400 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
401 else
Chad Rosierc1183952012-06-26 22:30:43 +0000402 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000403 AttributeList::AS_Declspec);
404 } else if (Ident->getName() == "property") {
405 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000406 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000408 if (Tok.isNot(tok::l_paren)) {
409 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
410 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000411 return;
John McCall5e77d762013-04-16 07:28:30 +0000412 }
413 BalancedDelimiterTracker T(*this, tok::l_paren);
414 T.expectAndConsume(diag::err_expected_lparen_after,
415 Ident->getNameStart(), tok::r_paren);
416
417 enum AccessorKind {
418 AK_Invalid = -1,
419 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
420 };
421 IdentifierInfo *AccessorNames[] = { 0, 0 };
422 bool HasInvalidAccessor = false;
423
424 // Parse the accessor specifications.
425 while (true) {
426 // Stop if this doesn't look like an accessor spec.
427 if (!Tok.is(tok::identifier)) {
428 // If the user wrote a completely empty list, use a special diagnostic.
429 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
430 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
431 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
432 break;
433 }
434
435 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
436 break;
437 }
438
439 AccessorKind Kind;
440 SourceLocation KindLoc = Tok.getLocation();
441 StringRef KindStr = Tok.getIdentifierInfo()->getName();
442 if (KindStr == "get") {
443 Kind = AK_Get;
444 } else if (KindStr == "put") {
445 Kind = AK_Put;
446
447 // Recover from the common mistake of using 'set' instead of 'put'.
448 } else if (KindStr == "set") {
449 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
450 << FixItHint::CreateReplacement(KindLoc, "put");
451 Kind = AK_Put;
452
453 // Handle the mistake of forgetting the accessor kind by skipping
454 // this accessor.
455 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
456 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
457 ConsumeToken();
458 HasInvalidAccessor = true;
459 goto next_property_accessor;
460
461 // Otherwise, complain about the unknown accessor kind.
462 } else {
463 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
464 HasInvalidAccessor = true;
465 Kind = AK_Invalid;
466
467 // Try to keep parsing unless it doesn't look like an accessor spec.
468 if (!NextToken().is(tok::equal)) break;
469 }
470
471 // Consume the identifier.
472 ConsumeToken();
473
474 // Consume the '='.
475 if (Tok.is(tok::equal)) {
476 ConsumeToken();
477 } else {
478 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
479 << KindStr;
480 break;
481 }
482
483 // Expect the method name.
484 if (!Tok.is(tok::identifier)) {
485 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
486 break;
487 }
488
489 if (Kind == AK_Invalid) {
490 // Just drop invalid accessors.
491 } else if (AccessorNames[Kind] != NULL) {
492 // Complain about the repeated accessor, ignore it, and keep parsing.
493 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
494 } else {
495 AccessorNames[Kind] = Tok.getIdentifierInfo();
496 }
497 ConsumeToken();
498
499 next_property_accessor:
500 // Keep processing accessors until we run out.
501 if (Tok.is(tok::comma)) {
502 ConsumeAnyToken();
503 continue;
504
505 // If we run into the ')', stop without consuming it.
506 } else if (Tok.is(tok::r_paren)) {
507 break;
508 } else {
509 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
510 break;
511 }
512 }
513
514 // Only add the property attribute if it was well-formed.
515 if (!HasInvalidAccessor) {
516 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
517 SourceLocation(),
518 AccessorNames[AK_Get], AccessorNames[AK_Put],
519 AttributeList::AS_Declspec);
520 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000521 T.skipToEnd();
522 } else {
523 // We don't recognize this as a valid declspec, but instead of creating the
524 // attribute and allowing sema to warn about it, we will warn here instead.
525 // This is because some attributes have multiple spellings, but we need to
526 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000527 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000528 // both locations.
529 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
530
531 // If there's an open paren, we should eat the open and close parens under
532 // the assumption that this unknown declspec has parameters.
533 BalancedDelimiterTracker T(*this, tok::l_paren);
534 if (!T.consumeOpen())
535 T.skipToEnd();
536 }
537}
538
Eli Friedman06de2b52009-06-08 07:21:15 +0000539/// [MS] decl-specifier:
540/// __declspec ( extended-decl-modifier-seq )
541///
542/// [MS] extended-decl-modifier-seq:
543/// extended-decl-modifier[opt]
544/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000545void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000546 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000547
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000548 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000549 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000550 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000551 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000552 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000553
Chad Rosierc1183952012-06-26 22:30:43 +0000554 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000555 // you can specify multiple attributes per declspec.
556 while (Tok.getKind() != tok::r_paren) {
557 // We expect either a well-known identifier or a generic string. Anything
558 // else is a malformed declspec.
559 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000560 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000561 Tok.getKind() != tok::kw_restrict) {
562 Diag(Tok, diag::err_ms_declspec_type);
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000565 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000566
567 IdentifierInfo *AttrName;
568 SourceLocation AttrNameLoc;
569 if (IsString) {
570 SmallString<8> StrBuffer;
571 bool Invalid = false;
572 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
573 if (Invalid) {
574 T.skipToEnd();
575 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000576 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000577 AttrName = PP.getIdentifierInfo(Str);
578 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000579 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 AttrName = Tok.getIdentifierInfo();
581 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000582 }
Chad Rosierc1183952012-06-26 22:30:43 +0000583
Aaron Ballman478faed2012-06-19 22:09:27 +0000584 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000585 // If we have a generic string, we will allow it because there is no
586 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000587 // (for instance, SAL declspecs in older versions of MSVC).
588 //
Chad Rosierc1183952012-06-26 22:30:43 +0000589 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000590 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000591 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000592 0, 0, AttributeList::AS_Declspec);
593 else
594 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000595 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000596 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000597}
598
John McCall53fa7142010-12-24 02:08:15 +0000599void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000600 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000601 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000602 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000603 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000604 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000605 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
606 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000607 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000608 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000609 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000610}
611
John McCall53fa7142010-12-24 02:08:15 +0000612void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000613 // Treat these like attributes
614 while (Tok.is(tok::kw___pascal)) {
615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
616 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000618 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000619 }
John McCall53fa7142010-12-24 02:08:15 +0000620}
621
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000622void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
623 // Treat these like attributes
624 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000625 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000626 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000627 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
628 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000629 }
630}
631
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000632void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000633 // FIXME: The mapping from attribute spelling to semantics should be
634 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000635 SourceLocation Loc = Tok.getLocation();
636 switch(Tok.getKind()) {
637 // OpenCL qualifiers:
638 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000639 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000640 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000641 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000642 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000643 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000644
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000646 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000648 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000650
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000651 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000652 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000654 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000656
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000658 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000660 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000661 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000662
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000664 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000665 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000666 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000668
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000670 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000671 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000672 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000673 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000674
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000675 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000676 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000677 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000678 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000679 break;
680 default: break;
681 }
682}
683
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000684/// \brief Parse a version number.
685///
686/// version:
687/// simple-integer
688/// simple-integer ',' simple-integer
689/// simple-integer ',' simple-integer ',' simple-integer
690VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
691 Range = Tok.getLocation();
692
693 if (!Tok.is(tok::numeric_constant)) {
694 Diag(Tok, diag::err_expected_version);
695 SkipUntil(tok::comma, tok::r_paren, true, true, true);
696 return VersionTuple();
697 }
698
699 // Parse the major (and possibly minor and subminor) versions, which
700 // are stored in the numeric constant. We utilize a quirk of the
701 // lexer, which is that it handles something like 1.2.3 as a single
702 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000703 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000704 Buffer.resize(Tok.getLength()+1);
705 const char *ThisTokBegin = &Buffer[0];
706
707 // Get the spelling of the token, which eliminates trigraphs, etc.
708 bool Invalid = false;
709 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
710 if (Invalid)
711 return VersionTuple();
712
713 // Parse the major version.
714 unsigned AfterMajor = 0;
715 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000716 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000717 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
718 ++AfterMajor;
719 }
720
721 if (AfterMajor == 0) {
722 Diag(Tok, diag::err_expected_version);
723 SkipUntil(tok::comma, tok::r_paren, true, true, true);
724 return VersionTuple();
725 }
726
727 if (AfterMajor == ActualLength) {
728 ConsumeToken();
729
730 // We only had a single version component.
731 if (Major == 0) {
732 Diag(Tok, diag::err_zero_version);
733 return VersionTuple();
734 }
735
736 return VersionTuple(Major);
737 }
738
739 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
740 Diag(Tok, diag::err_expected_version);
741 SkipUntil(tok::comma, tok::r_paren, true, true, true);
742 return VersionTuple();
743 }
744
745 // Parse the minor version.
746 unsigned AfterMinor = AfterMajor + 1;
747 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000748 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
750 ++AfterMinor;
751 }
752
753 if (AfterMinor == ActualLength) {
754 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000755
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000756 // We had major.minor.
757 if (Major == 0 && Minor == 0) {
758 Diag(Tok, diag::err_zero_version);
759 return VersionTuple();
760 }
761
Chad Rosierc1183952012-06-26 22:30:43 +0000762 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000763 }
764
765 // If what follows is not a '.', we have a problem.
766 if (ThisTokBegin[AfterMinor] != '.') {
767 Diag(Tok, diag::err_expected_version);
768 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000769 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000770 }
771
772 // Parse the subminor version.
773 unsigned AfterSubminor = AfterMinor + 1;
774 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000775 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000776 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
777 ++AfterSubminor;
778 }
779
780 if (AfterSubminor != ActualLength) {
781 Diag(Tok, diag::err_expected_version);
782 SkipUntil(tok::comma, tok::r_paren, true, true, true);
783 return VersionTuple();
784 }
785 ConsumeToken();
786 return VersionTuple(Major, Minor, Subminor);
787}
788
789/// \brief Parse the contents of the "availability" attribute.
790///
791/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000792/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000793///
794/// platform:
795/// identifier
796///
797/// version-arg-list:
798/// version-arg
799/// version-arg ',' version-arg-list
800///
801/// version-arg:
802/// 'introduced' '=' version
803/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000804/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000805/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000806/// opt-message:
807/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000808void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
809 SourceLocation AvailabilityLoc,
810 ParsedAttributes &attrs,
811 SourceLocation *endLoc) {
812 SourceLocation PlatformLoc;
813 IdentifierInfo *Platform = 0;
814
815 enum { Introduced, Deprecated, Obsoleted, Unknown };
816 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000817 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000818
819 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822 Diag(Tok, diag::err_expected_lparen);
823 return;
824 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000825
826 // Parse the platform name,
827 if (Tok.isNot(tok::identifier)) {
828 Diag(Tok, diag::err_availability_expected_platform);
829 SkipUntil(tok::r_paren);
830 return;
831 }
832 Platform = Tok.getIdentifierInfo();
833 PlatformLoc = ConsumeToken();
834
835 // Parse the ',' following the platform name.
836 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
837 return;
838
839 // If we haven't grabbed the pointers for the identifiers
840 // "introduced", "deprecated", and "obsoleted", do so now.
841 if (!Ident_introduced) {
842 Ident_introduced = PP.getIdentifierInfo("introduced");
843 Ident_deprecated = PP.getIdentifierInfo("deprecated");
844 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000845 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000846 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000847 }
848
849 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000850 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000851 do {
852 if (Tok.isNot(tok::identifier)) {
853 Diag(Tok, diag::err_availability_expected_change);
854 SkipUntil(tok::r_paren);
855 return;
856 }
857 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
858 SourceLocation KeywordLoc = ConsumeToken();
859
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000860 if (Keyword == Ident_unavailable) {
861 if (UnavailableLoc.isValid()) {
862 Diag(KeywordLoc, diag::err_availability_redundant)
863 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000864 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000865 UnavailableLoc = KeywordLoc;
866
867 if (Tok.isNot(tok::comma))
868 break;
869
870 ConsumeToken();
871 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000872 }
873
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000874 if (Tok.isNot(tok::equal)) {
875 Diag(Tok, diag::err_expected_equal_after)
876 << Keyword;
877 SkipUntil(tok::r_paren);
878 return;
879 }
880 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000881 if (Keyword == Ident_message) {
882 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000883 Diag(Tok, diag::err_expected_string_literal)
884 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000885 SkipUntil(tok::r_paren);
886 return;
887 }
888 MessageExpr = ParseStringLiteralExpression();
889 break;
890 }
Chad Rosierc1183952012-06-26 22:30:43 +0000891
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000892 SourceRange VersionRange;
893 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000894
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000895 if (Version.empty()) {
896 SkipUntil(tok::r_paren);
897 return;
898 }
899
900 unsigned Index;
901 if (Keyword == Ident_introduced)
902 Index = Introduced;
903 else if (Keyword == Ident_deprecated)
904 Index = Deprecated;
905 else if (Keyword == Ident_obsoleted)
906 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000907 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000908 Index = Unknown;
909
910 if (Index < Unknown) {
911 if (!Changes[Index].KeywordLoc.isInvalid()) {
912 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000913 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000914 << SourceRange(Changes[Index].KeywordLoc,
915 Changes[Index].VersionRange.getEnd());
916 }
917
918 Changes[Index].KeywordLoc = KeywordLoc;
919 Changes[Index].Version = Version;
920 Changes[Index].VersionRange = VersionRange;
921 } else {
922 Diag(KeywordLoc, diag::err_availability_unknown_change)
923 << Keyword << VersionRange;
924 }
925
926 if (Tok.isNot(tok::comma))
927 break;
928
929 ConsumeToken();
930 } while (true);
931
932 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000933 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000934 return;
935
936 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000937 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000938
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000939 // The 'unavailable' availability cannot be combined with any other
940 // availability changes. Make sure that hasn't happened.
941 if (UnavailableLoc.isValid()) {
942 bool Complained = false;
943 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
944 if (Changes[Index].KeywordLoc.isValid()) {
945 if (!Complained) {
946 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
947 << SourceRange(Changes[Index].KeywordLoc,
948 Changes[Index].VersionRange.getEnd());
949 Complained = true;
950 }
951
952 // Clear out the availability.
953 Changes[Index] = AvailabilityChange();
954 }
955 }
956 }
957
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000958 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000959 attrs.addNew(&Availability,
960 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000961 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000962 Platform, PlatformLoc,
963 Changes[Introduced],
964 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000965 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000966 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000967 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000968}
969
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000970
Bill Wendling44426052012-12-20 19:22:21 +0000971// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
973
974void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
975
976void Parser::LateParsedClass::ParseLexedAttributes() {
977 Self->ParseLexedAttributes(*Class);
978}
979
980void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000981 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000982}
983
984/// Wrapper class which calls ParseLexedAttribute, after setting up the
985/// scope appropriately.
986void Parser::ParseLexedAttributes(ParsingClass &Class) {
987 // Deal with templates
988 // FIXME: Test cases to make sure this does the right thing for templates.
989 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
990 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
991 HasTemplateScope);
992 if (HasTemplateScope)
993 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
994
Douglas Gregor3024f072012-04-16 07:05:22 +0000995 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000996 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000997 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000998 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
999 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1000
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001001 // Enter the scope of nested classes
1002 if (!AlreadyHasClassScope)
1003 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1004 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001005 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001006 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1007 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1008 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 }
Chad Rosierc1183952012-06-26 22:30:43 +00001010
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001011 if (!AlreadyHasClassScope)
1012 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1013 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001014}
1015
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001016
1017/// \brief Parse all attributes in LAs, and attach them to Decl D.
1018void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1019 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001020 assert(LAs.parseSoon() &&
1021 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001022 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001023 if (D)
1024 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001025 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001026 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001027 }
1028 LAs.clear();
1029}
1030
1031
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032/// \brief Finish parsing an attribute for which parsing was delayed.
1033/// This will be called at the end of parsing a class declaration
1034/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001035/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001036/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001037void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1038 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001039 // Save the current token position.
1040 SourceLocation OrigLoc = Tok.getLocation();
1041
1042 // Append the current token at the end of the new token stream so that it
1043 // doesn't get lost.
1044 LA.Toks.push_back(Tok);
1045 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1046 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001047 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001048
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001049 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001050 // FIXME: Do not warn on C++11 attributes, once we start supporting
1051 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001052 Diag(Tok, diag::warn_attribute_on_function_definition)
1053 << LA.AttrName.getName();
1054 }
1055
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001056 ParsedAttributes Attrs(AttrFactory);
1057 SourceLocation endLoc;
1058
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001059 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001060 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001061 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1062 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001063
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001064 // Allow 'this' within late-parsed attributes.
1065 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
1066 /*TypeQuals=*/0,
1067 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001068
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001069 if (LA.Decls.size() == 1) {
1070 // If the Decl is templatized, add template parameters to scope.
1071 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1072 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1073 if (HasTemplateScope)
1074 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001075
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001076 // If the Decl is on a function, add function parameters to the scope.
1077 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1078 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1079 if (HasFunScope)
1080 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001081
Michael Han23214e52012-10-03 01:56:22 +00001082 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001083 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001084
1085 if (HasFunScope) {
1086 Actions.ActOnExitFunctionContext();
1087 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1088 }
1089 if (HasTemplateScope) {
1090 TempScope.Exit();
1091 }
1092 } else {
1093 // If there are multiple decls, then the decl cannot be within the
1094 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001095 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001096 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001097 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001098 } else {
1099 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001100 }
1101
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001102 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1103 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1104 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001105
1106 if (Tok.getLocation() != OrigLoc) {
1107 // Due to a parsing error, we either went over the cached tokens or
1108 // there are still cached tokens left, so we skip the leftover tokens.
1109 // Since this is an uncommon situation that should be avoided, use the
1110 // expensive isBeforeInTranslationUnit call.
1111 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1112 OrigLoc))
1113 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001114 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001115 }
1116}
1117
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001118/// \brief Wrapper around a case statement checking if AttrName is
1119/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001120bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001121 return llvm::StringSwitch<bool>(AttrName)
1122 .Case("guarded_by", true)
1123 .Case("guarded_var", true)
1124 .Case("pt_guarded_by", true)
1125 .Case("pt_guarded_var", true)
1126 .Case("lockable", true)
1127 .Case("scoped_lockable", true)
1128 .Case("no_thread_safety_analysis", true)
1129 .Case("acquired_after", true)
1130 .Case("acquired_before", true)
1131 .Case("exclusive_lock_function", true)
1132 .Case("shared_lock_function", true)
1133 .Case("exclusive_trylock_function", true)
1134 .Case("shared_trylock_function", true)
1135 .Case("unlock_function", true)
1136 .Case("lock_returned", true)
1137 .Case("locks_excluded", true)
1138 .Case("exclusive_locks_required", true)
1139 .Case("shared_locks_required", true)
1140 .Default(false);
1141}
1142
1143/// \brief Parse the contents of thread safety attributes. These
1144/// should always be parsed as an expression list.
1145///
1146/// We need to special case the parsing due to the fact that if the first token
1147/// of the first argument is an identifier, the main parse loop will store
1148/// that token as a "parameter" and the rest of
1149/// the arguments will be added to a list of "arguments". However,
1150/// subsequent tokens in the first argument are lost. We instead parse each
1151/// argument as an expression and add all arguments to the list of "arguments".
1152/// In future, we will take advantage of this special case to also
1153/// deal with some argument scoping issues here (for example, referring to a
1154/// function parameter in the attribute on that function).
1155void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1156 SourceLocation AttrNameLoc,
1157 ParsedAttributes &Attrs,
1158 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001159 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001160
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001161 BalancedDelimiterTracker T(*this, tok::l_paren);
1162 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001163
Benjamin Kramerf0623432012-08-23 22:51:59 +00001164 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001165 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001166
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001167 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001168 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001169 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001170 ExprResult ArgExpr(ParseAssignmentExpression());
1171 if (ArgExpr.isInvalid()) {
1172 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001173 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001174 break;
1175 } else {
1176 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001177 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001178 if (Tok.isNot(tok::comma))
1179 break;
1180 ConsumeToken(); // Eat the comma, move to the next argument
1181 }
1182 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001183 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001184 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001185 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001186 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001187 if (EndLoc)
1188 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001189}
1190
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001191void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1192 SourceLocation AttrNameLoc,
1193 ParsedAttributes &Attrs,
1194 SourceLocation *EndLoc) {
1195 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1196
1197 BalancedDelimiterTracker T(*this, tok::l_paren);
1198 T.consumeOpen();
1199
1200 if (Tok.isNot(tok::identifier)) {
1201 Diag(Tok, diag::err_expected_ident);
1202 T.skipToEnd();
1203 return;
1204 }
1205 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1206 SourceLocation ArgumentKindLoc = ConsumeToken();
1207
1208 if (Tok.isNot(tok::comma)) {
1209 Diag(Tok, diag::err_expected_comma);
1210 T.skipToEnd();
1211 return;
1212 }
1213 ConsumeToken();
1214
1215 SourceRange MatchingCTypeRange;
1216 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1217 if (MatchingCType.isInvalid()) {
1218 T.skipToEnd();
1219 return;
1220 }
1221
1222 bool LayoutCompatible = false;
1223 bool MustBeNull = false;
1224 while (Tok.is(tok::comma)) {
1225 ConsumeToken();
1226 if (Tok.isNot(tok::identifier)) {
1227 Diag(Tok, diag::err_expected_ident);
1228 T.skipToEnd();
1229 return;
1230 }
1231 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1232 if (Flag->isStr("layout_compatible"))
1233 LayoutCompatible = true;
1234 else if (Flag->isStr("must_be_null"))
1235 MustBeNull = true;
1236 else {
1237 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1238 T.skipToEnd();
1239 return;
1240 }
1241 ConsumeToken(); // consume flag
1242 }
1243
1244 if (!T.consumeClose()) {
1245 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1246 ArgumentKind, ArgumentKindLoc,
1247 MatchingCType.release(), LayoutCompatible,
1248 MustBeNull, AttributeList::AS_GNU);
1249 }
1250
1251 if (EndLoc)
1252 *EndLoc = T.getCloseLocation();
1253}
1254
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001255/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1256/// of a C++11 attribute-specifier in a location where an attribute is not
1257/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1258/// situation.
1259///
1260/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1261/// this doesn't appear to actually be an attribute-specifier, and the caller
1262/// should try to parse it.
1263bool Parser::DiagnoseProhibitedCXX11Attribute() {
1264 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1265
1266 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1267 case CAK_NotAttributeSpecifier:
1268 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1269 return false;
1270
1271 case CAK_InvalidAttributeSpecifier:
1272 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1273 return false;
1274
1275 case CAK_AttributeSpecifier:
1276 // Parse and discard the attributes.
1277 SourceLocation BeginLoc = ConsumeBracket();
1278 ConsumeBracket();
1279 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1280 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1281 SourceLocation EndLoc = ConsumeBracket();
1282 Diag(BeginLoc, diag::err_attributes_not_allowed)
1283 << SourceRange(BeginLoc, EndLoc);
1284 return true;
1285 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001286 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001287}
1288
Richard Smith98155ad2013-02-20 01:17:14 +00001289/// \brief We have found the opening square brackets of a C++11
1290/// attribute-specifier in a location where an attribute is not permitted, but
1291/// we know where the attributes ought to be written. Parse them anyway, and
1292/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001293void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1294 SourceLocation CorrectLocation) {
1295 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1296 Tok.is(tok::kw_alignas));
1297
1298 // Consume the attributes.
1299 SourceLocation Loc = Tok.getLocation();
1300 ParseCXX11Attributes(Attrs);
1301 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1302
1303 Diag(Loc, diag::err_attributes_not_allowed)
1304 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1305 << FixItHint::CreateRemoval(AttrRange);
1306}
1307
John McCall53fa7142010-12-24 02:08:15 +00001308void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1309 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1310 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001311}
1312
Michael Han64536a62012-11-06 19:34:54 +00001313void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1314 AttributeList *AttrList = attrs.getList();
1315 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001316 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001317 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001318 << AttrList->getName();
1319 AttrList->setInvalid();
1320 }
1321 AttrList = AttrList->getNext();
1322 }
1323}
1324
Chris Lattner53361ac2006-08-10 05:19:57 +00001325/// ParseDeclaration - Parse a full 'declaration', which consists of
1326/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001327/// 'Context' should be a Declarator::TheContext value. This returns the
1328/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001329///
1330/// declaration: [C99 6.7]
1331/// block-declaration ->
1332/// simple-declaration
1333/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001334/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001335/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001336/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001337/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001338/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001339/// others... [FIXME]
1340///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001341Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1342 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001343 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001344 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001345 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001346 // Must temporarily exit the objective-c container scope for
1347 // parsing c none objective-c decls.
1348 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001349
John McCall48871652010-08-21 09:40:31 +00001350 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001351 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001352 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001353 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001354 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001355 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001356 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001357 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001358 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001359 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001360 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001361 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001362 SourceLocation InlineLoc = ConsumeToken();
1363 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1364 break;
1365 }
Chad Rosierc1183952012-06-26 22:30:43 +00001366 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001367 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001368 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001369 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001370 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001371 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001372 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001373 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001374 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001375 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001376 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001377 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001378 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001379 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001380 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001381 default:
John McCall53fa7142010-12-24 02:08:15 +00001382 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001383 }
Chad Rosierc1183952012-06-26 22:30:43 +00001384
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001385 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001386 // single decl, convert it now. Alias declarations can also declare a type;
1387 // include that too if it is present.
1388 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001389}
1390
1391/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1392/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001393/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1394/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001395///[C90/C++]init-declarator-list ';' [TODO]
1396/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001397///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001398/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001399/// attribute-specifier-seq[opt] type-specifier-seq declarator
1400///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001401/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001402/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001403///
1404/// If FRI is non-null, we might be parsing a for-range-declaration instead
1405/// of a simple-declaration. If we find that we are, we also parse the
1406/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001407Parser::DeclGroupPtrTy
1408Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1409 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001410 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001411 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001412 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001413 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001414
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001415 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001416 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001417
Chris Lattner0e894622006-08-13 19:58:17 +00001418 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1419 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001420 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001421 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001422 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001423 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001424 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001425 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001426 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001427 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001428 }
Chad Rosierc1183952012-06-26 22:30:43 +00001429
Richard Smith2386c8b2013-02-22 09:06:26 +00001430 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001431 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001432}
Mike Stump11289f42009-09-09 15:08:12 +00001433
Richard Smith09f76ee2011-10-19 21:33:05 +00001434/// Returns true if this might be the start of a declarator, or a common typo
1435/// for a declarator.
1436bool Parser::MightBeDeclarator(unsigned Context) {
1437 switch (Tok.getKind()) {
1438 case tok::annot_cxxscope:
1439 case tok::annot_template_id:
1440 case tok::caret:
1441 case tok::code_completion:
1442 case tok::coloncolon:
1443 case tok::ellipsis:
1444 case tok::kw___attribute:
1445 case tok::kw_operator:
1446 case tok::l_paren:
1447 case tok::star:
1448 return true;
1449
1450 case tok::amp:
1451 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001452 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001453
Richard Smithc8a79032012-01-09 22:31:44 +00001454 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001455 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001456 NextToken().is(tok::l_square);
1457
1458 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001459 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001460
Richard Smith09f76ee2011-10-19 21:33:05 +00001461 case tok::identifier:
1462 switch (NextToken().getKind()) {
1463 case tok::code_completion:
1464 case tok::coloncolon:
1465 case tok::comma:
1466 case tok::equal:
1467 case tok::equalequal: // Might be a typo for '='.
1468 case tok::kw_alignas:
1469 case tok::kw_asm:
1470 case tok::kw___attribute:
1471 case tok::l_brace:
1472 case tok::l_paren:
1473 case tok::l_square:
1474 case tok::less:
1475 case tok::r_brace:
1476 case tok::r_paren:
1477 case tok::r_square:
1478 case tok::semi:
1479 return true;
1480
1481 case tok::colon:
1482 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001483 // and in block scope it's probably a label. Inside a class definition,
1484 // this is a bit-field.
1485 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001486 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001487
1488 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001489 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001490
1491 default:
1492 return false;
1493 }
1494
1495 default:
1496 return false;
1497 }
1498}
1499
Richard Smithb8caac82012-04-11 20:59:20 +00001500/// Skip until we reach something which seems like a sensible place to pick
1501/// up parsing after a malformed declaration. This will sometimes stop sooner
1502/// than SkipUntil(tok::r_brace) would, but will never stop later.
1503void Parser::SkipMalformedDecl() {
1504 while (true) {
1505 switch (Tok.getKind()) {
1506 case tok::l_brace:
1507 // Skip until matching }, then stop. We've probably skipped over
1508 // a malformed class or function definition or similar.
1509 ConsumeBrace();
1510 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1511 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1512 // This declaration isn't over yet. Keep skipping.
1513 continue;
1514 }
1515 if (Tok.is(tok::semi))
1516 ConsumeToken();
1517 return;
1518
1519 case tok::l_square:
1520 ConsumeBracket();
1521 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1522 continue;
1523
1524 case tok::l_paren:
1525 ConsumeParen();
1526 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1527 continue;
1528
1529 case tok::r_brace:
1530 return;
1531
1532 case tok::semi:
1533 ConsumeToken();
1534 return;
1535
1536 case tok::kw_inline:
1537 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001538 // a good place to pick back up parsing, except in an Objective-C
1539 // @interface context.
1540 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1541 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001542 return;
1543 break;
1544
1545 case tok::kw_namespace:
1546 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001547 // place to pick back up parsing, except in an Objective-C
1548 // @interface context.
1549 if (Tok.isAtStartOfLine() &&
1550 (!ParsingInObjCContainer || CurParsedObjCImpl))
1551 return;
1552 break;
1553
1554 case tok::at:
1555 // @end is very much like } in Objective-C contexts.
1556 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1557 ParsingInObjCContainer)
1558 return;
1559 break;
1560
1561 case tok::minus:
1562 case tok::plus:
1563 // - and + probably start new method declarations in Objective-C contexts.
1564 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001565 return;
1566 break;
1567
1568 case tok::eof:
1569 return;
1570
1571 default:
1572 break;
1573 }
1574
1575 ConsumeAnyToken();
1576 }
1577}
1578
John McCalld5a36322009-11-03 19:26:08 +00001579/// ParseDeclGroup - Having concluded that this is either a function
1580/// definition or a group of object declarations, actually parse the
1581/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001582Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1583 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001584 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001585 SourceLocation *DeclEnd,
1586 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001587 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001588 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001589 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001590
John McCalld5a36322009-11-03 19:26:08 +00001591 // Bail out if the first declarator didn't seem well-formed.
1592 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001593 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001594 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001595 }
Mike Stump11289f42009-09-09 15:08:12 +00001596
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001597 // Save late-parsed attributes for now; they need to be parsed in the
1598 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001599 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1600 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001601 if (D.isFunctionDeclarator())
1602 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1603
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001604 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001605 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001606 // Look at the next token to make sure that this isn't a function
1607 // declaration. We have to check this because __attribute__ might be the
1608 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001609 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 if (AllowFunctionDefinitions) {
1612 if (isStartOfFunctionDefinition(D)) {
1613 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1614 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001615
Douglas Gregor012efe22013-04-16 16:01:32 +00001616 // Recover by treating the 'typedef' as spurious.
1617 DS.ClearStorageClassSpecs();
1618 }
1619
1620 Decl *TheDecl =
1621 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1622 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001623 }
1624
Douglas Gregor012efe22013-04-16 16:01:32 +00001625 if (isDeclarationSpecifier()) {
1626 // If there is an invalid declaration specifier right after the function
1627 // prototype, then we must be in a missing semicolon case where this isn't
1628 // actually a body. Just fall through into the code that handles it as a
1629 // prototype, and let the top-level code handle the erroneous declspec
1630 // where it would otherwise expect a comma or semicolon.
1631 } else {
1632 Diag(Tok, diag::err_expected_fn_body);
1633 SkipUntil(tok::semi);
1634 return DeclGroupPtrTy();
1635 }
John McCalld5a36322009-11-03 19:26:08 +00001636 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001637 if (Tok.is(tok::l_brace)) {
1638 Diag(Tok, diag::err_function_definition_not_allowed);
1639 SkipUntil(tok::r_brace, true, true);
1640 }
John McCalld5a36322009-11-03 19:26:08 +00001641 }
1642 }
1643
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001644 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001645 return DeclGroupPtrTy();
1646
1647 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1648 // must parse and analyze the for-range-initializer before the declaration is
1649 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001650 //
1651 // Handle the Objective-C for-in loop variable similarly, although we
1652 // don't need to parse the container in advance.
1653 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1654 bool IsForRangeLoop = false;
1655 if (Tok.is(tok::colon)) {
1656 IsForRangeLoop = true;
1657 FRI->ColonLoc = ConsumeToken();
1658 if (Tok.is(tok::l_brace))
1659 FRI->RangeExpr = ParseBraceInitializer();
1660 else
1661 FRI->RangeExpr = ParseExpression();
1662 }
1663
Richard Smith02e85f32011-04-14 22:09:26 +00001664 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001665 if (IsForRangeLoop)
1666 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001667 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001668 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001669 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1670 }
1671
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001672 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001673 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001674 if (LateParsedAttrs.size() > 0)
1675 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001676 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001677 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001678 DeclsInGroup.push_back(FirstDecl);
1679
Richard Smith09f76ee2011-10-19 21:33:05 +00001680 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001681
John McCalld5a36322009-11-03 19:26:08 +00001682 // If we don't have a comma, it is either the end of the list (a ';') or an
1683 // error, bail out.
1684 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001685 SourceLocation CommaLoc = ConsumeToken();
1686
1687 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1688 // This comma was followed by a line-break and something which can't be
1689 // the start of a declarator. The comma was probably a typo for a
1690 // semicolon.
1691 Diag(CommaLoc, diag::err_expected_semi_declaration)
1692 << FixItHint::CreateReplacement(CommaLoc, ";");
1693 ExpectSemi = false;
1694 break;
1695 }
John McCalld5a36322009-11-03 19:26:08 +00001696
1697 // Parse the next declarator.
1698 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001699 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001700
1701 // Accept attributes in an init-declarator. In the first declarator in a
1702 // declaration, these would be part of the declspec. In subsequent
1703 // declarators, they become part of the declarator itself, so that they
1704 // don't apply to declarators after *this* one. Examples:
1705 // short __attribute__((common)) var; -> declspec
1706 // short var __attribute__((common)); -> declarator
1707 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001708 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001709
1710 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001711 if (!D.isInvalidType()) {
1712 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1713 D.complete(ThisDecl);
1714 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001715 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001716 }
John McCalld5a36322009-11-03 19:26:08 +00001717 }
1718
1719 if (DeclEnd)
1720 *DeclEnd = Tok.getLocation();
1721
Richard Smith09f76ee2011-10-19 21:33:05 +00001722 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001723 ExpectAndConsumeSemi(Context == Declarator::FileContext
1724 ? diag::err_invalid_token_after_toplevel_declarator
1725 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001726 // Okay, there was no semicolon and one was expected. If we see a
1727 // declaration specifier, just assume it was missing and continue parsing.
1728 // Otherwise things are very confused and we skip to recover.
1729 if (!isDeclarationSpecifier()) {
1730 SkipUntil(tok::r_brace, true, true);
1731 if (Tok.is(tok::semi))
1732 ConsumeToken();
1733 }
John McCalld5a36322009-11-03 19:26:08 +00001734 }
1735
Douglas Gregor0be31a22010-07-02 17:43:08 +00001736 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001737 DeclsInGroup.data(),
1738 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001739}
1740
Richard Smith02e85f32011-04-14 22:09:26 +00001741/// Parse an optional simple-asm-expr and attributes, and attach them to a
1742/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001743bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001744 // If a simple-asm-expr is present, parse it.
1745 if (Tok.is(tok::kw_asm)) {
1746 SourceLocation Loc;
1747 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1748 if (AsmLabel.isInvalid()) {
1749 SkipUntil(tok::semi, true, true);
1750 return true;
1751 }
1752
1753 D.setAsmLabel(AsmLabel.release());
1754 D.SetRangeEnd(Loc);
1755 }
1756
1757 MaybeParseGNUAttributes(D);
1758 return false;
1759}
1760
Douglas Gregor23996282009-05-12 21:31:51 +00001761/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1762/// declarator'. This method parses the remainder of the declaration
1763/// (including any attributes or initializer, among other things) and
1764/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001765///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001766/// init-declarator: [C99 6.7]
1767/// declarator
1768/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001769/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1770/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001771/// [C++] declarator initializer[opt]
1772///
1773/// [C++] initializer:
1774/// [C++] '=' initializer-clause
1775/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001776/// [C++0x] '=' 'default' [TODO]
1777/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001778/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001779///
1780/// According to the standard grammar, =default and =delete are function
1781/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001782///
John McCall48871652010-08-21 09:40:31 +00001783Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001784 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001785 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001786 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001787
Richard Smith02e85f32011-04-14 22:09:26 +00001788 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1789}
Mike Stump11289f42009-09-09 15:08:12 +00001790
Richard Smith02e85f32011-04-14 22:09:26 +00001791Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1792 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001793 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001794 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001795 switch (TemplateInfo.Kind) {
1796 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001797 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001798 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001799
Douglas Gregor450f00842009-09-25 18:43:00 +00001800 case ParsedTemplateInfo::Template:
1801 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001802 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001803 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001804 D);
1805 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001806
Douglas Gregor450f00842009-09-25 18:43:00 +00001807 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001808 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001809 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001810 TemplateInfo.ExternLoc,
1811 TemplateInfo.TemplateLoc,
1812 D);
1813 if (ThisRes.isInvalid()) {
1814 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001815 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001816 }
Chad Rosierc1183952012-06-26 22:30:43 +00001817
Douglas Gregor450f00842009-09-25 18:43:00 +00001818 ThisDecl = ThisRes.get();
1819 break;
1820 }
1821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Richard Smith74aeef52013-04-26 16:15:35 +00001823 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001824
Douglas Gregor23996282009-05-12 21:31:51 +00001825 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001826 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001827 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001828 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001829 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001830 if (D.isFunctionDeclarator())
1831 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1832 << 1 /* delete */;
1833 else
1834 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001835 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001836 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001837 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1838 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001839 else
1840 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001841 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001842 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001843 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001844 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001845 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001846
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001847 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001848 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001849 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001850 cutOffParsing();
1851 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001852 }
Chad Rosierc1183952012-06-26 22:30:43 +00001853
John McCalldadc5752010-08-24 06:29:42 +00001854 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001855
David Blaikiebbafb8a2012-03-11 07:00:24 +00001856 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001857 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001858 ExitScope();
1859 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001860
Douglas Gregor23996282009-05-12 21:31:51 +00001861 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001862 SkipUntil(tok::comma, true, true);
1863 Actions.ActOnInitializerError(ThisDecl);
1864 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001865 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1866 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001867 }
1868 } else if (Tok.is(tok::l_paren)) {
1869 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001870 BalancedDelimiterTracker T(*this, tok::l_paren);
1871 T.consumeOpen();
1872
Benjamin Kramerf0623432012-08-23 22:51:59 +00001873 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001874 CommaLocsTy CommaLocs;
1875
David Blaikiebbafb8a2012-03-11 07:00:24 +00001876 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001877 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001878 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001879 }
1880
Douglas Gregor23996282009-05-12 21:31:51 +00001881 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001882 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001883 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001884
David Blaikiebbafb8a2012-03-11 07:00:24 +00001885 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001886 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001887 ExitScope();
1888 }
Douglas Gregor23996282009-05-12 21:31:51 +00001889 } else {
1890 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001891 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001892
1893 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1894 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001895
David Blaikiebbafb8a2012-03-11 07:00:24 +00001896 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001897 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001898 ExitScope();
1899 }
1900
Sebastian Redla9351792012-02-11 23:51:47 +00001901 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1902 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001903 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001904 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1905 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001906 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001907 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001908 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001909 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001910 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1911
Sebastian Redl3da34892011-06-05 12:23:16 +00001912 if (D.getCXXScopeSpec().isSet()) {
1913 EnterScope(0);
1914 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1915 }
1916
1917 ExprResult Init(ParseBraceInitializer());
1918
1919 if (D.getCXXScopeSpec().isSet()) {
1920 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1921 ExitScope();
1922 }
1923
1924 if (Init.isInvalid()) {
1925 Actions.ActOnInitializerError(ThisDecl);
1926 } else
1927 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1928 /*DirectInit=*/true, TypeContainsAuto);
1929
Douglas Gregor23996282009-05-12 21:31:51 +00001930 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001931 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001932 }
1933
Richard Smithb2bc2e62011-02-21 20:05:19 +00001934 Actions.FinalizeDeclaration(ThisDecl);
1935
Douglas Gregor23996282009-05-12 21:31:51 +00001936 return ThisDecl;
1937}
1938
Chris Lattner1890ac82006-08-13 01:16:23 +00001939/// ParseSpecifierQualifierList
1940/// specifier-qualifier-list:
1941/// type-specifier specifier-qualifier-list[opt]
1942/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001943/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001944///
Richard Smithc5b05522012-03-12 07:56:15 +00001945void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1946 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001947 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1948 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001949 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001950 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001951
Chris Lattner1890ac82006-08-13 01:16:23 +00001952 // Validate declspec for type-name.
1953 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001954 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1955 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001956 Diag(Tok, diag::err_expected_type);
1957 DS.SetTypeSpecError();
1958 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1959 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001960 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001961 if (!DS.hasTypeSpecifier())
1962 DS.SetTypeSpecError();
1963 }
Mike Stump11289f42009-09-09 15:08:12 +00001964
Chris Lattner1b22eed2006-11-28 05:12:07 +00001965 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001966 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001967 if (DS.getStorageClassSpecLoc().isValid())
1968 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1969 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001970 Diag(DS.getThreadStorageClassSpecLoc(),
1971 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001972 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001973 }
Mike Stump11289f42009-09-09 15:08:12 +00001974
Chris Lattner1b22eed2006-11-28 05:12:07 +00001975 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001976 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001977 if (DS.isInlineSpecified())
1978 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1979 if (DS.isVirtualSpecified())
1980 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1981 if (DS.isExplicitSpecified())
1982 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001983 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001984 }
Richard Smithc5b05522012-03-12 07:56:15 +00001985
1986 // Issue diagnostic and remove constexpr specfier if present.
1987 if (DS.isConstexprSpecified()) {
1988 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1989 DS.ClearConstexprSpec();
1990 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001991}
Chris Lattner53361ac2006-08-10 05:19:57 +00001992
Chris Lattner6cc055a2009-04-12 20:42:31 +00001993/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1994/// specified token is valid after the identifier in a declarator which
1995/// immediately follows the declspec. For example, these things are valid:
1996///
1997/// int x [ 4]; // direct-declarator
1998/// int x ( int y); // direct-declarator
1999/// int(int x ) // direct-declarator
2000/// int x ; // simple-declaration
2001/// int x = 17; // init-declarator-list
2002/// int x , y; // init-declarator-list
2003/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002004/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002005/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002006///
2007/// This is not, because 'x' does not immediately follow the declspec (though
2008/// ')' happens to be valid anyway).
2009/// int (x)
2010///
2011static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2012 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2013 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002014 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002015}
2016
Chris Lattner20a0c612009-04-14 21:34:55 +00002017
2018/// ParseImplicitInt - This method is called when we have an non-typename
2019/// identifier in a declspec (which normally terminates the decl spec) when
2020/// the declspec has no type specifier. In this case, the declspec is either
2021/// malformed or is "implicit int" (in K&R and C89).
2022///
2023/// This method handles diagnosing this prettily and returns false if the
2024/// declspec is done being processed. If it recovers and thinks there may be
2025/// other pieces of declspec after it, it returns true.
2026///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002027bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002028 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002029 AccessSpecifier AS, DeclSpecContext DSC,
2030 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002031 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002032
Chris Lattner20a0c612009-04-14 21:34:55 +00002033 SourceLocation Loc = Tok.getLocation();
2034 // If we see an identifier that is not a type name, we normally would
2035 // parse it as the identifer being declared. However, when a typename
2036 // is typo'd or the definition is not included, this will incorrectly
2037 // parse the typename as the identifier name and fall over misparsing
2038 // later parts of the diagnostic.
2039 //
2040 // As such, we try to do some look-ahead in cases where this would
2041 // otherwise be an "implicit-int" case to see if this is invalid. For
2042 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2043 // an identifier with implicit int, we'd get a parse error because the
2044 // next token is obviously invalid for a type. Parse these as a case
2045 // with an invalid type specifier.
2046 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002047
Chris Lattner20a0c612009-04-14 21:34:55 +00002048 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002049 // error, do lookahead to try to do better recovery. This never applies
2050 // within a type specifier. Outside of C++, we allow this even if the
2051 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002052 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002053 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002054 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002055 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002056 // If this token is valid for implicit int, e.g. "static x = 4", then
2057 // we just avoid eating the identifier, so it will be parsed as the
2058 // identifier in the declarator.
2059 return false;
2060 }
Mike Stump11289f42009-09-09 15:08:12 +00002061
Richard Smitha952ebb2012-05-15 21:01:51 +00002062 if (getLangOpts().CPlusPlus &&
2063 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2064 // Don't require a type specifier if we have the 'auto' storage class
2065 // specifier in C++98 -- we'll promote it to a type specifier.
2066 return false;
2067 }
2068
Chris Lattner20a0c612009-04-14 21:34:55 +00002069 // Otherwise, if we don't consume this token, we are going to emit an
2070 // error anyway. Try to recover from various common problems. Check
2071 // to see if this was a reference to a tag name without a tag specified.
2072 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002073 //
2074 // C++ doesn't need this, and isTagName doesn't take SS.
2075 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002076 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002077 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002078
Douglas Gregor0be31a22010-07-02 17:43:08 +00002079 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002080 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002081 case DeclSpec::TST_enum:
2082 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2083 case DeclSpec::TST_union:
2084 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2085 case DeclSpec::TST_struct:
2086 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002087 case DeclSpec::TST_interface:
2088 TagName="__interface"; FixitTagName = "__interface ";
2089 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002090 case DeclSpec::TST_class:
2091 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002092 }
Mike Stump11289f42009-09-09 15:08:12 +00002093
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002094 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002095 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2096 LookupResult R(Actions, TokenName, SourceLocation(),
2097 Sema::LookupOrdinaryName);
2098
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002099 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002100 << TokenName << TagName << getLangOpts().CPlusPlus
2101 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2102
2103 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2104 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2105 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002106 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002107 << TokenName << TagName;
2108 }
Mike Stump11289f42009-09-09 15:08:12 +00002109
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002110 // Parse this as a tag as if the missing tag were present.
2111 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002112 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002113 else
Richard Smithc5b05522012-03-12 07:56:15 +00002114 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002115 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002116 return true;
2117 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002118 }
Mike Stump11289f42009-09-09 15:08:12 +00002119
Richard Smithfe904f02012-05-15 21:29:55 +00002120 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002121 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002122 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2123 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002124 // Look ahead to the next token to try to figure out what this declaration
2125 // was supposed to be.
2126 switch (NextToken().getKind()) {
2127 case tok::comma:
2128 case tok::equal:
2129 case tok::kw_asm:
2130 case tok::l_brace:
2131 case tok::l_square:
2132 case tok::semi:
2133 // This looks like a variable declaration. The type is probably missing.
2134 // We're done parsing decl-specifiers.
2135 return false;
2136
2137 case tok::l_paren: {
2138 // static x(4); // 'x' is not a type
2139 // x(int n); // 'x' is not a type
2140 // x (*p)[]; // 'x' is a type
2141 //
2142 // Since we're in an error case (or the rare 'implicit int in C++' MS
2143 // extension), we can afford to perform a tentative parse to determine
2144 // which case we're in.
2145 TentativeParsingAction PA(*this);
2146 ConsumeToken();
2147 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2148 PA.Revert();
2149 if (TPR == TPResult::False())
2150 return false;
2151 // The identifier is followed by a parenthesized declarator.
2152 // It's supposed to be a type.
2153 break;
2154 }
2155
2156 default:
2157 // This is probably supposed to be a type. This includes cases like:
2158 // int f(itn);
2159 // struct S { unsinged : 4; };
2160 break;
2161 }
2162 }
2163
Chad Rosierc1183952012-06-26 22:30:43 +00002164 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002165 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002166 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002167 IdentifierInfo *II = Tok.getIdentifierInfo();
2168 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002169 // The action emitted a diagnostic, so we don't have to.
2170 if (T) {
2171 // The action has suggested that the type T could be used. Set that as
2172 // the type in the declaration specifiers, consume the would-be type
2173 // name token, and we're done.
2174 const char *PrevSpec;
2175 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002176 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002177 DS.SetRangeEnd(Tok.getLocation());
2178 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002179 // There may be other declaration specifiers after this.
2180 return true;
2181 } else if (II != Tok.getIdentifierInfo()) {
2182 // If no type was suggested, the correction is to a keyword
2183 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002184 // There may be other declaration specifiers after this.
2185 return true;
2186 }
Chad Rosierc1183952012-06-26 22:30:43 +00002187
Douglas Gregor15e56022009-10-13 23:27:22 +00002188 // Fall through; the action had no suggestion for us.
2189 } else {
2190 // The action did not emit a diagnostic, so emit one now.
2191 SourceRange R;
2192 if (SS) R = SS->getRange();
2193 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2194 }
Mike Stump11289f42009-09-09 15:08:12 +00002195
Douglas Gregor15e56022009-10-13 23:27:22 +00002196 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002197 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002198 DS.SetRangeEnd(Tok.getLocation());
2199 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002200
Chris Lattner20a0c612009-04-14 21:34:55 +00002201 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2202 // avoid rippling error messages on subsequent uses of the same type,
2203 // could be useful if #include was forgotten.
2204 return false;
2205}
2206
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002207/// \brief Determine the declaration specifier context from the declarator
2208/// context.
2209///
2210/// \param Context the declarator context, which is one of the
2211/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002212Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002213Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2214 if (Context == Declarator::MemberContext)
2215 return DSC_class;
2216 if (Context == Declarator::FileContext)
2217 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002218 if (Context == Declarator::TrailingReturnContext)
2219 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002220 return DSC_normal;
2221}
2222
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002223/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2224///
2225/// FIXME: Simply returns an alignof() expression if the argument is a
2226/// type. Ideally, the type should be propagated directly into Sema.
2227///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002228/// [C11] type-id
2229/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002230/// [C++0x] type-id ...[opt]
2231/// [C++0x] assignment-expression ...[opt]
2232ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2233 SourceLocation &EllipsisLoc) {
2234 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002235 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002236 SourceLocation TypeLoc = Tok.getLocation();
2237 ParsedType Ty = ParseTypeName().get();
2238 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002239 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2240 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002241 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002242 ER = ParseConstantExpression();
2243
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002244 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002245 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002246
2247 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002248}
2249
2250/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2251/// attribute to Attrs.
2252///
2253/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002254/// [C11] '_Alignas' '(' type-id ')'
2255/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002256/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2257/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002258void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002259 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002260 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2261 "Not an alignment-specifier!");
2262
Richard Smithd11c7a12013-01-29 01:48:07 +00002263 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2264 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002265
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002266 BalancedDelimiterTracker T(*this, tok::l_paren);
2267 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002268 return;
2269
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002270 SourceLocation EllipsisLoc;
2271 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002272 if (ArgExpr.isInvalid()) {
2273 SkipUntil(tok::r_paren);
2274 return;
2275 }
2276
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002277 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002278 if (EndLoc)
2279 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002280
Benjamin Kramerf0623432012-08-23 22:51:59 +00002281 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002282 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002283 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smith44c247f2013-02-22 08:32:16 +00002284 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002285}
2286
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002287/// ParseDeclarationSpecifiers
2288/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002289/// storage-class-specifier declaration-specifiers[opt]
2290/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002291/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002292/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002293/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002294/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002295///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002296/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002297/// 'typedef'
2298/// 'extern'
2299/// 'static'
2300/// 'auto'
2301/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002302/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002303/// [C++11] 'thread_local'
2304/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002305/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002306/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002307/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002308/// [C++] 'virtual'
2309/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002310/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002311/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002312/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002313
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002314///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002315void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002316 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002317 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002318 DeclSpecContext DSContext,
2319 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002320 if (DS.getSourceRange().isInvalid()) {
2321 DS.SetRangeStart(Tok.getLocation());
2322 DS.SetRangeEnd(Tok.getLocation());
2323 }
Chad Rosierc1183952012-06-26 22:30:43 +00002324
Douglas Gregordf593fb2011-11-07 17:33:42 +00002325 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002326 bool AttrsLastTime = false;
2327 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002328 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002329 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002330 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002331 unsigned DiagID = 0;
2332
Chris Lattner4d8f8732006-11-28 05:05:08 +00002333 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002334
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002335 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002336 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002337 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002338 if (!AttrsLastTime)
2339 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002340 else {
2341 // Reject C++11 attributes that appertain to decl specifiers as
2342 // we don't support any C++11 attributes that appertain to decl
2343 // specifiers. This also conforms to what g++ 4.8 is doing.
2344 ProhibitCXX11Attributes(attrs);
2345
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002346 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002347 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002348
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002349 // If this is not a declaration specifier token, we're done reading decl
2350 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002351 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002352 return;
Mike Stump11289f42009-09-09 15:08:12 +00002353
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002354 case tok::l_square:
2355 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002356 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002357 goto DoneWithDeclSpec;
2358
2359 ProhibitAttributes(attrs);
2360 // FIXME: It would be good to recover by accepting the attributes,
2361 // but attempting to do that now would cause serious
2362 // madness in terms of diagnostics.
2363 attrs.clear();
2364 attrs.Range = SourceRange();
2365
2366 ParseCXX11Attributes(attrs);
2367 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002368 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002369
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002370 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002371 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002372 if (DS.hasTypeSpecifier()) {
2373 bool AllowNonIdentifiers
2374 = (getCurScope()->getFlags() & (Scope::ControlScope |
2375 Scope::BlockScope |
2376 Scope::TemplateParamScope |
2377 Scope::FunctionPrototypeScope |
2378 Scope::AtCatchScope)) == 0;
2379 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002380 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002381 (DSContext == DSC_class && DS.isFriendSpecified());
2382
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002383 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002384 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002385 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002386 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002387 }
2388
Douglas Gregor80039242011-02-15 20:33:25 +00002389 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2390 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2391 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002392 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002393 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002394 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002395 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002396 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002397 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002398
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002399 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002400 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002401 }
2402
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002403 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002404 // C++ scope specifier. Annotate and loop, or bail out on error.
2405 if (TryAnnotateCXXScopeToken(true)) {
2406 if (!DS.hasTypeSpecifier())
2407 DS.SetTypeSpecError();
2408 goto DoneWithDeclSpec;
2409 }
John McCall8bc2a702010-03-01 18:20:46 +00002410 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2411 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002412 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002413
2414 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002415 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002416 goto DoneWithDeclSpec;
2417
John McCall9dab4e62009-12-12 11:40:51 +00002418 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002419 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2420 Tok.getAnnotationRange(),
2421 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002422
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002423 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002424 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002425 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002426 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002427 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002428 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002429
2430 // C++ [class.qual]p2:
2431 // In a lookup in which the constructor is an acceptable lookup
2432 // result and the nested-name-specifier nominates a class C:
2433 //
2434 // - if the name specified after the
2435 // nested-name-specifier, when looked up in C, is the
2436 // injected-class-name of C (Clause 9), or
2437 //
2438 // - if the name specified after the nested-name-specifier
2439 // is the same as the identifier or the
2440 // simple-template-id's template-name in the last
2441 // component of the nested-name-specifier,
2442 //
2443 // the name is instead considered to name the constructor of
2444 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002445 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002446 // Thus, if the template-name is actually the constructor
2447 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002448 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002449 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002450 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002451 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002452 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002453 if (isConstructorDeclarator()) {
2454 // The user meant this to be an out-of-line constructor
2455 // definition, but template arguments are not allowed
2456 // there. Just allow this as a constructor; we'll
2457 // complain about it later.
2458 goto DoneWithDeclSpec;
2459 }
2460
2461 // The user meant this to name a type, but it actually names
2462 // a constructor with some extraneous template
2463 // arguments. Complain, then parse it as a type as the user
2464 // intended.
2465 Diag(TemplateId->TemplateNameLoc,
2466 diag::err_out_of_line_template_id_names_constructor)
2467 << TemplateId->Name;
2468 }
2469
John McCall9dab4e62009-12-12 11:40:51 +00002470 DS.getTypeSpecScope() = SS;
2471 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002472 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002473 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002474 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002475 continue;
2476 }
2477
Douglas Gregorc5790df2009-09-28 07:26:33 +00002478 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002479 DS.getTypeSpecScope() = SS;
2480 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002481 if (Tok.getAnnotationValue()) {
2482 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002483 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002484 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002485 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002486 if (isInvalid)
2487 break;
John McCallba7bf592010-08-24 05:47:05 +00002488 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002489 else
2490 DS.SetTypeSpecError();
2491 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2492 ConsumeToken(); // The typename
2493 }
2494
Douglas Gregor167fa622009-03-25 15:40:00 +00002495 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002496 goto DoneWithDeclSpec;
2497
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002498 // If we're in a context where the identifier could be a class name,
2499 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002500 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002501 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002502 &SS)) {
2503 if (isConstructorDeclarator())
2504 goto DoneWithDeclSpec;
2505
2506 // As noted in C++ [class.qual]p2 (cited above), when the name
2507 // of the class is qualified in a context where it could name
2508 // a constructor, its a constructor name. However, we've
2509 // looked at the declarator, and the user probably meant this
2510 // to be a type. Complain that it isn't supposed to be treated
2511 // as a type, then proceed to parse it as a type.
2512 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2513 << Next.getIdentifierInfo();
2514 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002515
John McCallba7bf592010-08-24 05:47:05 +00002516 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2517 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002518 getCurScope(), &SS,
2519 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002520 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002521 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002522
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002523 // If the referenced identifier is not a type, then this declspec is
2524 // erroneous: We already checked about that it has no type specifier, and
2525 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002526 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002527 if (TypeRep == 0) {
2528 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002529 ParsedAttributesWithRange Attrs(AttrFactory);
2530 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2531 if (!Attrs.empty()) {
2532 AttrsLastTime = true;
2533 attrs.takeAllFrom(Attrs);
2534 }
2535 continue;
2536 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002537 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002538 }
Mike Stump11289f42009-09-09 15:08:12 +00002539
John McCall9dab4e62009-12-12 11:40:51 +00002540 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002541 ConsumeToken(); // The C++ scope.
2542
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002544 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002545 if (isInvalid)
2546 break;
Mike Stump11289f42009-09-09 15:08:12 +00002547
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002548 DS.SetRangeEnd(Tok.getLocation());
2549 ConsumeToken(); // The typename.
2550
2551 continue;
2552 }
Mike Stump11289f42009-09-09 15:08:12 +00002553
Chris Lattnere387d9e2009-01-21 19:48:37 +00002554 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002555 if (Tok.getAnnotationValue()) {
2556 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002558 DiagID, T);
2559 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002560 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002561
Chris Lattner005fc1b2010-04-05 18:18:31 +00002562 if (isInvalid)
2563 break;
2564
Chris Lattnere387d9e2009-01-21 19:48:37 +00002565 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2566 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002567
Chris Lattnere387d9e2009-01-21 19:48:37 +00002568 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2569 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002570 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002571 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002572 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002573
Chris Lattnere387d9e2009-01-21 19:48:37 +00002574 continue;
2575 }
Mike Stump11289f42009-09-09 15:08:12 +00002576
Douglas Gregor06873092011-04-28 15:48:45 +00002577 case tok::kw___is_signed:
2578 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2579 // typically treats it as a trait. If we see __is_signed as it appears
2580 // in libstdc++, e.g.,
2581 //
2582 // static const bool __is_signed;
2583 //
2584 // then treat __is_signed as an identifier rather than as a keyword.
2585 if (DS.getTypeSpecType() == TST_bool &&
2586 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2587 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2588 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2589 Tok.setKind(tok::identifier);
2590 }
2591
2592 // We're done with the declaration-specifiers.
2593 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002594
Chris Lattner16fac4f2008-07-26 01:18:38 +00002595 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002596 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002597 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002598 // In C++, check to see if this is a scope specifier like foo::bar::, if
2599 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002600 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002601 if (TryAnnotateCXXScopeToken(true)) {
2602 if (!DS.hasTypeSpecifier())
2603 DS.SetTypeSpecError();
2604 goto DoneWithDeclSpec;
2605 }
2606 if (!Tok.is(tok::identifier))
2607 continue;
2608 }
Mike Stump11289f42009-09-09 15:08:12 +00002609
Chris Lattner16fac4f2008-07-26 01:18:38 +00002610 // This identifier can only be a typedef name if we haven't already seen
2611 // a type-specifier. Without this check we misparse:
2612 // typedef int X; struct Y { short X; }; as 'short int'.
2613 if (DS.hasTypeSpecifier())
2614 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002615
John Thompson22334602010-02-05 00:12:22 +00002616 // Check for need to substitute AltiVec keyword tokens.
2617 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2618 break;
2619
Richard Smith3092a3b2012-05-09 18:56:43 +00002620 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2621 // allow the use of a typedef name as a type specifier.
2622 if (DS.isTypeAltiVecVector())
2623 goto DoneWithDeclSpec;
2624
John McCallba7bf592010-08-24 05:47:05 +00002625 ParsedType TypeRep =
2626 Actions.getTypeName(*Tok.getIdentifierInfo(),
2627 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002628
Chris Lattner6cc055a2009-04-12 20:42:31 +00002629 // If this is not a typedef name, don't parse it as part of the declspec,
2630 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002631 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002632 ParsedAttributesWithRange Attrs(AttrFactory);
2633 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2634 if (!Attrs.empty()) {
2635 AttrsLastTime = true;
2636 attrs.takeAllFrom(Attrs);
2637 }
2638 continue;
2639 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002640 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002641 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002642
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002643 // If we're in a context where the identifier could be a class name,
2644 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002645 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002646 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002647 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002648 goto DoneWithDeclSpec;
2649
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002650 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002651 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002652 if (isInvalid)
2653 break;
Mike Stump11289f42009-09-09 15:08:12 +00002654
Chris Lattner16fac4f2008-07-26 01:18:38 +00002655 DS.SetRangeEnd(Tok.getLocation());
2656 ConsumeToken(); // The identifier
2657
2658 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2659 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002660 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002661 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002662 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002663
Steve Naroffcd5e7822008-09-22 10:28:57 +00002664 // Need to support trailing type qualifiers (e.g. "id<p> const").
2665 // If a type specifier follows, it will be diagnosed elsewhere.
2666 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002667 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002668
2669 // type-name
2670 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002671 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002672 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002673 // This template-id does not refer to a type name, so we're
2674 // done with the type-specifiers.
2675 goto DoneWithDeclSpec;
2676 }
2677
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002678 // If we're in a context where the template-id could be a
2679 // constructor name or specialization, check whether this is a
2680 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002681 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002682 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002683 isConstructorDeclarator())
2684 goto DoneWithDeclSpec;
2685
Douglas Gregor7f741122009-02-25 19:37:18 +00002686 // Turn the template-id annotation token into a type annotation
2687 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002688 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002689 continue;
2690 }
2691
Chris Lattnere37e2332006-08-15 04:50:22 +00002692 // GNU attributes support.
2693 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002694 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002695 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002696
2697 // Microsoft declspec support.
2698 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002699 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002700 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002701
Steve Naroff44ac7772008-12-25 14:16:32 +00002702 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002703 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002704 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002705 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002706 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002707 // FIXME: This does not work correctly if it is set to be a declspec
2708 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002709 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002710 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002711 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002712 }
Eli Friedman53339e02009-06-08 23:27:34 +00002713
2714 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002715 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002716 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002717 case tok::kw___cdecl:
2718 case tok::kw___stdcall:
2719 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002720 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002721 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002722 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002723 continue;
2724
Dawn Perchik335e16b2010-09-03 01:29:35 +00002725 // Borland single token adornments.
2726 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002727 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002728 continue;
2729
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002730 // OpenCL single token adornments.
2731 case tok::kw___kernel:
2732 ParseOpenCLAttributes(DS.getAttributes());
2733 continue;
2734
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002735 // storage-class-specifier
2736 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002737 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2738 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002739 break;
2740 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002741 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002742 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002743 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2744 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002745 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002746 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002747 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2748 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002749 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002750 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002751 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002752 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002753 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2754 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002755 break;
2756 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002757 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002758 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002759 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2760 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002761 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002762 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002763 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002764 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2766 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002767 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002768 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2769 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002770 break;
2771 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002772 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2773 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002774 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002775 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002776 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2777 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002778 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002779 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002780 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2781 PrevSpec, DiagID);
2782 break;
2783 case tok::kw_thread_local:
2784 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2785 PrevSpec, DiagID);
2786 break;
2787 case tok::kw__Thread_local:
2788 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2789 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002790 break;
Mike Stump11289f42009-09-09 15:08:12 +00002791
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002792 // function-specifier
2793 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002794 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002795 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002796 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002797 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002798 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002799 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002800 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002801 break;
Richard Smith0015f092013-01-17 22:16:11 +00002802 case tok::kw__Noreturn:
2803 if (!getLangOpts().C11)
2804 Diag(Loc, diag::ext_c11_noreturn);
2805 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2806 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002807
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002808 // alignment-specifier
2809 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002810 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002811 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002812 ParseAlignmentSpecifier(DS.getAttributes());
2813 continue;
2814
Anders Carlssoncd8db412009-05-06 04:46:28 +00002815 // friend
2816 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002817 if (DSContext == DSC_class)
2818 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2819 else {
2820 PrevSpec = ""; // not actually used by the diagnostic
2821 DiagID = diag::err_friend_invalid_in_context;
2822 isInvalid = true;
2823 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002824 break;
Mike Stump11289f42009-09-09 15:08:12 +00002825
Douglas Gregor26701a42011-09-09 02:06:17 +00002826 // Modules
2827 case tok::kw___module_private__:
2828 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2829 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002830
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002831 // constexpr
2832 case tok::kw_constexpr:
2833 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2834 break;
2835
Chris Lattnere387d9e2009-01-21 19:48:37 +00002836 // type-specifier
2837 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002838 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2839 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002840 break;
2841 case tok::kw_long:
2842 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002843 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2844 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002845 else
John McCall49bfce42009-08-03 20:12:06 +00002846 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2847 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002848 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002849 case tok::kw___int64:
2850 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2851 DiagID);
2852 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002853 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002854 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2855 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002856 break;
2857 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002858 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2859 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002860 break;
2861 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002862 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2863 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002864 break;
2865 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002866 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2867 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002868 break;
2869 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002870 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2871 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002872 break;
2873 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002874 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2875 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002876 break;
2877 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002878 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2879 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002880 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002881 case tok::kw___int128:
2882 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2883 DiagID);
2884 break;
2885 case tok::kw_half:
2886 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2887 DiagID);
2888 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002889 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002890 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2891 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002892 break;
2893 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2895 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002896 break;
2897 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2899 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002900 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002901 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2903 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002904 break;
2905 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2907 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002908 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002909 case tok::kw_bool:
2910 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002911 if (Tok.is(tok::kw_bool) &&
2912 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2913 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2914 PrevSpec = ""; // Not used by the diagnostic.
2915 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002916 // For better error recovery.
2917 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002918 isInvalid = true;
2919 } else {
2920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2921 DiagID);
2922 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002923 break;
2924 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2926 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002927 break;
2928 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2930 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002931 break;
2932 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002933 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2934 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002935 break;
John Thompson22334602010-02-05 00:12:22 +00002936 case tok::kw___vector:
2937 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2938 break;
2939 case tok::kw___pixel:
2940 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2941 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002942 case tok::kw_image1d_t:
2943 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2944 PrevSpec, DiagID);
2945 break;
2946 case tok::kw_image1d_array_t:
2947 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2948 PrevSpec, DiagID);
2949 break;
2950 case tok::kw_image1d_buffer_t:
2951 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2952 PrevSpec, DiagID);
2953 break;
2954 case tok::kw_image2d_t:
2955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2956 PrevSpec, DiagID);
2957 break;
2958 case tok::kw_image2d_array_t:
2959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2960 PrevSpec, DiagID);
2961 break;
2962 case tok::kw_image3d_t:
2963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2964 PrevSpec, DiagID);
2965 break;
Guy Benyei61054192013-02-07 10:55:47 +00002966 case tok::kw_sampler_t:
2967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2968 PrevSpec, DiagID);
2969 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002970 case tok::kw_event_t:
2971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2972 PrevSpec, DiagID);
2973 break;
John McCall39439732011-04-09 22:50:59 +00002974 case tok::kw___unknown_anytype:
2975 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2976 PrevSpec, DiagID);
2977 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002978
2979 // class-specifier:
2980 case tok::kw_class:
2981 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002982 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002983 case tok::kw_union: {
2984 tok::TokenKind Kind = Tok.getKind();
2985 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002986
2987 // These are attributes following class specifiers.
2988 // To produce better diagnostic, we parse them when
2989 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002990 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002991 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002992 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002993
2994 // If there are attributes following class specifier,
2995 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002996 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002997 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002998 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002999 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003000 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003001 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003002
3003 // enum-specifier:
3004 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003005 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003006 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003007 continue;
3008
3009 // cv-qualifier:
3010 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003011 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003012 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003013 break;
3014 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003016 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003017 break;
3018 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003019 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003020 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003021 break;
3022
Douglas Gregor333489b2009-03-27 23:10:48 +00003023 // C++ typename-specifier:
3024 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003025 if (TryAnnotateTypeOrScopeToken()) {
3026 DS.SetTypeSpecError();
3027 goto DoneWithDeclSpec;
3028 }
3029 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003030 continue;
3031 break;
3032
Chris Lattnere387d9e2009-01-21 19:48:37 +00003033 // GNU typeof support.
3034 case tok::kw_typeof:
3035 ParseTypeofSpecifier(DS);
3036 continue;
3037
David Blaikie15a430a2011-12-04 05:04:18 +00003038 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003039 ParseDecltypeSpecifier(DS);
3040 continue;
3041
Alexis Hunt4a257072011-05-19 05:37:45 +00003042 case tok::kw___underlying_type:
3043 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003044 continue;
3045
3046 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003047 // C11 6.7.2.4/4:
3048 // If the _Atomic keyword is immediately followed by a left parenthesis,
3049 // it is interpreted as a type specifier (with a type name), not as a
3050 // type qualifier.
3051 if (NextToken().is(tok::l_paren)) {
3052 ParseAtomicSpecifier(DS);
3053 continue;
3054 }
3055 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3056 getLangOpts());
3057 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003058
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003059 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003060 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003061 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003062 goto DoneWithDeclSpec;
3063 case tok::kw___private:
3064 case tok::kw___global:
3065 case tok::kw___local:
3066 case tok::kw___constant:
3067 case tok::kw___read_only:
3068 case tok::kw___write_only:
3069 case tok::kw___read_write:
3070 ParseOpenCLQualifiers(DS);
3071 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003072
Steve Naroffcfdf6162008-06-05 00:02:44 +00003073 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003074 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003075 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3076 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003077 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003078 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003079
Douglas Gregor3a001f42010-11-19 17:10:50 +00003080 if (!ParseObjCProtocolQualifiers(DS))
3081 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3082 << FixItHint::CreateInsertion(Loc, "id")
3083 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003084
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003085 // Need to support trailing type qualifiers (e.g. "id<p> const").
3086 // If a type specifier follows, it will be diagnosed elsewhere.
3087 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003088 }
John McCall49bfce42009-08-03 20:12:06 +00003089 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003090 if (isInvalid) {
3091 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003092 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003093
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003094 if (DiagID == diag::ext_duplicate_declspec)
3095 Diag(Tok, DiagID)
3096 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3097 else
3098 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003099 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003100
Chris Lattner2e232092008-03-13 06:29:04 +00003101 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003102 if (DiagID != diag::err_bool_redeclaration)
3103 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003104
3105 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003106 }
3107}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003108
Chris Lattner70ae4912007-10-29 04:42:53 +00003109/// ParseStructDeclaration - Parse a struct declaration without the terminating
3110/// semicolon.
3111///
Chris Lattner90a26b02007-01-23 04:38:16 +00003112/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003113/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003114/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003115/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003116/// struct-declarator-list:
3117/// struct-declarator
3118/// struct-declarator-list ',' struct-declarator
3119/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3120/// struct-declarator:
3121/// declarator
3122/// [GNU] declarator attributes[opt]
3123/// declarator[opt] ':' constant-expression
3124/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3125///
Chris Lattnera12405b2008-04-10 06:46:29 +00003126void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003127ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003128
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003129 if (Tok.is(tok::kw___extension__)) {
3130 // __extension__ silences extension warnings in the subexpression.
3131 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003132 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003133 return ParseStructDeclaration(DS, Fields);
3134 }
Mike Stump11289f42009-09-09 15:08:12 +00003135
Steve Naroff97170802007-08-20 22:28:22 +00003136 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003137 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003138
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003139 // If there are no declarators, this is a free-standing declaration
3140 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003141 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003142 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3143 DS);
3144 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003145 return;
3146 }
3147
3148 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003149 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003150 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003151 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003152 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003153 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003154
Bill Wendling44426052012-12-20 19:22:21 +00003155 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003156 if (!FirstDeclarator)
3157 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003158
Steve Naroff97170802007-08-20 22:28:22 +00003159 /// struct-declarator: declarator
3160 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003161 if (Tok.isNot(tok::colon)) {
3162 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3163 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003164 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003165 }
Mike Stump11289f42009-09-09 15:08:12 +00003166
Chris Lattner76c72282007-10-09 17:33:22 +00003167 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003168 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003169 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003170 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003171 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003172 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003173 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003174 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003175
Steve Naroff97170802007-08-20 22:28:22 +00003176 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003177 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003178
John McCallcfefb6d2009-11-03 02:38:08 +00003179 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003180 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003181
Steve Naroff97170802007-08-20 22:28:22 +00003182 // If we don't have a comma, it is either the end of the list (a ';')
3183 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003184 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003185 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003186
Steve Naroff97170802007-08-20 22:28:22 +00003187 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003188 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003189
John McCallcfefb6d2009-11-03 02:38:08 +00003190 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003191 }
Steve Naroff97170802007-08-20 22:28:22 +00003192}
3193
3194/// ParseStructUnionBody
3195/// struct-contents:
3196/// struct-declaration-list
3197/// [EXT] empty
3198/// [GNU] "struct-declaration-list" without terminatoring ';'
3199/// struct-declaration-list:
3200/// struct-declaration
3201/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003202/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003203///
Chris Lattner1300fb92007-01-23 23:42:53 +00003204void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003205 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003206 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3207 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003208 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003209
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003210 BalancedDelimiterTracker T(*this, tok::l_brace);
3211 if (T.consumeOpen())
3212 return;
Mike Stump11289f42009-09-09 15:08:12 +00003213
Douglas Gregor658b9552009-01-09 22:42:13 +00003214 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003215 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003216
Andy Gibbs22e140b2013-04-03 09:31:19 +00003217 // Empty structs are an extension in C (C99 6.7.2.1p7).
3218 if (Tok.is(tok::r_brace)) {
Richard Smithe4345902011-12-29 21:57:33 +00003219 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3220 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3221 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003222
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003223 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003224
Chris Lattner7b9ace62007-01-23 20:11:08 +00003225 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003226 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003227 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003228
Chris Lattner736ed5d2007-06-09 05:59:07 +00003229 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003230 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003231 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003232 continue;
3233 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003234
Andy Gibbsc804e082013-04-03 09:46:04 +00003235 // Parse _Static_assert declaration.
3236 if (Tok.is(tok::kw__Static_assert)) {
3237 SourceLocation DeclEnd;
3238 ParseStaticAssertDeclaration(DeclEnd);
3239 continue;
3240 }
3241
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003242 if (Tok.is(tok::annot_pragma_pack)) {
3243 HandlePragmaPack();
3244 continue;
3245 }
3246
3247 if (Tok.is(tok::annot_pragma_align)) {
3248 HandlePragmaAlign();
3249 continue;
3250 }
3251
John McCallcfefb6d2009-11-03 02:38:08 +00003252 if (!Tok.is(tok::at)) {
3253 struct CFieldCallback : FieldCallback {
3254 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003255 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003256 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003257
John McCall48871652010-08-21 09:40:31 +00003258 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003259 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003260 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3261
Eli Friedman934dbbf2012-08-08 23:53:27 +00003262 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003263 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003264 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003265 FD.D.getDeclSpec().getSourceRange().getBegin(),
3266 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003267 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003268 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003269 }
John McCallcfefb6d2009-11-03 02:38:08 +00003270 } Callback(*this, TagDecl, FieldDecls);
3271
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003272 // Parse all the comma separated declarators.
3273 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003274 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003275 } else { // Handle @defs
3276 ConsumeToken();
3277 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3278 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003279 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003280 continue;
3281 }
3282 ConsumeToken();
3283 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3284 if (!Tok.is(tok::identifier)) {
3285 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003286 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003287 continue;
3288 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003289 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003290 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003291 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003292 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3293 ConsumeToken();
3294 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003295 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003296
Chris Lattner76c72282007-10-09 17:33:22 +00003297 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003298 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003299 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003300 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003301 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003302 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003303 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3304 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003305 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003306 // If we stopped at a ';', eat it.
3307 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003308 }
3309 }
Mike Stump11289f42009-09-09 15:08:12 +00003310
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003311 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003312
John McCall084e83d2011-03-24 11:26:52 +00003313 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003314 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003315 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003316
Douglas Gregor0be31a22010-07-02 17:43:08 +00003317 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003318 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003319 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003320 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003321 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003322 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3323 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003324}
3325
Chris Lattner3b561a32006-08-13 00:12:11 +00003326/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003327/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003328/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003329///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003330/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3331/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003332/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3333/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003334/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003335/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003336///
Richard Smith7d137e32012-03-23 03:33:32 +00003337/// [C++11] enum-head '{' enumerator-list[opt] '}'
3338/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003339///
Richard Smith7d137e32012-03-23 03:33:32 +00003340/// enum-head: [C++11]
3341/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3342/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3343/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003344///
Richard Smith7d137e32012-03-23 03:33:32 +00003345/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003346/// 'enum'
3347/// 'enum' 'class'
3348/// 'enum' 'struct'
3349///
Richard Smith7d137e32012-03-23 03:33:32 +00003350/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003351/// ':' type-specifier-seq
3352///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003353/// [C++] elaborated-type-specifier:
3354/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3355///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003356void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003357 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003358 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003359 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003360 if (Tok.is(tok::code_completion)) {
3361 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003362 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003363 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003364 }
John McCallcb432fa2011-07-06 05:58:41 +00003365
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003366 // If attributes exist after tag, parse them.
3367 ParsedAttributesWithRange attrs(AttrFactory);
3368 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003369 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003370
3371 // If declspecs exist after tag, parse them.
3372 while (Tok.is(tok::kw___declspec))
3373 ParseMicrosoftDeclSpec(attrs);
3374
Richard Smith0f8ee222012-01-10 01:33:14 +00003375 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003376 bool IsScopedUsingClassTag = false;
3377
John McCallbeae29a2012-06-23 22:30:04 +00003378 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003379 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3380 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3381 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003382 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003383 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003384
Bill Wendling44426052012-12-20 19:22:21 +00003385 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003386 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003387 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003388
3389 // They are allowed afterwards, though.
3390 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003391 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003392 while (Tok.is(tok::kw___declspec))
3393 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003394 }
Richard Smith7d137e32012-03-23 03:33:32 +00003395
John McCall6347b682012-05-07 06:16:58 +00003396 // C++11 [temp.explicit]p12:
3397 // The usual access controls do not apply to names used to specify
3398 // explicit instantiations.
3399 // We extend this to also cover explicit specializations. Note that
3400 // we don't suppress if this turns out to be an elaborated type
3401 // specifier.
3402 bool shouldDelayDiagsInTag =
3403 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3404 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3405 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003406
Richard Smithbfdb1082012-03-12 08:56:40 +00003407 // Enum definitions should not be parsed in a trailing-return-type.
3408 bool AllowDeclaration = DSC != DSC_trailing;
3409
3410 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003411 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003412 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003413
Abramo Bagnarad7548482010-05-19 21:37:53 +00003414 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003415 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003416 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3417 // if a fixed underlying type is allowed.
3418 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003419
3420 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003421 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003422 return;
3423
3424 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003425 Diag(Tok, diag::err_expected_ident);
3426 if (Tok.isNot(tok::l_brace)) {
3427 // Has no name and is not a definition.
3428 // Skip the rest of this declarator, up until the comma or semicolon.
3429 SkipUntil(tok::comma, true);
3430 return;
3431 }
3432 }
3433 }
Mike Stump11289f42009-09-09 15:08:12 +00003434
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003435 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003436 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003437 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003438 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003439
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003440 // Skip the rest of this declarator, up until the comma or semicolon.
3441 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003442 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003443 }
Mike Stump11289f42009-09-09 15:08:12 +00003444
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003445 // If an identifier is present, consume and remember it.
3446 IdentifierInfo *Name = 0;
3447 SourceLocation NameLoc;
3448 if (Tok.is(tok::identifier)) {
3449 Name = Tok.getIdentifierInfo();
3450 NameLoc = ConsumeToken();
3451 }
Mike Stump11289f42009-09-09 15:08:12 +00003452
Richard Smith0f8ee222012-01-10 01:33:14 +00003453 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003454 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3455 // declaration of a scoped enumeration.
3456 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003457 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003458 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003459 }
3460
John McCall6347b682012-05-07 06:16:58 +00003461 // Okay, end the suppression area. We'll decide whether to emit the
3462 // diagnostics in a second.
3463 if (shouldDelayDiagsInTag)
3464 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003465
Douglas Gregor0bf31402010-10-08 23:50:27 +00003466 TypeResult BaseType;
3467
Douglas Gregord1f69f62010-12-01 17:42:47 +00003468 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003469 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003470 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003471 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003472 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003473 // If we're in class scope, this can either be an enum declaration with
3474 // an underlying type, or a declaration of a bitfield member. We try to
3475 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003476 // (integer literal, sizeof); if it's still ambiguous, we then consider
3477 // anything that's a simple-type-specifier followed by '(' as an
3478 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003479 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003480 EnterExpressionEvaluationContext Unevaluated(Actions,
3481 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003482 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003483 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003484 // bit-field. This is the common case.
3485 if (TPR == TPResult::True())
3486 PossibleBitfield = true;
3487 // If the next token starts a type-specifier-seq, it may be either a
3488 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003489 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003490 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003491 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003492 GetLookAheadToken(2).getKind() == tok::semi) {
3493 // Consume the ':'.
3494 ConsumeToken();
3495 } else {
3496 // We have the start of a type-specifier-seq, so we have to perform
3497 // tentative parsing to determine whether we have an expression or a
3498 // type.
3499 TentativeParsingAction TPA(*this);
3500
3501 // Consume the ':'.
3502 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003503
3504 // If we see a type specifier followed by an open-brace, we have an
3505 // ambiguity between an underlying type and a C++11 braced
3506 // function-style cast. Resolve this by always treating it as an
3507 // underlying type.
3508 // FIXME: The standard is not entirely clear on how to disambiguate in
3509 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003510 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003511 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003512 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003513 // We'll parse this as a bitfield later.
3514 PossibleBitfield = true;
3515 TPA.Revert();
3516 } else {
3517 // We have a type-specifier-seq.
3518 TPA.Commit();
3519 }
3520 }
3521 } else {
3522 // Consume the ':'.
3523 ConsumeToken();
3524 }
3525
3526 if (!PossibleBitfield) {
3527 SourceRange Range;
3528 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003529
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003530 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003531 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003532 } else if (!getLangOpts().ObjC2) {
3533 if (getLangOpts().CPlusPlus)
3534 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3535 else
3536 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3537 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003538 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003539 }
3540
Richard Smith0f8ee222012-01-10 01:33:14 +00003541 // There are four options here. If we have 'friend enum foo;' then this is a
3542 // friend declaration, and cannot have an accompanying definition. If we have
3543 // 'enum foo;', then this is a forward declaration. If we have
3544 // 'enum foo {...' then this is a definition. Otherwise we have something
3545 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003546 //
3547 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3548 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3549 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3550 //
John McCallfaf5fb42010-08-26 23:41:50 +00003551 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003552 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003553 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003554 } else if (Tok.is(tok::l_brace)) {
3555 if (DS.isFriendSpecified()) {
3556 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3557 << SourceRange(DS.getFriendSpecLoc());
3558 ConsumeBrace();
3559 SkipUntil(tok::r_brace);
3560 TUK = Sema::TUK_Friend;
3561 } else {
3562 TUK = Sema::TUK_Definition;
3563 }
Richard Smith369b9f92012-06-25 21:37:02 +00003564 } else if (DSC != DSC_type_specifier &&
3565 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003566 (Tok.isAtStartOfLine() &&
3567 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003568 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3569 if (Tok.isNot(tok::semi)) {
3570 // A semicolon was missing after this declaration. Diagnose and recover.
3571 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3572 "enum");
3573 PP.EnterToken(Tok);
3574 Tok.setKind(tok::semi);
3575 }
John McCall6347b682012-05-07 06:16:58 +00003576 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003577 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003578 }
3579
3580 // If this is an elaborated type specifier, and we delayed
3581 // diagnostics before, just merge them into the current pool.
3582 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3583 diagsFromTag.redelay();
3584 }
Richard Smith7d137e32012-03-23 03:33:32 +00003585
3586 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003587 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003588 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003589 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003590 // Skip the rest of this declarator, up until the comma or semicolon.
3591 Diag(Tok, diag::err_enum_template);
3592 SkipUntil(tok::comma, true);
3593 return;
3594 }
3595
3596 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3597 // Enumerations can't be explicitly instantiated.
3598 DS.SetTypeSpecError();
3599 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3600 return;
3601 }
3602
3603 assert(TemplateInfo.TemplateParams && "no template parameters");
3604 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3605 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003606 }
Chad Rosierc1183952012-06-26 22:30:43 +00003607
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003608 if (TUK == Sema::TUK_Reference)
3609 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003610
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003611 if (!Name && TUK != Sema::TUK_Definition) {
3612 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003613
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003614 // Skip the rest of this declarator, up until the comma or semicolon.
3615 SkipUntil(tok::comma, true);
3616 return;
3617 }
Richard Smith7d137e32012-03-23 03:33:32 +00003618
Douglas Gregord6ab8742009-05-28 23:31:59 +00003619 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003620 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003621 const char *PrevSpec = 0;
3622 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003623 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003624 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003625 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003626 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003627 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003628
Douglas Gregorba41d012010-04-24 16:38:41 +00003629 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003630 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003631 // dependent tag.
3632 if (!Name) {
3633 DS.SetTypeSpecError();
3634 Diag(Tok, diag::err_expected_type_name_after_typename);
3635 return;
3636 }
Chad Rosierc1183952012-06-26 22:30:43 +00003637
Douglas Gregor0be31a22010-07-02 17:43:08 +00003638 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003639 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003640 NameLoc);
3641 if (Type.isInvalid()) {
3642 DS.SetTypeSpecError();
3643 return;
3644 }
Chad Rosierc1183952012-06-26 22:30:43 +00003645
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003646 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3647 NameLoc.isValid() ? NameLoc : StartLoc,
3648 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003649 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003650
Douglas Gregorba41d012010-04-24 16:38:41 +00003651 return;
3652 }
Mike Stump11289f42009-09-09 15:08:12 +00003653
John McCall48871652010-08-21 09:40:31 +00003654 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003655 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003656 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003657 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003658 ConsumeBrace();
3659 SkipUntil(tok::r_brace);
3660 }
Chad Rosierc1183952012-06-26 22:30:43 +00003661
Douglas Gregorba41d012010-04-24 16:38:41 +00003662 DS.SetTypeSpecError();
3663 return;
3664 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003665
Richard Smith369b9f92012-06-25 21:37:02 +00003666 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003667 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003668
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003669 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3670 NameLoc.isValid() ? NameLoc : StartLoc,
3671 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003672 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003673}
3674
Chris Lattnerc1915e22007-01-25 07:29:02 +00003675/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3676/// enumerator-list:
3677/// enumerator
3678/// enumerator-list ',' enumerator
3679/// enumerator:
3680/// enumeration-constant
3681/// enumeration-constant '=' constant-expression
3682/// enumeration-constant:
3683/// identifier
3684///
John McCall48871652010-08-21 09:40:31 +00003685void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003686 // Enter the scope of the enum body and start the definition.
3687 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003688 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003689
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003690 BalancedDelimiterTracker T(*this, tok::l_brace);
3691 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003692
Chris Lattner37256fb2007-08-27 17:24:30 +00003693 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003694 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003695 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003696
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003697 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003698
John McCall48871652010-08-21 09:40:31 +00003699 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003700
Chris Lattnerc1915e22007-01-25 07:29:02 +00003701 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003702 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003703 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3704 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003705
John McCall811a0f52010-10-22 23:36:17 +00003706 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003707 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003708 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003709 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003710 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003711
Chris Lattnerc1915e22007-01-25 07:29:02 +00003712 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003713 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003714 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003715
Chris Lattner76c72282007-10-09 17:33:22 +00003716 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003717 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003718 AssignedVal = ParseConstantExpression();
3719 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003720 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003721 }
Mike Stump11289f42009-09-09 15:08:12 +00003722
Chris Lattnerc1915e22007-01-25 07:29:02 +00003723 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003724 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3725 LastEnumConstDecl,
3726 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003727 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003728 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003729 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003730
Chris Lattner4ef40012007-06-11 01:28:17 +00003731 EnumConstantDecls.push_back(EnumConstDecl);
3732 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003733
Douglas Gregorce66d022010-09-07 14:51:08 +00003734 if (Tok.is(tok::identifier)) {
3735 // We're missing a comma between enumerators.
3736 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003737 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003738 << FixItHint::CreateInsertion(Loc, ", ");
3739 continue;
3740 }
Chad Rosierc1183952012-06-26 22:30:43 +00003741
Chris Lattner76c72282007-10-09 17:33:22 +00003742 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003743 break;
3744 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003745
Richard Smith5d164bc2011-10-15 05:09:34 +00003746 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003747 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003748 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3749 diag::ext_enumerator_list_comma_cxx :
3750 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003751 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003752 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003753 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3754 << FixItHint::CreateRemoval(CommaLoc);
3755 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003756 }
Mike Stump11289f42009-09-09 15:08:12 +00003757
Chris Lattnerc1915e22007-01-25 07:29:02 +00003758 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003759 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003760
Chris Lattnerc1915e22007-01-25 07:29:02 +00003761 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003762 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003763 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003764
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003765 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003766 EnumDecl, EnumConstantDecls,
3767 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003768 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003769
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003770 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003771 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3772 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003773
3774 // The next token must be valid after an enum definition. If not, a ';'
3775 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003776 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3777 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003778 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3779 // Push this token back into the preprocessor and change our current token
3780 // to ';' so that the rest of the code recovers as though there were an
3781 // ';' after the definition.
3782 PP.EnterToken(Tok);
3783 Tok.setKind(tok::semi);
3784 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003785}
Chris Lattner3b561a32006-08-13 00:12:11 +00003786
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003787/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003788/// start of a type-qualifier-list.
3789bool Parser::isTypeQualifier() const {
3790 switch (Tok.getKind()) {
3791 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003792
3793 // type-qualifier only in OpenCL
3794 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003795 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003796
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003797 // type-qualifier
3798 case tok::kw_const:
3799 case tok::kw_volatile:
3800 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003801 case tok::kw___private:
3802 case tok::kw___local:
3803 case tok::kw___global:
3804 case tok::kw___constant:
3805 case tok::kw___read_only:
3806 case tok::kw___read_write:
3807 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003808 return true;
3809 }
3810}
3811
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003812/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3813/// is definitely a type-specifier. Return false if it isn't part of a type
3814/// specifier or if we're not sure.
3815bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3816 switch (Tok.getKind()) {
3817 default: return false;
3818 // type-specifiers
3819 case tok::kw_short:
3820 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003821 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003822 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003823 case tok::kw_signed:
3824 case tok::kw_unsigned:
3825 case tok::kw__Complex:
3826 case tok::kw__Imaginary:
3827 case tok::kw_void:
3828 case tok::kw_char:
3829 case tok::kw_wchar_t:
3830 case tok::kw_char16_t:
3831 case tok::kw_char32_t:
3832 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003833 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003834 case tok::kw_float:
3835 case tok::kw_double:
3836 case tok::kw_bool:
3837 case tok::kw__Bool:
3838 case tok::kw__Decimal32:
3839 case tok::kw__Decimal64:
3840 case tok::kw__Decimal128:
3841 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003842
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003843 // OpenCL specific types:
3844 case tok::kw_image1d_t:
3845 case tok::kw_image1d_array_t:
3846 case tok::kw_image1d_buffer_t:
3847 case tok::kw_image2d_t:
3848 case tok::kw_image2d_array_t:
3849 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003850 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003851 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003852
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003853 // struct-or-union-specifier (C99) or class-specifier (C++)
3854 case tok::kw_class:
3855 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003856 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003857 case tok::kw_union:
3858 // enum-specifier
3859 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003860
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003861 // typedef-name
3862 case tok::annot_typename:
3863 return true;
3864 }
3865}
3866
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003867/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003868/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003869bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003870 switch (Tok.getKind()) {
3871 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003872
Chris Lattner020bab92009-01-04 23:41:41 +00003873 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003874 if (TryAltiVecVectorToken())
3875 return true;
3876 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003877 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003878 // Annotate typenames and C++ scope specifiers. If we get one, just
3879 // recurse to handle whatever we get.
3880 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003881 return true;
3882 if (Tok.is(tok::identifier))
3883 return false;
3884 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003885
Chris Lattner020bab92009-01-04 23:41:41 +00003886 case tok::coloncolon: // ::foo::bar
3887 if (NextToken().is(tok::kw_new) || // ::new
3888 NextToken().is(tok::kw_delete)) // ::delete
3889 return false;
3890
Chris Lattner020bab92009-01-04 23:41:41 +00003891 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003892 return true;
3893 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003894
Chris Lattnere37e2332006-08-15 04:50:22 +00003895 // GNU attributes support.
3896 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003897 // GNU typeof support.
3898 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003899
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003900 // type-specifiers
3901 case tok::kw_short:
3902 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003903 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003904 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003905 case tok::kw_signed:
3906 case tok::kw_unsigned:
3907 case tok::kw__Complex:
3908 case tok::kw__Imaginary:
3909 case tok::kw_void:
3910 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003911 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003912 case tok::kw_char16_t:
3913 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003914 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003915 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003916 case tok::kw_float:
3917 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003918 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003919 case tok::kw__Bool:
3920 case tok::kw__Decimal32:
3921 case tok::kw__Decimal64:
3922 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003923 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003924
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003925 // OpenCL specific types:
3926 case tok::kw_image1d_t:
3927 case tok::kw_image1d_array_t:
3928 case tok::kw_image1d_buffer_t:
3929 case tok::kw_image2d_t:
3930 case tok::kw_image2d_array_t:
3931 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003932 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003933 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003934
Chris Lattner861a2262008-04-13 18:59:07 +00003935 // struct-or-union-specifier (C99) or class-specifier (C++)
3936 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003937 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003938 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003939 case tok::kw_union:
3940 // enum-specifier
3941 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003942
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003943 // type-qualifier
3944 case tok::kw_const:
3945 case tok::kw_volatile:
3946 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003947
John McCallea0a39e2012-11-14 00:49:39 +00003948 // Debugger support.
3949 case tok::kw___unknown_anytype:
3950
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003951 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003952 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003953 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003954
Chris Lattner409bf7d2008-10-20 00:25:30 +00003955 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3956 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003957 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003958
Steve Naroff44ac7772008-12-25 14:16:32 +00003959 case tok::kw___cdecl:
3960 case tok::kw___stdcall:
3961 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003962 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003963 case tok::kw___w64:
3964 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003965 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003966 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003967 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003968
3969 case tok::kw___private:
3970 case tok::kw___local:
3971 case tok::kw___global:
3972 case tok::kw___constant:
3973 case tok::kw___read_only:
3974 case tok::kw___read_write:
3975 case tok::kw___write_only:
3976
Eli Friedman53339e02009-06-08 23:27:34 +00003977 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003978
3979 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003980 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003981
Richard Smith8e1ac332013-03-28 01:55:44 +00003982 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00003983 case tok::kw__Atomic:
3984 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003985 }
3986}
3987
Chris Lattneracd58a32006-08-06 17:24:14 +00003988/// isDeclarationSpecifier() - Return true if the current token is part of a
3989/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003990///
3991/// \param DisambiguatingWithExpression True to indicate that the purpose of
3992/// this check is to disambiguate between an expression and a declaration.
3993bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003994 switch (Tok.getKind()) {
3995 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003996
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003997 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003998 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003999
Chris Lattner020bab92009-01-04 23:41:41 +00004000 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004001 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004002 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004003 return false;
John Thompson22334602010-02-05 00:12:22 +00004004 if (TryAltiVecVectorToken())
4005 return true;
4006 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004007 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004008 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004009 // Annotate typenames and C++ scope specifiers. If we get one, just
4010 // recurse to handle whatever we get.
4011 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004012 return true;
4013 if (Tok.is(tok::identifier))
4014 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004015
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004016 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004017 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004018 // expression is permitted, then this is probably a class message send
4019 // missing the initial '['. In this case, we won't consider this to be
4020 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004021 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004022 isStartOfObjCClassMessageMissingOpenBracket())
4023 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004024
John McCall1f476a12010-02-26 08:45:28 +00004025 return isDeclarationSpecifier();
4026
Chris Lattner020bab92009-01-04 23:41:41 +00004027 case tok::coloncolon: // ::foo::bar
4028 if (NextToken().is(tok::kw_new) || // ::new
4029 NextToken().is(tok::kw_delete)) // ::delete
4030 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004031
Chris Lattner020bab92009-01-04 23:41:41 +00004032 // Annotate typenames and C++ scope specifiers. If we get one, just
4033 // recurse to handle whatever we get.
4034 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004035 return true;
4036 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004037
Chris Lattneracd58a32006-08-06 17:24:14 +00004038 // storage-class-specifier
4039 case tok::kw_typedef:
4040 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004041 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004042 case tok::kw_static:
4043 case tok::kw_auto:
4044 case tok::kw_register:
4045 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004046 case tok::kw_thread_local:
4047 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004048
Douglas Gregor26701a42011-09-09 02:06:17 +00004049 // Modules
4050 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004051
John McCallea0a39e2012-11-14 00:49:39 +00004052 // Debugger support
4053 case tok::kw___unknown_anytype:
4054
Chris Lattneracd58a32006-08-06 17:24:14 +00004055 // type-specifiers
4056 case tok::kw_short:
4057 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004058 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004059 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004060 case tok::kw_signed:
4061 case tok::kw_unsigned:
4062 case tok::kw__Complex:
4063 case tok::kw__Imaginary:
4064 case tok::kw_void:
4065 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004066 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004067 case tok::kw_char16_t:
4068 case tok::kw_char32_t:
4069
Chris Lattneracd58a32006-08-06 17:24:14 +00004070 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004071 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004072 case tok::kw_float:
4073 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004074 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004075 case tok::kw__Bool:
4076 case tok::kw__Decimal32:
4077 case tok::kw__Decimal64:
4078 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004079 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004080
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004081 // OpenCL specific types:
4082 case tok::kw_image1d_t:
4083 case tok::kw_image1d_array_t:
4084 case tok::kw_image1d_buffer_t:
4085 case tok::kw_image2d_t:
4086 case tok::kw_image2d_array_t:
4087 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004088 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004089 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004090
Chris Lattner861a2262008-04-13 18:59:07 +00004091 // struct-or-union-specifier (C99) or class-specifier (C++)
4092 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004093 case tok::kw_struct:
4094 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004095 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004096 // enum-specifier
4097 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004098
Chris Lattneracd58a32006-08-06 17:24:14 +00004099 // type-qualifier
4100 case tok::kw_const:
4101 case tok::kw_volatile:
4102 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004103
Chris Lattneracd58a32006-08-06 17:24:14 +00004104 // function-specifier
4105 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004106 case tok::kw_virtual:
4107 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004108 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004109
Richard Smith1dba27c2013-01-29 09:02:09 +00004110 // alignment-specifier
4111 case tok::kw__Alignas:
4112
Richard Smithd16fe122012-10-25 00:00:53 +00004113 // friend keyword.
4114 case tok::kw_friend:
4115
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004116 // static_assert-declaration
4117 case tok::kw__Static_assert:
4118
Chris Lattner599e47e2007-08-09 17:01:07 +00004119 // GNU typeof support.
4120 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004121
Chris Lattner599e47e2007-08-09 17:01:07 +00004122 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004123 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004124
Richard Smithd16fe122012-10-25 00:00:53 +00004125 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004126 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004127 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004128
Richard Smith8e1ac332013-03-28 01:55:44 +00004129 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004130 case tok::kw__Atomic:
4131 return true;
4132
Chris Lattner8b2ec162008-07-26 03:38:44 +00004133 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4134 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004135 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004136
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004137 // typedef-name
4138 case tok::annot_typename:
4139 return !DisambiguatingWithExpression ||
4140 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004141
Steve Narofff192fab2009-01-06 19:34:12 +00004142 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004143 case tok::kw___cdecl:
4144 case tok::kw___stdcall:
4145 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004146 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004147 case tok::kw___w64:
4148 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004149 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004150 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004151 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004152 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004153
4154 case tok::kw___private:
4155 case tok::kw___local:
4156 case tok::kw___global:
4157 case tok::kw___constant:
4158 case tok::kw___read_only:
4159 case tok::kw___read_write:
4160 case tok::kw___write_only:
4161
Eli Friedman53339e02009-06-08 23:27:34 +00004162 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004163 }
4164}
4165
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004166bool Parser::isConstructorDeclarator() {
4167 TentativeParsingAction TPA(*this);
4168
4169 // Parse the C++ scope specifier.
4170 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004171 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004172 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004173 TPA.Revert();
4174 return false;
4175 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004176
4177 // Parse the constructor name.
4178 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4179 // We already know that we have a constructor name; just consume
4180 // the token.
4181 ConsumeToken();
4182 } else {
4183 TPA.Revert();
4184 return false;
4185 }
4186
Richard Smith43f340f2012-03-27 23:05:05 +00004187 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004188 if (Tok.isNot(tok::l_paren)) {
4189 TPA.Revert();
4190 return false;
4191 }
4192 ConsumeParen();
4193
Richard Smith43f340f2012-03-27 23:05:05 +00004194 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4195 // that we have a constructor.
4196 if (Tok.is(tok::r_paren) ||
4197 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004198 TPA.Revert();
4199 return true;
4200 }
4201
4202 // If we need to, enter the specified scope.
4203 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004204 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004205 DeclScopeObj.EnterDeclaratorScope();
4206
Francois Pichet79f3a872011-01-31 04:54:32 +00004207 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004208 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004209 MaybeParseMicrosoftAttributes(Attrs);
4210
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004211 // Check whether the next token(s) are part of a declaration
4212 // specifier, in which case we have the start of a parameter and,
4213 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004214 bool IsConstructor = false;
4215 if (isDeclarationSpecifier())
4216 IsConstructor = true;
4217 else if (Tok.is(tok::identifier) ||
4218 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4219 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4220 // This might be a parenthesized member name, but is more likely to
4221 // be a constructor declaration with an invalid argument type. Keep
4222 // looking.
4223 if (Tok.is(tok::annot_cxxscope))
4224 ConsumeToken();
4225 ConsumeToken();
4226
4227 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004228 // which must have one of the following syntactic forms (see the
4229 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004230 switch (Tok.getKind()) {
4231 case tok::l_paren:
4232 // C(X ( int));
4233 case tok::l_square:
4234 // C(X [ 5]);
4235 // C(X [ [attribute]]);
4236 case tok::coloncolon:
4237 // C(X :: Y);
4238 // C(X :: *p);
4239 case tok::r_paren:
4240 // C(X )
4241 // Assume this isn't a constructor, rather than assuming it's a
4242 // constructor with an unnamed parameter of an ill-formed type.
4243 break;
4244
4245 default:
4246 IsConstructor = true;
4247 break;
4248 }
4249 }
4250
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004251 TPA.Revert();
4252 return IsConstructor;
4253}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004254
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004255/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004256/// type-qualifier-list: [C99 6.7.5]
4257/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004258/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004259/// [ only if VendorAttributesAllowed=true ]
4260/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004261/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004262/// [ only if VendorAttributesAllowed=true ]
4263/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004264/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004265/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004266///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004267void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4268 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004269 bool CXX11AttributesAllowed,
4270 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004271 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004272 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004273 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004274 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004275 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004276 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004277
4278 SourceLocation EndLoc;
4279
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004280 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004281 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004282 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004283 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004284 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004285
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004286 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004287 case tok::code_completion:
4288 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004289 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004290
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004291 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004292 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004293 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004294 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004295 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004296 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004297 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004298 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004299 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004300 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004301 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004302 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004303 case tok::kw__Atomic:
4304 if (!AtomicAllowed)
4305 goto DoneWithTypeQuals;
4306 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4307 getLangOpts());
4308 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004309
4310 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004311 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004312 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004313 goto DoneWithTypeQuals;
4314 case tok::kw___private:
4315 case tok::kw___global:
4316 case tok::kw___local:
4317 case tok::kw___constant:
4318 case tok::kw___read_only:
4319 case tok::kw___write_only:
4320 case tok::kw___read_write:
4321 ParseOpenCLQualifiers(DS);
4322 break;
4323
Eli Friedman53339e02009-06-08 23:27:34 +00004324 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004325 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004326 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004327 case tok::kw___cdecl:
4328 case tok::kw___stdcall:
4329 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004330 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004331 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004332 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004333 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004334 continue;
4335 }
4336 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004337 case tok::kw___pascal:
4338 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004339 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004340 continue;
4341 }
4342 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004343 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004344 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004345 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004346 continue; // do *not* consume the next token!
4347 }
4348 // otherwise, FALL THROUGH!
4349 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004350 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004351 // If this is not a type-qualifier token, we're done reading type
4352 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004353 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004354 if (EndLoc.isValid())
4355 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004356 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004357 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004358
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004359 // If the specifier combination wasn't legal, issue a diagnostic.
4360 if (isInvalid) {
4361 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004362 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004363 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004364 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004365 }
4366}
4367
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004368
4369/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4370///
4371void Parser::ParseDeclarator(Declarator &D) {
4372 /// This implements the 'declarator' production in the C grammar, then checks
4373 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004374 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004375}
4376
Richard Smith0efa75c2012-03-29 01:16:42 +00004377static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4378 if (Kind == tok::star || Kind == tok::caret)
4379 return true;
4380
4381 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4382 if (!Lang.CPlusPlus)
4383 return false;
4384
4385 return Kind == tok::amp || Kind == tok::ampamp;
4386}
4387
Sebastian Redlbd150f42008-11-21 19:14:01 +00004388/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4389/// is parsed by the function passed to it. Pass null, and the direct-declarator
4390/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004391/// ptr-operator production.
4392///
Richard Smith09f76ee2011-10-19 21:33:05 +00004393/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004394/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4395/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004396///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004397/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4398/// [C] pointer[opt] direct-declarator
4399/// [C++] direct-declarator
4400/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004401///
4402/// pointer: [C99 6.7.5]
4403/// '*' type-qualifier-list[opt]
4404/// '*' type-qualifier-list[opt] pointer
4405///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004406/// ptr-operator:
4407/// '*' cv-qualifier-seq[opt]
4408/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004409/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004410/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004411/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004412/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004413void Parser::ParseDeclaratorInternal(Declarator &D,
4414 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004415 if (Diags.hasAllExtensionsSilenced())
4416 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004417
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004418 // C++ member pointers start with a '::' or a nested-name.
4419 // Member pointers get special handling, since there's no place for the
4420 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004421 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004422 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4423 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004424 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4425 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004426 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004427 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004428
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004429 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004430 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004431 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004432 if (D.mayHaveIdentifier())
4433 D.getCXXScopeSpec() = SS;
4434 else
4435 AnnotateScopeToken(SS, true);
4436
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004437 if (DirectDeclParser)
4438 (this->*DirectDeclParser)(D);
4439 return;
4440 }
4441
4442 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004443 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004444 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004445 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004446 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004447
4448 // Recurse to parse whatever is left.
4449 ParseDeclaratorInternal(D, DirectDeclParser);
4450
4451 // Sema will have to catch (syntactically invalid) pointers into global
4452 // scope. It has to catch pointers into namespace scope anyway.
4453 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004454 Loc),
4455 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004456 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004457 return;
4458 }
4459 }
4460
4461 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004462 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004463 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004464 if (DirectDeclParser)
4465 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004466 return;
4467 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004468
Sebastian Redled0f3b02009-03-15 22:02:01 +00004469 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4470 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004471 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004472 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004473
Chris Lattner9eac9312009-03-27 04:18:06 +00004474 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004475 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004476 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004477
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004478 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004479 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004480 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004481
Bill Wendling3708c182007-05-27 10:15:43 +00004482 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004483 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004484 if (Kind == tok::star)
4485 // Remember that we parsed a pointer type, and remember the type-quals.
4486 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004487 DS.getConstSpecLoc(),
4488 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004489 DS.getRestrictSpecLoc()),
4490 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004491 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004492 else
4493 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004494 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004495 Loc),
4496 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004497 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004498 } else {
4499 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004500 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004501
Sebastian Redl3b27be62009-03-23 00:00:23 +00004502 // Complain about rvalue references in C++03, but then go on and build
4503 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004504 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004505 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004506 diag::warn_cxx98_compat_rvalue_reference :
4507 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004508
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004509 // GNU-style and C++11 attributes are allowed here, as is restrict.
4510 ParseTypeQualifierListOpt(DS);
4511 D.ExtendWithDeclSpec(DS);
4512
Bill Wendling93efb222007-06-02 23:28:54 +00004513 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4514 // cv-qualifiers are introduced through the use of a typedef or of a
4515 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004516 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4517 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4518 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004519 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004520 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4521 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004522 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004523 // 'restrict' is permitted as an extension.
4524 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4525 Diag(DS.getAtomicSpecLoc(),
4526 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004527 }
Bill Wendling3708c182007-05-27 10:15:43 +00004528
4529 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004530 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004531
Douglas Gregor66583c52008-11-03 15:51:28 +00004532 if (D.getNumTypeObjects() > 0) {
4533 // C++ [dcl.ref]p4: There shall be no references to references.
4534 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4535 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004536 if (const IdentifierInfo *II = D.getIdentifier())
4537 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4538 << II;
4539 else
4540 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4541 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004542
Sebastian Redlbd150f42008-11-21 19:14:01 +00004543 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004544 // can go ahead and build the (technically ill-formed)
4545 // declarator: reference collapsing will take care of it.
4546 }
4547 }
4548
Richard Smith8e1ac332013-03-28 01:55:44 +00004549 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004550 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004551 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004552 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004553 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004554 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004555}
4556
Richard Smith0efa75c2012-03-29 01:16:42 +00004557static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4558 SourceLocation EllipsisLoc) {
4559 if (EllipsisLoc.isValid()) {
4560 FixItHint Insertion;
4561 if (!D.getEllipsisLoc().isValid()) {
4562 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4563 D.setEllipsisLoc(EllipsisLoc);
4564 }
4565 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4566 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4567 }
4568}
4569
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004570/// ParseDirectDeclarator
4571/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004572/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004573/// '(' declarator ')'
4574/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004575/// [C90] direct-declarator '[' constant-expression[opt] ']'
4576/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4577/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4578/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4579/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004580/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4581/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004582/// direct-declarator '(' parameter-type-list ')'
4583/// direct-declarator '(' identifier-list[opt] ')'
4584/// [GNU] direct-declarator '(' parameter-forward-declarations
4585/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004586/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4587/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004588/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4589/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4590/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004591/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004592/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004593///
4594/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004595/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004596/// '::'[opt] nested-name-specifier[opt] type-name
4597///
4598/// id-expression: [C++ 5.1]
4599/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004600/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004601///
4602/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004603/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004604/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004605/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004606/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004607/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004608///
Richard Smith1453e312012-03-27 01:42:32 +00004609/// Note, any additional constructs added here may need corresponding changes
4610/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004611void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004612 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004613
David Blaikiebbafb8a2012-03-11 07:00:24 +00004614 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004615 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004616 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004617 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4618 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004619 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004620 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004621 }
4622
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004623 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004624 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004625 // Change the declaration context for name lookup, until this function
4626 // is exited (and the declarator has been parsed).
4627 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004628 }
4629
Douglas Gregor27b4c162010-12-23 22:44:42 +00004630 // C++0x [dcl.fct]p14:
4631 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004632 // of a parameter-declaration-clause without a preceding comma. In
4633 // this case, the ellipsis is parsed as part of the
4634 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004635 // parameter pack that has not been expanded; otherwise, it is parsed
4636 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004637 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004638 !((D.getContext() == Declarator::PrototypeContext ||
4639 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004640 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004641 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004642 !Actions.containsUnexpandedParameterPacks(D))) {
4643 SourceLocation EllipsisLoc = ConsumeToken();
4644 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4645 // The ellipsis was put in the wrong place. Recover, and explain to
4646 // the user what they should have done.
4647 ParseDeclarator(D);
4648 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4649 return;
4650 } else
4651 D.setEllipsisLoc(EllipsisLoc);
4652
4653 // The ellipsis can't be followed by a parenthesized declarator. We
4654 // check for that in ParseParenDeclarator, after we have disambiguated
4655 // the l_paren token.
4656 }
4657
Douglas Gregor7861a802009-11-03 01:35:08 +00004658 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4659 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4660 // We found something that indicates the start of an unqualified-id.
4661 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004662 bool AllowConstructorName;
4663 if (D.getDeclSpec().hasTypeSpecifier())
4664 AllowConstructorName = false;
4665 else if (D.getCXXScopeSpec().isSet())
4666 AllowConstructorName =
4667 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004668 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004669 else
4670 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4671
Abramo Bagnara7945c982012-01-27 09:46:47 +00004672 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004673 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4674 /*EnteringContext=*/true,
4675 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004676 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004677 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004678 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004679 D.getName()) ||
4680 // Once we're past the identifier, if the scope was bad, mark the
4681 // whole declarator bad.
4682 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004683 D.SetIdentifier(0, Tok.getLocation());
4684 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004685 } else {
4686 // Parsed the unqualified-id; update range information and move along.
4687 if (D.getSourceRange().getBegin().isInvalid())
4688 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4689 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004690 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004691 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004692 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004693 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004694 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004695 "There's a C++-specific check for tok::identifier above");
4696 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4697 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4698 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004699 goto PastIdentifier;
4700 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004701
Douglas Gregor7861a802009-11-03 01:35:08 +00004702 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004703 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004704 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004705 // Example: 'char (*X)' or 'int (*XX)(void)'
4706 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004707
4708 // If the declarator was parenthesized, we entered the declarator
4709 // scope when parsing the parenthesized declarator, then exited
4710 // the scope already. Re-enter the scope, if we need to.
4711 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004712 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004713 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004714 if (!D.isInvalidType() &&
4715 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004716 // Change the declaration context for name lookup, until this function
4717 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004718 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004719 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004720 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004721 // This could be something simple like "int" (in which case the declarator
4722 // portion is empty), if an abstract-declarator is allowed.
4723 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004724
4725 // The grammar for abstract-pack-declarator does not allow grouping parens.
4726 // FIXME: Revisit this once core issue 1488 is resolved.
4727 if (D.hasEllipsis() && D.hasGroupingParens())
4728 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4729 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004730 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004731 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004732 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004733 if (D.getContext() == Declarator::MemberContext)
4734 Diag(Tok, diag::err_expected_member_name_or_semi)
4735 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004736 else if (getLangOpts().CPlusPlus) {
4737 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4738 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4739 else
4740 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4741 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004742 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004743 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004744 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004745 }
Mike Stump11289f42009-09-09 15:08:12 +00004746
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004747 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004748 assert(D.isPastIdentifier() &&
4749 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004750
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004751 // Don't parse attributes unless we have parsed an unparenthesized name.
4752 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004753 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004754
Chris Lattneracd58a32006-08-06 17:24:14 +00004755 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004756 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004757 // Enter function-declaration scope, limiting any declarators to the
4758 // function prototype scope, including parameter declarators.
4759 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004760 Scope::FunctionPrototypeScope|Scope::DeclScope|
4761 (D.isFunctionDeclaratorAFunctionDeclaration()
4762 ? Scope::FunctionDeclarationScope : 0));
4763
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004764 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4765 // In such a case, check if we actually have a function declarator; if it
4766 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004767 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004768 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4769 // The name of the declarator, if any, is tentatively declared within
4770 // a possible direct initializer.
4771 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4772 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4773 TentativelyDeclaredIdentifiers.pop_back();
4774 if (!IsFunctionDecl)
4775 break;
4776 }
John McCall084e83d2011-03-24 11:26:52 +00004777 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004778 BalancedDelimiterTracker T(*this, tok::l_paren);
4779 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004780 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004781 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004782 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004783 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004784 } else {
4785 break;
4786 }
4787 }
Chad Rosierc1183952012-06-26 22:30:43 +00004788}
Chris Lattneracd58a32006-08-06 17:24:14 +00004789
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004790/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4791/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004792/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004793/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4794///
4795/// direct-declarator:
4796/// '(' declarator ')'
4797/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004798/// direct-declarator '(' parameter-type-list ')'
4799/// direct-declarator '(' identifier-list[opt] ')'
4800/// [GNU] direct-declarator '(' parameter-forward-declarations
4801/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004802///
4803void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004804 BalancedDelimiterTracker T(*this, tok::l_paren);
4805 T.consumeOpen();
4806
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004807 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004808
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004809 // Eat any attributes before we look at whether this is a grouping or function
4810 // declarator paren. If this is a grouping paren, the attribute applies to
4811 // the type being built up, for example:
4812 // int (__attribute__(()) *x)(long y)
4813 // If this ends up not being a grouping paren, the attribute applies to the
4814 // first argument, for example:
4815 // int (__attribute__(()) int x)
4816 // In either case, we need to eat any attributes to be able to determine what
4817 // sort of paren this is.
4818 //
John McCall084e83d2011-03-24 11:26:52 +00004819 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004820 bool RequiresArg = false;
4821 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004822 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004823
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004824 // We require that the argument list (if this is a non-grouping paren) be
4825 // present even if the attribute list was empty.
4826 RequiresArg = true;
4827 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004828
Steve Naroff44ac7772008-12-25 14:16:32 +00004829 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004830 ParseMicrosoftTypeAttributes(attrs);
4831
Dawn Perchik335e16b2010-09-03 01:29:35 +00004832 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004833 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004834 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004835
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004836 // If we haven't past the identifier yet (or where the identifier would be
4837 // stored, if this is an abstract declarator), then this is probably just
4838 // grouping parens. However, if this could be an abstract-declarator, then
4839 // this could also be the start of function arguments (consider 'void()').
4840 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004841
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004842 if (!D.mayOmitIdentifier()) {
4843 // If this can't be an abstract-declarator, this *must* be a grouping
4844 // paren, because we haven't seen the identifier yet.
4845 isGrouping = true;
4846 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004847 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4848 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004849 isDeclarationSpecifier() || // 'int(int)' is a function.
4850 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004851 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4852 // considered to be a type, not a K&R identifier-list.
4853 isGrouping = false;
4854 } else {
4855 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4856 isGrouping = true;
4857 }
Mike Stump11289f42009-09-09 15:08:12 +00004858
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004859 // If this is a grouping paren, handle:
4860 // direct-declarator: '(' declarator ')'
4861 // direct-declarator: '(' attributes declarator ')'
4862 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004863 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4864 D.setEllipsisLoc(SourceLocation());
4865
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004866 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004867 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004868 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004869 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004870 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004871 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004872 T.getCloseLocation()),
4873 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004874
4875 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004876
4877 // An ellipsis cannot be placed outside parentheses.
4878 if (EllipsisLoc.isValid())
4879 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4880
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004881 return;
4882 }
Mike Stump11289f42009-09-09 15:08:12 +00004883
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004884 // Okay, if this wasn't a grouping paren, it must be the start of a function
4885 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004886 // identifier (and remember where it would have been), then call into
4887 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004888 D.SetIdentifier(0, Tok.getLocation());
4889
David Blaikie15a430a2011-12-04 05:04:18 +00004890 // Enter function-declaration scope, limiting any declarators to the
4891 // function prototype scope, including parameter declarators.
4892 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004893 Scope::FunctionPrototypeScope | Scope::DeclScope |
4894 (D.isFunctionDeclaratorAFunctionDeclaration()
4895 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004896 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004897 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004898}
4899
4900/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4901/// declarator D up to a paren, which indicates that we are parsing function
4902/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004903///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004904/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4905/// immediately after the open paren - they should be considered to be the
4906/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004907///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004908/// If RequiresArg is true, then the first argument of the function is required
4909/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004910///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004911/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4912/// (C++11) ref-qualifier[opt], exception-specification[opt],
4913/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4914///
4915/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004916/// dynamic-exception-specification
4917/// noexcept-specification
4918///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004919void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004920 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004921 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004922 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004923 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004924 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004925 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004926 // lparen is already consumed!
4927 assert(D.isPastIdentifier() && "Should not call before identifier!");
4928
4929 // This should be true when the function has typed arguments.
4930 // Otherwise, it is treated as a K&R-style function.
4931 bool HasProto = false;
4932 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004933 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004934 // Remember where we see an ellipsis, if any.
4935 SourceLocation EllipsisLoc;
4936
4937 DeclSpec DS(AttrFactory);
4938 bool RefQualifierIsLValueRef = true;
4939 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004940 SourceLocation ConstQualifierLoc;
4941 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004942 ExceptionSpecificationType ESpecType = EST_None;
4943 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004944 SmallVector<ParsedType, 2> DynamicExceptions;
4945 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004946 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004947 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004948 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004949
James Molloy6f8780b2012-02-29 10:24:19 +00004950 Actions.ActOnStartFunctionDeclarator();
4951
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004952 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4953 EndLoc is the end location for the function declarator.
4954 They differ for trailing return types. */
4955 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004956 SourceLocation LParenLoc, RParenLoc;
4957 LParenLoc = Tracker.getOpenLocation();
4958 StartLoc = LParenLoc;
4959
Douglas Gregor9e66af42011-07-05 16:44:18 +00004960 if (isFunctionDeclaratorIdentifierList()) {
4961 if (RequiresArg)
4962 Diag(Tok, diag::err_argument_required_after_attribute);
4963
4964 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4965
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004966 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004967 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004968 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004969 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004970 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004971 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004972 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004973 else if (RequiresArg)
4974 Diag(Tok, diag::err_argument_required_after_attribute);
4975
David Blaikiebbafb8a2012-03-11 07:00:24 +00004976 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004977
4978 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004979 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004980 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004981 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004982 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004983
David Blaikiebbafb8a2012-03-11 07:00:24 +00004984 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004985 // FIXME: Accept these components in any order, and produce fixits to
4986 // correct the order if the user gets it wrong. Ideally we should deal
4987 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004988
4989 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00004990 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4991 /*CXX11AttributesAllowed*/ false,
4992 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004993 if (!DS.getSourceRange().getEnd().isInvalid()) {
4994 EndLoc = DS.getSourceRange().getEnd();
4995 ConstQualifierLoc = DS.getConstSpecLoc();
4996 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4997 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004998
4999 // Parse ref-qualifier[opt].
5000 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005001 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005002 diag::warn_cxx98_compat_ref_qualifier :
5003 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005004
Douglas Gregor9e66af42011-07-05 16:44:18 +00005005 RefQualifierIsLValueRef = Tok.is(tok::amp);
5006 RefQualifierLoc = ConsumeToken();
5007 EndLoc = RefQualifierLoc;
5008 }
5009
Douglas Gregor3024f072012-04-16 07:05:22 +00005010 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005011 // If a declaration declares a member function or member function
5012 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005013 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005014 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005015 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005016 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005017 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005018 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005019 (D.getContext() == Declarator::MemberContext
5020 ? !D.getDeclSpec().isFriendSpecified()
5021 : D.getContext() == Declarator::FileContext &&
5022 D.getCXXScopeSpec().isValid() &&
5023 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005024 Sema::CXXThisScopeRAII ThisScope(Actions,
5025 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005026 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005027 (D.getDeclSpec().isConstexprSpecified() &&
5028 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005029 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005030 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005031
Douglas Gregor9e66af42011-07-05 16:44:18 +00005032 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005033 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005034 DynamicExceptions,
5035 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005036 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005037 if (ESpecType != EST_None)
5038 EndLoc = ESpecRange.getEnd();
5039
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005040 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5041 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005042 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005043
Douglas Gregor9e66af42011-07-05 16:44:18 +00005044 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005045 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005046 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005047 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005048 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5049 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005050 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005051 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005052 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005053 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005054 }
5055 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005056 }
5057
5058 // Remember that we parsed a function type, and remember the attributes.
5059 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005060 IsAmbiguous,
5061 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005062 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005063 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005064 DS.getTypeQualifiers(),
5065 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005066 RefQualifierLoc, ConstQualifierLoc,
5067 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005068 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005069 ESpecType, ESpecRange.getBegin(),
5070 DynamicExceptions.data(),
5071 DynamicExceptionRanges.data(),
5072 DynamicExceptions.size(),
5073 NoexceptExpr.isUsable() ?
5074 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005075 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005076 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005077 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005078
5079 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005080}
5081
5082/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5083/// identifier list form for a K&R-style function: void foo(a,b,c)
5084///
5085/// Note that identifier-lists are only allowed for normal declarators, not for
5086/// abstract-declarators.
5087bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005088 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005089 && Tok.is(tok::identifier)
5090 && !TryAltiVecVectorToken()
5091 // K&R identifier lists can't have typedefs as identifiers, per C99
5092 // 6.7.5.3p11.
5093 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5094 // Identifier lists follow a really simple grammar: the identifiers can
5095 // be followed *only* by a ", identifier" or ")". However, K&R
5096 // identifier lists are really rare in the brave new modern world, and
5097 // it is very common for someone to typo a type in a non-K&R style
5098 // list. If we are presented with something like: "void foo(intptr x,
5099 // float y)", we don't want to start parsing the function declarator as
5100 // though it is a K&R style declarator just because intptr is an
5101 // invalid type.
5102 //
5103 // To handle this, we check to see if the token after the first
5104 // identifier is a "," or ")". Only then do we parse it as an
5105 // identifier list.
5106 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5107}
5108
5109/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5110/// we found a K&R-style identifier list instead of a typed parameter list.
5111///
5112/// After returning, ParamInfo will hold the parsed parameters.
5113///
5114/// identifier-list: [C99 6.7.5]
5115/// identifier
5116/// identifier-list ',' identifier
5117///
5118void Parser::ParseFunctionDeclaratorIdentifierList(
5119 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005120 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005121 // If there was no identifier specified for the declarator, either we are in
5122 // an abstract-declarator, or we are in a parameter declarator which was found
5123 // to be abstract. In abstract-declarators, identifier lists are not valid:
5124 // diagnose this.
5125 if (!D.getIdentifier())
5126 Diag(Tok, diag::ext_ident_list_in_param);
5127
5128 // Maintain an efficient lookup of params we have seen so far.
5129 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5130
5131 while (1) {
5132 // If this isn't an identifier, report the error and skip until ')'.
5133 if (Tok.isNot(tok::identifier)) {
5134 Diag(Tok, diag::err_expected_ident);
5135 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5136 // Forget we parsed anything.
5137 ParamInfo.clear();
5138 return;
5139 }
5140
5141 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5142
5143 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5144 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5145 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5146
5147 // Verify that the argument identifier has not already been mentioned.
5148 if (!ParamsSoFar.insert(ParmII)) {
5149 Diag(Tok, diag::err_param_redefinition) << ParmII;
5150 } else {
5151 // Remember this identifier in ParamInfo.
5152 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5153 Tok.getLocation(),
5154 0));
5155 }
5156
5157 // Eat the identifier.
5158 ConsumeToken();
5159
5160 // The list continues if we see a comma.
5161 if (Tok.isNot(tok::comma))
5162 break;
5163 ConsumeToken();
5164 }
5165}
5166
5167/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5168/// after the opening parenthesis. This function will not parse a K&R-style
5169/// identifier list.
5170///
Richard Smith2620cd92012-04-11 04:01:28 +00005171/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5172/// caller parsed those arguments immediately after the open paren - they should
5173/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005174///
5175/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5176/// be the location of the ellipsis, if any was parsed.
5177///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005178/// parameter-type-list: [C99 6.7.5]
5179/// parameter-list
5180/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005181/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005182///
5183/// parameter-list: [C99 6.7.5]
5184/// parameter-declaration
5185/// parameter-list ',' parameter-declaration
5186///
5187/// parameter-declaration: [C99 6.7.5]
5188/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005189/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005190/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005191/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005192/// declaration-specifiers abstract-declarator[opt]
5193/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005194/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005195/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005196/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005197///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005198void Parser::ParseParameterDeclarationClause(
5199 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005200 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005201 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005202 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00005203
Chris Lattner371ed4e2008-04-06 06:57:35 +00005204 while (1) {
5205 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005206 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5207 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005208 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005209 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005210 }
Mike Stump11289f42009-09-09 15:08:12 +00005211
Chris Lattner371ed4e2008-04-06 06:57:35 +00005212 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005213 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005214 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005215
Richard Smith2620cd92012-04-11 04:01:28 +00005216 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005217 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005218
John McCall53fa7142010-12-24 02:08:15 +00005219 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005220 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005221
5222 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005223
5224 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005225 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005226 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005227 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5228 // too much hassle.
5229 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005230
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005231 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005232
Chris Lattner371ed4e2008-04-06 06:57:35 +00005233 // Parse the declarator. This is "PrototypeContext", because we must
5234 // accept either 'declarator' or 'abstract-declarator' here.
5235 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5236 ParseDeclarator(ParmDecl);
5237
5238 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005239 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005240
Chris Lattner371ed4e2008-04-06 06:57:35 +00005241 // Remember this parsed parameter in ParamInfo.
5242 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005243
Douglas Gregor4d87df52008-12-16 21:30:33 +00005244 // DefArgToks is used when the parsing of default arguments needs
5245 // to be delayed.
5246 CachedTokens *DefArgToks = 0;
5247
Chris Lattner371ed4e2008-04-06 06:57:35 +00005248 // If no parameter was specified, verify that *something* was specified,
5249 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005250 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5251 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005252 // Completely missing, emit error.
5253 Diag(DSStart, diag::err_missing_param);
5254 } else {
5255 // Otherwise, we have something. Add it and let semantic analysis try
5256 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005257
Chris Lattner371ed4e2008-04-06 06:57:35 +00005258 // Inform the actions module about the parameter declarator, so it gets
5259 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005260 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005261
5262 // Parse the default argument, if any. We parse the default
5263 // arguments in all dialects; the semantic analysis in
5264 // ActOnParamDefaultArgument will reject the default argument in
5265 // C.
5266 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005267 SourceLocation EqualLoc = Tok.getLocation();
5268
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005269 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005270 if (D.getContext() == Declarator::MemberContext) {
5271 // If we're inside a class definition, cache the tokens
5272 // corresponding to the default argument. We'll actually parse
5273 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005274 // FIXME: Can we use a smart pointer for Toks?
5275 DefArgToks = new CachedTokens;
5276
Mike Stump11289f42009-09-09 15:08:12 +00005277 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005278 /*StopAtSemi=*/true,
5279 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005280 delete DefArgToks;
5281 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005282 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005283 } else {
5284 // Mark the end of the default argument so that we know when to
5285 // stop when we parse it later on.
5286 Token DefArgEnd;
5287 DefArgEnd.startToken();
5288 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5289 DefArgEnd.setLocation(Tok.getLocation());
5290 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005291 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005292 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005293 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005294 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005295 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005296 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005297
Chad Rosierc1183952012-06-26 22:30:43 +00005298 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005299 // used.
5300 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005301 Sema::PotentiallyEvaluatedIfUsed,
5302 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005303
Sebastian Redldb63af22012-03-14 15:54:00 +00005304 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005305 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005306 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005307 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005308 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005309 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005310 if (DefArgResult.isInvalid()) {
5311 Actions.ActOnParamDefaultArgumentError(Param);
5312 SkipUntil(tok::comma, tok::r_paren, true, true);
5313 } else {
5314 // Inform the actions module about the default argument
5315 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005316 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005317 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005318 }
5319 }
Mike Stump11289f42009-09-09 15:08:12 +00005320
5321 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5322 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005323 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005324 }
5325
5326 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005327 if (Tok.isNot(tok::comma)) {
5328 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005329 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005330
David Blaikiebbafb8a2012-03-11 07:00:24 +00005331 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005332 // We have ellipsis without a preceding ',', which is ill-formed
5333 // in C. Complain and provide the fix.
5334 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005335 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005336 }
5337 }
Chad Rosierc1183952012-06-26 22:30:43 +00005338
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005339 break;
5340 }
Mike Stump11289f42009-09-09 15:08:12 +00005341
Chris Lattner371ed4e2008-04-06 06:57:35 +00005342 // Consume the comma.
5343 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005344 }
Mike Stump11289f42009-09-09 15:08:12 +00005345
Chris Lattner6c940e62008-04-06 06:34:08 +00005346}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005347
Chris Lattnere8074e62006-08-06 18:30:15 +00005348/// [C90] direct-declarator '[' constant-expression[opt] ']'
5349/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5350/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5351/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5352/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005353/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5354/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005355void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005356 if (CheckProhibitedCXX11Attribute())
5357 return;
5358
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005359 BalancedDelimiterTracker T(*this, tok::l_square);
5360 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005361
Chris Lattner84a11622008-12-18 07:27:21 +00005362 // C array syntax has many features, but by-far the most common is [] and [4].
5363 // This code does a fast path to handle some of the most obvious cases.
5364 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005365 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005366 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005367 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005368
Chris Lattner84a11622008-12-18 07:27:21 +00005369 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005370 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005371 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005372 T.getOpenLocation(),
5373 T.getCloseLocation()),
5374 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005375 return;
5376 } else if (Tok.getKind() == tok::numeric_constant &&
5377 GetLookAheadToken(1).is(tok::r_square)) {
5378 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005379 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005380 ConsumeToken();
5381
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005382 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005383 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005384 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005385
Chris Lattner84a11622008-12-18 07:27:21 +00005386 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005387 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005388 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005389 T.getOpenLocation(),
5390 T.getCloseLocation()),
5391 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005392 return;
5393 }
Mike Stump11289f42009-09-09 15:08:12 +00005394
Chris Lattnere8074e62006-08-06 18:30:15 +00005395 // If valid, this location is the position where we read the 'static' keyword.
5396 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005397 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005398 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005399
Chris Lattnere8074e62006-08-06 18:30:15 +00005400 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005401 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005402 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005403 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005404
Chris Lattnere8074e62006-08-06 18:30:15 +00005405 // If we haven't already read 'static', check to see if there is one after the
5406 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005407 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005408 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005409
Chris Lattnere8074e62006-08-06 18:30:15 +00005410 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005411 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005412 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005413
Chris Lattner521ff2b2008-04-06 05:26:30 +00005414 // Handle the case where we have '[*]' as the array size. However, a leading
5415 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005416 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005417 // infrequent, use of lookahead is not costly here.
5418 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005419 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005420
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005421 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005422 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005423 StaticLoc = SourceLocation(); // Drop the static.
5424 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005425 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005426 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005427 // Note, in C89, this production uses the constant-expr production instead
5428 // of assignment-expr. The only difference is that assignment-expr allows
5429 // things like '=' and '*='. Sema rejects these in C89 mode because they
5430 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005431
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005432 // Parse the constant-expression or assignment-expression now (depending
5433 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005434 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005435 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005436 } else {
5437 EnterExpressionEvaluationContext Unevaluated(Actions,
5438 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005439 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005440 }
Chris Lattner62591722006-08-12 18:40:58 +00005441 }
Mike Stump11289f42009-09-09 15:08:12 +00005442
Chris Lattner62591722006-08-12 18:40:58 +00005443 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005444 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005445 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005446 // If the expression was invalid, skip it.
5447 SkipUntil(tok::r_square);
5448 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005449 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005450
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005451 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005452
John McCall084e83d2011-03-24 11:26:52 +00005453 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005454 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005455
Chris Lattner84a11622008-12-18 07:27:21 +00005456 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005457 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005458 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005459 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005460 T.getOpenLocation(),
5461 T.getCloseLocation()),
5462 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005463}
5464
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005465/// [GNU] typeof-specifier:
5466/// typeof ( expressions )
5467/// typeof ( type-name )
5468/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005469///
5470void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005471 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005472 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005473 SourceLocation StartLoc = ConsumeToken();
5474
John McCalle8595032010-01-13 20:03:27 +00005475 const bool hasParens = Tok.is(tok::l_paren);
5476
Eli Friedman15681d62012-09-26 04:34:21 +00005477 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5478 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005479
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005480 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005481 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005482 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005483 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5484 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005485 if (hasParens)
5486 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005487
5488 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005489 // FIXME: Not accurate, the range gets one token more than it should.
5490 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005491 else
5492 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005493
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005494 if (isCastExpr) {
5495 if (!CastTy) {
5496 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005497 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005498 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005499
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005500 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005501 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005502 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5503 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005504 DiagID, CastTy))
5505 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005506 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005507 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005508
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005509 // If we get here, the operand to the typeof was an expresion.
5510 if (Operand.isInvalid()) {
5511 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005512 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005513 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005514
Eli Friedmane0afc982012-01-21 01:01:51 +00005515 // We might need to transform the operand if it is potentially evaluated.
5516 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5517 if (Operand.isInvalid()) {
5518 DS.SetTypeSpecError();
5519 return;
5520 }
5521
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005522 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005523 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005524 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5525 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005526 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005527 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005528}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005529
Benjamin Kramere56f3932011-12-23 17:00:35 +00005530/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005531/// _Atomic ( type-name )
5532///
5533void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005534 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5535 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005536
5537 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005538 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005539 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005540 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005541
5542 TypeResult Result = ParseTypeName();
5543 if (Result.isInvalid()) {
5544 SkipUntil(tok::r_paren);
5545 return;
5546 }
5547
5548 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005549 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005550
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005551 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005552 return;
5553
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005554 DS.setTypeofParensRange(T.getRange());
5555 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005556
5557 const char *PrevSpec = 0;
5558 unsigned DiagID;
5559 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5560 DiagID, Result.release()))
5561 Diag(StartLoc, DiagID) << PrevSpec;
5562}
5563
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005564
5565/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5566/// from TryAltiVecVectorToken.
5567bool Parser::TryAltiVecVectorTokenOutOfLine() {
5568 Token Next = NextToken();
5569 switch (Next.getKind()) {
5570 default: return false;
5571 case tok::kw_short:
5572 case tok::kw_long:
5573 case tok::kw_signed:
5574 case tok::kw_unsigned:
5575 case tok::kw_void:
5576 case tok::kw_char:
5577 case tok::kw_int:
5578 case tok::kw_float:
5579 case tok::kw_double:
5580 case tok::kw_bool:
5581 case tok::kw___pixel:
5582 Tok.setKind(tok::kw___vector);
5583 return true;
5584 case tok::identifier:
5585 if (Next.getIdentifierInfo() == Ident_pixel) {
5586 Tok.setKind(tok::kw___vector);
5587 return true;
5588 }
5589 return false;
5590 }
5591}
5592
5593bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5594 const char *&PrevSpec, unsigned &DiagID,
5595 bool &isInvalid) {
5596 if (Tok.getIdentifierInfo() == Ident_vector) {
5597 Token Next = NextToken();
5598 switch (Next.getKind()) {
5599 case tok::kw_short:
5600 case tok::kw_long:
5601 case tok::kw_signed:
5602 case tok::kw_unsigned:
5603 case tok::kw_void:
5604 case tok::kw_char:
5605 case tok::kw_int:
5606 case tok::kw_float:
5607 case tok::kw_double:
5608 case tok::kw_bool:
5609 case tok::kw___pixel:
5610 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5611 return true;
5612 case tok::identifier:
5613 if (Next.getIdentifierInfo() == Ident_pixel) {
5614 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5615 return true;
5616 }
5617 break;
5618 default:
5619 break;
5620 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005621 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005622 DS.isTypeAltiVecVector()) {
5623 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5624 return true;
5625 }
5626 return false;
5627}