blob: 681468ddc79e3a5ede8d40a62f6777d56b87dafd [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"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +0000104/// token lookahead. Comment from gcc: "If they start with an identifier
105/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106/// start with that identifier; otherwise they are an expression list."
107///
Richard Smithb12bf692011-10-17 21:20:17 +0000108/// GCC does not require the ',' between attribs in an attribute-list.
109///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110/// At the moment, I am not doing 2 token lookahead. I am also unaware of
111/// any attributes that don't work (based on my limited testing). Most
112/// attributes are very simple in practice. Until we find a bug, I don't see
113/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000114
John McCall53fa7142010-12-24 02:08:15 +0000115void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000116 SourceLocation *endLoc,
117 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner76c72282007-10-09 17:33:22 +0000120 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 ConsumeToken();
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
123 "attribute")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000125 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000126 }
127 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
128 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000129 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 }
131 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000132 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
133 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000134 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
136 ConsumeToken();
137 continue;
138 }
139 // we have an identifier or declaration specifier (const, int, etc.)
140 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
141 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000142
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000143 if (Tok.is(tok::l_paren)) {
144 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000145 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 LateParsedAttribute *LA =
147 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
148 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000149
Bill Wendling44426052012-12-20 19:22:21 +0000150 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000151 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000152 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000153 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000154
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000155 // consume everything up to and including the matching right parens
156 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 Token Eof;
159 Eof.startToken();
160 Eof.setLocation(Tok.getLocation());
161 LA->Toks.push_back(Eof);
162 } else {
Michael Han23214e52012-10-03 01:56:22 +0000163 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000164 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000165 }
166 } else {
John McCall084e83d2011-03-24 11:26:52 +0000167 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000168 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000169 }
170 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000171 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000172 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000173 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
175 SkipUntil(tok::r_paren, false);
176 }
John McCall53fa7142010-12-24 02:08:15 +0000177 if (endLoc)
178 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000181
Douglas Gregord2472d42013-05-02 23:25:32 +0000182/// \brief Determine whether the given attribute has all expression arguments.
183static bool attributeHasExprArgs(const IdentifierInfo &II) {
184 return llvm::StringSwitch<bool>(II.getName())
185#include "clang/Parse/AttrExprArgs.inc"
186 .Default(false);
187}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000188
Michael Han23214e52012-10-03 01:56:22 +0000189/// Parse the arguments to a parameterized GNU attribute or
190/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000191void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
192 SourceLocation AttrNameLoc,
193 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000194 SourceLocation *EndLoc,
195 IdentifierInfo *ScopeName,
196 SourceLocation ScopeLoc,
197 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000198
199 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
200
201 // Availability attributes have their own grammar.
202 if (AttrName->isStr("availability")) {
203 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
206 // Thread safety attributes fit into the FIXME case above, so we
207 // just parse the arguments as a list of expressions
208 if (IsThreadSafetyAttribute(AttrName->getName())) {
209 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
210 return;
211 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000212 // Type safety attributes have their own grammar.
213 if (AttrName->isStr("type_tag_for_datatype")) {
214 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
215 return;
216 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000217
218 ConsumeParen(); // ignore the left paren loc for now
219
Richard Smithb12bf692011-10-17 21:20:17 +0000220 IdentifierInfo *ParmName = 0;
221 SourceLocation ParmLoc;
222 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000223
Joey Goulyaba589c2013-03-08 09:42:32 +0000224 TypeResult T;
225 SourceRange TypeRange;
226 bool TypeParsed = false;
227
Richard Smithb12bf692011-10-17 21:20:17 +0000228 switch (Tok.getKind()) {
229 case tok::kw_char:
230 case tok::kw_wchar_t:
231 case tok::kw_char16_t:
232 case tok::kw_char32_t:
233 case tok::kw_bool:
234 case tok::kw_short:
235 case tok::kw_int:
236 case tok::kw_long:
237 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000238 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000239 case tok::kw_signed:
240 case tok::kw_unsigned:
241 case tok::kw_float:
242 case tok::kw_double:
243 case tok::kw_void:
244 case tok::kw_typeof:
245 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000246 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000247 T = ParseTypeName(&TypeRange);
248 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000249 break;
250
251 case tok::identifier:
Joey Goulyaba589c2013-03-08 09:42:32 +0000252 if (AttrName->isStr("vec_type_hint")) {
253 T = ParseTypeName(&TypeRange);
254 TypeParsed = true;
255 break;
256 }
Douglas Gregord2472d42013-05-02 23:25:32 +0000257 // If the attribute has all expression arguments, and not a "parameter",
258 // break out to handle it below.
259 if (attributeHasExprArgs(*AttrName))
260 break;
Richard Smithb12bf692011-10-17 21:20:17 +0000261 ParmName = Tok.getIdentifierInfo();
262 ParmLoc = ConsumeToken();
263 break;
264
265 default:
266 break;
267 }
268
Benjamin Kramerf0623432012-08-23 22:51:59 +0000269 ExprVector ArgExprs;
Joey Goulyaba589c2013-03-08 09:42:32 +0000270 bool isInvalid = false;
271 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000272
Joey Goulyaba589c2013-03-08 09:42:32 +0000273 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithb12bf692011-10-17 21:20:17 +0000274 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
275 // Eat the comma.
276 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000277 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000278
Richard Smithb12bf692011-10-17 21:20:17 +0000279 // Parse the non-empty comma-separated list of expressions.
280 while (1) {
281 ExprResult ArgExpr(ParseAssignmentExpression());
282 if (ArgExpr.isInvalid()) {
283 SkipUntil(tok::r_paren);
284 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000285 }
Richard Smithb12bf692011-10-17 21:20:17 +0000286 ArgExprs.push_back(ArgExpr.release());
287 if (Tok.isNot(tok::comma))
288 break;
289 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000290 }
Richard Smithb12bf692011-10-17 21:20:17 +0000291 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000292 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
293 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
294 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000295 while (Tok.is(tok::identifier)) {
296 ConsumeToken();
297 if (Tok.is(tok::greater))
298 break;
299 if (Tok.is(tok::comma)) {
300 ConsumeToken();
301 continue;
302 }
303 }
304 if (Tok.isNot(tok::greater))
305 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000306 SkipUntil(tok::r_paren, false, true); // skip until ')'
307 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000308 } else if (AttrName->isStr("vec_type_hint")) {
309 if (T.get() && !T.isInvalid())
310 isParmType = true;
311 else {
312 if (Tok.is(tok::identifier))
313 ConsumeToken();
314 if (TypeParsed)
315 isInvalid = true;
316 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000317 }
Richard Smithb12bf692011-10-17 21:20:17 +0000318
319 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000320 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
321 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000322 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000323 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000324 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
325 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
326 } else {
327 AttributeList *attr = Attrs.addNew(
328 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
329 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
330 if (BuiltinType &&
331 attr->getKind() == AttributeList::AT_IBOutletCollection)
332 Diag(Tok, diag::err_iboutletcollection_builtintype);
333 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000334 }
335}
336
Chad Rosierc1183952012-06-26 22:30:43 +0000337/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000338/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000339void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000340 SourceLocation AttrNameLoc,
341 ParsedAttributes &Attrs)
342{
343 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000344 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000345 AttrName->getNameStart(), tok::r_paren))
346 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000347
Aaron Ballman478faed2012-06-19 22:09:27 +0000348 ExprResult ArgExpr(ParseConstantExpression());
349 if (ArgExpr.isInvalid()) {
350 T.skipToEnd();
351 return;
352 }
353 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000354 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000355 &ExprList, 1, AttributeList::AS_Declspec);
356
357 T.consumeClose();
358}
359
Chad Rosierc1183952012-06-26 22:30:43 +0000360/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000361/// arguments.
362bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
363 return llvm::StringSwitch<bool>(Ident->getName())
364 .Case("dllimport", true)
365 .Case("dllexport", true)
366 .Case("noreturn", true)
367 .Case("nothrow", true)
368 .Case("noinline", true)
369 .Case("naked", true)
370 .Case("appdomain", true)
371 .Case("process", true)
372 .Case("jitintrinsic", true)
373 .Case("noalias", true)
374 .Case("restrict", true)
375 .Case("novtable", true)
376 .Case("selectany", true)
377 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000378 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000379 .Default(false);
380}
381
Chad Rosierc1183952012-06-26 22:30:43 +0000382/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000383/// parameters). Will return false if we properly handled the declspec, or
384/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000385void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000386 SourceLocation Loc,
387 ParsedAttributes &Attrs) {
388 // Try to handle the easy case first -- these declspecs all take a single
389 // parameter as their argument.
390 if (llvm::StringSwitch<bool>(Ident->getName())
391 .Case("uuid", true)
392 .Case("align", true)
393 .Case("allocate", true)
394 .Default(false)) {
395 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
396 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000397 // The deprecated declspec has an optional single argument, so we will
398 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000399 // not.
400 if (Tok.getKind() == tok::l_paren)
401 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
402 else
Chad Rosierc1183952012-06-26 22:30:43 +0000403 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000404 AttributeList::AS_Declspec);
405 } else if (Ident->getName() == "property") {
406 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000407 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000409 if (Tok.isNot(tok::l_paren)) {
410 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
411 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 return;
John McCall5e77d762013-04-16 07:28:30 +0000413 }
414 BalancedDelimiterTracker T(*this, tok::l_paren);
415 T.expectAndConsume(diag::err_expected_lparen_after,
416 Ident->getNameStart(), tok::r_paren);
417
418 enum AccessorKind {
419 AK_Invalid = -1,
420 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
421 };
422 IdentifierInfo *AccessorNames[] = { 0, 0 };
423 bool HasInvalidAccessor = false;
424
425 // Parse the accessor specifications.
426 while (true) {
427 // Stop if this doesn't look like an accessor spec.
428 if (!Tok.is(tok::identifier)) {
429 // If the user wrote a completely empty list, use a special diagnostic.
430 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
431 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
432 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
433 break;
434 }
435
436 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
437 break;
438 }
439
440 AccessorKind Kind;
441 SourceLocation KindLoc = Tok.getLocation();
442 StringRef KindStr = Tok.getIdentifierInfo()->getName();
443 if (KindStr == "get") {
444 Kind = AK_Get;
445 } else if (KindStr == "put") {
446 Kind = AK_Put;
447
448 // Recover from the common mistake of using 'set' instead of 'put'.
449 } else if (KindStr == "set") {
450 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
451 << FixItHint::CreateReplacement(KindLoc, "put");
452 Kind = AK_Put;
453
454 // Handle the mistake of forgetting the accessor kind by skipping
455 // this accessor.
456 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
457 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
458 ConsumeToken();
459 HasInvalidAccessor = true;
460 goto next_property_accessor;
461
462 // Otherwise, complain about the unknown accessor kind.
463 } else {
464 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
465 HasInvalidAccessor = true;
466 Kind = AK_Invalid;
467
468 // Try to keep parsing unless it doesn't look like an accessor spec.
469 if (!NextToken().is(tok::equal)) break;
470 }
471
472 // Consume the identifier.
473 ConsumeToken();
474
475 // Consume the '='.
476 if (Tok.is(tok::equal)) {
477 ConsumeToken();
478 } else {
479 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
480 << KindStr;
481 break;
482 }
483
484 // Expect the method name.
485 if (!Tok.is(tok::identifier)) {
486 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
487 break;
488 }
489
490 if (Kind == AK_Invalid) {
491 // Just drop invalid accessors.
492 } else if (AccessorNames[Kind] != NULL) {
493 // Complain about the repeated accessor, ignore it, and keep parsing.
494 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
495 } else {
496 AccessorNames[Kind] = Tok.getIdentifierInfo();
497 }
498 ConsumeToken();
499
500 next_property_accessor:
501 // Keep processing accessors until we run out.
502 if (Tok.is(tok::comma)) {
503 ConsumeAnyToken();
504 continue;
505
506 // If we run into the ')', stop without consuming it.
507 } else if (Tok.is(tok::r_paren)) {
508 break;
509 } else {
510 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
511 break;
512 }
513 }
514
515 // Only add the property attribute if it was well-formed.
516 if (!HasInvalidAccessor) {
517 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
518 SourceLocation(),
519 AccessorNames[AK_Get], AccessorNames[AK_Put],
520 AttributeList::AS_Declspec);
521 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000522 T.skipToEnd();
523 } else {
524 // We don't recognize this as a valid declspec, but instead of creating the
525 // attribute and allowing sema to warn about it, we will warn here instead.
526 // This is because some attributes have multiple spellings, but we need to
527 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000528 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000529 // both locations.
530 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
531
532 // If there's an open paren, we should eat the open and close parens under
533 // the assumption that this unknown declspec has parameters.
534 BalancedDelimiterTracker T(*this, tok::l_paren);
535 if (!T.consumeOpen())
536 T.skipToEnd();
537 }
538}
539
Eli Friedman06de2b52009-06-08 07:21:15 +0000540/// [MS] decl-specifier:
541/// __declspec ( extended-decl-modifier-seq )
542///
543/// [MS] extended-decl-modifier-seq:
544/// extended-decl-modifier[opt]
545/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000546void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000547 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000548
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000549 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000551 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000552 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000553 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000554
Chad Rosierc1183952012-06-26 22:30:43 +0000555 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000556 // you can specify multiple attributes per declspec.
557 while (Tok.getKind() != tok::r_paren) {
558 // We expect either a well-known identifier or a generic string. Anything
559 // else is a malformed declspec.
560 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000561 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000562 Tok.getKind() != tok::kw_restrict) {
563 Diag(Tok, diag::err_ms_declspec_type);
564 T.skipToEnd();
565 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000566 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000567
568 IdentifierInfo *AttrName;
569 SourceLocation AttrNameLoc;
570 if (IsString) {
571 SmallString<8> StrBuffer;
572 bool Invalid = false;
573 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
574 if (Invalid) {
575 T.skipToEnd();
576 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000577 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000578 AttrName = PP.getIdentifierInfo(Str);
579 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000580 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000581 AttrName = Tok.getIdentifierInfo();
582 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000583 }
Chad Rosierc1183952012-06-26 22:30:43 +0000584
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000586 // If we have a generic string, we will allow it because there is no
587 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000588 // (for instance, SAL declspecs in older versions of MSVC).
589 //
Chad Rosierc1183952012-06-26 22:30:43 +0000590 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000591 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000592 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000593 0, 0, AttributeList::AS_Declspec);
594 else
595 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000596 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000597 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000598}
599
John McCall53fa7142010-12-24 02:08:15 +0000600void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000601 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000602 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000603 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000604 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000605 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
606 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000607 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
608 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000609 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000610 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000611 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000612}
613
John McCall53fa7142010-12-24 02:08:15 +0000614void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000615 // Treat these like attributes
616 while (Tok.is(tok::kw___pascal)) {
617 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
618 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000619 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000620 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000621 }
John McCall53fa7142010-12-24 02:08:15 +0000622}
623
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000624void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
625 // Treat these like attributes
626 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000627 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000628 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000629 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
630 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000631 }
632}
633
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000634void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000635 // FIXME: The mapping from attribute spelling to semantics should be
636 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637 SourceLocation Loc = Tok.getLocation();
638 switch(Tok.getKind()) {
639 // OpenCL qualifiers:
640 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000641 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000642 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000643 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000644 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000646
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000648 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000650 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000651 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000652
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000658
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000660 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000661 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000662 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000664
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000665 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000667 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000670
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000671 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000672 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000673 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000674 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000675 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000676
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000677 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000678 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000679 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000680 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000681 break;
682 default: break;
683 }
684}
685
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000686/// \brief Parse a version number.
687///
688/// version:
689/// simple-integer
690/// simple-integer ',' simple-integer
691/// simple-integer ',' simple-integer ',' simple-integer
692VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
693 Range = Tok.getLocation();
694
695 if (!Tok.is(tok::numeric_constant)) {
696 Diag(Tok, diag::err_expected_version);
697 SkipUntil(tok::comma, tok::r_paren, true, true, true);
698 return VersionTuple();
699 }
700
701 // Parse the major (and possibly minor and subminor) versions, which
702 // are stored in the numeric constant. We utilize a quirk of the
703 // lexer, which is that it handles something like 1.2.3 as a single
704 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000705 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000706 Buffer.resize(Tok.getLength()+1);
707 const char *ThisTokBegin = &Buffer[0];
708
709 // Get the spelling of the token, which eliminates trigraphs, etc.
710 bool Invalid = false;
711 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
712 if (Invalid)
713 return VersionTuple();
714
715 // Parse the major version.
716 unsigned AfterMajor = 0;
717 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000718 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000719 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
720 ++AfterMajor;
721 }
722
723 if (AfterMajor == 0) {
724 Diag(Tok, diag::err_expected_version);
725 SkipUntil(tok::comma, tok::r_paren, true, true, true);
726 return VersionTuple();
727 }
728
729 if (AfterMajor == ActualLength) {
730 ConsumeToken();
731
732 // We only had a single version component.
733 if (Major == 0) {
734 Diag(Tok, diag::err_zero_version);
735 return VersionTuple();
736 }
737
738 return VersionTuple(Major);
739 }
740
741 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
742 Diag(Tok, diag::err_expected_version);
743 SkipUntil(tok::comma, tok::r_paren, true, true, true);
744 return VersionTuple();
745 }
746
747 // Parse the minor version.
748 unsigned AfterMinor = AfterMajor + 1;
749 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000750 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000751 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
752 ++AfterMinor;
753 }
754
755 if (AfterMinor == ActualLength) {
756 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000757
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000758 // We had major.minor.
759 if (Major == 0 && Minor == 0) {
760 Diag(Tok, diag::err_zero_version);
761 return VersionTuple();
762 }
763
Chad Rosierc1183952012-06-26 22:30:43 +0000764 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 }
766
767 // If what follows is not a '.', we have a problem.
768 if (ThisTokBegin[AfterMinor] != '.') {
769 Diag(Tok, diag::err_expected_version);
770 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000771 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000772 }
773
774 // Parse the subminor version.
775 unsigned AfterSubminor = AfterMinor + 1;
776 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000777 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000778 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
779 ++AfterSubminor;
780 }
781
782 if (AfterSubminor != ActualLength) {
783 Diag(Tok, diag::err_expected_version);
784 SkipUntil(tok::comma, tok::r_paren, true, true, true);
785 return VersionTuple();
786 }
787 ConsumeToken();
788 return VersionTuple(Major, Minor, Subminor);
789}
790
791/// \brief Parse the contents of the "availability" attribute.
792///
793/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000794/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000795///
796/// platform:
797/// identifier
798///
799/// version-arg-list:
800/// version-arg
801/// version-arg ',' version-arg-list
802///
803/// version-arg:
804/// 'introduced' '=' version
805/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000806/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000807/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000808/// opt-message:
809/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
811 SourceLocation AvailabilityLoc,
812 ParsedAttributes &attrs,
813 SourceLocation *endLoc) {
814 SourceLocation PlatformLoc;
815 IdentifierInfo *Platform = 0;
816
817 enum { Introduced, Deprecated, Obsoleted, Unknown };
818 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000819 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820
821 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000822 BalancedDelimiterTracker T(*this, tok::l_paren);
823 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824 Diag(Tok, diag::err_expected_lparen);
825 return;
826 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000827
828 // Parse the platform name,
829 if (Tok.isNot(tok::identifier)) {
830 Diag(Tok, diag::err_availability_expected_platform);
831 SkipUntil(tok::r_paren);
832 return;
833 }
834 Platform = Tok.getIdentifierInfo();
835 PlatformLoc = ConsumeToken();
836
837 // Parse the ',' following the platform name.
838 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
839 return;
840
841 // If we haven't grabbed the pointers for the identifiers
842 // "introduced", "deprecated", and "obsoleted", do so now.
843 if (!Ident_introduced) {
844 Ident_introduced = PP.getIdentifierInfo("introduced");
845 Ident_deprecated = PP.getIdentifierInfo("deprecated");
846 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000847 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000848 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000849 }
850
851 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000852 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000853 do {
854 if (Tok.isNot(tok::identifier)) {
855 Diag(Tok, diag::err_availability_expected_change);
856 SkipUntil(tok::r_paren);
857 return;
858 }
859 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
860 SourceLocation KeywordLoc = ConsumeToken();
861
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000862 if (Keyword == Ident_unavailable) {
863 if (UnavailableLoc.isValid()) {
864 Diag(KeywordLoc, diag::err_availability_redundant)
865 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000866 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000867 UnavailableLoc = KeywordLoc;
868
869 if (Tok.isNot(tok::comma))
870 break;
871
872 ConsumeToken();
873 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000874 }
875
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000876 if (Tok.isNot(tok::equal)) {
877 Diag(Tok, diag::err_expected_equal_after)
878 << Keyword;
879 SkipUntil(tok::r_paren);
880 return;
881 }
882 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000883 if (Keyword == Ident_message) {
884 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000885 Diag(Tok, diag::err_expected_string_literal)
886 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000887 SkipUntil(tok::r_paren);
888 return;
889 }
890 MessageExpr = ParseStringLiteralExpression();
891 break;
892 }
Chad Rosierc1183952012-06-26 22:30:43 +0000893
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000894 SourceRange VersionRange;
895 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000896
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000897 if (Version.empty()) {
898 SkipUntil(tok::r_paren);
899 return;
900 }
901
902 unsigned Index;
903 if (Keyword == Ident_introduced)
904 Index = Introduced;
905 else if (Keyword == Ident_deprecated)
906 Index = Deprecated;
907 else if (Keyword == Ident_obsoleted)
908 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000909 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000910 Index = Unknown;
911
912 if (Index < Unknown) {
913 if (!Changes[Index].KeywordLoc.isInvalid()) {
914 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000915 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000916 << SourceRange(Changes[Index].KeywordLoc,
917 Changes[Index].VersionRange.getEnd());
918 }
919
920 Changes[Index].KeywordLoc = KeywordLoc;
921 Changes[Index].Version = Version;
922 Changes[Index].VersionRange = VersionRange;
923 } else {
924 Diag(KeywordLoc, diag::err_availability_unknown_change)
925 << Keyword << VersionRange;
926 }
927
928 if (Tok.isNot(tok::comma))
929 break;
930
931 ConsumeToken();
932 } while (true);
933
934 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000935 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000936 return;
937
938 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000939 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000940
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000941 // The 'unavailable' availability cannot be combined with any other
942 // availability changes. Make sure that hasn't happened.
943 if (UnavailableLoc.isValid()) {
944 bool Complained = false;
945 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
946 if (Changes[Index].KeywordLoc.isValid()) {
947 if (!Complained) {
948 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
949 << SourceRange(Changes[Index].KeywordLoc,
950 Changes[Index].VersionRange.getEnd());
951 Complained = true;
952 }
953
954 // Clear out the availability.
955 Changes[Index] = AvailabilityChange();
956 }
957 }
958 }
959
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000960 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000961 attrs.addNew(&Availability,
962 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000963 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000964 Platform, PlatformLoc,
965 Changes[Introduced],
966 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000967 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000968 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000969 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000970}
971
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972
Bill Wendling44426052012-12-20 19:22:21 +0000973// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000974// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
975
976void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
977
978void Parser::LateParsedClass::ParseLexedAttributes() {
979 Self->ParseLexedAttributes(*Class);
980}
981
982void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000983 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000984}
985
986/// Wrapper class which calls ParseLexedAttribute, after setting up the
987/// scope appropriately.
988void Parser::ParseLexedAttributes(ParsingClass &Class) {
989 // Deal with templates
990 // FIXME: Test cases to make sure this does the right thing for templates.
991 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
992 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
993 HasTemplateScope);
994 if (HasTemplateScope)
995 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
996
Douglas Gregor3024f072012-04-16 07:05:22 +0000997 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000998 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000999 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001000 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1001 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1002
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001003 // Enter the scope of nested classes
1004 if (!AlreadyHasClassScope)
1005 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1006 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001007 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001008 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1009 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1010 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001011 }
Chad Rosierc1183952012-06-26 22:30:43 +00001012
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001013 if (!AlreadyHasClassScope)
1014 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1015 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001016}
1017
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001018
1019/// \brief Parse all attributes in LAs, and attach them to Decl D.
1020void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1021 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001022 assert(LAs.parseSoon() &&
1023 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001024 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001025 if (D)
1026 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001027 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001028 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001029 }
1030 LAs.clear();
1031}
1032
1033
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001034/// \brief Finish parsing an attribute for which parsing was delayed.
1035/// This will be called at the end of parsing a class declaration
1036/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001037/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001038/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001039void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1040 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001041 // Save the current token position.
1042 SourceLocation OrigLoc = Tok.getLocation();
1043
1044 // Append the current token at the end of the new token stream so that it
1045 // doesn't get lost.
1046 LA.Toks.push_back(Tok);
1047 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1048 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001049 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001050
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001051 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001052 // FIXME: Do not warn on C++11 attributes, once we start supporting
1053 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001054 Diag(Tok, diag::warn_attribute_on_function_definition)
1055 << LA.AttrName.getName();
1056 }
1057
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001058 ParsedAttributes Attrs(AttrFactory);
1059 SourceLocation endLoc;
1060
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001061 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001062 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001063 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1064 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001065
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001066 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001067 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1068 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001069
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001070 if (LA.Decls.size() == 1) {
1071 // If the Decl is templatized, add template parameters to scope.
1072 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1073 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1074 if (HasTemplateScope)
1075 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001076
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001077 // If the Decl is on a function, add function parameters to the scope.
1078 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1079 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1080 if (HasFunScope)
1081 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001082
Michael Han23214e52012-10-03 01:56:22 +00001083 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001084 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001085
1086 if (HasFunScope) {
1087 Actions.ActOnExitFunctionContext();
1088 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1089 }
1090 if (HasTemplateScope) {
1091 TempScope.Exit();
1092 }
1093 } else {
1094 // If there are multiple decls, then the decl cannot be within the
1095 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001096 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001097 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001098 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001099 } else {
1100 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001101 }
1102
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001103 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1104 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1105 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001106
1107 if (Tok.getLocation() != OrigLoc) {
1108 // Due to a parsing error, we either went over the cached tokens or
1109 // there are still cached tokens left, so we skip the leftover tokens.
1110 // Since this is an uncommon situation that should be avoided, use the
1111 // expensive isBeforeInTranslationUnit call.
1112 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1113 OrigLoc))
1114 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001115 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001116 }
1117}
1118
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001119/// \brief Wrapper around a case statement checking if AttrName is
1120/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001121bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001122 return llvm::StringSwitch<bool>(AttrName)
1123 .Case("guarded_by", true)
1124 .Case("guarded_var", true)
1125 .Case("pt_guarded_by", true)
1126 .Case("pt_guarded_var", true)
1127 .Case("lockable", true)
1128 .Case("scoped_lockable", true)
1129 .Case("no_thread_safety_analysis", true)
1130 .Case("acquired_after", true)
1131 .Case("acquired_before", true)
1132 .Case("exclusive_lock_function", true)
1133 .Case("shared_lock_function", true)
1134 .Case("exclusive_trylock_function", true)
1135 .Case("shared_trylock_function", true)
1136 .Case("unlock_function", true)
1137 .Case("lock_returned", true)
1138 .Case("locks_excluded", true)
1139 .Case("exclusive_locks_required", true)
1140 .Case("shared_locks_required", true)
1141 .Default(false);
1142}
1143
1144/// \brief Parse the contents of thread safety attributes. These
1145/// should always be parsed as an expression list.
1146///
1147/// We need to special case the parsing due to the fact that if the first token
1148/// of the first argument is an identifier, the main parse loop will store
1149/// that token as a "parameter" and the rest of
1150/// the arguments will be added to a list of "arguments". However,
1151/// subsequent tokens in the first argument are lost. We instead parse each
1152/// argument as an expression and add all arguments to the list of "arguments".
1153/// In future, we will take advantage of this special case to also
1154/// deal with some argument scoping issues here (for example, referring to a
1155/// function parameter in the attribute on that function).
1156void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1157 SourceLocation AttrNameLoc,
1158 ParsedAttributes &Attrs,
1159 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001160 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001161
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001162 BalancedDelimiterTracker T(*this, tok::l_paren);
1163 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001164
Benjamin Kramerf0623432012-08-23 22:51:59 +00001165 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001166 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001167
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001168 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001169 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001170 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001171 ExprResult ArgExpr(ParseAssignmentExpression());
1172 if (ArgExpr.isInvalid()) {
1173 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001174 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001175 break;
1176 } else {
1177 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001178 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001179 if (Tok.isNot(tok::comma))
1180 break;
1181 ConsumeToken(); // Eat the comma, move to the next argument
1182 }
1183 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001184 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001185 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001186 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001187 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001188 if (EndLoc)
1189 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001190}
1191
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001192void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1193 SourceLocation AttrNameLoc,
1194 ParsedAttributes &Attrs,
1195 SourceLocation *EndLoc) {
1196 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1197
1198 BalancedDelimiterTracker T(*this, tok::l_paren);
1199 T.consumeOpen();
1200
1201 if (Tok.isNot(tok::identifier)) {
1202 Diag(Tok, diag::err_expected_ident);
1203 T.skipToEnd();
1204 return;
1205 }
1206 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1207 SourceLocation ArgumentKindLoc = ConsumeToken();
1208
1209 if (Tok.isNot(tok::comma)) {
1210 Diag(Tok, diag::err_expected_comma);
1211 T.skipToEnd();
1212 return;
1213 }
1214 ConsumeToken();
1215
1216 SourceRange MatchingCTypeRange;
1217 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1218 if (MatchingCType.isInvalid()) {
1219 T.skipToEnd();
1220 return;
1221 }
1222
1223 bool LayoutCompatible = false;
1224 bool MustBeNull = false;
1225 while (Tok.is(tok::comma)) {
1226 ConsumeToken();
1227 if (Tok.isNot(tok::identifier)) {
1228 Diag(Tok, diag::err_expected_ident);
1229 T.skipToEnd();
1230 return;
1231 }
1232 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1233 if (Flag->isStr("layout_compatible"))
1234 LayoutCompatible = true;
1235 else if (Flag->isStr("must_be_null"))
1236 MustBeNull = true;
1237 else {
1238 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1239 T.skipToEnd();
1240 return;
1241 }
1242 ConsumeToken(); // consume flag
1243 }
1244
1245 if (!T.consumeClose()) {
1246 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1247 ArgumentKind, ArgumentKindLoc,
1248 MatchingCType.release(), LayoutCompatible,
1249 MustBeNull, AttributeList::AS_GNU);
1250 }
1251
1252 if (EndLoc)
1253 *EndLoc = T.getCloseLocation();
1254}
1255
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001256/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1257/// of a C++11 attribute-specifier in a location where an attribute is not
1258/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1259/// situation.
1260///
1261/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1262/// this doesn't appear to actually be an attribute-specifier, and the caller
1263/// should try to parse it.
1264bool Parser::DiagnoseProhibitedCXX11Attribute() {
1265 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1266
1267 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1268 case CAK_NotAttributeSpecifier:
1269 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1270 return false;
1271
1272 case CAK_InvalidAttributeSpecifier:
1273 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1274 return false;
1275
1276 case CAK_AttributeSpecifier:
1277 // Parse and discard the attributes.
1278 SourceLocation BeginLoc = ConsumeBracket();
1279 ConsumeBracket();
1280 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1281 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1282 SourceLocation EndLoc = ConsumeBracket();
1283 Diag(BeginLoc, diag::err_attributes_not_allowed)
1284 << SourceRange(BeginLoc, EndLoc);
1285 return true;
1286 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001287 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001288}
1289
Richard Smith98155ad2013-02-20 01:17:14 +00001290/// \brief We have found the opening square brackets of a C++11
1291/// attribute-specifier in a location where an attribute is not permitted, but
1292/// we know where the attributes ought to be written. Parse them anyway, and
1293/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001294void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1295 SourceLocation CorrectLocation) {
1296 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1297 Tok.is(tok::kw_alignas));
1298
1299 // Consume the attributes.
1300 SourceLocation Loc = Tok.getLocation();
1301 ParseCXX11Attributes(Attrs);
1302 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1303
1304 Diag(Loc, diag::err_attributes_not_allowed)
1305 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1306 << FixItHint::CreateRemoval(AttrRange);
1307}
1308
John McCall53fa7142010-12-24 02:08:15 +00001309void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1310 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1311 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001312}
1313
Michael Han64536a62012-11-06 19:34:54 +00001314void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1315 AttributeList *AttrList = attrs.getList();
1316 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001317 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001318 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001319 << AttrList->getName();
1320 AttrList->setInvalid();
1321 }
1322 AttrList = AttrList->getNext();
1323 }
1324}
1325
Chris Lattner53361ac2006-08-10 05:19:57 +00001326/// ParseDeclaration - Parse a full 'declaration', which consists of
1327/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001328/// 'Context' should be a Declarator::TheContext value. This returns the
1329/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001330///
1331/// declaration: [C99 6.7]
1332/// block-declaration ->
1333/// simple-declaration
1334/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001335/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001336/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001337/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001338/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001339/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001340/// others... [FIXME]
1341///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001342Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1343 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001344 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001345 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001346 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001347 // Must temporarily exit the objective-c container scope for
1348 // parsing c none objective-c decls.
1349 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001350
John McCall48871652010-08-21 09:40:31 +00001351 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001352 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001353 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001354 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001355 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001357 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001358 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001359 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001360 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001361 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001362 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001363 SourceLocation InlineLoc = ConsumeToken();
1364 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1365 break;
1366 }
Chad Rosierc1183952012-06-26 22:30:43 +00001367 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001368 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001369 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001370 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001371 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001372 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001373 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001374 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001375 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001376 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001377 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001378 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001379 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001380 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001381 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001382 default:
John McCall53fa7142010-12-24 02:08:15 +00001383 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001384 }
Chad Rosierc1183952012-06-26 22:30:43 +00001385
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001386 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001387 // single decl, convert it now. Alias declarations can also declare a type;
1388 // include that too if it is present.
1389 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001390}
1391
1392/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1393/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001394/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1395/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001396///[C90/C++]init-declarator-list ';' [TODO]
1397/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001398///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001399/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001400/// attribute-specifier-seq[opt] type-specifier-seq declarator
1401///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001402/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001403/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001404///
1405/// If FRI is non-null, we might be parsing a for-range-declaration instead
1406/// of a simple-declaration. If we find that we are, we also parse the
1407/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001408Parser::DeclGroupPtrTy
1409Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1410 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001411 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001412 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001413 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001414 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001415
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001416 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001417 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001418
Chris Lattner0e894622006-08-13 19:58:17 +00001419 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1420 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001421 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001422 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001423 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001424 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001425 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001426 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001427 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001428 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001429 }
Chad Rosierc1183952012-06-26 22:30:43 +00001430
Richard Smith2386c8b2013-02-22 09:06:26 +00001431 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001432 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001433}
Mike Stump11289f42009-09-09 15:08:12 +00001434
Richard Smith09f76ee2011-10-19 21:33:05 +00001435/// Returns true if this might be the start of a declarator, or a common typo
1436/// for a declarator.
1437bool Parser::MightBeDeclarator(unsigned Context) {
1438 switch (Tok.getKind()) {
1439 case tok::annot_cxxscope:
1440 case tok::annot_template_id:
1441 case tok::caret:
1442 case tok::code_completion:
1443 case tok::coloncolon:
1444 case tok::ellipsis:
1445 case tok::kw___attribute:
1446 case tok::kw_operator:
1447 case tok::l_paren:
1448 case tok::star:
1449 return true;
1450
1451 case tok::amp:
1452 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001453 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001454
Richard Smithc8a79032012-01-09 22:31:44 +00001455 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001456 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001457 NextToken().is(tok::l_square);
1458
1459 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001460 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001461
Richard Smith09f76ee2011-10-19 21:33:05 +00001462 case tok::identifier:
1463 switch (NextToken().getKind()) {
1464 case tok::code_completion:
1465 case tok::coloncolon:
1466 case tok::comma:
1467 case tok::equal:
1468 case tok::equalequal: // Might be a typo for '='.
1469 case tok::kw_alignas:
1470 case tok::kw_asm:
1471 case tok::kw___attribute:
1472 case tok::l_brace:
1473 case tok::l_paren:
1474 case tok::l_square:
1475 case tok::less:
1476 case tok::r_brace:
1477 case tok::r_paren:
1478 case tok::r_square:
1479 case tok::semi:
1480 return true;
1481
1482 case tok::colon:
1483 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001484 // and in block scope it's probably a label. Inside a class definition,
1485 // this is a bit-field.
1486 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001487 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001488
1489 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001490 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001491
1492 default:
1493 return false;
1494 }
1495
1496 default:
1497 return false;
1498 }
1499}
1500
Richard Smithb8caac82012-04-11 20:59:20 +00001501/// Skip until we reach something which seems like a sensible place to pick
1502/// up parsing after a malformed declaration. This will sometimes stop sooner
1503/// than SkipUntil(tok::r_brace) would, but will never stop later.
1504void Parser::SkipMalformedDecl() {
1505 while (true) {
1506 switch (Tok.getKind()) {
1507 case tok::l_brace:
1508 // Skip until matching }, then stop. We've probably skipped over
1509 // a malformed class or function definition or similar.
1510 ConsumeBrace();
1511 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1512 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1513 // This declaration isn't over yet. Keep skipping.
1514 continue;
1515 }
1516 if (Tok.is(tok::semi))
1517 ConsumeToken();
1518 return;
1519
1520 case tok::l_square:
1521 ConsumeBracket();
1522 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1523 continue;
1524
1525 case tok::l_paren:
1526 ConsumeParen();
1527 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1528 continue;
1529
1530 case tok::r_brace:
1531 return;
1532
1533 case tok::semi:
1534 ConsumeToken();
1535 return;
1536
1537 case tok::kw_inline:
1538 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001539 // a good place to pick back up parsing, except in an Objective-C
1540 // @interface context.
1541 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1542 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001543 return;
1544 break;
1545
1546 case tok::kw_namespace:
1547 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001548 // place to pick back up parsing, except in an Objective-C
1549 // @interface context.
1550 if (Tok.isAtStartOfLine() &&
1551 (!ParsingInObjCContainer || CurParsedObjCImpl))
1552 return;
1553 break;
1554
1555 case tok::at:
1556 // @end is very much like } in Objective-C contexts.
1557 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1558 ParsingInObjCContainer)
1559 return;
1560 break;
1561
1562 case tok::minus:
1563 case tok::plus:
1564 // - and + probably start new method declarations in Objective-C contexts.
1565 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001566 return;
1567 break;
1568
1569 case tok::eof:
1570 return;
1571
1572 default:
1573 break;
1574 }
1575
1576 ConsumeAnyToken();
1577 }
1578}
1579
John McCalld5a36322009-11-03 19:26:08 +00001580/// ParseDeclGroup - Having concluded that this is either a function
1581/// definition or a group of object declarations, actually parse the
1582/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001583Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1584 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001585 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001586 SourceLocation *DeclEnd,
1587 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001588 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001589 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001590 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001591
John McCalld5a36322009-11-03 19:26:08 +00001592 // Bail out if the first declarator didn't seem well-formed.
1593 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001594 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001595 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001596 }
Mike Stump11289f42009-09-09 15:08:12 +00001597
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001598 // Save late-parsed attributes for now; they need to be parsed in the
1599 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001600 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1601 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001602 if (D.isFunctionDeclarator())
1603 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1604
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001605 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001606 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001607 // Look at the next token to make sure that this isn't a function
1608 // declaration. We have to check this because __attribute__ might be the
1609 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001610 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001611
Douglas Gregor012efe22013-04-16 16:01:32 +00001612 if (AllowFunctionDefinitions) {
1613 if (isStartOfFunctionDefinition(D)) {
1614 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1615 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001616
Douglas Gregor012efe22013-04-16 16:01:32 +00001617 // Recover by treating the 'typedef' as spurious.
1618 DS.ClearStorageClassSpecs();
1619 }
1620
1621 Decl *TheDecl =
1622 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1623 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001624 }
1625
Douglas Gregor012efe22013-04-16 16:01:32 +00001626 if (isDeclarationSpecifier()) {
1627 // If there is an invalid declaration specifier right after the function
1628 // prototype, then we must be in a missing semicolon case where this isn't
1629 // actually a body. Just fall through into the code that handles it as a
1630 // prototype, and let the top-level code handle the erroneous declspec
1631 // where it would otherwise expect a comma or semicolon.
1632 } else {
1633 Diag(Tok, diag::err_expected_fn_body);
1634 SkipUntil(tok::semi);
1635 return DeclGroupPtrTy();
1636 }
John McCalld5a36322009-11-03 19:26:08 +00001637 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001638 if (Tok.is(tok::l_brace)) {
1639 Diag(Tok, diag::err_function_definition_not_allowed);
1640 SkipUntil(tok::r_brace, true, true);
1641 }
John McCalld5a36322009-11-03 19:26:08 +00001642 }
1643 }
1644
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001645 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001646 return DeclGroupPtrTy();
1647
1648 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1649 // must parse and analyze the for-range-initializer before the declaration is
1650 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001651 //
1652 // Handle the Objective-C for-in loop variable similarly, although we
1653 // don't need to parse the container in advance.
1654 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1655 bool IsForRangeLoop = false;
1656 if (Tok.is(tok::colon)) {
1657 IsForRangeLoop = true;
1658 FRI->ColonLoc = ConsumeToken();
1659 if (Tok.is(tok::l_brace))
1660 FRI->RangeExpr = ParseBraceInitializer();
1661 else
1662 FRI->RangeExpr = ParseExpression();
1663 }
1664
Richard Smith02e85f32011-04-14 22:09:26 +00001665 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001666 if (IsForRangeLoop)
1667 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001668 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001669 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001670 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1671 }
1672
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001673 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001674 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001675 if (LateParsedAttrs.size() > 0)
1676 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001677 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001678 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001679 DeclsInGroup.push_back(FirstDecl);
1680
Richard Smith09f76ee2011-10-19 21:33:05 +00001681 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001682
John McCalld5a36322009-11-03 19:26:08 +00001683 // If we don't have a comma, it is either the end of the list (a ';') or an
1684 // error, bail out.
1685 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001686 SourceLocation CommaLoc = ConsumeToken();
1687
1688 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1689 // This comma was followed by a line-break and something which can't be
1690 // the start of a declarator. The comma was probably a typo for a
1691 // semicolon.
1692 Diag(CommaLoc, diag::err_expected_semi_declaration)
1693 << FixItHint::CreateReplacement(CommaLoc, ";");
1694 ExpectSemi = false;
1695 break;
1696 }
John McCalld5a36322009-11-03 19:26:08 +00001697
1698 // Parse the next declarator.
1699 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001700 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001701
1702 // Accept attributes in an init-declarator. In the first declarator in a
1703 // declaration, these would be part of the declspec. In subsequent
1704 // declarators, they become part of the declarator itself, so that they
1705 // don't apply to declarators after *this* one. Examples:
1706 // short __attribute__((common)) var; -> declspec
1707 // short var __attribute__((common)); -> declarator
1708 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001709 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001710
1711 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001712 if (!D.isInvalidType()) {
1713 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1714 D.complete(ThisDecl);
1715 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001716 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001717 }
John McCalld5a36322009-11-03 19:26:08 +00001718 }
1719
1720 if (DeclEnd)
1721 *DeclEnd = Tok.getLocation();
1722
Richard Smith09f76ee2011-10-19 21:33:05 +00001723 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001724 ExpectAndConsumeSemi(Context == Declarator::FileContext
1725 ? diag::err_invalid_token_after_toplevel_declarator
1726 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001727 // Okay, there was no semicolon and one was expected. If we see a
1728 // declaration specifier, just assume it was missing and continue parsing.
1729 // Otherwise things are very confused and we skip to recover.
1730 if (!isDeclarationSpecifier()) {
1731 SkipUntil(tok::r_brace, true, true);
1732 if (Tok.is(tok::semi))
1733 ConsumeToken();
1734 }
John McCalld5a36322009-11-03 19:26:08 +00001735 }
1736
Douglas Gregor0be31a22010-07-02 17:43:08 +00001737 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001738 DeclsInGroup.data(),
1739 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001740}
1741
Richard Smith02e85f32011-04-14 22:09:26 +00001742/// Parse an optional simple-asm-expr and attributes, and attach them to a
1743/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001744bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001745 // If a simple-asm-expr is present, parse it.
1746 if (Tok.is(tok::kw_asm)) {
1747 SourceLocation Loc;
1748 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1749 if (AsmLabel.isInvalid()) {
1750 SkipUntil(tok::semi, true, true);
1751 return true;
1752 }
1753
1754 D.setAsmLabel(AsmLabel.release());
1755 D.SetRangeEnd(Loc);
1756 }
1757
1758 MaybeParseGNUAttributes(D);
1759 return false;
1760}
1761
Douglas Gregor23996282009-05-12 21:31:51 +00001762/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1763/// declarator'. This method parses the remainder of the declaration
1764/// (including any attributes or initializer, among other things) and
1765/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001766///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001767/// init-declarator: [C99 6.7]
1768/// declarator
1769/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001770/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1771/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001772/// [C++] declarator initializer[opt]
1773///
1774/// [C++] initializer:
1775/// [C++] '=' initializer-clause
1776/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001777/// [C++0x] '=' 'default' [TODO]
1778/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001779/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001780///
1781/// According to the standard grammar, =default and =delete are function
1782/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001783///
John McCall48871652010-08-21 09:40:31 +00001784Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001785 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001786 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001787 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001788
Richard Smith02e85f32011-04-14 22:09:26 +00001789 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1790}
Mike Stump11289f42009-09-09 15:08:12 +00001791
Richard Smith02e85f32011-04-14 22:09:26 +00001792Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1793 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001794 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001795 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001796 switch (TemplateInfo.Kind) {
1797 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001798 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001799 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001800
Douglas Gregor450f00842009-09-25 18:43:00 +00001801 case ParsedTemplateInfo::Template:
1802 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001803 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001804 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001805 D);
1806 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001807
Douglas Gregor450f00842009-09-25 18:43:00 +00001808 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001809 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001810 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001811 TemplateInfo.ExternLoc,
1812 TemplateInfo.TemplateLoc,
1813 D);
1814 if (ThisRes.isInvalid()) {
1815 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001816 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001817 }
Chad Rosierc1183952012-06-26 22:30:43 +00001818
Douglas Gregor450f00842009-09-25 18:43:00 +00001819 ThisDecl = ThisRes.get();
1820 break;
1821 }
1822 }
Mike Stump11289f42009-09-09 15:08:12 +00001823
Richard Smith74aeef52013-04-26 16:15:35 +00001824 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001825
Douglas Gregor23996282009-05-12 21:31:51 +00001826 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001827 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001828 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001829 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001830 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001831 if (D.isFunctionDeclarator())
1832 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1833 << 1 /* delete */;
1834 else
1835 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001836 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001837 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001838 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1839 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001840 else
1841 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001842 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001843 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001844 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001845 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001846 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001847
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001848 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001849 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001850 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001851 cutOffParsing();
1852 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001853 }
Chad Rosierc1183952012-06-26 22:30:43 +00001854
John McCalldadc5752010-08-24 06:29:42 +00001855 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001856
David Blaikiebbafb8a2012-03-11 07:00:24 +00001857 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001858 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001859 ExitScope();
1860 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001861
Douglas Gregor23996282009-05-12 21:31:51 +00001862 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001863 SkipUntil(tok::comma, true, true);
1864 Actions.ActOnInitializerError(ThisDecl);
1865 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001866 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1867 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001868 }
1869 } else if (Tok.is(tok::l_paren)) {
1870 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001871 BalancedDelimiterTracker T(*this, tok::l_paren);
1872 T.consumeOpen();
1873
Benjamin Kramerf0623432012-08-23 22:51:59 +00001874 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001875 CommaLocsTy CommaLocs;
1876
David Blaikiebbafb8a2012-03-11 07:00:24 +00001877 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001878 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001879 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001880 }
1881
Douglas Gregor23996282009-05-12 21:31:51 +00001882 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001883 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001884 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001885
David Blaikiebbafb8a2012-03-11 07:00:24 +00001886 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001887 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001888 ExitScope();
1889 }
Douglas Gregor23996282009-05-12 21:31:51 +00001890 } else {
1891 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001892 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001893
1894 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1895 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001896
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001899 ExitScope();
1900 }
1901
Sebastian Redla9351792012-02-11 23:51:47 +00001902 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1903 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001904 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001905 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1906 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001907 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001908 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001909 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001910 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001911 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1912
Sebastian Redl3da34892011-06-05 12:23:16 +00001913 if (D.getCXXScopeSpec().isSet()) {
1914 EnterScope(0);
1915 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1916 }
1917
1918 ExprResult Init(ParseBraceInitializer());
1919
1920 if (D.getCXXScopeSpec().isSet()) {
1921 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1922 ExitScope();
1923 }
1924
1925 if (Init.isInvalid()) {
1926 Actions.ActOnInitializerError(ThisDecl);
1927 } else
1928 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1929 /*DirectInit=*/true, TypeContainsAuto);
1930
Douglas Gregor23996282009-05-12 21:31:51 +00001931 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001932 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001933 }
1934
Richard Smithb2bc2e62011-02-21 20:05:19 +00001935 Actions.FinalizeDeclaration(ThisDecl);
1936
Douglas Gregor23996282009-05-12 21:31:51 +00001937 return ThisDecl;
1938}
1939
Chris Lattner1890ac82006-08-13 01:16:23 +00001940/// ParseSpecifierQualifierList
1941/// specifier-qualifier-list:
1942/// type-specifier specifier-qualifier-list[opt]
1943/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001944/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001945///
Richard Smithc5b05522012-03-12 07:56:15 +00001946void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1947 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001948 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1949 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001950 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001951 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001952
Chris Lattner1890ac82006-08-13 01:16:23 +00001953 // Validate declspec for type-name.
1954 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001955 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1956 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001957 Diag(Tok, diag::err_expected_type);
1958 DS.SetTypeSpecError();
1959 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1960 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001961 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001962 if (!DS.hasTypeSpecifier())
1963 DS.SetTypeSpecError();
1964 }
Mike Stump11289f42009-09-09 15:08:12 +00001965
Chris Lattner1b22eed2006-11-28 05:12:07 +00001966 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001967 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001968 if (DS.getStorageClassSpecLoc().isValid())
1969 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1970 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001971 Diag(DS.getThreadStorageClassSpecLoc(),
1972 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001973 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001974 }
Mike Stump11289f42009-09-09 15:08:12 +00001975
Chris Lattner1b22eed2006-11-28 05:12:07 +00001976 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001977 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001978 if (DS.isInlineSpecified())
1979 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1980 if (DS.isVirtualSpecified())
1981 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1982 if (DS.isExplicitSpecified())
1983 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001984 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001985 }
Richard Smithc5b05522012-03-12 07:56:15 +00001986
1987 // Issue diagnostic and remove constexpr specfier if present.
1988 if (DS.isConstexprSpecified()) {
1989 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1990 DS.ClearConstexprSpec();
1991 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001992}
Chris Lattner53361ac2006-08-10 05:19:57 +00001993
Chris Lattner6cc055a2009-04-12 20:42:31 +00001994/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1995/// specified token is valid after the identifier in a declarator which
1996/// immediately follows the declspec. For example, these things are valid:
1997///
1998/// int x [ 4]; // direct-declarator
1999/// int x ( int y); // direct-declarator
2000/// int(int x ) // direct-declarator
2001/// int x ; // simple-declaration
2002/// int x = 17; // init-declarator-list
2003/// int x , y; // init-declarator-list
2004/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002005/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002006/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002007///
2008/// This is not, because 'x' does not immediately follow the declspec (though
2009/// ')' happens to be valid anyway).
2010/// int (x)
2011///
2012static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2013 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2014 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002015 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002016}
2017
Chris Lattner20a0c612009-04-14 21:34:55 +00002018
2019/// ParseImplicitInt - This method is called when we have an non-typename
2020/// identifier in a declspec (which normally terminates the decl spec) when
2021/// the declspec has no type specifier. In this case, the declspec is either
2022/// malformed or is "implicit int" (in K&R and C89).
2023///
2024/// This method handles diagnosing this prettily and returns false if the
2025/// declspec is done being processed. If it recovers and thinks there may be
2026/// other pieces of declspec after it, it returns true.
2027///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002028bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002029 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002030 AccessSpecifier AS, DeclSpecContext DSC,
2031 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002032 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002033
Chris Lattner20a0c612009-04-14 21:34:55 +00002034 SourceLocation Loc = Tok.getLocation();
2035 // If we see an identifier that is not a type name, we normally would
2036 // parse it as the identifer being declared. However, when a typename
2037 // is typo'd or the definition is not included, this will incorrectly
2038 // parse the typename as the identifier name and fall over misparsing
2039 // later parts of the diagnostic.
2040 //
2041 // As such, we try to do some look-ahead in cases where this would
2042 // otherwise be an "implicit-int" case to see if this is invalid. For
2043 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2044 // an identifier with implicit int, we'd get a parse error because the
2045 // next token is obviously invalid for a type. Parse these as a case
2046 // with an invalid type specifier.
2047 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002048
Chris Lattner20a0c612009-04-14 21:34:55 +00002049 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002050 // error, do lookahead to try to do better recovery. This never applies
2051 // within a type specifier. Outside of C++, we allow this even if the
2052 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002053 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002054 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002055 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002056 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002057 // If this token is valid for implicit int, e.g. "static x = 4", then
2058 // we just avoid eating the identifier, so it will be parsed as the
2059 // identifier in the declarator.
2060 return false;
2061 }
Mike Stump11289f42009-09-09 15:08:12 +00002062
Richard Smitha952ebb2012-05-15 21:01:51 +00002063 if (getLangOpts().CPlusPlus &&
2064 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2065 // Don't require a type specifier if we have the 'auto' storage class
2066 // specifier in C++98 -- we'll promote it to a type specifier.
2067 return false;
2068 }
2069
Chris Lattner20a0c612009-04-14 21:34:55 +00002070 // Otherwise, if we don't consume this token, we are going to emit an
2071 // error anyway. Try to recover from various common problems. Check
2072 // to see if this was a reference to a tag name without a tag specified.
2073 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002074 //
2075 // C++ doesn't need this, and isTagName doesn't take SS.
2076 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002077 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002078 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002079
Douglas Gregor0be31a22010-07-02 17:43:08 +00002080 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002081 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002082 case DeclSpec::TST_enum:
2083 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2084 case DeclSpec::TST_union:
2085 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2086 case DeclSpec::TST_struct:
2087 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002088 case DeclSpec::TST_interface:
2089 TagName="__interface"; FixitTagName = "__interface ";
2090 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002091 case DeclSpec::TST_class:
2092 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002093 }
Mike Stump11289f42009-09-09 15:08:12 +00002094
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002095 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002096 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2097 LookupResult R(Actions, TokenName, SourceLocation(),
2098 Sema::LookupOrdinaryName);
2099
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002100 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002101 << TokenName << TagName << getLangOpts().CPlusPlus
2102 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2103
2104 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2105 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2106 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002107 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002108 << TokenName << TagName;
2109 }
Mike Stump11289f42009-09-09 15:08:12 +00002110
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002111 // Parse this as a tag as if the missing tag were present.
2112 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002113 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002114 else
Richard Smithc5b05522012-03-12 07:56:15 +00002115 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002116 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002117 return true;
2118 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002119 }
Mike Stump11289f42009-09-09 15:08:12 +00002120
Richard Smithfe904f02012-05-15 21:29:55 +00002121 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002122 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002123 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2124 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002125 // Look ahead to the next token to try to figure out what this declaration
2126 // was supposed to be.
2127 switch (NextToken().getKind()) {
2128 case tok::comma:
2129 case tok::equal:
2130 case tok::kw_asm:
2131 case tok::l_brace:
2132 case tok::l_square:
2133 case tok::semi:
2134 // This looks like a variable declaration. The type is probably missing.
2135 // We're done parsing decl-specifiers.
2136 return false;
2137
2138 case tok::l_paren: {
2139 // static x(4); // 'x' is not a type
2140 // x(int n); // 'x' is not a type
2141 // x (*p)[]; // 'x' is a type
2142 //
2143 // Since we're in an error case (or the rare 'implicit int in C++' MS
2144 // extension), we can afford to perform a tentative parse to determine
2145 // which case we're in.
2146 TentativeParsingAction PA(*this);
2147 ConsumeToken();
2148 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2149 PA.Revert();
2150 if (TPR == TPResult::False())
2151 return false;
2152 // The identifier is followed by a parenthesized declarator.
2153 // It's supposed to be a type.
2154 break;
2155 }
2156
2157 default:
2158 // This is probably supposed to be a type. This includes cases like:
2159 // int f(itn);
2160 // struct S { unsinged : 4; };
2161 break;
2162 }
2163 }
2164
Chad Rosierc1183952012-06-26 22:30:43 +00002165 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002166 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002167 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002168 IdentifierInfo *II = Tok.getIdentifierInfo();
2169 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002170 // The action emitted a diagnostic, so we don't have to.
2171 if (T) {
2172 // The action has suggested that the type T could be used. Set that as
2173 // the type in the declaration specifiers, consume the would-be type
2174 // name token, and we're done.
2175 const char *PrevSpec;
2176 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002177 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002178 DS.SetRangeEnd(Tok.getLocation());
2179 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002180 // There may be other declaration specifiers after this.
2181 return true;
2182 } else if (II != Tok.getIdentifierInfo()) {
2183 // If no type was suggested, the correction is to a keyword
2184 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002185 // There may be other declaration specifiers after this.
2186 return true;
2187 }
Chad Rosierc1183952012-06-26 22:30:43 +00002188
Douglas Gregor15e56022009-10-13 23:27:22 +00002189 // Fall through; the action had no suggestion for us.
2190 } else {
2191 // The action did not emit a diagnostic, so emit one now.
2192 SourceRange R;
2193 if (SS) R = SS->getRange();
2194 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2195 }
Mike Stump11289f42009-09-09 15:08:12 +00002196
Douglas Gregor15e56022009-10-13 23:27:22 +00002197 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002198 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002199 DS.SetRangeEnd(Tok.getLocation());
2200 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002201
Chris Lattner20a0c612009-04-14 21:34:55 +00002202 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2203 // avoid rippling error messages on subsequent uses of the same type,
2204 // could be useful if #include was forgotten.
2205 return false;
2206}
2207
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002208/// \brief Determine the declaration specifier context from the declarator
2209/// context.
2210///
2211/// \param Context the declarator context, which is one of the
2212/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002213Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002214Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2215 if (Context == Declarator::MemberContext)
2216 return DSC_class;
2217 if (Context == Declarator::FileContext)
2218 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002219 if (Context == Declarator::TrailingReturnContext)
2220 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002221 return DSC_normal;
2222}
2223
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002224/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2225///
2226/// FIXME: Simply returns an alignof() expression if the argument is a
2227/// type. Ideally, the type should be propagated directly into Sema.
2228///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002229/// [C11] type-id
2230/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002231/// [C++0x] type-id ...[opt]
2232/// [C++0x] assignment-expression ...[opt]
2233ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2234 SourceLocation &EllipsisLoc) {
2235 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002236 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002237 SourceLocation TypeLoc = Tok.getLocation();
2238 ParsedType Ty = ParseTypeName().get();
2239 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002240 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2241 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002242 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002243 ER = ParseConstantExpression();
2244
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002245 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002246 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002247
2248 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002249}
2250
2251/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2252/// attribute to Attrs.
2253///
2254/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002255/// [C11] '_Alignas' '(' type-id ')'
2256/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002257/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2258/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002259void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002260 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002261 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2262 "Not an alignment-specifier!");
2263
Richard Smithd11c7a12013-01-29 01:48:07 +00002264 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2265 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002266
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002267 BalancedDelimiterTracker T(*this, tok::l_paren);
2268 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002269 return;
2270
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002271 SourceLocation EllipsisLoc;
2272 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002273 if (ArgExpr.isInvalid()) {
2274 SkipUntil(tok::r_paren);
2275 return;
2276 }
2277
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002278 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002279 if (EndLoc)
2280 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002281
Benjamin Kramerf0623432012-08-23 22:51:59 +00002282 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002283 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002284 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smith44c247f2013-02-22 08:32:16 +00002285 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002286}
2287
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002288/// ParseDeclarationSpecifiers
2289/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002290/// storage-class-specifier declaration-specifiers[opt]
2291/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002292/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002293/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002294/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002295/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002296///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002297/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002298/// 'typedef'
2299/// 'extern'
2300/// 'static'
2301/// 'auto'
2302/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002303/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002304/// [C++11] 'thread_local'
2305/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002306/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002307/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002308/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002309/// [C++] 'virtual'
2310/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002311/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002312/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002313/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002314
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002315///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002316void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002317 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002318 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002319 DeclSpecContext DSContext,
2320 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002321 if (DS.getSourceRange().isInvalid()) {
2322 DS.SetRangeStart(Tok.getLocation());
2323 DS.SetRangeEnd(Tok.getLocation());
2324 }
Chad Rosierc1183952012-06-26 22:30:43 +00002325
Douglas Gregordf593fb2011-11-07 17:33:42 +00002326 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002327 bool AttrsLastTime = false;
2328 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002329 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002330 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002331 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002332 unsigned DiagID = 0;
2333
Chris Lattner4d8f8732006-11-28 05:05:08 +00002334 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002335
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002336 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002337 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002338 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002339 if (!AttrsLastTime)
2340 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002341 else {
2342 // Reject C++11 attributes that appertain to decl specifiers as
2343 // we don't support any C++11 attributes that appertain to decl
2344 // specifiers. This also conforms to what g++ 4.8 is doing.
2345 ProhibitCXX11Attributes(attrs);
2346
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002347 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002348 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002349
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002350 // If this is not a declaration specifier token, we're done reading decl
2351 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002352 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002353 return;
Mike Stump11289f42009-09-09 15:08:12 +00002354
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002355 case tok::l_square:
2356 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002357 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002358 goto DoneWithDeclSpec;
2359
2360 ProhibitAttributes(attrs);
2361 // FIXME: It would be good to recover by accepting the attributes,
2362 // but attempting to do that now would cause serious
2363 // madness in terms of diagnostics.
2364 attrs.clear();
2365 attrs.Range = SourceRange();
2366
2367 ParseCXX11Attributes(attrs);
2368 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002369 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002370
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002371 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002372 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002373 if (DS.hasTypeSpecifier()) {
2374 bool AllowNonIdentifiers
2375 = (getCurScope()->getFlags() & (Scope::ControlScope |
2376 Scope::BlockScope |
2377 Scope::TemplateParamScope |
2378 Scope::FunctionPrototypeScope |
2379 Scope::AtCatchScope)) == 0;
2380 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002381 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002382 (DSContext == DSC_class && DS.isFriendSpecified());
2383
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002384 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002385 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002386 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002387 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002388 }
2389
Douglas Gregor80039242011-02-15 20:33:25 +00002390 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2391 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2392 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002393 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002394 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002395 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002396 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002397 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002398 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002399
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002400 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002401 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002402 }
2403
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002404 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002405 // C++ scope specifier. Annotate and loop, or bail out on error.
2406 if (TryAnnotateCXXScopeToken(true)) {
2407 if (!DS.hasTypeSpecifier())
2408 DS.SetTypeSpecError();
2409 goto DoneWithDeclSpec;
2410 }
John McCall8bc2a702010-03-01 18:20:46 +00002411 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2412 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002413 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002414
2415 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002416 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002417 goto DoneWithDeclSpec;
2418
John McCall9dab4e62009-12-12 11:40:51 +00002419 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002420 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2421 Tok.getAnnotationRange(),
2422 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002423
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002424 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002425 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002426 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002427 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002428 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002429 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002430
2431 // C++ [class.qual]p2:
2432 // In a lookup in which the constructor is an acceptable lookup
2433 // result and the nested-name-specifier nominates a class C:
2434 //
2435 // - if the name specified after the
2436 // nested-name-specifier, when looked up in C, is the
2437 // injected-class-name of C (Clause 9), or
2438 //
2439 // - if the name specified after the nested-name-specifier
2440 // is the same as the identifier or the
2441 // simple-template-id's template-name in the last
2442 // component of the nested-name-specifier,
2443 //
2444 // the name is instead considered to name the constructor of
2445 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002446 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002447 // Thus, if the template-name is actually the constructor
2448 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002449 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002450 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002451 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002452 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002453 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002454 if (isConstructorDeclarator()) {
2455 // The user meant this to be an out-of-line constructor
2456 // definition, but template arguments are not allowed
2457 // there. Just allow this as a constructor; we'll
2458 // complain about it later.
2459 goto DoneWithDeclSpec;
2460 }
2461
2462 // The user meant this to name a type, but it actually names
2463 // a constructor with some extraneous template
2464 // arguments. Complain, then parse it as a type as the user
2465 // intended.
2466 Diag(TemplateId->TemplateNameLoc,
2467 diag::err_out_of_line_template_id_names_constructor)
2468 << TemplateId->Name;
2469 }
2470
John McCall9dab4e62009-12-12 11:40:51 +00002471 DS.getTypeSpecScope() = SS;
2472 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002473 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002474 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002475 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002476 continue;
2477 }
2478
Douglas Gregorc5790df2009-09-28 07:26:33 +00002479 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002480 DS.getTypeSpecScope() = SS;
2481 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002482 if (Tok.getAnnotationValue()) {
2483 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002485 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002486 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002487 if (isInvalid)
2488 break;
John McCallba7bf592010-08-24 05:47:05 +00002489 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002490 else
2491 DS.SetTypeSpecError();
2492 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2493 ConsumeToken(); // The typename
2494 }
2495
Douglas Gregor167fa622009-03-25 15:40:00 +00002496 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002497 goto DoneWithDeclSpec;
2498
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002499 // If we're in a context where the identifier could be a class name,
2500 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002501 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002502 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002503 &SS)) {
2504 if (isConstructorDeclarator())
2505 goto DoneWithDeclSpec;
2506
2507 // As noted in C++ [class.qual]p2 (cited above), when the name
2508 // of the class is qualified in a context where it could name
2509 // a constructor, its a constructor name. However, we've
2510 // looked at the declarator, and the user probably meant this
2511 // to be a type. Complain that it isn't supposed to be treated
2512 // as a type, then proceed to parse it as a type.
2513 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2514 << Next.getIdentifierInfo();
2515 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002516
John McCallba7bf592010-08-24 05:47:05 +00002517 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2518 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002519 getCurScope(), &SS,
2520 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002521 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002522 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002523
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002524 // If the referenced identifier is not a type, then this declspec is
2525 // erroneous: We already checked about that it has no type specifier, and
2526 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002527 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002528 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002529 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002530 ParsedAttributesWithRange Attrs(AttrFactory);
2531 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2532 if (!Attrs.empty()) {
2533 AttrsLastTime = true;
2534 attrs.takeAllFrom(Attrs);
2535 }
2536 continue;
2537 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002538 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002539 }
Mike Stump11289f42009-09-09 15:08:12 +00002540
John McCall9dab4e62009-12-12 11:40:51 +00002541 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002542 ConsumeToken(); // The C++ scope.
2543
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002545 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002546 if (isInvalid)
2547 break;
Mike Stump11289f42009-09-09 15:08:12 +00002548
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002549 DS.SetRangeEnd(Tok.getLocation());
2550 ConsumeToken(); // The typename.
2551
2552 continue;
2553 }
Mike Stump11289f42009-09-09 15:08:12 +00002554
Chris Lattnere387d9e2009-01-21 19:48:37 +00002555 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002556 if (Tok.getAnnotationValue()) {
2557 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002559 DiagID, T);
2560 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002561 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002562
Chris Lattner005fc1b2010-04-05 18:18:31 +00002563 if (isInvalid)
2564 break;
2565
Chris Lattnere387d9e2009-01-21 19:48:37 +00002566 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2567 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattnere387d9e2009-01-21 19:48:37 +00002569 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2570 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002571 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002572 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002573 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002574
Chris Lattnere387d9e2009-01-21 19:48:37 +00002575 continue;
2576 }
Mike Stump11289f42009-09-09 15:08:12 +00002577
Douglas Gregor06873092011-04-28 15:48:45 +00002578 case tok::kw___is_signed:
2579 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2580 // typically treats it as a trait. If we see __is_signed as it appears
2581 // in libstdc++, e.g.,
2582 //
2583 // static const bool __is_signed;
2584 //
2585 // then treat __is_signed as an identifier rather than as a keyword.
2586 if (DS.getTypeSpecType() == TST_bool &&
2587 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2588 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2589 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2590 Tok.setKind(tok::identifier);
2591 }
2592
2593 // We're done with the declaration-specifiers.
2594 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002595
Chris Lattner16fac4f2008-07-26 01:18:38 +00002596 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002597 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002598 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002599 // In C++, check to see if this is a scope specifier like foo::bar::, if
2600 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002601 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002602 if (TryAnnotateCXXScopeToken(true)) {
2603 if (!DS.hasTypeSpecifier())
2604 DS.SetTypeSpecError();
2605 goto DoneWithDeclSpec;
2606 }
2607 if (!Tok.is(tok::identifier))
2608 continue;
2609 }
Mike Stump11289f42009-09-09 15:08:12 +00002610
Chris Lattner16fac4f2008-07-26 01:18:38 +00002611 // This identifier can only be a typedef name if we haven't already seen
2612 // a type-specifier. Without this check we misparse:
2613 // typedef int X; struct Y { short X; }; as 'short int'.
2614 if (DS.hasTypeSpecifier())
2615 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002616
John Thompson22334602010-02-05 00:12:22 +00002617 // Check for need to substitute AltiVec keyword tokens.
2618 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2619 break;
2620
Richard Smith3092a3b2012-05-09 18:56:43 +00002621 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2622 // allow the use of a typedef name as a type specifier.
2623 if (DS.isTypeAltiVecVector())
2624 goto DoneWithDeclSpec;
2625
John McCallba7bf592010-08-24 05:47:05 +00002626 ParsedType TypeRep =
2627 Actions.getTypeName(*Tok.getIdentifierInfo(),
2628 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002629
Chris Lattner6cc055a2009-04-12 20:42:31 +00002630 // If this is not a typedef name, don't parse it as part of the declspec,
2631 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002632 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002633 ParsedAttributesWithRange Attrs(AttrFactory);
2634 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2635 if (!Attrs.empty()) {
2636 AttrsLastTime = true;
2637 attrs.takeAllFrom(Attrs);
2638 }
2639 continue;
2640 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002641 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002642 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002643
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002644 // If we're in a context where the identifier could be a class name,
2645 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002646 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002647 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002648 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002649 goto DoneWithDeclSpec;
2650
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002651 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002652 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002653 if (isInvalid)
2654 break;
Mike Stump11289f42009-09-09 15:08:12 +00002655
Chris Lattner16fac4f2008-07-26 01:18:38 +00002656 DS.SetRangeEnd(Tok.getLocation());
2657 ConsumeToken(); // The identifier
2658
2659 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2660 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002661 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002662 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002663 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002664
Steve Naroffcd5e7822008-09-22 10:28:57 +00002665 // Need to support trailing type qualifiers (e.g. "id<p> const").
2666 // If a type specifier follows, it will be diagnosed elsewhere.
2667 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002668 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002669
2670 // type-name
2671 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002672 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002673 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002674 // This template-id does not refer to a type name, so we're
2675 // done with the type-specifiers.
2676 goto DoneWithDeclSpec;
2677 }
2678
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002679 // If we're in a context where the template-id could be a
2680 // constructor name or specialization, check whether this is a
2681 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002682 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002683 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002684 isConstructorDeclarator())
2685 goto DoneWithDeclSpec;
2686
Douglas Gregor7f741122009-02-25 19:37:18 +00002687 // Turn the template-id annotation token into a type annotation
2688 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002689 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002690 continue;
2691 }
2692
Chris Lattnere37e2332006-08-15 04:50:22 +00002693 // GNU attributes support.
2694 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002695 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002696 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002697
2698 // Microsoft declspec support.
2699 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002700 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002701 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002702
Steve Naroff44ac7772008-12-25 14:16:32 +00002703 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002704 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002705 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002706 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002707 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002708 // FIXME: This does not work correctly if it is set to be a declspec
2709 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002710 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002711 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002712 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002713 }
Eli Friedman53339e02009-06-08 23:27:34 +00002714
Aaron Ballman317a77f2013-05-22 23:25:32 +00002715 case tok::kw___sptr:
2716 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002717 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002718 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002719 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002720 case tok::kw___cdecl:
2721 case tok::kw___stdcall:
2722 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002723 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002724 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002725 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002726 continue;
2727
Dawn Perchik335e16b2010-09-03 01:29:35 +00002728 // Borland single token adornments.
2729 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002730 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002731 continue;
2732
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002733 // OpenCL single token adornments.
2734 case tok::kw___kernel:
2735 ParseOpenCLAttributes(DS.getAttributes());
2736 continue;
2737
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002738 // storage-class-specifier
2739 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002740 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2741 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002742 break;
2743 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002744 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002745 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002746 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2747 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002748 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002749 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002750 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2751 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002752 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002753 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002754 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002755 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002756 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2757 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002758 break;
2759 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002760 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002761 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002762 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2763 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002764 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002765 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002766 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002767 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2769 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002770 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002771 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2772 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002773 break;
2774 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002775 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2776 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002777 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002778 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002779 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2780 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002781 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002782 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002783 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2784 PrevSpec, DiagID);
2785 break;
2786 case tok::kw_thread_local:
2787 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2788 PrevSpec, DiagID);
2789 break;
2790 case tok::kw__Thread_local:
2791 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2792 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002793 break;
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002795 // function-specifier
2796 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002797 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002798 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002799 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002800 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002801 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002802 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002803 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002804 break;
Richard Smith0015f092013-01-17 22:16:11 +00002805 case tok::kw__Noreturn:
2806 if (!getLangOpts().C11)
2807 Diag(Loc, diag::ext_c11_noreturn);
2808 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2809 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002810
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002811 // alignment-specifier
2812 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002813 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002814 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002815 ParseAlignmentSpecifier(DS.getAttributes());
2816 continue;
2817
Anders Carlssoncd8db412009-05-06 04:46:28 +00002818 // friend
2819 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002820 if (DSContext == DSC_class)
2821 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2822 else {
2823 PrevSpec = ""; // not actually used by the diagnostic
2824 DiagID = diag::err_friend_invalid_in_context;
2825 isInvalid = true;
2826 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002827 break;
Mike Stump11289f42009-09-09 15:08:12 +00002828
Douglas Gregor26701a42011-09-09 02:06:17 +00002829 // Modules
2830 case tok::kw___module_private__:
2831 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2832 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002833
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002834 // constexpr
2835 case tok::kw_constexpr:
2836 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2837 break;
2838
Chris Lattnere387d9e2009-01-21 19:48:37 +00002839 // type-specifier
2840 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002841 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2842 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002843 break;
2844 case tok::kw_long:
2845 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002846 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2847 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002848 else
John McCall49bfce42009-08-03 20:12:06 +00002849 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2850 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002851 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002852 case tok::kw___int64:
2853 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2854 DiagID);
2855 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002856 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002857 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2858 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002859 break;
2860 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002861 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2862 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002863 break;
2864 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002865 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2866 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002867 break;
2868 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002869 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2870 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002871 break;
2872 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2874 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002875 break;
2876 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2878 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002879 break;
2880 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2882 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002883 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002884 case tok::kw___int128:
2885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2886 DiagID);
2887 break;
2888 case tok::kw_half:
2889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2890 DiagID);
2891 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002892 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2894 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002895 break;
2896 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2898 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002899 break;
2900 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002901 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2902 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002903 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002904 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2906 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002907 break;
2908 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2910 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002911 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002912 case tok::kw_bool:
2913 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002914 if (Tok.is(tok::kw_bool) &&
2915 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2916 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2917 PrevSpec = ""; // Not used by the diagnostic.
2918 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002919 // For better error recovery.
2920 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002921 isInvalid = true;
2922 } else {
2923 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2924 DiagID);
2925 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002926 break;
2927 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2929 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002930 break;
2931 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2933 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002934 break;
2935 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2937 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002938 break;
John Thompson22334602010-02-05 00:12:22 +00002939 case tok::kw___vector:
2940 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2941 break;
2942 case tok::kw___pixel:
2943 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2944 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002945 case tok::kw_image1d_t:
2946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2947 PrevSpec, DiagID);
2948 break;
2949 case tok::kw_image1d_array_t:
2950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2951 PrevSpec, DiagID);
2952 break;
2953 case tok::kw_image1d_buffer_t:
2954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2955 PrevSpec, DiagID);
2956 break;
2957 case tok::kw_image2d_t:
2958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2959 PrevSpec, DiagID);
2960 break;
2961 case tok::kw_image2d_array_t:
2962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2963 PrevSpec, DiagID);
2964 break;
2965 case tok::kw_image3d_t:
2966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2967 PrevSpec, DiagID);
2968 break;
Guy Benyei61054192013-02-07 10:55:47 +00002969 case tok::kw_sampler_t:
2970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2971 PrevSpec, DiagID);
2972 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002973 case tok::kw_event_t:
2974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2975 PrevSpec, DiagID);
2976 break;
John McCall39439732011-04-09 22:50:59 +00002977 case tok::kw___unknown_anytype:
2978 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2979 PrevSpec, DiagID);
2980 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002981
2982 // class-specifier:
2983 case tok::kw_class:
2984 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002985 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002986 case tok::kw_union: {
2987 tok::TokenKind Kind = Tok.getKind();
2988 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002989
2990 // These are attributes following class specifiers.
2991 // To produce better diagnostic, we parse them when
2992 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002993 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002994 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002995 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002996
2997 // If there are attributes following class specifier,
2998 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002999 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003000 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003001 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003002 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003003 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003004 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003005
3006 // enum-specifier:
3007 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003008 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003009 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003010 continue;
3011
3012 // cv-qualifier:
3013 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003015 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003016 break;
3017 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003019 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003020 break;
3021 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003022 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003023 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003024 break;
3025
Douglas Gregor333489b2009-03-27 23:10:48 +00003026 // C++ typename-specifier:
3027 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003028 if (TryAnnotateTypeOrScopeToken()) {
3029 DS.SetTypeSpecError();
3030 goto DoneWithDeclSpec;
3031 }
3032 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003033 continue;
3034 break;
3035
Chris Lattnere387d9e2009-01-21 19:48:37 +00003036 // GNU typeof support.
3037 case tok::kw_typeof:
3038 ParseTypeofSpecifier(DS);
3039 continue;
3040
David Blaikie15a430a2011-12-04 05:04:18 +00003041 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003042 ParseDecltypeSpecifier(DS);
3043 continue;
3044
Alexis Hunt4a257072011-05-19 05:37:45 +00003045 case tok::kw___underlying_type:
3046 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003047 continue;
3048
3049 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003050 // C11 6.7.2.4/4:
3051 // If the _Atomic keyword is immediately followed by a left parenthesis,
3052 // it is interpreted as a type specifier (with a type name), not as a
3053 // type qualifier.
3054 if (NextToken().is(tok::l_paren)) {
3055 ParseAtomicSpecifier(DS);
3056 continue;
3057 }
3058 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3059 getLangOpts());
3060 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003061
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003062 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003063 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003064 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003065 goto DoneWithDeclSpec;
3066 case tok::kw___private:
3067 case tok::kw___global:
3068 case tok::kw___local:
3069 case tok::kw___constant:
3070 case tok::kw___read_only:
3071 case tok::kw___write_only:
3072 case tok::kw___read_write:
3073 ParseOpenCLQualifiers(DS);
3074 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003075
Steve Naroffcfdf6162008-06-05 00:02:44 +00003076 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003077 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003078 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3079 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003080 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003081 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003082
Douglas Gregor3a001f42010-11-19 17:10:50 +00003083 if (!ParseObjCProtocolQualifiers(DS))
3084 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3085 << FixItHint::CreateInsertion(Loc, "id")
3086 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003087
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003088 // Need to support trailing type qualifiers (e.g. "id<p> const").
3089 // If a type specifier follows, it will be diagnosed elsewhere.
3090 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003091 }
John McCall49bfce42009-08-03 20:12:06 +00003092 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003093 if (isInvalid) {
3094 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003095 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003096
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003097 if (DiagID == diag::ext_duplicate_declspec)
3098 Diag(Tok, DiagID)
3099 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3100 else
3101 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003102 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003103
Chris Lattner2e232092008-03-13 06:29:04 +00003104 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003105 if (DiagID != diag::err_bool_redeclaration)
3106 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003107
3108 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003109 }
3110}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003111
Chris Lattner70ae4912007-10-29 04:42:53 +00003112/// ParseStructDeclaration - Parse a struct declaration without the terminating
3113/// semicolon.
3114///
Chris Lattner90a26b02007-01-23 04:38:16 +00003115/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003116/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003117/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003118/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003119/// struct-declarator-list:
3120/// struct-declarator
3121/// struct-declarator-list ',' struct-declarator
3122/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3123/// struct-declarator:
3124/// declarator
3125/// [GNU] declarator attributes[opt]
3126/// declarator[opt] ':' constant-expression
3127/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3128///
Chris Lattnera12405b2008-04-10 06:46:29 +00003129void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003130ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003131
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003132 if (Tok.is(tok::kw___extension__)) {
3133 // __extension__ silences extension warnings in the subexpression.
3134 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003135 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003136 return ParseStructDeclaration(DS, Fields);
3137 }
Mike Stump11289f42009-09-09 15:08:12 +00003138
Steve Naroff97170802007-08-20 22:28:22 +00003139 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003140 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003141
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003142 // If there are no declarators, this is a free-standing declaration
3143 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003144 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003145 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3146 DS);
3147 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003148 return;
3149 }
3150
3151 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003152 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003153 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003154 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003155 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003156 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003157
Bill Wendling44426052012-12-20 19:22:21 +00003158 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003159 if (!FirstDeclarator)
3160 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003161
Steve Naroff97170802007-08-20 22:28:22 +00003162 /// struct-declarator: declarator
3163 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003164 if (Tok.isNot(tok::colon)) {
3165 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3166 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003167 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003168 }
Mike Stump11289f42009-09-09 15:08:12 +00003169
Chris Lattner76c72282007-10-09 17:33:22 +00003170 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003171 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003172 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003173 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003174 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003175 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003176 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003177 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003178
Steve Naroff97170802007-08-20 22:28:22 +00003179 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003180 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003181
John McCallcfefb6d2009-11-03 02:38:08 +00003182 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003183 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003184
Steve Naroff97170802007-08-20 22:28:22 +00003185 // If we don't have a comma, it is either the end of the list (a ';')
3186 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003187 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003188 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003189
Steve Naroff97170802007-08-20 22:28:22 +00003190 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003191 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003192
John McCallcfefb6d2009-11-03 02:38:08 +00003193 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003194 }
Steve Naroff97170802007-08-20 22:28:22 +00003195}
3196
3197/// ParseStructUnionBody
3198/// struct-contents:
3199/// struct-declaration-list
3200/// [EXT] empty
3201/// [GNU] "struct-declaration-list" without terminatoring ';'
3202/// struct-declaration-list:
3203/// struct-declaration
3204/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003205/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003206///
Chris Lattner1300fb92007-01-23 23:42:53 +00003207void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003208 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003209 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3210 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003211 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003212
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003213 BalancedDelimiterTracker T(*this, tok::l_brace);
3214 if (T.consumeOpen())
3215 return;
Mike Stump11289f42009-09-09 15:08:12 +00003216
Douglas Gregor658b9552009-01-09 22:42:13 +00003217 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003218 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003219
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003220 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003221
Chris Lattner7b9ace62007-01-23 20:11:08 +00003222 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003223 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003224 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003225
Chris Lattner736ed5d2007-06-09 05:59:07 +00003226 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003227 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003228 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003229 continue;
3230 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003231
Andy Gibbsc804e082013-04-03 09:46:04 +00003232 // Parse _Static_assert declaration.
3233 if (Tok.is(tok::kw__Static_assert)) {
3234 SourceLocation DeclEnd;
3235 ParseStaticAssertDeclaration(DeclEnd);
3236 continue;
3237 }
3238
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003239 if (Tok.is(tok::annot_pragma_pack)) {
3240 HandlePragmaPack();
3241 continue;
3242 }
3243
3244 if (Tok.is(tok::annot_pragma_align)) {
3245 HandlePragmaAlign();
3246 continue;
3247 }
3248
John McCallcfefb6d2009-11-03 02:38:08 +00003249 if (!Tok.is(tok::at)) {
3250 struct CFieldCallback : FieldCallback {
3251 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003252 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003253 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003254
John McCall48871652010-08-21 09:40:31 +00003255 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003256 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003257 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3258
Eli Friedman934dbbf2012-08-08 23:53:27 +00003259 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003260 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003261 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003262 FD.D.getDeclSpec().getSourceRange().getBegin(),
3263 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003264 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003265 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003266 }
John McCallcfefb6d2009-11-03 02:38:08 +00003267 } Callback(*this, TagDecl, FieldDecls);
3268
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003269 // Parse all the comma separated declarators.
3270 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003271 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003272 } else { // Handle @defs
3273 ConsumeToken();
3274 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3275 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003276 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003277 continue;
3278 }
3279 ConsumeToken();
3280 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3281 if (!Tok.is(tok::identifier)) {
3282 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003283 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003284 continue;
3285 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003286 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003287 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003288 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003289 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3290 ConsumeToken();
3291 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003292 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003293
Chris Lattner76c72282007-10-09 17:33:22 +00003294 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003295 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003296 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003297 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003298 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003299 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003300 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3301 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003302 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003303 // If we stopped at a ';', eat it.
3304 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003305 }
3306 }
Mike Stump11289f42009-09-09 15:08:12 +00003307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003308 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003309
John McCall084e83d2011-03-24 11:26:52 +00003310 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003311 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003312 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003313
Douglas Gregor0be31a22010-07-02 17:43:08 +00003314 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003315 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003316 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003317 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003318 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003319 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3320 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003321}
3322
Chris Lattner3b561a32006-08-13 00:12:11 +00003323/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003324/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003325/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003326///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003327/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3328/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003329/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3330/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003331/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003332/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003333///
Richard Smith7d137e32012-03-23 03:33:32 +00003334/// [C++11] enum-head '{' enumerator-list[opt] '}'
3335/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003336///
Richard Smith7d137e32012-03-23 03:33:32 +00003337/// enum-head: [C++11]
3338/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3339/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3340/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003341///
Richard Smith7d137e32012-03-23 03:33:32 +00003342/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003343/// 'enum'
3344/// 'enum' 'class'
3345/// 'enum' 'struct'
3346///
Richard Smith7d137e32012-03-23 03:33:32 +00003347/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003348/// ':' type-specifier-seq
3349///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003350/// [C++] elaborated-type-specifier:
3351/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3352///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003353void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003354 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003355 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003356 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003357 if (Tok.is(tok::code_completion)) {
3358 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003359 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003360 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003361 }
John McCallcb432fa2011-07-06 05:58:41 +00003362
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003363 // If attributes exist after tag, parse them.
3364 ParsedAttributesWithRange attrs(AttrFactory);
3365 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003366 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003367
3368 // If declspecs exist after tag, parse them.
3369 while (Tok.is(tok::kw___declspec))
3370 ParseMicrosoftDeclSpec(attrs);
3371
Richard Smith0f8ee222012-01-10 01:33:14 +00003372 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003373 bool IsScopedUsingClassTag = false;
3374
John McCallbeae29a2012-06-23 22:30:04 +00003375 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003376 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3377 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3378 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003379 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003380 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003381
Bill Wendling44426052012-12-20 19:22:21 +00003382 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003383 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003384 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003385
3386 // They are allowed afterwards, though.
3387 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003388 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003389 while (Tok.is(tok::kw___declspec))
3390 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003391 }
Richard Smith7d137e32012-03-23 03:33:32 +00003392
John McCall6347b682012-05-07 06:16:58 +00003393 // C++11 [temp.explicit]p12:
3394 // The usual access controls do not apply to names used to specify
3395 // explicit instantiations.
3396 // We extend this to also cover explicit specializations. Note that
3397 // we don't suppress if this turns out to be an elaborated type
3398 // specifier.
3399 bool shouldDelayDiagsInTag =
3400 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3401 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3402 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003403
Richard Smithbfdb1082012-03-12 08:56:40 +00003404 // Enum definitions should not be parsed in a trailing-return-type.
3405 bool AllowDeclaration = DSC != DSC_trailing;
3406
3407 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003408 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003409 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003410
Abramo Bagnarad7548482010-05-19 21:37:53 +00003411 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003412 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003413 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3414 // if a fixed underlying type is allowed.
3415 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003416
3417 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003418 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003419 return;
3420
3421 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003422 Diag(Tok, diag::err_expected_ident);
3423 if (Tok.isNot(tok::l_brace)) {
3424 // Has no name and is not a definition.
3425 // Skip the rest of this declarator, up until the comma or semicolon.
3426 SkipUntil(tok::comma, true);
3427 return;
3428 }
3429 }
3430 }
Mike Stump11289f42009-09-09 15:08:12 +00003431
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003432 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003433 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003434 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003435 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003436
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003437 // Skip the rest of this declarator, up until the comma or semicolon.
3438 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003439 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003440 }
Mike Stump11289f42009-09-09 15:08:12 +00003441
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003442 // If an identifier is present, consume and remember it.
3443 IdentifierInfo *Name = 0;
3444 SourceLocation NameLoc;
3445 if (Tok.is(tok::identifier)) {
3446 Name = Tok.getIdentifierInfo();
3447 NameLoc = ConsumeToken();
3448 }
Mike Stump11289f42009-09-09 15:08:12 +00003449
Richard Smith0f8ee222012-01-10 01:33:14 +00003450 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003451 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3452 // declaration of a scoped enumeration.
3453 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003454 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003455 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003456 }
3457
John McCall6347b682012-05-07 06:16:58 +00003458 // Okay, end the suppression area. We'll decide whether to emit the
3459 // diagnostics in a second.
3460 if (shouldDelayDiagsInTag)
3461 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003462
Douglas Gregor0bf31402010-10-08 23:50:27 +00003463 TypeResult BaseType;
3464
Douglas Gregord1f69f62010-12-01 17:42:47 +00003465 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003466 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003467 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003468 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003469 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003470 // If we're in class scope, this can either be an enum declaration with
3471 // an underlying type, or a declaration of a bitfield member. We try to
3472 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003473 // (integer literal, sizeof); if it's still ambiguous, we then consider
3474 // anything that's a simple-type-specifier followed by '(' as an
3475 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003476 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003477 EnterExpressionEvaluationContext Unevaluated(Actions,
3478 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003479 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003480 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003481 // bit-field. This is the common case.
3482 if (TPR == TPResult::True())
3483 PossibleBitfield = true;
3484 // If the next token starts a type-specifier-seq, it may be either a
3485 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003486 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003487 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003488 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003489 GetLookAheadToken(2).getKind() == tok::semi) {
3490 // Consume the ':'.
3491 ConsumeToken();
3492 } else {
3493 // We have the start of a type-specifier-seq, so we have to perform
3494 // tentative parsing to determine whether we have an expression or a
3495 // type.
3496 TentativeParsingAction TPA(*this);
3497
3498 // Consume the ':'.
3499 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003500
3501 // If we see a type specifier followed by an open-brace, we have an
3502 // ambiguity between an underlying type and a C++11 braced
3503 // function-style cast. Resolve this by always treating it as an
3504 // underlying type.
3505 // FIXME: The standard is not entirely clear on how to disambiguate in
3506 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003507 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003508 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003509 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003510 // We'll parse this as a bitfield later.
3511 PossibleBitfield = true;
3512 TPA.Revert();
3513 } else {
3514 // We have a type-specifier-seq.
3515 TPA.Commit();
3516 }
3517 }
3518 } else {
3519 // Consume the ':'.
3520 ConsumeToken();
3521 }
3522
3523 if (!PossibleBitfield) {
3524 SourceRange Range;
3525 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003526
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003527 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003528 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003529 } else if (!getLangOpts().ObjC2) {
3530 if (getLangOpts().CPlusPlus)
3531 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3532 else
3533 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3534 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003535 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003536 }
3537
Richard Smith0f8ee222012-01-10 01:33:14 +00003538 // There are four options here. If we have 'friend enum foo;' then this is a
3539 // friend declaration, and cannot have an accompanying definition. If we have
3540 // 'enum foo;', then this is a forward declaration. If we have
3541 // 'enum foo {...' then this is a definition. Otherwise we have something
3542 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003543 //
3544 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3545 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3546 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3547 //
John McCallfaf5fb42010-08-26 23:41:50 +00003548 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003549 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003550 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003551 } else if (Tok.is(tok::l_brace)) {
3552 if (DS.isFriendSpecified()) {
3553 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3554 << SourceRange(DS.getFriendSpecLoc());
3555 ConsumeBrace();
3556 SkipUntil(tok::r_brace);
3557 TUK = Sema::TUK_Friend;
3558 } else {
3559 TUK = Sema::TUK_Definition;
3560 }
Richard Smith369b9f92012-06-25 21:37:02 +00003561 } else if (DSC != DSC_type_specifier &&
3562 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003563 (Tok.isAtStartOfLine() &&
3564 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003565 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3566 if (Tok.isNot(tok::semi)) {
3567 // A semicolon was missing after this declaration. Diagnose and recover.
3568 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3569 "enum");
3570 PP.EnterToken(Tok);
3571 Tok.setKind(tok::semi);
3572 }
John McCall6347b682012-05-07 06:16:58 +00003573 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003574 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003575 }
3576
3577 // If this is an elaborated type specifier, and we delayed
3578 // diagnostics before, just merge them into the current pool.
3579 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3580 diagsFromTag.redelay();
3581 }
Richard Smith7d137e32012-03-23 03:33:32 +00003582
3583 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003584 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003585 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003586 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003587 // Skip the rest of this declarator, up until the comma or semicolon.
3588 Diag(Tok, diag::err_enum_template);
3589 SkipUntil(tok::comma, true);
3590 return;
3591 }
3592
3593 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3594 // Enumerations can't be explicitly instantiated.
3595 DS.SetTypeSpecError();
3596 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3597 return;
3598 }
3599
3600 assert(TemplateInfo.TemplateParams && "no template parameters");
3601 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3602 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003603 }
Chad Rosierc1183952012-06-26 22:30:43 +00003604
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003605 if (TUK == Sema::TUK_Reference)
3606 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003607
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003608 if (!Name && TUK != Sema::TUK_Definition) {
3609 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003610
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003611 // Skip the rest of this declarator, up until the comma or semicolon.
3612 SkipUntil(tok::comma, true);
3613 return;
3614 }
Richard Smith7d137e32012-03-23 03:33:32 +00003615
Douglas Gregord6ab8742009-05-28 23:31:59 +00003616 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003617 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003618 const char *PrevSpec = 0;
3619 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003620 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003621 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003622 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003623 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003624 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003625
Douglas Gregorba41d012010-04-24 16:38:41 +00003626 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003627 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003628 // dependent tag.
3629 if (!Name) {
3630 DS.SetTypeSpecError();
3631 Diag(Tok, diag::err_expected_type_name_after_typename);
3632 return;
3633 }
Chad Rosierc1183952012-06-26 22:30:43 +00003634
Douglas Gregor0be31a22010-07-02 17:43:08 +00003635 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003636 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003637 NameLoc);
3638 if (Type.isInvalid()) {
3639 DS.SetTypeSpecError();
3640 return;
3641 }
Chad Rosierc1183952012-06-26 22:30:43 +00003642
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003643 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3644 NameLoc.isValid() ? NameLoc : StartLoc,
3645 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003646 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003647
Douglas Gregorba41d012010-04-24 16:38:41 +00003648 return;
3649 }
Mike Stump11289f42009-09-09 15:08:12 +00003650
John McCall48871652010-08-21 09:40:31 +00003651 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003652 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003653 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003654 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003655 ConsumeBrace();
3656 SkipUntil(tok::r_brace);
3657 }
Chad Rosierc1183952012-06-26 22:30:43 +00003658
Douglas Gregorba41d012010-04-24 16:38:41 +00003659 DS.SetTypeSpecError();
3660 return;
3661 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003662
Richard Smith369b9f92012-06-25 21:37:02 +00003663 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003664 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003665
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003666 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3667 NameLoc.isValid() ? NameLoc : StartLoc,
3668 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003669 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003670}
3671
Chris Lattnerc1915e22007-01-25 07:29:02 +00003672/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3673/// enumerator-list:
3674/// enumerator
3675/// enumerator-list ',' enumerator
3676/// enumerator:
3677/// enumeration-constant
3678/// enumeration-constant '=' constant-expression
3679/// enumeration-constant:
3680/// identifier
3681///
John McCall48871652010-08-21 09:40:31 +00003682void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003683 // Enter the scope of the enum body and start the definition.
3684 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003685 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003686
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003687 BalancedDelimiterTracker T(*this, tok::l_brace);
3688 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003689
Chris Lattner37256fb2007-08-27 17:24:30 +00003690 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003691 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003692 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003693
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003694 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003695
John McCall48871652010-08-21 09:40:31 +00003696 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003697
Chris Lattnerc1915e22007-01-25 07:29:02 +00003698 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003699 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003700 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3701 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003702
John McCall811a0f52010-10-22 23:36:17 +00003703 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003704 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003705 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003706 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003707 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003708
Chris Lattnerc1915e22007-01-25 07:29:02 +00003709 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003710 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003711 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003712
Chris Lattner76c72282007-10-09 17:33:22 +00003713 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003714 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003715 AssignedVal = ParseConstantExpression();
3716 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003717 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003718 }
Mike Stump11289f42009-09-09 15:08:12 +00003719
Chris Lattnerc1915e22007-01-25 07:29:02 +00003720 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003721 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3722 LastEnumConstDecl,
3723 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003724 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003725 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003726 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003727
Chris Lattner4ef40012007-06-11 01:28:17 +00003728 EnumConstantDecls.push_back(EnumConstDecl);
3729 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003730
Douglas Gregorce66d022010-09-07 14:51:08 +00003731 if (Tok.is(tok::identifier)) {
3732 // We're missing a comma between enumerators.
3733 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003734 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003735 << FixItHint::CreateInsertion(Loc, ", ");
3736 continue;
3737 }
Chad Rosierc1183952012-06-26 22:30:43 +00003738
Chris Lattner76c72282007-10-09 17:33:22 +00003739 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003740 break;
3741 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003742
Richard Smith5d164bc2011-10-15 05:09:34 +00003743 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003744 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003745 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3746 diag::ext_enumerator_list_comma_cxx :
3747 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003748 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003749 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003750 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3751 << FixItHint::CreateRemoval(CommaLoc);
3752 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003753 }
Mike Stump11289f42009-09-09 15:08:12 +00003754
Chris Lattnerc1915e22007-01-25 07:29:02 +00003755 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003756 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003757
Chris Lattnerc1915e22007-01-25 07:29:02 +00003758 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003759 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003760 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003761
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003762 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003763 EnumDecl, EnumConstantDecls,
3764 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003765 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003766
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003767 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003768 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3769 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003770
3771 // The next token must be valid after an enum definition. If not, a ';'
3772 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003773 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3774 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003775 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3776 // Push this token back into the preprocessor and change our current token
3777 // to ';' so that the rest of the code recovers as though there were an
3778 // ';' after the definition.
3779 PP.EnterToken(Tok);
3780 Tok.setKind(tok::semi);
3781 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003782}
Chris Lattner3b561a32006-08-13 00:12:11 +00003783
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003784/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003785/// start of a type-qualifier-list.
3786bool Parser::isTypeQualifier() const {
3787 switch (Tok.getKind()) {
3788 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003789
3790 // type-qualifier only in OpenCL
3791 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003792 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003793
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003794 // type-qualifier
3795 case tok::kw_const:
3796 case tok::kw_volatile:
3797 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003798 case tok::kw___private:
3799 case tok::kw___local:
3800 case tok::kw___global:
3801 case tok::kw___constant:
3802 case tok::kw___read_only:
3803 case tok::kw___read_write:
3804 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003805 return true;
3806 }
3807}
3808
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003809/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3810/// is definitely a type-specifier. Return false if it isn't part of a type
3811/// specifier or if we're not sure.
3812bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3813 switch (Tok.getKind()) {
3814 default: return false;
3815 // type-specifiers
3816 case tok::kw_short:
3817 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003818 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003819 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003820 case tok::kw_signed:
3821 case tok::kw_unsigned:
3822 case tok::kw__Complex:
3823 case tok::kw__Imaginary:
3824 case tok::kw_void:
3825 case tok::kw_char:
3826 case tok::kw_wchar_t:
3827 case tok::kw_char16_t:
3828 case tok::kw_char32_t:
3829 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003830 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003831 case tok::kw_float:
3832 case tok::kw_double:
3833 case tok::kw_bool:
3834 case tok::kw__Bool:
3835 case tok::kw__Decimal32:
3836 case tok::kw__Decimal64:
3837 case tok::kw__Decimal128:
3838 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003839
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003840 // OpenCL specific types:
3841 case tok::kw_image1d_t:
3842 case tok::kw_image1d_array_t:
3843 case tok::kw_image1d_buffer_t:
3844 case tok::kw_image2d_t:
3845 case tok::kw_image2d_array_t:
3846 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003847 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003848 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003849
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003850 // struct-or-union-specifier (C99) or class-specifier (C++)
3851 case tok::kw_class:
3852 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003853 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003854 case tok::kw_union:
3855 // enum-specifier
3856 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003857
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003858 // typedef-name
3859 case tok::annot_typename:
3860 return true;
3861 }
3862}
3863
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003864/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003865/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003866bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003867 switch (Tok.getKind()) {
3868 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003869
Chris Lattner020bab92009-01-04 23:41:41 +00003870 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003871 if (TryAltiVecVectorToken())
3872 return true;
3873 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003874 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003875 // Annotate typenames and C++ scope specifiers. If we get one, just
3876 // recurse to handle whatever we get.
3877 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003878 return true;
3879 if (Tok.is(tok::identifier))
3880 return false;
3881 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003882
Chris Lattner020bab92009-01-04 23:41:41 +00003883 case tok::coloncolon: // ::foo::bar
3884 if (NextToken().is(tok::kw_new) || // ::new
3885 NextToken().is(tok::kw_delete)) // ::delete
3886 return false;
3887
Chris Lattner020bab92009-01-04 23:41:41 +00003888 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003889 return true;
3890 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003891
Chris Lattnere37e2332006-08-15 04:50:22 +00003892 // GNU attributes support.
3893 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003894 // GNU typeof support.
3895 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003896
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003897 // type-specifiers
3898 case tok::kw_short:
3899 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003900 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003901 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003902 case tok::kw_signed:
3903 case tok::kw_unsigned:
3904 case tok::kw__Complex:
3905 case tok::kw__Imaginary:
3906 case tok::kw_void:
3907 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003908 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003909 case tok::kw_char16_t:
3910 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003911 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003912 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003913 case tok::kw_float:
3914 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003915 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003916 case tok::kw__Bool:
3917 case tok::kw__Decimal32:
3918 case tok::kw__Decimal64:
3919 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003920 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003921
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003922 // OpenCL specific types:
3923 case tok::kw_image1d_t:
3924 case tok::kw_image1d_array_t:
3925 case tok::kw_image1d_buffer_t:
3926 case tok::kw_image2d_t:
3927 case tok::kw_image2d_array_t:
3928 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003929 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003930 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003931
Chris Lattner861a2262008-04-13 18:59:07 +00003932 // struct-or-union-specifier (C99) or class-specifier (C++)
3933 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003934 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003935 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003936 case tok::kw_union:
3937 // enum-specifier
3938 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003939
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003940 // type-qualifier
3941 case tok::kw_const:
3942 case tok::kw_volatile:
3943 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003944
John McCallea0a39e2012-11-14 00:49:39 +00003945 // Debugger support.
3946 case tok::kw___unknown_anytype:
3947
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003948 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003949 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003950 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003951
Chris Lattner409bf7d2008-10-20 00:25:30 +00003952 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3953 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003954 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003955
Steve Naroff44ac7772008-12-25 14:16:32 +00003956 case tok::kw___cdecl:
3957 case tok::kw___stdcall:
3958 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003959 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003960 case tok::kw___w64:
3961 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003962 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003963 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003964 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003965
3966 case tok::kw___private:
3967 case tok::kw___local:
3968 case tok::kw___global:
3969 case tok::kw___constant:
3970 case tok::kw___read_only:
3971 case tok::kw___read_write:
3972 case tok::kw___write_only:
3973
Eli Friedman53339e02009-06-08 23:27:34 +00003974 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003975
3976 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003977 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003978
Richard Smith8e1ac332013-03-28 01:55:44 +00003979 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00003980 case tok::kw__Atomic:
3981 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003982 }
3983}
3984
Chris Lattneracd58a32006-08-06 17:24:14 +00003985/// isDeclarationSpecifier() - Return true if the current token is part of a
3986/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003987///
3988/// \param DisambiguatingWithExpression True to indicate that the purpose of
3989/// this check is to disambiguate between an expression and a declaration.
3990bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003991 switch (Tok.getKind()) {
3992 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003993
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003994 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003995 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003996
Chris Lattner020bab92009-01-04 23:41:41 +00003997 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003998 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003999 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004000 return false;
John Thompson22334602010-02-05 00:12:22 +00004001 if (TryAltiVecVectorToken())
4002 return true;
4003 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004004 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004005 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004006 // Annotate typenames and C++ scope specifiers. If we get one, just
4007 // recurse to handle whatever we get.
4008 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004009 return true;
4010 if (Tok.is(tok::identifier))
4011 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004012
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004013 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004014 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004015 // expression is permitted, then this is probably a class message send
4016 // missing the initial '['. In this case, we won't consider this to be
4017 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004018 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004019 isStartOfObjCClassMessageMissingOpenBracket())
4020 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004021
John McCall1f476a12010-02-26 08:45:28 +00004022 return isDeclarationSpecifier();
4023
Chris Lattner020bab92009-01-04 23:41:41 +00004024 case tok::coloncolon: // ::foo::bar
4025 if (NextToken().is(tok::kw_new) || // ::new
4026 NextToken().is(tok::kw_delete)) // ::delete
4027 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004028
Chris Lattner020bab92009-01-04 23:41:41 +00004029 // Annotate typenames and C++ scope specifiers. If we get one, just
4030 // recurse to handle whatever we get.
4031 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004032 return true;
4033 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004034
Chris Lattneracd58a32006-08-06 17:24:14 +00004035 // storage-class-specifier
4036 case tok::kw_typedef:
4037 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004038 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004039 case tok::kw_static:
4040 case tok::kw_auto:
4041 case tok::kw_register:
4042 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004043 case tok::kw_thread_local:
4044 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004045
Douglas Gregor26701a42011-09-09 02:06:17 +00004046 // Modules
4047 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004048
John McCallea0a39e2012-11-14 00:49:39 +00004049 // Debugger support
4050 case tok::kw___unknown_anytype:
4051
Chris Lattneracd58a32006-08-06 17:24:14 +00004052 // type-specifiers
4053 case tok::kw_short:
4054 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004055 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004056 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004057 case tok::kw_signed:
4058 case tok::kw_unsigned:
4059 case tok::kw__Complex:
4060 case tok::kw__Imaginary:
4061 case tok::kw_void:
4062 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004063 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004064 case tok::kw_char16_t:
4065 case tok::kw_char32_t:
4066
Chris Lattneracd58a32006-08-06 17:24:14 +00004067 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004068 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004069 case tok::kw_float:
4070 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004071 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004072 case tok::kw__Bool:
4073 case tok::kw__Decimal32:
4074 case tok::kw__Decimal64:
4075 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004076 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004077
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004078 // OpenCL specific types:
4079 case tok::kw_image1d_t:
4080 case tok::kw_image1d_array_t:
4081 case tok::kw_image1d_buffer_t:
4082 case tok::kw_image2d_t:
4083 case tok::kw_image2d_array_t:
4084 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004085 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004086 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004087
Chris Lattner861a2262008-04-13 18:59:07 +00004088 // struct-or-union-specifier (C99) or class-specifier (C++)
4089 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004090 case tok::kw_struct:
4091 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004092 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004093 // enum-specifier
4094 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004095
Chris Lattneracd58a32006-08-06 17:24:14 +00004096 // type-qualifier
4097 case tok::kw_const:
4098 case tok::kw_volatile:
4099 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004100
Chris Lattneracd58a32006-08-06 17:24:14 +00004101 // function-specifier
4102 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004103 case tok::kw_virtual:
4104 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004105 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004106
Richard Smith1dba27c2013-01-29 09:02:09 +00004107 // alignment-specifier
4108 case tok::kw__Alignas:
4109
Richard Smithd16fe122012-10-25 00:00:53 +00004110 // friend keyword.
4111 case tok::kw_friend:
4112
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004113 // static_assert-declaration
4114 case tok::kw__Static_assert:
4115
Chris Lattner599e47e2007-08-09 17:01:07 +00004116 // GNU typeof support.
4117 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004118
Chris Lattner599e47e2007-08-09 17:01:07 +00004119 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004120 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004121
Richard Smithd16fe122012-10-25 00:00:53 +00004122 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004123 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004124 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004125
Richard Smith8e1ac332013-03-28 01:55:44 +00004126 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004127 case tok::kw__Atomic:
4128 return true;
4129
Chris Lattner8b2ec162008-07-26 03:38:44 +00004130 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4131 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004132 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004133
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004134 // typedef-name
4135 case tok::annot_typename:
4136 return !DisambiguatingWithExpression ||
4137 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004138
Steve Narofff192fab2009-01-06 19:34:12 +00004139 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004140 case tok::kw___cdecl:
4141 case tok::kw___stdcall:
4142 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004143 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004144 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004145 case tok::kw___sptr:
4146 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004147 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004148 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004149 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004150 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004151 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004152
4153 case tok::kw___private:
4154 case tok::kw___local:
4155 case tok::kw___global:
4156 case tok::kw___constant:
4157 case tok::kw___read_only:
4158 case tok::kw___read_write:
4159 case tok::kw___write_only:
4160
Eli Friedman53339e02009-06-08 23:27:34 +00004161 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004162 }
4163}
4164
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004165bool Parser::isConstructorDeclarator() {
4166 TentativeParsingAction TPA(*this);
4167
4168 // Parse the C++ scope specifier.
4169 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004170 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004171 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004172 TPA.Revert();
4173 return false;
4174 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004175
4176 // Parse the constructor name.
4177 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4178 // We already know that we have a constructor name; just consume
4179 // the token.
4180 ConsumeToken();
4181 } else {
4182 TPA.Revert();
4183 return false;
4184 }
4185
Richard Smith43f340f2012-03-27 23:05:05 +00004186 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004187 if (Tok.isNot(tok::l_paren)) {
4188 TPA.Revert();
4189 return false;
4190 }
4191 ConsumeParen();
4192
Richard Smith43f340f2012-03-27 23:05:05 +00004193 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4194 // that we have a constructor.
4195 if (Tok.is(tok::r_paren) ||
4196 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004197 TPA.Revert();
4198 return true;
4199 }
4200
4201 // If we need to, enter the specified scope.
4202 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004203 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004204 DeclScopeObj.EnterDeclaratorScope();
4205
Francois Pichet79f3a872011-01-31 04:54:32 +00004206 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004207 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004208 MaybeParseMicrosoftAttributes(Attrs);
4209
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004210 // Check whether the next token(s) are part of a declaration
4211 // specifier, in which case we have the start of a parameter and,
4212 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004213 bool IsConstructor = false;
4214 if (isDeclarationSpecifier())
4215 IsConstructor = true;
4216 else if (Tok.is(tok::identifier) ||
4217 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4218 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4219 // This might be a parenthesized member name, but is more likely to
4220 // be a constructor declaration with an invalid argument type. Keep
4221 // looking.
4222 if (Tok.is(tok::annot_cxxscope))
4223 ConsumeToken();
4224 ConsumeToken();
4225
4226 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004227 // which must have one of the following syntactic forms (see the
4228 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004229 switch (Tok.getKind()) {
4230 case tok::l_paren:
4231 // C(X ( int));
4232 case tok::l_square:
4233 // C(X [ 5]);
4234 // C(X [ [attribute]]);
4235 case tok::coloncolon:
4236 // C(X :: Y);
4237 // C(X :: *p);
4238 case tok::r_paren:
4239 // C(X )
4240 // Assume this isn't a constructor, rather than assuming it's a
4241 // constructor with an unnamed parameter of an ill-formed type.
4242 break;
4243
4244 default:
4245 IsConstructor = true;
4246 break;
4247 }
4248 }
4249
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004250 TPA.Revert();
4251 return IsConstructor;
4252}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004253
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004254/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004255/// type-qualifier-list: [C99 6.7.5]
4256/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004257/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004258/// [ only if VendorAttributesAllowed=true ]
4259/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004260/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004261/// [ only if VendorAttributesAllowed=true ]
4262/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004263/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004264/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004265///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004266void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4267 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004268 bool CXX11AttributesAllowed,
4269 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004270 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004271 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004272 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004273 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004274 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004275 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004276
4277 SourceLocation EndLoc;
4278
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004279 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004280 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004281 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004282 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004283 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004284
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004285 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004286 case tok::code_completion:
4287 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004288 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004289
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004290 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004291 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004292 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004293 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004294 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004295 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004296 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004297 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004298 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004299 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004300 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004301 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004302 case tok::kw__Atomic:
4303 if (!AtomicAllowed)
4304 goto DoneWithTypeQuals;
4305 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4306 getLangOpts());
4307 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004308
4309 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004310 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004311 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004312 goto DoneWithTypeQuals;
4313 case tok::kw___private:
4314 case tok::kw___global:
4315 case tok::kw___local:
4316 case tok::kw___constant:
4317 case tok::kw___read_only:
4318 case tok::kw___write_only:
4319 case tok::kw___read_write:
4320 ParseOpenCLQualifiers(DS);
4321 break;
4322
Aaron Ballman317a77f2013-05-22 23:25:32 +00004323 case tok::kw___sptr:
4324 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004325 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004326 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004327 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004328 case tok::kw___cdecl:
4329 case tok::kw___stdcall:
4330 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004331 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004332 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004333 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004334 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004335 continue;
4336 }
4337 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004338 case tok::kw___pascal:
4339 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004340 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004341 continue;
4342 }
4343 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004344 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004345 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004346 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004347 continue; // do *not* consume the next token!
4348 }
4349 // otherwise, FALL THROUGH!
4350 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004351 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004352 // If this is not a type-qualifier token, we're done reading type
4353 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004354 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004355 if (EndLoc.isValid())
4356 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004357 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004358 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004359
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004360 // If the specifier combination wasn't legal, issue a diagnostic.
4361 if (isInvalid) {
4362 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004363 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004364 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004365 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004366 }
4367}
4368
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004369
4370/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4371///
4372void Parser::ParseDeclarator(Declarator &D) {
4373 /// This implements the 'declarator' production in the C grammar, then checks
4374 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004375 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004376}
4377
Richard Smith0efa75c2012-03-29 01:16:42 +00004378static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4379 if (Kind == tok::star || Kind == tok::caret)
4380 return true;
4381
4382 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4383 if (!Lang.CPlusPlus)
4384 return false;
4385
4386 return Kind == tok::amp || Kind == tok::ampamp;
4387}
4388
Sebastian Redlbd150f42008-11-21 19:14:01 +00004389/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4390/// is parsed by the function passed to it. Pass null, and the direct-declarator
4391/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004392/// ptr-operator production.
4393///
Richard Smith09f76ee2011-10-19 21:33:05 +00004394/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004395/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4396/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004397///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004398/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4399/// [C] pointer[opt] direct-declarator
4400/// [C++] direct-declarator
4401/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004402///
4403/// pointer: [C99 6.7.5]
4404/// '*' type-qualifier-list[opt]
4405/// '*' type-qualifier-list[opt] pointer
4406///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004407/// ptr-operator:
4408/// '*' cv-qualifier-seq[opt]
4409/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004410/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004411/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004412/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004413/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004414void Parser::ParseDeclaratorInternal(Declarator &D,
4415 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004416 if (Diags.hasAllExtensionsSilenced())
4417 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004418
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004419 // C++ member pointers start with a '::' or a nested-name.
4420 // Member pointers get special handling, since there's no place for the
4421 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004422 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004423 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4424 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004425 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4426 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004427 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004428 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004429
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004430 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004431 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004432 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004433 if (D.mayHaveIdentifier())
4434 D.getCXXScopeSpec() = SS;
4435 else
4436 AnnotateScopeToken(SS, true);
4437
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004438 if (DirectDeclParser)
4439 (this->*DirectDeclParser)(D);
4440 return;
4441 }
4442
4443 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004444 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004445 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004446 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004447 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004448
4449 // Recurse to parse whatever is left.
4450 ParseDeclaratorInternal(D, DirectDeclParser);
4451
4452 // Sema will have to catch (syntactically invalid) pointers into global
4453 // scope. It has to catch pointers into namespace scope anyway.
4454 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004455 Loc),
4456 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004457 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004458 return;
4459 }
4460 }
4461
4462 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004463 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004464 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004465 if (DirectDeclParser)
4466 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004467 return;
4468 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004469
Sebastian Redled0f3b02009-03-15 22:02:01 +00004470 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4471 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004472 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004473 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004474
Chris Lattner9eac9312009-03-27 04:18:06 +00004475 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004476 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004477 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004478
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004479 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004480 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004481 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004482
Bill Wendling3708c182007-05-27 10:15:43 +00004483 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004484 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004485 if (Kind == tok::star)
4486 // Remember that we parsed a pointer type, and remember the type-quals.
4487 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004488 DS.getConstSpecLoc(),
4489 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004490 DS.getRestrictSpecLoc()),
4491 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004492 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004493 else
4494 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004495 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004496 Loc),
4497 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004498 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004499 } else {
4500 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004501 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004502
Sebastian Redl3b27be62009-03-23 00:00:23 +00004503 // Complain about rvalue references in C++03, but then go on and build
4504 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004505 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004506 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004507 diag::warn_cxx98_compat_rvalue_reference :
4508 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004509
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004510 // GNU-style and C++11 attributes are allowed here, as is restrict.
4511 ParseTypeQualifierListOpt(DS);
4512 D.ExtendWithDeclSpec(DS);
4513
Bill Wendling93efb222007-06-02 23:28:54 +00004514 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4515 // cv-qualifiers are introduced through the use of a typedef or of a
4516 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004517 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4518 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4519 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004520 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004521 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4522 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004523 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004524 // 'restrict' is permitted as an extension.
4525 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4526 Diag(DS.getAtomicSpecLoc(),
4527 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004528 }
Bill Wendling3708c182007-05-27 10:15:43 +00004529
4530 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004531 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004532
Douglas Gregor66583c52008-11-03 15:51:28 +00004533 if (D.getNumTypeObjects() > 0) {
4534 // C++ [dcl.ref]p4: There shall be no references to references.
4535 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4536 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004537 if (const IdentifierInfo *II = D.getIdentifier())
4538 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4539 << II;
4540 else
4541 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4542 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004543
Sebastian Redlbd150f42008-11-21 19:14:01 +00004544 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004545 // can go ahead and build the (technically ill-formed)
4546 // declarator: reference collapsing will take care of it.
4547 }
4548 }
4549
Richard Smith8e1ac332013-03-28 01:55:44 +00004550 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004551 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004552 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004553 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004554 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004555 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004556}
4557
Richard Smith0efa75c2012-03-29 01:16:42 +00004558static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4559 SourceLocation EllipsisLoc) {
4560 if (EllipsisLoc.isValid()) {
4561 FixItHint Insertion;
4562 if (!D.getEllipsisLoc().isValid()) {
4563 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4564 D.setEllipsisLoc(EllipsisLoc);
4565 }
4566 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4567 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4568 }
4569}
4570
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004571/// ParseDirectDeclarator
4572/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004573/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004574/// '(' declarator ')'
4575/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004576/// [C90] direct-declarator '[' constant-expression[opt] ']'
4577/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4578/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4579/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4580/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004581/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4582/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004583/// direct-declarator '(' parameter-type-list ')'
4584/// direct-declarator '(' identifier-list[opt] ')'
4585/// [GNU] direct-declarator '(' parameter-forward-declarations
4586/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004587/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4588/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004589/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4590/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4591/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004592/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004593/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004594///
4595/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004596/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004597/// '::'[opt] nested-name-specifier[opt] type-name
4598///
4599/// id-expression: [C++ 5.1]
4600/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004601/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004602///
4603/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004604/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004605/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004606/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004607/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004608/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004609///
Richard Smith1453e312012-03-27 01:42:32 +00004610/// Note, any additional constructs added here may need corresponding changes
4611/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004612void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004613 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004614
David Blaikiebbafb8a2012-03-11 07:00:24 +00004615 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004616 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004617 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004618 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4619 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004620 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004621 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004622 }
4623
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004624 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004625 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004626 // Change the declaration context for name lookup, until this function
4627 // is exited (and the declarator has been parsed).
4628 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004629 }
4630
Douglas Gregor27b4c162010-12-23 22:44:42 +00004631 // C++0x [dcl.fct]p14:
4632 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004633 // of a parameter-declaration-clause without a preceding comma. In
4634 // this case, the ellipsis is parsed as part of the
4635 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004636 // parameter pack that has not been expanded; otherwise, it is parsed
4637 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004638 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004639 !((D.getContext() == Declarator::PrototypeContext ||
4640 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004641 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004642 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004643 !Actions.containsUnexpandedParameterPacks(D))) {
4644 SourceLocation EllipsisLoc = ConsumeToken();
4645 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4646 // The ellipsis was put in the wrong place. Recover, and explain to
4647 // the user what they should have done.
4648 ParseDeclarator(D);
4649 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4650 return;
4651 } else
4652 D.setEllipsisLoc(EllipsisLoc);
4653
4654 // The ellipsis can't be followed by a parenthesized declarator. We
4655 // check for that in ParseParenDeclarator, after we have disambiguated
4656 // the l_paren token.
4657 }
4658
Douglas Gregor7861a802009-11-03 01:35:08 +00004659 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4660 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4661 // We found something that indicates the start of an unqualified-id.
4662 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004663 bool AllowConstructorName;
4664 if (D.getDeclSpec().hasTypeSpecifier())
4665 AllowConstructorName = false;
4666 else if (D.getCXXScopeSpec().isSet())
4667 AllowConstructorName =
4668 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004669 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004670 else
4671 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4672
Abramo Bagnara7945c982012-01-27 09:46:47 +00004673 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004674 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4675 /*EnteringContext=*/true,
4676 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004677 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004678 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004679 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004680 D.getName()) ||
4681 // Once we're past the identifier, if the scope was bad, mark the
4682 // whole declarator bad.
4683 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004684 D.SetIdentifier(0, Tok.getLocation());
4685 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004686 } else {
4687 // Parsed the unqualified-id; update range information and move along.
4688 if (D.getSourceRange().getBegin().isInvalid())
4689 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4690 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004691 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004692 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004693 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004694 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004695 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004696 "There's a C++-specific check for tok::identifier above");
4697 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4698 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4699 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004700 goto PastIdentifier;
4701 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004702
Douglas Gregor7861a802009-11-03 01:35:08 +00004703 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004704 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004705 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004706 // Example: 'char (*X)' or 'int (*XX)(void)'
4707 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004708
4709 // If the declarator was parenthesized, we entered the declarator
4710 // scope when parsing the parenthesized declarator, then exited
4711 // the scope already. Re-enter the scope, if we need to.
4712 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004713 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004714 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004715 if (!D.isInvalidType() &&
4716 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004717 // Change the declaration context for name lookup, until this function
4718 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004719 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004720 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004721 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004722 // This could be something simple like "int" (in which case the declarator
4723 // portion is empty), if an abstract-declarator is allowed.
4724 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004725
4726 // The grammar for abstract-pack-declarator does not allow grouping parens.
4727 // FIXME: Revisit this once core issue 1488 is resolved.
4728 if (D.hasEllipsis() && D.hasGroupingParens())
4729 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4730 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004731 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004732 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004733 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004734 if (D.getContext() == Declarator::MemberContext)
4735 Diag(Tok, diag::err_expected_member_name_or_semi)
4736 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004737 else if (getLangOpts().CPlusPlus) {
4738 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4739 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4740 else
4741 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4742 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004743 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004744 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004745 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004746 }
Mike Stump11289f42009-09-09 15:08:12 +00004747
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004748 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004749 assert(D.isPastIdentifier() &&
4750 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004751
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004752 // Don't parse attributes unless we have parsed an unparenthesized name.
4753 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004754 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004755
Chris Lattneracd58a32006-08-06 17:24:14 +00004756 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004757 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004758 // Enter function-declaration scope, limiting any declarators to the
4759 // function prototype scope, including parameter declarators.
4760 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004761 Scope::FunctionPrototypeScope|Scope::DeclScope|
4762 (D.isFunctionDeclaratorAFunctionDeclaration()
4763 ? Scope::FunctionDeclarationScope : 0));
4764
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004765 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4766 // In such a case, check if we actually have a function declarator; if it
4767 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004768 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004769 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4770 // The name of the declarator, if any, is tentatively declared within
4771 // a possible direct initializer.
4772 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4773 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4774 TentativelyDeclaredIdentifiers.pop_back();
4775 if (!IsFunctionDecl)
4776 break;
4777 }
John McCall084e83d2011-03-24 11:26:52 +00004778 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004779 BalancedDelimiterTracker T(*this, tok::l_paren);
4780 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004781 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004782 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004783 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004784 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004785 } else {
4786 break;
4787 }
4788 }
Chad Rosierc1183952012-06-26 22:30:43 +00004789}
Chris Lattneracd58a32006-08-06 17:24:14 +00004790
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004791/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4792/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004793/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004794/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4795///
4796/// direct-declarator:
4797/// '(' declarator ')'
4798/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004799/// direct-declarator '(' parameter-type-list ')'
4800/// direct-declarator '(' identifier-list[opt] ')'
4801/// [GNU] direct-declarator '(' parameter-forward-declarations
4802/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004803///
4804void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004805 BalancedDelimiterTracker T(*this, tok::l_paren);
4806 T.consumeOpen();
4807
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004808 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004809
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004810 // Eat any attributes before we look at whether this is a grouping or function
4811 // declarator paren. If this is a grouping paren, the attribute applies to
4812 // the type being built up, for example:
4813 // int (__attribute__(()) *x)(long y)
4814 // If this ends up not being a grouping paren, the attribute applies to the
4815 // first argument, for example:
4816 // int (__attribute__(()) int x)
4817 // In either case, we need to eat any attributes to be able to determine what
4818 // sort of paren this is.
4819 //
John McCall084e83d2011-03-24 11:26:52 +00004820 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004821 bool RequiresArg = false;
4822 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004823 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004824
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004825 // We require that the argument list (if this is a non-grouping paren) be
4826 // present even if the attribute list was empty.
4827 RequiresArg = true;
4828 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004829
Steve Naroff44ac7772008-12-25 14:16:32 +00004830 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004831 ParseMicrosoftTypeAttributes(attrs);
4832
Dawn Perchik335e16b2010-09-03 01:29:35 +00004833 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004834 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004835 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004836
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004837 // If we haven't past the identifier yet (or where the identifier would be
4838 // stored, if this is an abstract declarator), then this is probably just
4839 // grouping parens. However, if this could be an abstract-declarator, then
4840 // this could also be the start of function arguments (consider 'void()').
4841 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004842
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004843 if (!D.mayOmitIdentifier()) {
4844 // If this can't be an abstract-declarator, this *must* be a grouping
4845 // paren, because we haven't seen the identifier yet.
4846 isGrouping = true;
4847 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004848 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4849 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004850 isDeclarationSpecifier() || // 'int(int)' is a function.
4851 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004852 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4853 // considered to be a type, not a K&R identifier-list.
4854 isGrouping = false;
4855 } else {
4856 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4857 isGrouping = true;
4858 }
Mike Stump11289f42009-09-09 15:08:12 +00004859
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004860 // If this is a grouping paren, handle:
4861 // direct-declarator: '(' declarator ')'
4862 // direct-declarator: '(' attributes declarator ')'
4863 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004864 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4865 D.setEllipsisLoc(SourceLocation());
4866
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004867 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004868 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004869 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004870 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004871 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004872 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004873 T.getCloseLocation()),
4874 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004875
4876 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004877
4878 // An ellipsis cannot be placed outside parentheses.
4879 if (EllipsisLoc.isValid())
4880 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4881
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004882 return;
4883 }
Mike Stump11289f42009-09-09 15:08:12 +00004884
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004885 // Okay, if this wasn't a grouping paren, it must be the start of a function
4886 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004887 // identifier (and remember where it would have been), then call into
4888 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004889 D.SetIdentifier(0, Tok.getLocation());
4890
David Blaikie15a430a2011-12-04 05:04:18 +00004891 // Enter function-declaration scope, limiting any declarators to the
4892 // function prototype scope, including parameter declarators.
4893 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004894 Scope::FunctionPrototypeScope | Scope::DeclScope |
4895 (D.isFunctionDeclaratorAFunctionDeclaration()
4896 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004897 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004898 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004899}
4900
4901/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4902/// declarator D up to a paren, which indicates that we are parsing function
4903/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004904///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004905/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4906/// immediately after the open paren - they should be considered to be the
4907/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004908///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004909/// If RequiresArg is true, then the first argument of the function is required
4910/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004911///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004912/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4913/// (C++11) ref-qualifier[opt], exception-specification[opt],
4914/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4915///
4916/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004917/// dynamic-exception-specification
4918/// noexcept-specification
4919///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004920void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004921 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004922 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004923 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004924 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004925 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004926 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004927 // lparen is already consumed!
4928 assert(D.isPastIdentifier() && "Should not call before identifier!");
4929
4930 // This should be true when the function has typed arguments.
4931 // Otherwise, it is treated as a K&R-style function.
4932 bool HasProto = false;
4933 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004934 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004935 // Remember where we see an ellipsis, if any.
4936 SourceLocation EllipsisLoc;
4937
4938 DeclSpec DS(AttrFactory);
4939 bool RefQualifierIsLValueRef = true;
4940 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004941 SourceLocation ConstQualifierLoc;
4942 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004943 ExceptionSpecificationType ESpecType = EST_None;
4944 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004945 SmallVector<ParsedType, 2> DynamicExceptions;
4946 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004947 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004948 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004949 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004950
James Molloy6f8780b2012-02-29 10:24:19 +00004951 Actions.ActOnStartFunctionDeclarator();
4952
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004953 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4954 EndLoc is the end location for the function declarator.
4955 They differ for trailing return types. */
4956 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004957 SourceLocation LParenLoc, RParenLoc;
4958 LParenLoc = Tracker.getOpenLocation();
4959 StartLoc = LParenLoc;
4960
Douglas Gregor9e66af42011-07-05 16:44:18 +00004961 if (isFunctionDeclaratorIdentifierList()) {
4962 if (RequiresArg)
4963 Diag(Tok, diag::err_argument_required_after_attribute);
4964
4965 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4966
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004967 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004968 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004969 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004970 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004971 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004972 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004973 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004974 else if (RequiresArg)
4975 Diag(Tok, diag::err_argument_required_after_attribute);
4976
David Blaikiebbafb8a2012-03-11 07:00:24 +00004977 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004978
4979 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004980 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004981 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004982 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004983 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004984
David Blaikiebbafb8a2012-03-11 07:00:24 +00004985 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004986 // FIXME: Accept these components in any order, and produce fixits to
4987 // correct the order if the user gets it wrong. Ideally we should deal
4988 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004989
4990 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00004991 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4992 /*CXX11AttributesAllowed*/ false,
4993 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004994 if (!DS.getSourceRange().getEnd().isInvalid()) {
4995 EndLoc = DS.getSourceRange().getEnd();
4996 ConstQualifierLoc = DS.getConstSpecLoc();
4997 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4998 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004999
5000 // Parse ref-qualifier[opt].
5001 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005002 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005003 diag::warn_cxx98_compat_ref_qualifier :
5004 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005005
Douglas Gregor9e66af42011-07-05 16:44:18 +00005006 RefQualifierIsLValueRef = Tok.is(tok::amp);
5007 RefQualifierLoc = ConsumeToken();
5008 EndLoc = RefQualifierLoc;
5009 }
5010
Douglas Gregor3024f072012-04-16 07:05:22 +00005011 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005012 // If a declaration declares a member function or member function
5013 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005014 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005015 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005016 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005017 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005018 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005019 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005020 (D.getContext() == Declarator::MemberContext
5021 ? !D.getDeclSpec().isFriendSpecified()
5022 : D.getContext() == Declarator::FileContext &&
5023 D.getCXXScopeSpec().isValid() &&
5024 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005025 Sema::CXXThisScopeRAII ThisScope(Actions,
5026 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005027 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005028 (D.getDeclSpec().isConstexprSpecified() &&
5029 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005030 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005031 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005032
Douglas Gregor9e66af42011-07-05 16:44:18 +00005033 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005034 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005035 DynamicExceptions,
5036 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005037 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005038 if (ESpecType != EST_None)
5039 EndLoc = ESpecRange.getEnd();
5040
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005041 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5042 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005043 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005044
Douglas Gregor9e66af42011-07-05 16:44:18 +00005045 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005046 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005047 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005048 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005049 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5050 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005051 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005052 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005053 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005054 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005055 }
5056 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005057 }
5058
5059 // Remember that we parsed a function type, and remember the attributes.
5060 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005061 IsAmbiguous,
5062 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005063 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005064 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005065 DS.getTypeQualifiers(),
5066 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005067 RefQualifierLoc, ConstQualifierLoc,
5068 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005069 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005070 ESpecType, ESpecRange.getBegin(),
5071 DynamicExceptions.data(),
5072 DynamicExceptionRanges.data(),
5073 DynamicExceptions.size(),
5074 NoexceptExpr.isUsable() ?
5075 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005076 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005077 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005078 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005079
5080 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005081}
5082
5083/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5084/// identifier list form for a K&R-style function: void foo(a,b,c)
5085///
5086/// Note that identifier-lists are only allowed for normal declarators, not for
5087/// abstract-declarators.
5088bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005089 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005090 && Tok.is(tok::identifier)
5091 && !TryAltiVecVectorToken()
5092 // K&R identifier lists can't have typedefs as identifiers, per C99
5093 // 6.7.5.3p11.
5094 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5095 // Identifier lists follow a really simple grammar: the identifiers can
5096 // be followed *only* by a ", identifier" or ")". However, K&R
5097 // identifier lists are really rare in the brave new modern world, and
5098 // it is very common for someone to typo a type in a non-K&R style
5099 // list. If we are presented with something like: "void foo(intptr x,
5100 // float y)", we don't want to start parsing the function declarator as
5101 // though it is a K&R style declarator just because intptr is an
5102 // invalid type.
5103 //
5104 // To handle this, we check to see if the token after the first
5105 // identifier is a "," or ")". Only then do we parse it as an
5106 // identifier list.
5107 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5108}
5109
5110/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5111/// we found a K&R-style identifier list instead of a typed parameter list.
5112///
5113/// After returning, ParamInfo will hold the parsed parameters.
5114///
5115/// identifier-list: [C99 6.7.5]
5116/// identifier
5117/// identifier-list ',' identifier
5118///
5119void Parser::ParseFunctionDeclaratorIdentifierList(
5120 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005121 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005122 // If there was no identifier specified for the declarator, either we are in
5123 // an abstract-declarator, or we are in a parameter declarator which was found
5124 // to be abstract. In abstract-declarators, identifier lists are not valid:
5125 // diagnose this.
5126 if (!D.getIdentifier())
5127 Diag(Tok, diag::ext_ident_list_in_param);
5128
5129 // Maintain an efficient lookup of params we have seen so far.
5130 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5131
5132 while (1) {
5133 // If this isn't an identifier, report the error and skip until ')'.
5134 if (Tok.isNot(tok::identifier)) {
5135 Diag(Tok, diag::err_expected_ident);
5136 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5137 // Forget we parsed anything.
5138 ParamInfo.clear();
5139 return;
5140 }
5141
5142 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5143
5144 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5145 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5146 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5147
5148 // Verify that the argument identifier has not already been mentioned.
5149 if (!ParamsSoFar.insert(ParmII)) {
5150 Diag(Tok, diag::err_param_redefinition) << ParmII;
5151 } else {
5152 // Remember this identifier in ParamInfo.
5153 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5154 Tok.getLocation(),
5155 0));
5156 }
5157
5158 // Eat the identifier.
5159 ConsumeToken();
5160
5161 // The list continues if we see a comma.
5162 if (Tok.isNot(tok::comma))
5163 break;
5164 ConsumeToken();
5165 }
5166}
5167
5168/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5169/// after the opening parenthesis. This function will not parse a K&R-style
5170/// identifier list.
5171///
Richard Smith2620cd92012-04-11 04:01:28 +00005172/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5173/// caller parsed those arguments immediately after the open paren - they should
5174/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005175///
5176/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5177/// be the location of the ellipsis, if any was parsed.
5178///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005179/// parameter-type-list: [C99 6.7.5]
5180/// parameter-list
5181/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005182/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005183///
5184/// parameter-list: [C99 6.7.5]
5185/// parameter-declaration
5186/// parameter-list ',' parameter-declaration
5187///
5188/// parameter-declaration: [C99 6.7.5]
5189/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005190/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005191/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005192/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005193/// declaration-specifiers abstract-declarator[opt]
5194/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005195/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005196/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005197/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005198///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005199void Parser::ParseParameterDeclarationClause(
5200 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005201 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005202 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005203 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00005204
Chris Lattner371ed4e2008-04-06 06:57:35 +00005205 while (1) {
5206 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005207 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5208 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005209 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005210 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005211 }
Mike Stump11289f42009-09-09 15:08:12 +00005212
Chris Lattner371ed4e2008-04-06 06:57:35 +00005213 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005214 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005215 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005216
Richard Smith2620cd92012-04-11 04:01:28 +00005217 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005218 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005219
John McCall53fa7142010-12-24 02:08:15 +00005220 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005221 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005222
5223 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005224
5225 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005226 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005227 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005228 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5229 // too much hassle.
5230 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005231
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005232 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005233
Chris Lattner371ed4e2008-04-06 06:57:35 +00005234 // Parse the declarator. This is "PrototypeContext", because we must
5235 // accept either 'declarator' or 'abstract-declarator' here.
5236 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5237 ParseDeclarator(ParmDecl);
5238
5239 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005240 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005241
Chris Lattner371ed4e2008-04-06 06:57:35 +00005242 // Remember this parsed parameter in ParamInfo.
5243 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005244
Douglas Gregor4d87df52008-12-16 21:30:33 +00005245 // DefArgToks is used when the parsing of default arguments needs
5246 // to be delayed.
5247 CachedTokens *DefArgToks = 0;
5248
Chris Lattner371ed4e2008-04-06 06:57:35 +00005249 // If no parameter was specified, verify that *something* was specified,
5250 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005251 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5252 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005253 // Completely missing, emit error.
5254 Diag(DSStart, diag::err_missing_param);
5255 } else {
5256 // Otherwise, we have something. Add it and let semantic analysis try
5257 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005258
Chris Lattner371ed4e2008-04-06 06:57:35 +00005259 // Inform the actions module about the parameter declarator, so it gets
5260 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005261 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005262
5263 // Parse the default argument, if any. We parse the default
5264 // arguments in all dialects; the semantic analysis in
5265 // ActOnParamDefaultArgument will reject the default argument in
5266 // C.
5267 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005268 SourceLocation EqualLoc = Tok.getLocation();
5269
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005270 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005271 if (D.getContext() == Declarator::MemberContext) {
5272 // If we're inside a class definition, cache the tokens
5273 // corresponding to the default argument. We'll actually parse
5274 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005275 // FIXME: Can we use a smart pointer for Toks?
5276 DefArgToks = new CachedTokens;
5277
Mike Stump11289f42009-09-09 15:08:12 +00005278 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005279 /*StopAtSemi=*/true,
5280 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005281 delete DefArgToks;
5282 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005283 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005284 } else {
5285 // Mark the end of the default argument so that we know when to
5286 // stop when we parse it later on.
5287 Token DefArgEnd;
5288 DefArgEnd.startToken();
5289 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5290 DefArgEnd.setLocation(Tok.getLocation());
5291 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005292 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005293 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005294 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005295 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005296 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005297 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005298
Chad Rosierc1183952012-06-26 22:30:43 +00005299 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005300 // used.
5301 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005302 Sema::PotentiallyEvaluatedIfUsed,
5303 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005304
Sebastian Redldb63af22012-03-14 15:54:00 +00005305 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005306 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005307 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005308 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005309 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005310 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005311 if (DefArgResult.isInvalid()) {
5312 Actions.ActOnParamDefaultArgumentError(Param);
5313 SkipUntil(tok::comma, tok::r_paren, true, true);
5314 } else {
5315 // Inform the actions module about the default argument
5316 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005317 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005318 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005319 }
5320 }
Mike Stump11289f42009-09-09 15:08:12 +00005321
5322 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5323 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005324 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005325 }
5326
5327 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005328 if (Tok.isNot(tok::comma)) {
5329 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005330 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005331
David Blaikiebbafb8a2012-03-11 07:00:24 +00005332 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005333 // We have ellipsis without a preceding ',', which is ill-formed
5334 // in C. Complain and provide the fix.
5335 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005336 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005337 }
5338 }
Chad Rosierc1183952012-06-26 22:30:43 +00005339
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005340 break;
5341 }
Mike Stump11289f42009-09-09 15:08:12 +00005342
Chris Lattner371ed4e2008-04-06 06:57:35 +00005343 // Consume the comma.
5344 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005345 }
Mike Stump11289f42009-09-09 15:08:12 +00005346
Chris Lattner6c940e62008-04-06 06:34:08 +00005347}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005348
Chris Lattnere8074e62006-08-06 18:30:15 +00005349/// [C90] direct-declarator '[' constant-expression[opt] ']'
5350/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5351/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5352/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5353/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005354/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5355/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005356void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005357 if (CheckProhibitedCXX11Attribute())
5358 return;
5359
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005360 BalancedDelimiterTracker T(*this, tok::l_square);
5361 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005362
Chris Lattner84a11622008-12-18 07:27:21 +00005363 // C array syntax has many features, but by-far the most common is [] and [4].
5364 // This code does a fast path to handle some of the most obvious cases.
5365 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005366 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005367 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005368 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005369
Chris Lattner84a11622008-12-18 07:27:21 +00005370 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005371 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005372 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005373 T.getOpenLocation(),
5374 T.getCloseLocation()),
5375 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005376 return;
5377 } else if (Tok.getKind() == tok::numeric_constant &&
5378 GetLookAheadToken(1).is(tok::r_square)) {
5379 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005380 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005381 ConsumeToken();
5382
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005383 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005384 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005385 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005386
Chris Lattner84a11622008-12-18 07:27:21 +00005387 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005388 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005389 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005390 T.getOpenLocation(),
5391 T.getCloseLocation()),
5392 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005393 return;
5394 }
Mike Stump11289f42009-09-09 15:08:12 +00005395
Chris Lattnere8074e62006-08-06 18:30:15 +00005396 // If valid, this location is the position where we read the 'static' keyword.
5397 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005398 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005399 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005400
Chris Lattnere8074e62006-08-06 18:30:15 +00005401 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005402 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005403 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005404 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005405
Chris Lattnere8074e62006-08-06 18:30:15 +00005406 // If we haven't already read 'static', check to see if there is one after the
5407 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005408 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005409 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005410
Chris Lattnere8074e62006-08-06 18:30:15 +00005411 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005412 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005413 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005414
Chris Lattner521ff2b2008-04-06 05:26:30 +00005415 // Handle the case where we have '[*]' as the array size. However, a leading
5416 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005417 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005418 // infrequent, use of lookahead is not costly here.
5419 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005420 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005421
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005422 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005423 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005424 StaticLoc = SourceLocation(); // Drop the static.
5425 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005426 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005427 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005428 // Note, in C89, this production uses the constant-expr production instead
5429 // of assignment-expr. The only difference is that assignment-expr allows
5430 // things like '=' and '*='. Sema rejects these in C89 mode because they
5431 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005432
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005433 // Parse the constant-expression or assignment-expression now (depending
5434 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005435 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005436 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005437 } else {
5438 EnterExpressionEvaluationContext Unevaluated(Actions,
5439 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005440 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005441 }
Chris Lattner62591722006-08-12 18:40:58 +00005442 }
Mike Stump11289f42009-09-09 15:08:12 +00005443
Chris Lattner62591722006-08-12 18:40:58 +00005444 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005445 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005446 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005447 // If the expression was invalid, skip it.
5448 SkipUntil(tok::r_square);
5449 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005450 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005451
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005452 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005453
John McCall084e83d2011-03-24 11:26:52 +00005454 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005455 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005456
Chris Lattner84a11622008-12-18 07:27:21 +00005457 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005458 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005459 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005460 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005461 T.getOpenLocation(),
5462 T.getCloseLocation()),
5463 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005464}
5465
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005466/// [GNU] typeof-specifier:
5467/// typeof ( expressions )
5468/// typeof ( type-name )
5469/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005470///
5471void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005472 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005473 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005474 SourceLocation StartLoc = ConsumeToken();
5475
John McCalle8595032010-01-13 20:03:27 +00005476 const bool hasParens = Tok.is(tok::l_paren);
5477
Eli Friedman15681d62012-09-26 04:34:21 +00005478 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5479 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005480
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005481 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005482 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005483 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005484 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5485 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005486 if (hasParens)
5487 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005488
5489 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005490 // FIXME: Not accurate, the range gets one token more than it should.
5491 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005492 else
5493 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005494
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005495 if (isCastExpr) {
5496 if (!CastTy) {
5497 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005498 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005499 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005500
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005501 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005502 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005503 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5504 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005505 DiagID, CastTy))
5506 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005507 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005508 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005509
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005510 // If we get here, the operand to the typeof was an expresion.
5511 if (Operand.isInvalid()) {
5512 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005513 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005514 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005515
Eli Friedmane0afc982012-01-21 01:01:51 +00005516 // We might need to transform the operand if it is potentially evaluated.
5517 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5518 if (Operand.isInvalid()) {
5519 DS.SetTypeSpecError();
5520 return;
5521 }
5522
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005523 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005524 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005525 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5526 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005527 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005528 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005529}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005530
Benjamin Kramere56f3932011-12-23 17:00:35 +00005531/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005532/// _Atomic ( type-name )
5533///
5534void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005535 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5536 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005537
5538 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005539 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005540 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005541 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005542
5543 TypeResult Result = ParseTypeName();
5544 if (Result.isInvalid()) {
5545 SkipUntil(tok::r_paren);
5546 return;
5547 }
5548
5549 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005550 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005551
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005552 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005553 return;
5554
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005555 DS.setTypeofParensRange(T.getRange());
5556 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005557
5558 const char *PrevSpec = 0;
5559 unsigned DiagID;
5560 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5561 DiagID, Result.release()))
5562 Diag(StartLoc, DiagID) << PrevSpec;
5563}
5564
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005565
5566/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5567/// from TryAltiVecVectorToken.
5568bool Parser::TryAltiVecVectorTokenOutOfLine() {
5569 Token Next = NextToken();
5570 switch (Next.getKind()) {
5571 default: return false;
5572 case tok::kw_short:
5573 case tok::kw_long:
5574 case tok::kw_signed:
5575 case tok::kw_unsigned:
5576 case tok::kw_void:
5577 case tok::kw_char:
5578 case tok::kw_int:
5579 case tok::kw_float:
5580 case tok::kw_double:
5581 case tok::kw_bool:
5582 case tok::kw___pixel:
5583 Tok.setKind(tok::kw___vector);
5584 return true;
5585 case tok::identifier:
5586 if (Next.getIdentifierInfo() == Ident_pixel) {
5587 Tok.setKind(tok::kw___vector);
5588 return true;
5589 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005590 if (Next.getIdentifierInfo() == Ident_bool) {
5591 Tok.setKind(tok::kw___vector);
5592 return true;
5593 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005594 return false;
5595 }
5596}
5597
5598bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5599 const char *&PrevSpec, unsigned &DiagID,
5600 bool &isInvalid) {
5601 if (Tok.getIdentifierInfo() == Ident_vector) {
5602 Token Next = NextToken();
5603 switch (Next.getKind()) {
5604 case tok::kw_short:
5605 case tok::kw_long:
5606 case tok::kw_signed:
5607 case tok::kw_unsigned:
5608 case tok::kw_void:
5609 case tok::kw_char:
5610 case tok::kw_int:
5611 case tok::kw_float:
5612 case tok::kw_double:
5613 case tok::kw_bool:
5614 case tok::kw___pixel:
5615 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5616 return true;
5617 case tok::identifier:
5618 if (Next.getIdentifierInfo() == Ident_pixel) {
5619 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5620 return true;
5621 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005622 if (Next.getIdentifierInfo() == Ident_bool) {
5623 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5624 return true;
5625 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005626 break;
5627 default:
5628 break;
5629 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005630 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005631 DS.isTypeAltiVecVector()) {
5632 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5633 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005634 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5635 DS.isTypeAltiVecVector()) {
5636 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5637 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005638 }
5639 return false;
5640}