blob: 2f0c1a3b8da70186ab29ebde776a811ab7dd9872 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
Chris Lattnerf5fbd792006-08-10 23:56:11 +000033/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000040 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000046
Chris Lattnerf5fbd792006-08-10 23:56:11 +000047 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000054
Chris Lattnerf5fbd792006-08-10 23:56:11 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor0be31a22010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000065}
66
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Alexis Hunt96d5c762009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000091/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000095///
Steve Naroff0f2fe172007-06-01 17:11:19 +000096/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000101///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithb12bf692011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000113
John McCall53fa7142010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner76c72282007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000141
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148
Bill Wendling44426052012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000153
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han23214e52012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000164 }
165 } else {
John McCall084e83d2011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000171 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall53fa7142010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000178 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000180
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000181
Michael Han23214e52012-10-03 01:56:22 +0000182/// Parse the arguments to a parameterized GNU attribute or
183/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000184void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
185 SourceLocation AttrNameLoc,
186 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000187 SourceLocation *EndLoc,
188 IdentifierInfo *ScopeName,
189 SourceLocation ScopeLoc,
190 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000191
192 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
193
194 // Availability attributes have their own grammar.
195 if (AttrName->isStr("availability")) {
196 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
197 return;
198 }
199 // Thread safety attributes fit into the FIXME case above, so we
200 // just parse the arguments as a list of expressions
201 if (IsThreadSafetyAttribute(AttrName->getName())) {
202 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000205 // Type safety attributes have their own grammar.
206 if (AttrName->isStr("type_tag_for_datatype")) {
207 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
208 return;
209 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000210
211 ConsumeParen(); // ignore the left paren loc for now
212
Richard Smithb12bf692011-10-17 21:20:17 +0000213 IdentifierInfo *ParmName = 0;
214 SourceLocation ParmLoc;
215 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000216
Joey Goulyaba589c2013-03-08 09:42:32 +0000217 TypeResult T;
218 SourceRange TypeRange;
219 bool TypeParsed = false;
220
Richard Smithb12bf692011-10-17 21:20:17 +0000221 switch (Tok.getKind()) {
222 case tok::kw_char:
223 case tok::kw_wchar_t:
224 case tok::kw_char16_t:
225 case tok::kw_char32_t:
226 case tok::kw_bool:
227 case tok::kw_short:
228 case tok::kw_int:
229 case tok::kw_long:
230 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000231 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000232 case tok::kw_signed:
233 case tok::kw_unsigned:
234 case tok::kw_float:
235 case tok::kw_double:
236 case tok::kw_void:
237 case tok::kw_typeof:
238 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000239 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000240 T = ParseTypeName(&TypeRange);
241 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000242 break;
243
244 case tok::identifier:
Joey Goulyaba589c2013-03-08 09:42:32 +0000245 if (AttrName->isStr("vec_type_hint")) {
246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
248 break;
249 }
Richard Smithb12bf692011-10-17 21:20:17 +0000250 ParmName = Tok.getIdentifierInfo();
251 ParmLoc = ConsumeToken();
252 break;
253
254 default:
255 break;
256 }
257
Benjamin Kramerf0623432012-08-23 22:51:59 +0000258 ExprVector ArgExprs;
Joey Goulyaba589c2013-03-08 09:42:32 +0000259 bool isInvalid = false;
260 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000261
Joey Goulyaba589c2013-03-08 09:42:32 +0000262 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithb12bf692011-10-17 21:20:17 +0000263 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
264 // Eat the comma.
265 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000266 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000267
Richard Smithb12bf692011-10-17 21:20:17 +0000268 // Parse the non-empty comma-separated list of expressions.
269 while (1) {
270 ExprResult ArgExpr(ParseAssignmentExpression());
271 if (ArgExpr.isInvalid()) {
272 SkipUntil(tok::r_paren);
273 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000274 }
Richard Smithb12bf692011-10-17 21:20:17 +0000275 ArgExprs.push_back(ArgExpr.release());
276 if (Tok.isNot(tok::comma))
277 break;
278 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000279 }
Richard Smithb12bf692011-10-17 21:20:17 +0000280 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000281 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
282 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
283 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000284 while (Tok.is(tok::identifier)) {
285 ConsumeToken();
286 if (Tok.is(tok::greater))
287 break;
288 if (Tok.is(tok::comma)) {
289 ConsumeToken();
290 continue;
291 }
292 }
293 if (Tok.isNot(tok::greater))
294 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000295 SkipUntil(tok::r_paren, false, true); // skip until ')'
296 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000297 } else if (AttrName->isStr("vec_type_hint")) {
298 if (T.get() && !T.isInvalid())
299 isParmType = true;
300 else {
301 if (Tok.is(tok::identifier))
302 ConsumeToken();
303 if (TypeParsed)
304 isInvalid = true;
305 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000306 }
Richard Smithb12bf692011-10-17 21:20:17 +0000307
308 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000309 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
310 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000311 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000312 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000313 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
314 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
315 } else {
316 AttributeList *attr = Attrs.addNew(
317 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
318 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
319 if (BuiltinType &&
320 attr->getKind() == AttributeList::AT_IBOutletCollection)
321 Diag(Tok, diag::err_iboutletcollection_builtintype);
322 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000323 }
324}
325
Chad Rosierc1183952012-06-26 22:30:43 +0000326/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000327/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000328void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000329 SourceLocation AttrNameLoc,
330 ParsedAttributes &Attrs)
331{
332 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000333 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000334 AttrName->getNameStart(), tok::r_paren))
335 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000336
Aaron Ballman478faed2012-06-19 22:09:27 +0000337 ExprResult ArgExpr(ParseConstantExpression());
338 if (ArgExpr.isInvalid()) {
339 T.skipToEnd();
340 return;
341 }
342 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000343 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000344 &ExprList, 1, AttributeList::AS_Declspec);
345
346 T.consumeClose();
347}
348
Chad Rosierc1183952012-06-26 22:30:43 +0000349/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000350/// arguments.
351bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
352 return llvm::StringSwitch<bool>(Ident->getName())
353 .Case("dllimport", true)
354 .Case("dllexport", true)
355 .Case("noreturn", true)
356 .Case("nothrow", true)
357 .Case("noinline", true)
358 .Case("naked", true)
359 .Case("appdomain", true)
360 .Case("process", true)
361 .Case("jitintrinsic", true)
362 .Case("noalias", true)
363 .Case("restrict", true)
364 .Case("novtable", true)
365 .Case("selectany", true)
366 .Case("thread", true)
367 .Default(false);
368}
369
Chad Rosierc1183952012-06-26 22:30:43 +0000370/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000371/// parameters). Will return false if we properly handled the declspec, or
372/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000373void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 SourceLocation Loc,
375 ParsedAttributes &Attrs) {
376 // Try to handle the easy case first -- these declspecs all take a single
377 // parameter as their argument.
378 if (llvm::StringSwitch<bool>(Ident->getName())
379 .Case("uuid", true)
380 .Case("align", true)
381 .Case("allocate", true)
382 .Default(false)) {
383 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
384 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000385 // The deprecated declspec has an optional single argument, so we will
386 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000387 // not.
388 if (Tok.getKind() == tok::l_paren)
389 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
390 else
Chad Rosierc1183952012-06-26 22:30:43 +0000391 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000392 AttributeList::AS_Declspec);
393 } else if (Ident->getName() == "property") {
394 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000395 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000396 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000397 if (Tok.isNot(tok::l_paren)) {
398 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
399 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000400 return;
John McCall5e77d762013-04-16 07:28:30 +0000401 }
402 BalancedDelimiterTracker T(*this, tok::l_paren);
403 T.expectAndConsume(diag::err_expected_lparen_after,
404 Ident->getNameStart(), tok::r_paren);
405
406 enum AccessorKind {
407 AK_Invalid = -1,
408 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
409 };
410 IdentifierInfo *AccessorNames[] = { 0, 0 };
411 bool HasInvalidAccessor = false;
412
413 // Parse the accessor specifications.
414 while (true) {
415 // Stop if this doesn't look like an accessor spec.
416 if (!Tok.is(tok::identifier)) {
417 // If the user wrote a completely empty list, use a special diagnostic.
418 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
419 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
420 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
421 break;
422 }
423
424 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
425 break;
426 }
427
428 AccessorKind Kind;
429 SourceLocation KindLoc = Tok.getLocation();
430 StringRef KindStr = Tok.getIdentifierInfo()->getName();
431 if (KindStr == "get") {
432 Kind = AK_Get;
433 } else if (KindStr == "put") {
434 Kind = AK_Put;
435
436 // Recover from the common mistake of using 'set' instead of 'put'.
437 } else if (KindStr == "set") {
438 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
439 << FixItHint::CreateReplacement(KindLoc, "put");
440 Kind = AK_Put;
441
442 // Handle the mistake of forgetting the accessor kind by skipping
443 // this accessor.
444 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
445 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
446 ConsumeToken();
447 HasInvalidAccessor = true;
448 goto next_property_accessor;
449
450 // Otherwise, complain about the unknown accessor kind.
451 } else {
452 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
453 HasInvalidAccessor = true;
454 Kind = AK_Invalid;
455
456 // Try to keep parsing unless it doesn't look like an accessor spec.
457 if (!NextToken().is(tok::equal)) break;
458 }
459
460 // Consume the identifier.
461 ConsumeToken();
462
463 // Consume the '='.
464 if (Tok.is(tok::equal)) {
465 ConsumeToken();
466 } else {
467 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
468 << KindStr;
469 break;
470 }
471
472 // Expect the method name.
473 if (!Tok.is(tok::identifier)) {
474 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
475 break;
476 }
477
478 if (Kind == AK_Invalid) {
479 // Just drop invalid accessors.
480 } else if (AccessorNames[Kind] != NULL) {
481 // Complain about the repeated accessor, ignore it, and keep parsing.
482 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
483 } else {
484 AccessorNames[Kind] = Tok.getIdentifierInfo();
485 }
486 ConsumeToken();
487
488 next_property_accessor:
489 // Keep processing accessors until we run out.
490 if (Tok.is(tok::comma)) {
491 ConsumeAnyToken();
492 continue;
493
494 // If we run into the ')', stop without consuming it.
495 } else if (Tok.is(tok::r_paren)) {
496 break;
497 } else {
498 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
499 break;
500 }
501 }
502
503 // Only add the property attribute if it was well-formed.
504 if (!HasInvalidAccessor) {
505 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
506 SourceLocation(),
507 AccessorNames[AK_Get], AccessorNames[AK_Put],
508 AttributeList::AS_Declspec);
509 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000510 T.skipToEnd();
511 } else {
512 // We don't recognize this as a valid declspec, but instead of creating the
513 // attribute and allowing sema to warn about it, we will warn here instead.
514 // This is because some attributes have multiple spellings, but we need to
515 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000516 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000517 // both locations.
518 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
519
520 // If there's an open paren, we should eat the open and close parens under
521 // the assumption that this unknown declspec has parameters.
522 BalancedDelimiterTracker T(*this, tok::l_paren);
523 if (!T.consumeOpen())
524 T.skipToEnd();
525 }
526}
527
Eli Friedman06de2b52009-06-08 07:21:15 +0000528/// [MS] decl-specifier:
529/// __declspec ( extended-decl-modifier-seq )
530///
531/// [MS] extended-decl-modifier-seq:
532/// extended-decl-modifier[opt]
533/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000534void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000535 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000536
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000537 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000539 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000540 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000541 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000542
Chad Rosierc1183952012-06-26 22:30:43 +0000543 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000544 // you can specify multiple attributes per declspec.
545 while (Tok.getKind() != tok::r_paren) {
546 // We expect either a well-known identifier or a generic string. Anything
547 // else is a malformed declspec.
548 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000549 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 Tok.getKind() != tok::kw_restrict) {
551 Diag(Tok, diag::err_ms_declspec_type);
552 T.skipToEnd();
553 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000554 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000555
556 IdentifierInfo *AttrName;
557 SourceLocation AttrNameLoc;
558 if (IsString) {
559 SmallString<8> StrBuffer;
560 bool Invalid = false;
561 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
562 if (Invalid) {
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000565 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000566 AttrName = PP.getIdentifierInfo(Str);
567 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000568 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000569 AttrName = Tok.getIdentifierInfo();
570 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000571 }
Chad Rosierc1183952012-06-26 22:30:43 +0000572
Aaron Ballman478faed2012-06-19 22:09:27 +0000573 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000574 // If we have a generic string, we will allow it because there is no
575 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 // (for instance, SAL declspecs in older versions of MSVC).
577 //
Chad Rosierc1183952012-06-26 22:30:43 +0000578 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000580 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000581 0, 0, AttributeList::AS_Declspec);
582 else
583 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000584 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000586}
587
John McCall53fa7142010-12-24 02:08:15 +0000588void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000589 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000590 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000591 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000592 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000593 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000594 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
595 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000596 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000597 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000598 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000599}
600
John McCall53fa7142010-12-24 02:08:15 +0000601void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000602 // Treat these like attributes
603 while (Tok.is(tok::kw___pascal)) {
604 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
605 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000606 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000607 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000608 }
John McCall53fa7142010-12-24 02:08:15 +0000609}
610
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000611void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
612 // Treat these like attributes
613 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000614 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000615 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000616 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
617 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000618 }
619}
620
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000621void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000622 // FIXME: The mapping from attribute spelling to semantics should be
623 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000624 SourceLocation Loc = Tok.getLocation();
625 switch(Tok.getKind()) {
626 // OpenCL qualifiers:
627 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000628 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000629 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000630 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000631 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000632 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000633
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000634 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000635 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000636 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000637 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000638 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000639
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000640 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000641 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000642 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000643 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000644 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000645
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000646 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000648 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000650 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000651
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000652 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000653 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000654 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000655 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000656 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000657
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000658 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000659 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000660 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000661 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000662 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000663
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000664 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000666 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 break;
669 default: break;
670 }
671}
672
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000673/// \brief Parse a version number.
674///
675/// version:
676/// simple-integer
677/// simple-integer ',' simple-integer
678/// simple-integer ',' simple-integer ',' simple-integer
679VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
680 Range = Tok.getLocation();
681
682 if (!Tok.is(tok::numeric_constant)) {
683 Diag(Tok, diag::err_expected_version);
684 SkipUntil(tok::comma, tok::r_paren, true, true, true);
685 return VersionTuple();
686 }
687
688 // Parse the major (and possibly minor and subminor) versions, which
689 // are stored in the numeric constant. We utilize a quirk of the
690 // lexer, which is that it handles something like 1.2.3 as a single
691 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000692 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000693 Buffer.resize(Tok.getLength()+1);
694 const char *ThisTokBegin = &Buffer[0];
695
696 // Get the spelling of the token, which eliminates trigraphs, etc.
697 bool Invalid = false;
698 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
699 if (Invalid)
700 return VersionTuple();
701
702 // Parse the major version.
703 unsigned AfterMajor = 0;
704 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000705 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000706 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
707 ++AfterMajor;
708 }
709
710 if (AfterMajor == 0) {
711 Diag(Tok, diag::err_expected_version);
712 SkipUntil(tok::comma, tok::r_paren, true, true, true);
713 return VersionTuple();
714 }
715
716 if (AfterMajor == ActualLength) {
717 ConsumeToken();
718
719 // We only had a single version component.
720 if (Major == 0) {
721 Diag(Tok, diag::err_zero_version);
722 return VersionTuple();
723 }
724
725 return VersionTuple(Major);
726 }
727
728 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
729 Diag(Tok, diag::err_expected_version);
730 SkipUntil(tok::comma, tok::r_paren, true, true, true);
731 return VersionTuple();
732 }
733
734 // Parse the minor version.
735 unsigned AfterMinor = AfterMajor + 1;
736 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000737 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000738 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
739 ++AfterMinor;
740 }
741
742 if (AfterMinor == ActualLength) {
743 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000744
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000745 // We had major.minor.
746 if (Major == 0 && Minor == 0) {
747 Diag(Tok, diag::err_zero_version);
748 return VersionTuple();
749 }
750
Chad Rosierc1183952012-06-26 22:30:43 +0000751 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000752 }
753
754 // If what follows is not a '.', we have a problem.
755 if (ThisTokBegin[AfterMinor] != '.') {
756 Diag(Tok, diag::err_expected_version);
757 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000758 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000759 }
760
761 // Parse the subminor version.
762 unsigned AfterSubminor = AfterMinor + 1;
763 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000764 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
766 ++AfterSubminor;
767 }
768
769 if (AfterSubminor != ActualLength) {
770 Diag(Tok, diag::err_expected_version);
771 SkipUntil(tok::comma, tok::r_paren, true, true, true);
772 return VersionTuple();
773 }
774 ConsumeToken();
775 return VersionTuple(Major, Minor, Subminor);
776}
777
778/// \brief Parse the contents of the "availability" attribute.
779///
780/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000781/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000782///
783/// platform:
784/// identifier
785///
786/// version-arg-list:
787/// version-arg
788/// version-arg ',' version-arg-list
789///
790/// version-arg:
791/// 'introduced' '=' version
792/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000793/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000794/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000795/// opt-message:
796/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000797void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
798 SourceLocation AvailabilityLoc,
799 ParsedAttributes &attrs,
800 SourceLocation *endLoc) {
801 SourceLocation PlatformLoc;
802 IdentifierInfo *Platform = 0;
803
804 enum { Introduced, Deprecated, Obsoleted, Unknown };
805 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000806 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000807
808 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000809 BalancedDelimiterTracker T(*this, tok::l_paren);
810 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000811 Diag(Tok, diag::err_expected_lparen);
812 return;
813 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000814
815 // Parse the platform name,
816 if (Tok.isNot(tok::identifier)) {
817 Diag(Tok, diag::err_availability_expected_platform);
818 SkipUntil(tok::r_paren);
819 return;
820 }
821 Platform = Tok.getIdentifierInfo();
822 PlatformLoc = ConsumeToken();
823
824 // Parse the ',' following the platform name.
825 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
826 return;
827
828 // If we haven't grabbed the pointers for the identifiers
829 // "introduced", "deprecated", and "obsoleted", do so now.
830 if (!Ident_introduced) {
831 Ident_introduced = PP.getIdentifierInfo("introduced");
832 Ident_deprecated = PP.getIdentifierInfo("deprecated");
833 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000834 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000835 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000836 }
837
838 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000839 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000840 do {
841 if (Tok.isNot(tok::identifier)) {
842 Diag(Tok, diag::err_availability_expected_change);
843 SkipUntil(tok::r_paren);
844 return;
845 }
846 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
847 SourceLocation KeywordLoc = ConsumeToken();
848
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000849 if (Keyword == Ident_unavailable) {
850 if (UnavailableLoc.isValid()) {
851 Diag(KeywordLoc, diag::err_availability_redundant)
852 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000853 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000854 UnavailableLoc = KeywordLoc;
855
856 if (Tok.isNot(tok::comma))
857 break;
858
859 ConsumeToken();
860 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000861 }
862
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000863 if (Tok.isNot(tok::equal)) {
864 Diag(Tok, diag::err_expected_equal_after)
865 << Keyword;
866 SkipUntil(tok::r_paren);
867 return;
868 }
869 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000870 if (Keyword == Ident_message) {
871 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000872 Diag(Tok, diag::err_expected_string_literal)
873 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000874 SkipUntil(tok::r_paren);
875 return;
876 }
877 MessageExpr = ParseStringLiteralExpression();
878 break;
879 }
Chad Rosierc1183952012-06-26 22:30:43 +0000880
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000881 SourceRange VersionRange;
882 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000883
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000884 if (Version.empty()) {
885 SkipUntil(tok::r_paren);
886 return;
887 }
888
889 unsigned Index;
890 if (Keyword == Ident_introduced)
891 Index = Introduced;
892 else if (Keyword == Ident_deprecated)
893 Index = Deprecated;
894 else if (Keyword == Ident_obsoleted)
895 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000896 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000897 Index = Unknown;
898
899 if (Index < Unknown) {
900 if (!Changes[Index].KeywordLoc.isInvalid()) {
901 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000902 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000903 << SourceRange(Changes[Index].KeywordLoc,
904 Changes[Index].VersionRange.getEnd());
905 }
906
907 Changes[Index].KeywordLoc = KeywordLoc;
908 Changes[Index].Version = Version;
909 Changes[Index].VersionRange = VersionRange;
910 } else {
911 Diag(KeywordLoc, diag::err_availability_unknown_change)
912 << Keyword << VersionRange;
913 }
914
915 if (Tok.isNot(tok::comma))
916 break;
917
918 ConsumeToken();
919 } while (true);
920
921 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000922 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000923 return;
924
925 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000926 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000927
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000928 // The 'unavailable' availability cannot be combined with any other
929 // availability changes. Make sure that hasn't happened.
930 if (UnavailableLoc.isValid()) {
931 bool Complained = false;
932 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
933 if (Changes[Index].KeywordLoc.isValid()) {
934 if (!Complained) {
935 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
936 << SourceRange(Changes[Index].KeywordLoc,
937 Changes[Index].VersionRange.getEnd());
938 Complained = true;
939 }
940
941 // Clear out the availability.
942 Changes[Index] = AvailabilityChange();
943 }
944 }
945 }
946
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000947 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000948 attrs.addNew(&Availability,
949 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000950 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000951 Platform, PlatformLoc,
952 Changes[Introduced],
953 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000954 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000955 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000956 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000957}
958
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000959
Bill Wendling44426052012-12-20 19:22:21 +0000960// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000961// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
962
963void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
964
965void Parser::LateParsedClass::ParseLexedAttributes() {
966 Self->ParseLexedAttributes(*Class);
967}
968
969void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000970 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000971}
972
973/// Wrapper class which calls ParseLexedAttribute, after setting up the
974/// scope appropriately.
975void Parser::ParseLexedAttributes(ParsingClass &Class) {
976 // Deal with templates
977 // FIXME: Test cases to make sure this does the right thing for templates.
978 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
979 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
980 HasTemplateScope);
981 if (HasTemplateScope)
982 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
983
Douglas Gregor3024f072012-04-16 07:05:22 +0000984 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000985 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000986 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000987 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
988 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
989
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000990 // Enter the scope of nested classes
991 if (!AlreadyHasClassScope)
992 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
993 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000994 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000995 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
996 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
997 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000998 }
Chad Rosierc1183952012-06-26 22:30:43 +0000999
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001000 if (!AlreadyHasClassScope)
1001 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1002 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001003}
1004
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001005
1006/// \brief Parse all attributes in LAs, and attach them to Decl D.
1007void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1008 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001009 assert(LAs.parseSoon() &&
1010 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001011 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001012 if (D)
1013 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001014 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001015 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001016 }
1017 LAs.clear();
1018}
1019
1020
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001021/// \brief Finish parsing an attribute for which parsing was delayed.
1022/// This will be called at the end of parsing a class declaration
1023/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001024/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001025/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001026void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1027 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028 // Save the current token position.
1029 SourceLocation OrigLoc = Tok.getLocation();
1030
1031 // Append the current token at the end of the new token stream so that it
1032 // doesn't get lost.
1033 LA.Toks.push_back(Tok);
1034 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1035 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001036 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001037
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001038 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001039 // FIXME: Do not warn on C++11 attributes, once we start supporting
1040 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001041 Diag(Tok, diag::warn_attribute_on_function_definition)
1042 << LA.AttrName.getName();
1043 }
1044
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001045 ParsedAttributes Attrs(AttrFactory);
1046 SourceLocation endLoc;
1047
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001048 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001049 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001050 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1051 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001052
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001053 // Allow 'this' within late-parsed attributes.
1054 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
1055 /*TypeQuals=*/0,
1056 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001057
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001058 if (LA.Decls.size() == 1) {
1059 // If the Decl is templatized, add template parameters to scope.
1060 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1061 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1062 if (HasTemplateScope)
1063 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001064
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001065 // If the Decl is on a function, add function parameters to the scope.
1066 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1067 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1068 if (HasFunScope)
1069 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001070
Michael Han23214e52012-10-03 01:56:22 +00001071 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001072 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001073
1074 if (HasFunScope) {
1075 Actions.ActOnExitFunctionContext();
1076 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1077 }
1078 if (HasTemplateScope) {
1079 TempScope.Exit();
1080 }
1081 } else {
1082 // If there are multiple decls, then the decl cannot be within the
1083 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001084 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001085 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001086 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001087 } else {
1088 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001089 }
1090
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001091 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1092 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1093 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001094
1095 if (Tok.getLocation() != OrigLoc) {
1096 // Due to a parsing error, we either went over the cached tokens or
1097 // there are still cached tokens left, so we skip the leftover tokens.
1098 // Since this is an uncommon situation that should be avoided, use the
1099 // expensive isBeforeInTranslationUnit call.
1100 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1101 OrigLoc))
1102 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001103 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001104 }
1105}
1106
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001107/// \brief Wrapper around a case statement checking if AttrName is
1108/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001109bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001110 return llvm::StringSwitch<bool>(AttrName)
1111 .Case("guarded_by", true)
1112 .Case("guarded_var", true)
1113 .Case("pt_guarded_by", true)
1114 .Case("pt_guarded_var", true)
1115 .Case("lockable", true)
1116 .Case("scoped_lockable", true)
1117 .Case("no_thread_safety_analysis", true)
1118 .Case("acquired_after", true)
1119 .Case("acquired_before", true)
1120 .Case("exclusive_lock_function", true)
1121 .Case("shared_lock_function", true)
1122 .Case("exclusive_trylock_function", true)
1123 .Case("shared_trylock_function", true)
1124 .Case("unlock_function", true)
1125 .Case("lock_returned", true)
1126 .Case("locks_excluded", true)
1127 .Case("exclusive_locks_required", true)
1128 .Case("shared_locks_required", true)
1129 .Default(false);
1130}
1131
1132/// \brief Parse the contents of thread safety attributes. These
1133/// should always be parsed as an expression list.
1134///
1135/// We need to special case the parsing due to the fact that if the first token
1136/// of the first argument is an identifier, the main parse loop will store
1137/// that token as a "parameter" and the rest of
1138/// the arguments will be added to a list of "arguments". However,
1139/// subsequent tokens in the first argument are lost. We instead parse each
1140/// argument as an expression and add all arguments to the list of "arguments".
1141/// In future, we will take advantage of this special case to also
1142/// deal with some argument scoping issues here (for example, referring to a
1143/// function parameter in the attribute on that function).
1144void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1145 SourceLocation AttrNameLoc,
1146 ParsedAttributes &Attrs,
1147 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001148 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001149
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001150 BalancedDelimiterTracker T(*this, tok::l_paren);
1151 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001152
Benjamin Kramerf0623432012-08-23 22:51:59 +00001153 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001154 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001155
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001156 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001157 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001158 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001159 ExprResult ArgExpr(ParseAssignmentExpression());
1160 if (ArgExpr.isInvalid()) {
1161 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001162 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001163 break;
1164 } else {
1165 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001166 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001167 if (Tok.isNot(tok::comma))
1168 break;
1169 ConsumeToken(); // Eat the comma, move to the next argument
1170 }
1171 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001172 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001173 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001174 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001175 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001176 if (EndLoc)
1177 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001178}
1179
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001180void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1181 SourceLocation AttrNameLoc,
1182 ParsedAttributes &Attrs,
1183 SourceLocation *EndLoc) {
1184 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1185
1186 BalancedDelimiterTracker T(*this, tok::l_paren);
1187 T.consumeOpen();
1188
1189 if (Tok.isNot(tok::identifier)) {
1190 Diag(Tok, diag::err_expected_ident);
1191 T.skipToEnd();
1192 return;
1193 }
1194 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1195 SourceLocation ArgumentKindLoc = ConsumeToken();
1196
1197 if (Tok.isNot(tok::comma)) {
1198 Diag(Tok, diag::err_expected_comma);
1199 T.skipToEnd();
1200 return;
1201 }
1202 ConsumeToken();
1203
1204 SourceRange MatchingCTypeRange;
1205 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1206 if (MatchingCType.isInvalid()) {
1207 T.skipToEnd();
1208 return;
1209 }
1210
1211 bool LayoutCompatible = false;
1212 bool MustBeNull = false;
1213 while (Tok.is(tok::comma)) {
1214 ConsumeToken();
1215 if (Tok.isNot(tok::identifier)) {
1216 Diag(Tok, diag::err_expected_ident);
1217 T.skipToEnd();
1218 return;
1219 }
1220 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1221 if (Flag->isStr("layout_compatible"))
1222 LayoutCompatible = true;
1223 else if (Flag->isStr("must_be_null"))
1224 MustBeNull = true;
1225 else {
1226 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1227 T.skipToEnd();
1228 return;
1229 }
1230 ConsumeToken(); // consume flag
1231 }
1232
1233 if (!T.consumeClose()) {
1234 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1235 ArgumentKind, ArgumentKindLoc,
1236 MatchingCType.release(), LayoutCompatible,
1237 MustBeNull, AttributeList::AS_GNU);
1238 }
1239
1240 if (EndLoc)
1241 *EndLoc = T.getCloseLocation();
1242}
1243
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001244/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1245/// of a C++11 attribute-specifier in a location where an attribute is not
1246/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1247/// situation.
1248///
1249/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1250/// this doesn't appear to actually be an attribute-specifier, and the caller
1251/// should try to parse it.
1252bool Parser::DiagnoseProhibitedCXX11Attribute() {
1253 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1254
1255 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1256 case CAK_NotAttributeSpecifier:
1257 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1258 return false;
1259
1260 case CAK_InvalidAttributeSpecifier:
1261 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1262 return false;
1263
1264 case CAK_AttributeSpecifier:
1265 // Parse and discard the attributes.
1266 SourceLocation BeginLoc = ConsumeBracket();
1267 ConsumeBracket();
1268 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1269 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1270 SourceLocation EndLoc = ConsumeBracket();
1271 Diag(BeginLoc, diag::err_attributes_not_allowed)
1272 << SourceRange(BeginLoc, EndLoc);
1273 return true;
1274 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001275 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001276}
1277
Richard Smith98155ad2013-02-20 01:17:14 +00001278/// \brief We have found the opening square brackets of a C++11
1279/// attribute-specifier in a location where an attribute is not permitted, but
1280/// we know where the attributes ought to be written. Parse them anyway, and
1281/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001282void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1283 SourceLocation CorrectLocation) {
1284 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1285 Tok.is(tok::kw_alignas));
1286
1287 // Consume the attributes.
1288 SourceLocation Loc = Tok.getLocation();
1289 ParseCXX11Attributes(Attrs);
1290 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1291
1292 Diag(Loc, diag::err_attributes_not_allowed)
1293 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1294 << FixItHint::CreateRemoval(AttrRange);
1295}
1296
John McCall53fa7142010-12-24 02:08:15 +00001297void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1298 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1299 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001300}
1301
Michael Han64536a62012-11-06 19:34:54 +00001302void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1303 AttributeList *AttrList = attrs.getList();
1304 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001305 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001306 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001307 << AttrList->getName();
1308 AttrList->setInvalid();
1309 }
1310 AttrList = AttrList->getNext();
1311 }
1312}
1313
Chris Lattner53361ac2006-08-10 05:19:57 +00001314/// ParseDeclaration - Parse a full 'declaration', which consists of
1315/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001316/// 'Context' should be a Declarator::TheContext value. This returns the
1317/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001318///
1319/// declaration: [C99 6.7]
1320/// block-declaration ->
1321/// simple-declaration
1322/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001323/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001324/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001325/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001326/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001327/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001328/// others... [FIXME]
1329///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001330Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1331 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001332 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001333 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001334 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001335 // Must temporarily exit the objective-c container scope for
1336 // parsing c none objective-c decls.
1337 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001338
John McCall48871652010-08-21 09:40:31 +00001339 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001340 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001341 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001342 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001343 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001344 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001345 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001346 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001347 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001348 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001349 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001351 SourceLocation InlineLoc = ConsumeToken();
1352 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1353 break;
1354 }
Chad Rosierc1183952012-06-26 22:30:43 +00001355 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001356 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001357 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001358 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001359 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001360 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001361 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001362 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001363 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001364 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001365 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001366 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001367 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001369 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001370 default:
John McCall53fa7142010-12-24 02:08:15 +00001371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001372 }
Chad Rosierc1183952012-06-26 22:30:43 +00001373
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001374 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001375 // single decl, convert it now. Alias declarations can also declare a type;
1376 // include that too if it is present.
1377 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001378}
1379
1380/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1381/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001382/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1383/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001384///[C90/C++]init-declarator-list ';' [TODO]
1385/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001386///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001387/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001388/// attribute-specifier-seq[opt] type-specifier-seq declarator
1389///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001390/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001391/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001392///
1393/// If FRI is non-null, we might be parsing a for-range-declaration instead
1394/// of a simple-declaration. If we find that we are, we also parse the
1395/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1398 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001399 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001400 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001401 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001402 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001403
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001404 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001405 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001406
Chris Lattner0e894622006-08-13 19:58:17 +00001407 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1408 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001409 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001410 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001411 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001412 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001413 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001414 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001415 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001416 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001417 }
Chad Rosierc1183952012-06-26 22:30:43 +00001418
Richard Smith2386c8b2013-02-22 09:06:26 +00001419 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001420 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001421}
Mike Stump11289f42009-09-09 15:08:12 +00001422
Richard Smith09f76ee2011-10-19 21:33:05 +00001423/// Returns true if this might be the start of a declarator, or a common typo
1424/// for a declarator.
1425bool Parser::MightBeDeclarator(unsigned Context) {
1426 switch (Tok.getKind()) {
1427 case tok::annot_cxxscope:
1428 case tok::annot_template_id:
1429 case tok::caret:
1430 case tok::code_completion:
1431 case tok::coloncolon:
1432 case tok::ellipsis:
1433 case tok::kw___attribute:
1434 case tok::kw_operator:
1435 case tok::l_paren:
1436 case tok::star:
1437 return true;
1438
1439 case tok::amp:
1440 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001441 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001442
Richard Smithc8a79032012-01-09 22:31:44 +00001443 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001444 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001445 NextToken().is(tok::l_square);
1446
1447 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001448 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001449
Richard Smith09f76ee2011-10-19 21:33:05 +00001450 case tok::identifier:
1451 switch (NextToken().getKind()) {
1452 case tok::code_completion:
1453 case tok::coloncolon:
1454 case tok::comma:
1455 case tok::equal:
1456 case tok::equalequal: // Might be a typo for '='.
1457 case tok::kw_alignas:
1458 case tok::kw_asm:
1459 case tok::kw___attribute:
1460 case tok::l_brace:
1461 case tok::l_paren:
1462 case tok::l_square:
1463 case tok::less:
1464 case tok::r_brace:
1465 case tok::r_paren:
1466 case tok::r_square:
1467 case tok::semi:
1468 return true;
1469
1470 case tok::colon:
1471 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001472 // and in block scope it's probably a label. Inside a class definition,
1473 // this is a bit-field.
1474 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001475 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001476
1477 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001478 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001479
1480 default:
1481 return false;
1482 }
1483
1484 default:
1485 return false;
1486 }
1487}
1488
Richard Smithb8caac82012-04-11 20:59:20 +00001489/// Skip until we reach something which seems like a sensible place to pick
1490/// up parsing after a malformed declaration. This will sometimes stop sooner
1491/// than SkipUntil(tok::r_brace) would, but will never stop later.
1492void Parser::SkipMalformedDecl() {
1493 while (true) {
1494 switch (Tok.getKind()) {
1495 case tok::l_brace:
1496 // Skip until matching }, then stop. We've probably skipped over
1497 // a malformed class or function definition or similar.
1498 ConsumeBrace();
1499 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1500 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1501 // This declaration isn't over yet. Keep skipping.
1502 continue;
1503 }
1504 if (Tok.is(tok::semi))
1505 ConsumeToken();
1506 return;
1507
1508 case tok::l_square:
1509 ConsumeBracket();
1510 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1511 continue;
1512
1513 case tok::l_paren:
1514 ConsumeParen();
1515 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1516 continue;
1517
1518 case tok::r_brace:
1519 return;
1520
1521 case tok::semi:
1522 ConsumeToken();
1523 return;
1524
1525 case tok::kw_inline:
1526 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001527 // a good place to pick back up parsing, except in an Objective-C
1528 // @interface context.
1529 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1530 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001531 return;
1532 break;
1533
1534 case tok::kw_namespace:
1535 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001536 // place to pick back up parsing, except in an Objective-C
1537 // @interface context.
1538 if (Tok.isAtStartOfLine() &&
1539 (!ParsingInObjCContainer || CurParsedObjCImpl))
1540 return;
1541 break;
1542
1543 case tok::at:
1544 // @end is very much like } in Objective-C contexts.
1545 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1546 ParsingInObjCContainer)
1547 return;
1548 break;
1549
1550 case tok::minus:
1551 case tok::plus:
1552 // - and + probably start new method declarations in Objective-C contexts.
1553 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001554 return;
1555 break;
1556
1557 case tok::eof:
1558 return;
1559
1560 default:
1561 break;
1562 }
1563
1564 ConsumeAnyToken();
1565 }
1566}
1567
John McCalld5a36322009-11-03 19:26:08 +00001568/// ParseDeclGroup - Having concluded that this is either a function
1569/// definition or a group of object declarations, actually parse the
1570/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001571Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1572 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001573 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001574 SourceLocation *DeclEnd,
1575 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001576 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001577 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001578 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001579
John McCalld5a36322009-11-03 19:26:08 +00001580 // Bail out if the first declarator didn't seem well-formed.
1581 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001582 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001583 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001584 }
Mike Stump11289f42009-09-09 15:08:12 +00001585
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001586 // Save late-parsed attributes for now; they need to be parsed in the
1587 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001588 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1589 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001590 if (D.isFunctionDeclarator())
1591 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1592
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001593 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001594 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001595 // Look at the next token to make sure that this isn't a function
1596 // declaration. We have to check this because __attribute__ might be the
1597 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001598 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001599
Douglas Gregor012efe22013-04-16 16:01:32 +00001600 if (AllowFunctionDefinitions) {
1601 if (isStartOfFunctionDefinition(D)) {
1602 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1603 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001604
Douglas Gregor012efe22013-04-16 16:01:32 +00001605 // Recover by treating the 'typedef' as spurious.
1606 DS.ClearStorageClassSpecs();
1607 }
1608
1609 Decl *TheDecl =
1610 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1611 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001612 }
1613
Douglas Gregor012efe22013-04-16 16:01:32 +00001614 if (isDeclarationSpecifier()) {
1615 // If there is an invalid declaration specifier right after the function
1616 // prototype, then we must be in a missing semicolon case where this isn't
1617 // actually a body. Just fall through into the code that handles it as a
1618 // prototype, and let the top-level code handle the erroneous declspec
1619 // where it would otherwise expect a comma or semicolon.
1620 } else {
1621 Diag(Tok, diag::err_expected_fn_body);
1622 SkipUntil(tok::semi);
1623 return DeclGroupPtrTy();
1624 }
John McCalld5a36322009-11-03 19:26:08 +00001625 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001626 if (Tok.is(tok::l_brace)) {
1627 Diag(Tok, diag::err_function_definition_not_allowed);
1628 SkipUntil(tok::r_brace, true, true);
1629 }
John McCalld5a36322009-11-03 19:26:08 +00001630 }
1631 }
1632
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001633 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001634 return DeclGroupPtrTy();
1635
1636 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1637 // must parse and analyze the for-range-initializer before the declaration is
1638 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001639 //
1640 // Handle the Objective-C for-in loop variable similarly, although we
1641 // don't need to parse the container in advance.
1642 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1643 bool IsForRangeLoop = false;
1644 if (Tok.is(tok::colon)) {
1645 IsForRangeLoop = true;
1646 FRI->ColonLoc = ConsumeToken();
1647 if (Tok.is(tok::l_brace))
1648 FRI->RangeExpr = ParseBraceInitializer();
1649 else
1650 FRI->RangeExpr = ParseExpression();
1651 }
1652
Richard Smith02e85f32011-04-14 22:09:26 +00001653 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001654 if (IsForRangeLoop)
1655 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001656 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001657 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001658 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1659 }
1660
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001661 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001662 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001663 if (LateParsedAttrs.size() > 0)
1664 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001665 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001666 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001667 DeclsInGroup.push_back(FirstDecl);
1668
Richard Smith09f76ee2011-10-19 21:33:05 +00001669 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001670
John McCalld5a36322009-11-03 19:26:08 +00001671 // If we don't have a comma, it is either the end of the list (a ';') or an
1672 // error, bail out.
1673 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001674 SourceLocation CommaLoc = ConsumeToken();
1675
1676 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1677 // This comma was followed by a line-break and something which can't be
1678 // the start of a declarator. The comma was probably a typo for a
1679 // semicolon.
1680 Diag(CommaLoc, diag::err_expected_semi_declaration)
1681 << FixItHint::CreateReplacement(CommaLoc, ";");
1682 ExpectSemi = false;
1683 break;
1684 }
John McCalld5a36322009-11-03 19:26:08 +00001685
1686 // Parse the next declarator.
1687 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001688 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001689
1690 // Accept attributes in an init-declarator. In the first declarator in a
1691 // declaration, these would be part of the declspec. In subsequent
1692 // declarators, they become part of the declarator itself, so that they
1693 // don't apply to declarators after *this* one. Examples:
1694 // short __attribute__((common)) var; -> declspec
1695 // short var __attribute__((common)); -> declarator
1696 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001697 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001698
1699 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001700 if (!D.isInvalidType()) {
1701 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1702 D.complete(ThisDecl);
1703 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001704 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001705 }
John McCalld5a36322009-11-03 19:26:08 +00001706 }
1707
1708 if (DeclEnd)
1709 *DeclEnd = Tok.getLocation();
1710
Richard Smith09f76ee2011-10-19 21:33:05 +00001711 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001712 ExpectAndConsumeSemi(Context == Declarator::FileContext
1713 ? diag::err_invalid_token_after_toplevel_declarator
1714 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001715 // Okay, there was no semicolon and one was expected. If we see a
1716 // declaration specifier, just assume it was missing and continue parsing.
1717 // Otherwise things are very confused and we skip to recover.
1718 if (!isDeclarationSpecifier()) {
1719 SkipUntil(tok::r_brace, true, true);
1720 if (Tok.is(tok::semi))
1721 ConsumeToken();
1722 }
John McCalld5a36322009-11-03 19:26:08 +00001723 }
1724
Douglas Gregor0be31a22010-07-02 17:43:08 +00001725 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001726 DeclsInGroup.data(),
1727 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001728}
1729
Richard Smith02e85f32011-04-14 22:09:26 +00001730/// Parse an optional simple-asm-expr and attributes, and attach them to a
1731/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001732bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001733 // If a simple-asm-expr is present, parse it.
1734 if (Tok.is(tok::kw_asm)) {
1735 SourceLocation Loc;
1736 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1737 if (AsmLabel.isInvalid()) {
1738 SkipUntil(tok::semi, true, true);
1739 return true;
1740 }
1741
1742 D.setAsmLabel(AsmLabel.release());
1743 D.SetRangeEnd(Loc);
1744 }
1745
1746 MaybeParseGNUAttributes(D);
1747 return false;
1748}
1749
Douglas Gregor23996282009-05-12 21:31:51 +00001750/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1751/// declarator'. This method parses the remainder of the declaration
1752/// (including any attributes or initializer, among other things) and
1753/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001754///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001755/// init-declarator: [C99 6.7]
1756/// declarator
1757/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001758/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1759/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001760/// [C++] declarator initializer[opt]
1761///
1762/// [C++] initializer:
1763/// [C++] '=' initializer-clause
1764/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001765/// [C++0x] '=' 'default' [TODO]
1766/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001767/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001768///
1769/// According to the standard grammar, =default and =delete are function
1770/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001771///
John McCall48871652010-08-21 09:40:31 +00001772Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001773 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001774 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001775 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001776
Richard Smith02e85f32011-04-14 22:09:26 +00001777 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1778}
Mike Stump11289f42009-09-09 15:08:12 +00001779
Richard Smith02e85f32011-04-14 22:09:26 +00001780Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1781 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001782 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001783 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001784 switch (TemplateInfo.Kind) {
1785 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001786 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001787 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001788
Douglas Gregor450f00842009-09-25 18:43:00 +00001789 case ParsedTemplateInfo::Template:
1790 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001791 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001792 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001793 D);
1794 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001795
Douglas Gregor450f00842009-09-25 18:43:00 +00001796 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001797 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001798 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001799 TemplateInfo.ExternLoc,
1800 TemplateInfo.TemplateLoc,
1801 D);
1802 if (ThisRes.isInvalid()) {
1803 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001804 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001805 }
Chad Rosierc1183952012-06-26 22:30:43 +00001806
Douglas Gregor450f00842009-09-25 18:43:00 +00001807 ThisDecl = ThisRes.get();
1808 break;
1809 }
1810 }
Mike Stump11289f42009-09-09 15:08:12 +00001811
Richard Smith74aeef52013-04-26 16:15:35 +00001812 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001813
Douglas Gregor23996282009-05-12 21:31:51 +00001814 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001815 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001816 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001817 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001818 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001819 if (D.isFunctionDeclarator())
1820 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1821 << 1 /* delete */;
1822 else
1823 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001824 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001825 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001826 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1827 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001828 else
1829 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001830 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001831 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001832 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001833 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001834 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001835
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001836 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001837 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001838 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001839 cutOffParsing();
1840 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001841 }
Chad Rosierc1183952012-06-26 22:30:43 +00001842
John McCalldadc5752010-08-24 06:29:42 +00001843 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001844
David Blaikiebbafb8a2012-03-11 07:00:24 +00001845 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001846 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001847 ExitScope();
1848 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001849
Douglas Gregor23996282009-05-12 21:31:51 +00001850 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001851 SkipUntil(tok::comma, true, true);
1852 Actions.ActOnInitializerError(ThisDecl);
1853 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001854 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1855 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001856 }
1857 } else if (Tok.is(tok::l_paren)) {
1858 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001859 BalancedDelimiterTracker T(*this, tok::l_paren);
1860 T.consumeOpen();
1861
Benjamin Kramerf0623432012-08-23 22:51:59 +00001862 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001863 CommaLocsTy CommaLocs;
1864
David Blaikiebbafb8a2012-03-11 07:00:24 +00001865 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001866 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001867 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001868 }
1869
Douglas Gregor23996282009-05-12 21:31:51 +00001870 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001871 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001872 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001873
David Blaikiebbafb8a2012-03-11 07:00:24 +00001874 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001875 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001876 ExitScope();
1877 }
Douglas Gregor23996282009-05-12 21:31:51 +00001878 } else {
1879 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001880 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001881
1882 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1883 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001884
David Blaikiebbafb8a2012-03-11 07:00:24 +00001885 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001886 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001887 ExitScope();
1888 }
1889
Sebastian Redla9351792012-02-11 23:51:47 +00001890 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1891 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001892 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001893 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1894 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001895 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001896 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001897 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001898 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001899 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1900
Sebastian Redl3da34892011-06-05 12:23:16 +00001901 if (D.getCXXScopeSpec().isSet()) {
1902 EnterScope(0);
1903 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1904 }
1905
1906 ExprResult Init(ParseBraceInitializer());
1907
1908 if (D.getCXXScopeSpec().isSet()) {
1909 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1910 ExitScope();
1911 }
1912
1913 if (Init.isInvalid()) {
1914 Actions.ActOnInitializerError(ThisDecl);
1915 } else
1916 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1917 /*DirectInit=*/true, TypeContainsAuto);
1918
Douglas Gregor23996282009-05-12 21:31:51 +00001919 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001920 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001921 }
1922
Richard Smithb2bc2e62011-02-21 20:05:19 +00001923 Actions.FinalizeDeclaration(ThisDecl);
1924
Douglas Gregor23996282009-05-12 21:31:51 +00001925 return ThisDecl;
1926}
1927
Chris Lattner1890ac82006-08-13 01:16:23 +00001928/// ParseSpecifierQualifierList
1929/// specifier-qualifier-list:
1930/// type-specifier specifier-qualifier-list[opt]
1931/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001932/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001933///
Richard Smithc5b05522012-03-12 07:56:15 +00001934void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1935 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001936 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1937 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001938 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001939 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001940
Chris Lattner1890ac82006-08-13 01:16:23 +00001941 // Validate declspec for type-name.
1942 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001943 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1944 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001945 Diag(Tok, diag::err_expected_type);
1946 DS.SetTypeSpecError();
1947 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1948 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001949 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001950 if (!DS.hasTypeSpecifier())
1951 DS.SetTypeSpecError();
1952 }
Mike Stump11289f42009-09-09 15:08:12 +00001953
Chris Lattner1b22eed2006-11-28 05:12:07 +00001954 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001955 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001956 if (DS.getStorageClassSpecLoc().isValid())
1957 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1958 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001959 Diag(DS.getThreadStorageClassSpecLoc(),
1960 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001961 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001962 }
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner1b22eed2006-11-28 05:12:07 +00001964 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001965 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001966 if (DS.isInlineSpecified())
1967 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1968 if (DS.isVirtualSpecified())
1969 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1970 if (DS.isExplicitSpecified())
1971 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001972 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001973 }
Richard Smithc5b05522012-03-12 07:56:15 +00001974
1975 // Issue diagnostic and remove constexpr specfier if present.
1976 if (DS.isConstexprSpecified()) {
1977 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1978 DS.ClearConstexprSpec();
1979 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001980}
Chris Lattner53361ac2006-08-10 05:19:57 +00001981
Chris Lattner6cc055a2009-04-12 20:42:31 +00001982/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1983/// specified token is valid after the identifier in a declarator which
1984/// immediately follows the declspec. For example, these things are valid:
1985///
1986/// int x [ 4]; // direct-declarator
1987/// int x ( int y); // direct-declarator
1988/// int(int x ) // direct-declarator
1989/// int x ; // simple-declaration
1990/// int x = 17; // init-declarator-list
1991/// int x , y; // init-declarator-list
1992/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001993/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001994/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001995///
1996/// This is not, because 'x' does not immediately follow the declspec (though
1997/// ')' happens to be valid anyway).
1998/// int (x)
1999///
2000static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2001 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2002 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002003 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002004}
2005
Chris Lattner20a0c612009-04-14 21:34:55 +00002006
2007/// ParseImplicitInt - This method is called when we have an non-typename
2008/// identifier in a declspec (which normally terminates the decl spec) when
2009/// the declspec has no type specifier. In this case, the declspec is either
2010/// malformed or is "implicit int" (in K&R and C89).
2011///
2012/// This method handles diagnosing this prettily and returns false if the
2013/// declspec is done being processed. If it recovers and thinks there may be
2014/// other pieces of declspec after it, it returns true.
2015///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002016bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002017 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002018 AccessSpecifier AS, DeclSpecContext DSC,
2019 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002020 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002021
Chris Lattner20a0c612009-04-14 21:34:55 +00002022 SourceLocation Loc = Tok.getLocation();
2023 // If we see an identifier that is not a type name, we normally would
2024 // parse it as the identifer being declared. However, when a typename
2025 // is typo'd or the definition is not included, this will incorrectly
2026 // parse the typename as the identifier name and fall over misparsing
2027 // later parts of the diagnostic.
2028 //
2029 // As such, we try to do some look-ahead in cases where this would
2030 // otherwise be an "implicit-int" case to see if this is invalid. For
2031 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2032 // an identifier with implicit int, we'd get a parse error because the
2033 // next token is obviously invalid for a type. Parse these as a case
2034 // with an invalid type specifier.
2035 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002036
Chris Lattner20a0c612009-04-14 21:34:55 +00002037 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002038 // error, do lookahead to try to do better recovery. This never applies
2039 // within a type specifier. Outside of C++, we allow this even if the
2040 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002041 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002042 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002043 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002044 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002045 // If this token is valid for implicit int, e.g. "static x = 4", then
2046 // we just avoid eating the identifier, so it will be parsed as the
2047 // identifier in the declarator.
2048 return false;
2049 }
Mike Stump11289f42009-09-09 15:08:12 +00002050
Richard Smitha952ebb2012-05-15 21:01:51 +00002051 if (getLangOpts().CPlusPlus &&
2052 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2053 // Don't require a type specifier if we have the 'auto' storage class
2054 // specifier in C++98 -- we'll promote it to a type specifier.
2055 return false;
2056 }
2057
Chris Lattner20a0c612009-04-14 21:34:55 +00002058 // Otherwise, if we don't consume this token, we are going to emit an
2059 // error anyway. Try to recover from various common problems. Check
2060 // to see if this was a reference to a tag name without a tag specified.
2061 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002062 //
2063 // C++ doesn't need this, and isTagName doesn't take SS.
2064 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002065 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002066 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002067
Douglas Gregor0be31a22010-07-02 17:43:08 +00002068 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002069 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002070 case DeclSpec::TST_enum:
2071 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2072 case DeclSpec::TST_union:
2073 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2074 case DeclSpec::TST_struct:
2075 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002076 case DeclSpec::TST_interface:
2077 TagName="__interface"; FixitTagName = "__interface ";
2078 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002079 case DeclSpec::TST_class:
2080 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002081 }
Mike Stump11289f42009-09-09 15:08:12 +00002082
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002083 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002084 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2085 LookupResult R(Actions, TokenName, SourceLocation(),
2086 Sema::LookupOrdinaryName);
2087
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002088 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002089 << TokenName << TagName << getLangOpts().CPlusPlus
2090 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2091
2092 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2093 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2094 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002095 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002096 << TokenName << TagName;
2097 }
Mike Stump11289f42009-09-09 15:08:12 +00002098
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002099 // Parse this as a tag as if the missing tag were present.
2100 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002101 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002102 else
Richard Smithc5b05522012-03-12 07:56:15 +00002103 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002104 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002105 return true;
2106 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002107 }
Mike Stump11289f42009-09-09 15:08:12 +00002108
Richard Smithfe904f02012-05-15 21:29:55 +00002109 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002110 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002111 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2112 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002113 // Look ahead to the next token to try to figure out what this declaration
2114 // was supposed to be.
2115 switch (NextToken().getKind()) {
2116 case tok::comma:
2117 case tok::equal:
2118 case tok::kw_asm:
2119 case tok::l_brace:
2120 case tok::l_square:
2121 case tok::semi:
2122 // This looks like a variable declaration. The type is probably missing.
2123 // We're done parsing decl-specifiers.
2124 return false;
2125
2126 case tok::l_paren: {
2127 // static x(4); // 'x' is not a type
2128 // x(int n); // 'x' is not a type
2129 // x (*p)[]; // 'x' is a type
2130 //
2131 // Since we're in an error case (or the rare 'implicit int in C++' MS
2132 // extension), we can afford to perform a tentative parse to determine
2133 // which case we're in.
2134 TentativeParsingAction PA(*this);
2135 ConsumeToken();
2136 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2137 PA.Revert();
2138 if (TPR == TPResult::False())
2139 return false;
2140 // The identifier is followed by a parenthesized declarator.
2141 // It's supposed to be a type.
2142 break;
2143 }
2144
2145 default:
2146 // This is probably supposed to be a type. This includes cases like:
2147 // int f(itn);
2148 // struct S { unsinged : 4; };
2149 break;
2150 }
2151 }
2152
Chad Rosierc1183952012-06-26 22:30:43 +00002153 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002154 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002155 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002156 IdentifierInfo *II = Tok.getIdentifierInfo();
2157 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002158 // The action emitted a diagnostic, so we don't have to.
2159 if (T) {
2160 // The action has suggested that the type T could be used. Set that as
2161 // the type in the declaration specifiers, consume the would-be type
2162 // name token, and we're done.
2163 const char *PrevSpec;
2164 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002165 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002166 DS.SetRangeEnd(Tok.getLocation());
2167 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002168 // There may be other declaration specifiers after this.
2169 return true;
2170 } else if (II != Tok.getIdentifierInfo()) {
2171 // If no type was suggested, the correction is to a keyword
2172 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002173 // There may be other declaration specifiers after this.
2174 return true;
2175 }
Chad Rosierc1183952012-06-26 22:30:43 +00002176
Douglas Gregor15e56022009-10-13 23:27:22 +00002177 // Fall through; the action had no suggestion for us.
2178 } else {
2179 // The action did not emit a diagnostic, so emit one now.
2180 SourceRange R;
2181 if (SS) R = SS->getRange();
2182 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2183 }
Mike Stump11289f42009-09-09 15:08:12 +00002184
Douglas Gregor15e56022009-10-13 23:27:22 +00002185 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002186 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002187 DS.SetRangeEnd(Tok.getLocation());
2188 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002189
Chris Lattner20a0c612009-04-14 21:34:55 +00002190 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2191 // avoid rippling error messages on subsequent uses of the same type,
2192 // could be useful if #include was forgotten.
2193 return false;
2194}
2195
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002196/// \brief Determine the declaration specifier context from the declarator
2197/// context.
2198///
2199/// \param Context the declarator context, which is one of the
2200/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002201Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002202Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2203 if (Context == Declarator::MemberContext)
2204 return DSC_class;
2205 if (Context == Declarator::FileContext)
2206 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002207 if (Context == Declarator::TrailingReturnContext)
2208 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002209 return DSC_normal;
2210}
2211
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002212/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2213///
2214/// FIXME: Simply returns an alignof() expression if the argument is a
2215/// type. Ideally, the type should be propagated directly into Sema.
2216///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002217/// [C11] type-id
2218/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002219/// [C++0x] type-id ...[opt]
2220/// [C++0x] assignment-expression ...[opt]
2221ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2222 SourceLocation &EllipsisLoc) {
2223 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002224 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002225 SourceLocation TypeLoc = Tok.getLocation();
2226 ParsedType Ty = ParseTypeName().get();
2227 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002228 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2229 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002230 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002231 ER = ParseConstantExpression();
2232
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002233 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002234 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002235
2236 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002237}
2238
2239/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2240/// attribute to Attrs.
2241///
2242/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002243/// [C11] '_Alignas' '(' type-id ')'
2244/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002245/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2246/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002247void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002248 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002249 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2250 "Not an alignment-specifier!");
2251
Richard Smithd11c7a12013-01-29 01:48:07 +00002252 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2253 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002254
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002255 BalancedDelimiterTracker T(*this, tok::l_paren);
2256 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002257 return;
2258
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002259 SourceLocation EllipsisLoc;
2260 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002261 if (ArgExpr.isInvalid()) {
2262 SkipUntil(tok::r_paren);
2263 return;
2264 }
2265
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002266 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002267 if (EndLoc)
2268 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002269
Benjamin Kramerf0623432012-08-23 22:51:59 +00002270 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002271 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002272 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smith44c247f2013-02-22 08:32:16 +00002273 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002274}
2275
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002276/// ParseDeclarationSpecifiers
2277/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002278/// storage-class-specifier declaration-specifiers[opt]
2279/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002280/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002281/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002282/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002283/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002284///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002285/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002286/// 'typedef'
2287/// 'extern'
2288/// 'static'
2289/// 'auto'
2290/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002291/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002292/// [C++11] 'thread_local'
2293/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002294/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002295/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002296/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002297/// [C++] 'virtual'
2298/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002299/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002300/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002301/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002302
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002303///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002304void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002305 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002306 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002307 DeclSpecContext DSContext,
2308 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002309 if (DS.getSourceRange().isInvalid()) {
2310 DS.SetRangeStart(Tok.getLocation());
2311 DS.SetRangeEnd(Tok.getLocation());
2312 }
Chad Rosierc1183952012-06-26 22:30:43 +00002313
Douglas Gregordf593fb2011-11-07 17:33:42 +00002314 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002315 bool AttrsLastTime = false;
2316 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002317 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002318 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002319 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002320 unsigned DiagID = 0;
2321
Chris Lattner4d8f8732006-11-28 05:05:08 +00002322 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002323
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002324 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002325 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002326 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002327 if (!AttrsLastTime)
2328 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002329 else {
2330 // Reject C++11 attributes that appertain to decl specifiers as
2331 // we don't support any C++11 attributes that appertain to decl
2332 // specifiers. This also conforms to what g++ 4.8 is doing.
2333 ProhibitCXX11Attributes(attrs);
2334
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002335 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002336 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002337
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002338 // If this is not a declaration specifier token, we're done reading decl
2339 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002340 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002341 return;
Mike Stump11289f42009-09-09 15:08:12 +00002342
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002343 case tok::l_square:
2344 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002345 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002346 goto DoneWithDeclSpec;
2347
2348 ProhibitAttributes(attrs);
2349 // FIXME: It would be good to recover by accepting the attributes,
2350 // but attempting to do that now would cause serious
2351 // madness in terms of diagnostics.
2352 attrs.clear();
2353 attrs.Range = SourceRange();
2354
2355 ParseCXX11Attributes(attrs);
2356 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002357 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002358
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002359 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002360 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002361 if (DS.hasTypeSpecifier()) {
2362 bool AllowNonIdentifiers
2363 = (getCurScope()->getFlags() & (Scope::ControlScope |
2364 Scope::BlockScope |
2365 Scope::TemplateParamScope |
2366 Scope::FunctionPrototypeScope |
2367 Scope::AtCatchScope)) == 0;
2368 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002369 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002370 (DSContext == DSC_class && DS.isFriendSpecified());
2371
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002372 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002373 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002374 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002375 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002376 }
2377
Douglas Gregor80039242011-02-15 20:33:25 +00002378 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2379 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2380 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002381 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002382 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002383 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002384 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002385 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002386 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002387
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002388 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002389 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002390 }
2391
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002392 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002393 // C++ scope specifier. Annotate and loop, or bail out on error.
2394 if (TryAnnotateCXXScopeToken(true)) {
2395 if (!DS.hasTypeSpecifier())
2396 DS.SetTypeSpecError();
2397 goto DoneWithDeclSpec;
2398 }
John McCall8bc2a702010-03-01 18:20:46 +00002399 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2400 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002401 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002402
2403 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002404 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002405 goto DoneWithDeclSpec;
2406
John McCall9dab4e62009-12-12 11:40:51 +00002407 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002408 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2409 Tok.getAnnotationRange(),
2410 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002411
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002412 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002413 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002414 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002415 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002416 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002417 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002418
2419 // C++ [class.qual]p2:
2420 // In a lookup in which the constructor is an acceptable lookup
2421 // result and the nested-name-specifier nominates a class C:
2422 //
2423 // - if the name specified after the
2424 // nested-name-specifier, when looked up in C, is the
2425 // injected-class-name of C (Clause 9), or
2426 //
2427 // - if the name specified after the nested-name-specifier
2428 // is the same as the identifier or the
2429 // simple-template-id's template-name in the last
2430 // component of the nested-name-specifier,
2431 //
2432 // the name is instead considered to name the constructor of
2433 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002434 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002435 // Thus, if the template-name is actually the constructor
2436 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002437 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002438 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002439 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002440 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002441 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002442 if (isConstructorDeclarator()) {
2443 // The user meant this to be an out-of-line constructor
2444 // definition, but template arguments are not allowed
2445 // there. Just allow this as a constructor; we'll
2446 // complain about it later.
2447 goto DoneWithDeclSpec;
2448 }
2449
2450 // The user meant this to name a type, but it actually names
2451 // a constructor with some extraneous template
2452 // arguments. Complain, then parse it as a type as the user
2453 // intended.
2454 Diag(TemplateId->TemplateNameLoc,
2455 diag::err_out_of_line_template_id_names_constructor)
2456 << TemplateId->Name;
2457 }
2458
John McCall9dab4e62009-12-12 11:40:51 +00002459 DS.getTypeSpecScope() = SS;
2460 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002461 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002462 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002463 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002464 continue;
2465 }
2466
Douglas Gregorc5790df2009-09-28 07:26:33 +00002467 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002468 DS.getTypeSpecScope() = SS;
2469 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002470 if (Tok.getAnnotationValue()) {
2471 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002473 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002474 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002475 if (isInvalid)
2476 break;
John McCallba7bf592010-08-24 05:47:05 +00002477 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002478 else
2479 DS.SetTypeSpecError();
2480 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2481 ConsumeToken(); // The typename
2482 }
2483
Douglas Gregor167fa622009-03-25 15:40:00 +00002484 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002485 goto DoneWithDeclSpec;
2486
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002487 // If we're in a context where the identifier could be a class name,
2488 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002489 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002490 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002491 &SS)) {
2492 if (isConstructorDeclarator())
2493 goto DoneWithDeclSpec;
2494
2495 // As noted in C++ [class.qual]p2 (cited above), when the name
2496 // of the class is qualified in a context where it could name
2497 // a constructor, its a constructor name. However, we've
2498 // looked at the declarator, and the user probably meant this
2499 // to be a type. Complain that it isn't supposed to be treated
2500 // as a type, then proceed to parse it as a type.
2501 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2502 << Next.getIdentifierInfo();
2503 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002504
John McCallba7bf592010-08-24 05:47:05 +00002505 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2506 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002507 getCurScope(), &SS,
2508 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002509 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002510 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002511
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002512 // If the referenced identifier is not a type, then this declspec is
2513 // erroneous: We already checked about that it has no type specifier, and
2514 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002515 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002516 if (TypeRep == 0) {
2517 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002518 ParsedAttributesWithRange Attrs(AttrFactory);
2519 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2520 if (!Attrs.empty()) {
2521 AttrsLastTime = true;
2522 attrs.takeAllFrom(Attrs);
2523 }
2524 continue;
2525 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002526 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002527 }
Mike Stump11289f42009-09-09 15:08:12 +00002528
John McCall9dab4e62009-12-12 11:40:51 +00002529 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002530 ConsumeToken(); // The C++ scope.
2531
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002533 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002534 if (isInvalid)
2535 break;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002537 DS.SetRangeEnd(Tok.getLocation());
2538 ConsumeToken(); // The typename.
2539
2540 continue;
2541 }
Mike Stump11289f42009-09-09 15:08:12 +00002542
Chris Lattnere387d9e2009-01-21 19:48:37 +00002543 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002544 if (Tok.getAnnotationValue()) {
2545 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002547 DiagID, T);
2548 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002549 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002550
Chris Lattner005fc1b2010-04-05 18:18:31 +00002551 if (isInvalid)
2552 break;
2553
Chris Lattnere387d9e2009-01-21 19:48:37 +00002554 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2555 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002556
Chris Lattnere387d9e2009-01-21 19:48:37 +00002557 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2558 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002559 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002560 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002561 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002562
Chris Lattnere387d9e2009-01-21 19:48:37 +00002563 continue;
2564 }
Mike Stump11289f42009-09-09 15:08:12 +00002565
Douglas Gregor06873092011-04-28 15:48:45 +00002566 case tok::kw___is_signed:
2567 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2568 // typically treats it as a trait. If we see __is_signed as it appears
2569 // in libstdc++, e.g.,
2570 //
2571 // static const bool __is_signed;
2572 //
2573 // then treat __is_signed as an identifier rather than as a keyword.
2574 if (DS.getTypeSpecType() == TST_bool &&
2575 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2576 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2577 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2578 Tok.setKind(tok::identifier);
2579 }
2580
2581 // We're done with the declaration-specifiers.
2582 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002583
Chris Lattner16fac4f2008-07-26 01:18:38 +00002584 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002585 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002586 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002587 // In C++, check to see if this is a scope specifier like foo::bar::, if
2588 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002589 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002590 if (TryAnnotateCXXScopeToken(true)) {
2591 if (!DS.hasTypeSpecifier())
2592 DS.SetTypeSpecError();
2593 goto DoneWithDeclSpec;
2594 }
2595 if (!Tok.is(tok::identifier))
2596 continue;
2597 }
Mike Stump11289f42009-09-09 15:08:12 +00002598
Chris Lattner16fac4f2008-07-26 01:18:38 +00002599 // This identifier can only be a typedef name if we haven't already seen
2600 // a type-specifier. Without this check we misparse:
2601 // typedef int X; struct Y { short X; }; as 'short int'.
2602 if (DS.hasTypeSpecifier())
2603 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002604
John Thompson22334602010-02-05 00:12:22 +00002605 // Check for need to substitute AltiVec keyword tokens.
2606 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2607 break;
2608
Richard Smith3092a3b2012-05-09 18:56:43 +00002609 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2610 // allow the use of a typedef name as a type specifier.
2611 if (DS.isTypeAltiVecVector())
2612 goto DoneWithDeclSpec;
2613
John McCallba7bf592010-08-24 05:47:05 +00002614 ParsedType TypeRep =
2615 Actions.getTypeName(*Tok.getIdentifierInfo(),
2616 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002617
Chris Lattner6cc055a2009-04-12 20:42:31 +00002618 // If this is not a typedef name, don't parse it as part of the declspec,
2619 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002620 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002621 ParsedAttributesWithRange Attrs(AttrFactory);
2622 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2623 if (!Attrs.empty()) {
2624 AttrsLastTime = true;
2625 attrs.takeAllFrom(Attrs);
2626 }
2627 continue;
2628 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002629 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002630 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002631
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002632 // If we're in a context where the identifier could be a class name,
2633 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002634 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002635 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002636 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002637 goto DoneWithDeclSpec;
2638
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002639 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002640 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002641 if (isInvalid)
2642 break;
Mike Stump11289f42009-09-09 15:08:12 +00002643
Chris Lattner16fac4f2008-07-26 01:18:38 +00002644 DS.SetRangeEnd(Tok.getLocation());
2645 ConsumeToken(); // The identifier
2646
2647 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2648 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002649 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002650 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002651 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002652
Steve Naroffcd5e7822008-09-22 10:28:57 +00002653 // Need to support trailing type qualifiers (e.g. "id<p> const").
2654 // If a type specifier follows, it will be diagnosed elsewhere.
2655 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002656 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002657
2658 // type-name
2659 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002660 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002661 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002662 // This template-id does not refer to a type name, so we're
2663 // done with the type-specifiers.
2664 goto DoneWithDeclSpec;
2665 }
2666
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002667 // If we're in a context where the template-id could be a
2668 // constructor name or specialization, check whether this is a
2669 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002670 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002671 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002672 isConstructorDeclarator())
2673 goto DoneWithDeclSpec;
2674
Douglas Gregor7f741122009-02-25 19:37:18 +00002675 // Turn the template-id annotation token into a type annotation
2676 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002677 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002678 continue;
2679 }
2680
Chris Lattnere37e2332006-08-15 04:50:22 +00002681 // GNU attributes support.
2682 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002683 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002684 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002685
2686 // Microsoft declspec support.
2687 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002688 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002689 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002690
Steve Naroff44ac7772008-12-25 14:16:32 +00002691 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002692 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002693 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002694 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002695 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002696 // FIXME: This does not work correctly if it is set to be a declspec
2697 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002698 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002699 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002700 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002701 }
Eli Friedman53339e02009-06-08 23:27:34 +00002702
2703 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002704 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002705 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002706 case tok::kw___cdecl:
2707 case tok::kw___stdcall:
2708 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002709 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002710 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002711 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002712 continue;
2713
Dawn Perchik335e16b2010-09-03 01:29:35 +00002714 // Borland single token adornments.
2715 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002716 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002717 continue;
2718
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002719 // OpenCL single token adornments.
2720 case tok::kw___kernel:
2721 ParseOpenCLAttributes(DS.getAttributes());
2722 continue;
2723
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002724 // storage-class-specifier
2725 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002726 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2727 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002728 break;
2729 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002730 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002731 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002732 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2733 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002734 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002735 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002736 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2737 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002738 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002739 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002740 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002741 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002742 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2743 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002744 break;
2745 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002746 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002747 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002748 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2749 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002750 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002751 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002752 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002753 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2755 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002756 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002757 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2758 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002759 break;
2760 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002761 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2762 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002763 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002764 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002765 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2766 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002767 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002768 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002769 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2770 PrevSpec, DiagID);
2771 break;
2772 case tok::kw_thread_local:
2773 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2774 PrevSpec, DiagID);
2775 break;
2776 case tok::kw__Thread_local:
2777 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2778 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002779 break;
Mike Stump11289f42009-09-09 15:08:12 +00002780
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002781 // function-specifier
2782 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002783 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002784 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002785 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002786 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002787 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002788 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002789 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002790 break;
Richard Smith0015f092013-01-17 22:16:11 +00002791 case tok::kw__Noreturn:
2792 if (!getLangOpts().C11)
2793 Diag(Loc, diag::ext_c11_noreturn);
2794 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2795 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002796
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002797 // alignment-specifier
2798 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002799 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002800 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002801 ParseAlignmentSpecifier(DS.getAttributes());
2802 continue;
2803
Anders Carlssoncd8db412009-05-06 04:46:28 +00002804 // friend
2805 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002806 if (DSContext == DSC_class)
2807 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2808 else {
2809 PrevSpec = ""; // not actually used by the diagnostic
2810 DiagID = diag::err_friend_invalid_in_context;
2811 isInvalid = true;
2812 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002813 break;
Mike Stump11289f42009-09-09 15:08:12 +00002814
Douglas Gregor26701a42011-09-09 02:06:17 +00002815 // Modules
2816 case tok::kw___module_private__:
2817 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2818 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002819
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002820 // constexpr
2821 case tok::kw_constexpr:
2822 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2823 break;
2824
Chris Lattnere387d9e2009-01-21 19:48:37 +00002825 // type-specifier
2826 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002827 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2828 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002829 break;
2830 case tok::kw_long:
2831 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002832 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2833 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002834 else
John McCall49bfce42009-08-03 20:12:06 +00002835 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2836 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002837 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002838 case tok::kw___int64:
2839 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2840 DiagID);
2841 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002842 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002843 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2844 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002845 break;
2846 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002847 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2848 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002849 break;
2850 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002851 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2852 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002853 break;
2854 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002855 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2856 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002857 break;
2858 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002859 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2860 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002861 break;
2862 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002863 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2864 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002865 break;
2866 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2868 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002869 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002870 case tok::kw___int128:
2871 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2872 DiagID);
2873 break;
2874 case tok::kw_half:
2875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2876 DiagID);
2877 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002878 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002879 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2880 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002881 break;
2882 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002883 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2884 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002885 break;
2886 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002887 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2888 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002889 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002890 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002891 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2892 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002893 break;
2894 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002895 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2896 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002897 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002898 case tok::kw_bool:
2899 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002900 if (Tok.is(tok::kw_bool) &&
2901 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2902 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2903 PrevSpec = ""; // Not used by the diagnostic.
2904 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002905 // For better error recovery.
2906 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002907 isInvalid = true;
2908 } else {
2909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2910 DiagID);
2911 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002912 break;
2913 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002914 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2915 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002916 break;
2917 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2919 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002920 break;
2921 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2923 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002924 break;
John Thompson22334602010-02-05 00:12:22 +00002925 case tok::kw___vector:
2926 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2927 break;
2928 case tok::kw___pixel:
2929 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2930 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002931 case tok::kw_image1d_t:
2932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2933 PrevSpec, DiagID);
2934 break;
2935 case tok::kw_image1d_array_t:
2936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2937 PrevSpec, DiagID);
2938 break;
2939 case tok::kw_image1d_buffer_t:
2940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2941 PrevSpec, DiagID);
2942 break;
2943 case tok::kw_image2d_t:
2944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2945 PrevSpec, DiagID);
2946 break;
2947 case tok::kw_image2d_array_t:
2948 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2949 PrevSpec, DiagID);
2950 break;
2951 case tok::kw_image3d_t:
2952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2953 PrevSpec, DiagID);
2954 break;
Guy Benyei61054192013-02-07 10:55:47 +00002955 case tok::kw_sampler_t:
2956 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2957 PrevSpec, DiagID);
2958 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002959 case tok::kw_event_t:
2960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2961 PrevSpec, DiagID);
2962 break;
John McCall39439732011-04-09 22:50:59 +00002963 case tok::kw___unknown_anytype:
2964 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2965 PrevSpec, DiagID);
2966 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002967
2968 // class-specifier:
2969 case tok::kw_class:
2970 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002971 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002972 case tok::kw_union: {
2973 tok::TokenKind Kind = Tok.getKind();
2974 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002975
2976 // These are attributes following class specifiers.
2977 // To produce better diagnostic, we parse them when
2978 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002979 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002980 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002981 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002982
2983 // If there are attributes following class specifier,
2984 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002985 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002986 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002987 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002988 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002989 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002990 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002991
2992 // enum-specifier:
2993 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002994 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002995 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002996 continue;
2997
2998 // cv-qualifier:
2999 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003000 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003001 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003002 break;
3003 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003004 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003005 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003006 break;
3007 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003008 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003009 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003010 break;
3011
Douglas Gregor333489b2009-03-27 23:10:48 +00003012 // C++ typename-specifier:
3013 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003014 if (TryAnnotateTypeOrScopeToken()) {
3015 DS.SetTypeSpecError();
3016 goto DoneWithDeclSpec;
3017 }
3018 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003019 continue;
3020 break;
3021
Chris Lattnere387d9e2009-01-21 19:48:37 +00003022 // GNU typeof support.
3023 case tok::kw_typeof:
3024 ParseTypeofSpecifier(DS);
3025 continue;
3026
David Blaikie15a430a2011-12-04 05:04:18 +00003027 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003028 ParseDecltypeSpecifier(DS);
3029 continue;
3030
Alexis Hunt4a257072011-05-19 05:37:45 +00003031 case tok::kw___underlying_type:
3032 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003033 continue;
3034
3035 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003036 // C11 6.7.2.4/4:
3037 // If the _Atomic keyword is immediately followed by a left parenthesis,
3038 // it is interpreted as a type specifier (with a type name), not as a
3039 // type qualifier.
3040 if (NextToken().is(tok::l_paren)) {
3041 ParseAtomicSpecifier(DS);
3042 continue;
3043 }
3044 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3045 getLangOpts());
3046 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003047
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003048 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003049 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003050 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003051 goto DoneWithDeclSpec;
3052 case tok::kw___private:
3053 case tok::kw___global:
3054 case tok::kw___local:
3055 case tok::kw___constant:
3056 case tok::kw___read_only:
3057 case tok::kw___write_only:
3058 case tok::kw___read_write:
3059 ParseOpenCLQualifiers(DS);
3060 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003061
Steve Naroffcfdf6162008-06-05 00:02:44 +00003062 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003063 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003064 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3065 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003066 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003067 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003068
Douglas Gregor3a001f42010-11-19 17:10:50 +00003069 if (!ParseObjCProtocolQualifiers(DS))
3070 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3071 << FixItHint::CreateInsertion(Loc, "id")
3072 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003073
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003074 // Need to support trailing type qualifiers (e.g. "id<p> const").
3075 // If a type specifier follows, it will be diagnosed elsewhere.
3076 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003077 }
John McCall49bfce42009-08-03 20:12:06 +00003078 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003079 if (isInvalid) {
3080 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003081 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003082
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003083 if (DiagID == diag::ext_duplicate_declspec)
3084 Diag(Tok, DiagID)
3085 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3086 else
3087 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003088 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003089
Chris Lattner2e232092008-03-13 06:29:04 +00003090 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003091 if (DiagID != diag::err_bool_redeclaration)
3092 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003093
3094 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003095 }
3096}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003097
Chris Lattner70ae4912007-10-29 04:42:53 +00003098/// ParseStructDeclaration - Parse a struct declaration without the terminating
3099/// semicolon.
3100///
Chris Lattner90a26b02007-01-23 04:38:16 +00003101/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003102/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003103/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003104/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003105/// struct-declarator-list:
3106/// struct-declarator
3107/// struct-declarator-list ',' struct-declarator
3108/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3109/// struct-declarator:
3110/// declarator
3111/// [GNU] declarator attributes[opt]
3112/// declarator[opt] ':' constant-expression
3113/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3114///
Chris Lattnera12405b2008-04-10 06:46:29 +00003115void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003116ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003117
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003118 if (Tok.is(tok::kw___extension__)) {
3119 // __extension__ silences extension warnings in the subexpression.
3120 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003121 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003122 return ParseStructDeclaration(DS, Fields);
3123 }
Mike Stump11289f42009-09-09 15:08:12 +00003124
Steve Naroff97170802007-08-20 22:28:22 +00003125 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003126 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003127
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003128 // If there are no declarators, this is a free-standing declaration
3129 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003130 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003131 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3132 DS);
3133 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003134 return;
3135 }
3136
3137 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003138 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003139 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003140 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003141 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003142 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003143
Bill Wendling44426052012-12-20 19:22:21 +00003144 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003145 if (!FirstDeclarator)
3146 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003147
Steve Naroff97170802007-08-20 22:28:22 +00003148 /// struct-declarator: declarator
3149 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003150 if (Tok.isNot(tok::colon)) {
3151 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3152 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003153 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003154 }
Mike Stump11289f42009-09-09 15:08:12 +00003155
Chris Lattner76c72282007-10-09 17:33:22 +00003156 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003157 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003158 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003159 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003160 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003161 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003162 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003163 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003164
Steve Naroff97170802007-08-20 22:28:22 +00003165 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003166 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003167
John McCallcfefb6d2009-11-03 02:38:08 +00003168 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003169 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003170
Steve Naroff97170802007-08-20 22:28:22 +00003171 // If we don't have a comma, it is either the end of the list (a ';')
3172 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003173 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003174 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003175
Steve Naroff97170802007-08-20 22:28:22 +00003176 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003177 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003178
John McCallcfefb6d2009-11-03 02:38:08 +00003179 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003180 }
Steve Naroff97170802007-08-20 22:28:22 +00003181}
3182
3183/// ParseStructUnionBody
3184/// struct-contents:
3185/// struct-declaration-list
3186/// [EXT] empty
3187/// [GNU] "struct-declaration-list" without terminatoring ';'
3188/// struct-declaration-list:
3189/// struct-declaration
3190/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003191/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003192///
Chris Lattner1300fb92007-01-23 23:42:53 +00003193void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003194 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003195 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3196 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003197 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003198
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003199 BalancedDelimiterTracker T(*this, tok::l_brace);
3200 if (T.consumeOpen())
3201 return;
Mike Stump11289f42009-09-09 15:08:12 +00003202
Douglas Gregor658b9552009-01-09 22:42:13 +00003203 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003204 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003205
Andy Gibbs22e140b2013-04-03 09:31:19 +00003206 // Empty structs are an extension in C (C99 6.7.2.1p7).
3207 if (Tok.is(tok::r_brace)) {
Richard Smithe4345902011-12-29 21:57:33 +00003208 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3209 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3210 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003211
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003212 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003213
Chris Lattner7b9ace62007-01-23 20:11:08 +00003214 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003215 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003216 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003217
Chris Lattner736ed5d2007-06-09 05:59:07 +00003218 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003219 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003220 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003221 continue;
3222 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003223
Andy Gibbsc804e082013-04-03 09:46:04 +00003224 // Parse _Static_assert declaration.
3225 if (Tok.is(tok::kw__Static_assert)) {
3226 SourceLocation DeclEnd;
3227 ParseStaticAssertDeclaration(DeclEnd);
3228 continue;
3229 }
3230
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003231 if (Tok.is(tok::annot_pragma_pack)) {
3232 HandlePragmaPack();
3233 continue;
3234 }
3235
3236 if (Tok.is(tok::annot_pragma_align)) {
3237 HandlePragmaAlign();
3238 continue;
3239 }
3240
John McCallcfefb6d2009-11-03 02:38:08 +00003241 if (!Tok.is(tok::at)) {
3242 struct CFieldCallback : FieldCallback {
3243 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003244 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003245 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003246
John McCall48871652010-08-21 09:40:31 +00003247 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003248 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003249 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3250
Eli Friedman934dbbf2012-08-08 23:53:27 +00003251 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003252 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003253 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003254 FD.D.getDeclSpec().getSourceRange().getBegin(),
3255 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003256 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003257 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003258 }
John McCallcfefb6d2009-11-03 02:38:08 +00003259 } Callback(*this, TagDecl, FieldDecls);
3260
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003261 // Parse all the comma separated declarators.
3262 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003263 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003264 } else { // Handle @defs
3265 ConsumeToken();
3266 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3267 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003268 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003269 continue;
3270 }
3271 ConsumeToken();
3272 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3273 if (!Tok.is(tok::identifier)) {
3274 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003275 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003276 continue;
3277 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003278 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003279 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003280 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003281 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3282 ConsumeToken();
3283 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003284 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003285
Chris Lattner76c72282007-10-09 17:33:22 +00003286 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003287 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003288 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003289 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003290 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003291 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003292 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3293 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003294 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003295 // If we stopped at a ';', eat it.
3296 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003297 }
3298 }
Mike Stump11289f42009-09-09 15:08:12 +00003299
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003300 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003301
John McCall084e83d2011-03-24 11:26:52 +00003302 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003303 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003304 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003305
Douglas Gregor0be31a22010-07-02 17:43:08 +00003306 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003307 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003308 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003309 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003310 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003311 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3312 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003313}
3314
Chris Lattner3b561a32006-08-13 00:12:11 +00003315/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003316/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003317/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003318///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003319/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3320/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003321/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3322/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003323/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003324/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003325///
Richard Smith7d137e32012-03-23 03:33:32 +00003326/// [C++11] enum-head '{' enumerator-list[opt] '}'
3327/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003328///
Richard Smith7d137e32012-03-23 03:33:32 +00003329/// enum-head: [C++11]
3330/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3331/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3332/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003333///
Richard Smith7d137e32012-03-23 03:33:32 +00003334/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003335/// 'enum'
3336/// 'enum' 'class'
3337/// 'enum' 'struct'
3338///
Richard Smith7d137e32012-03-23 03:33:32 +00003339/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003340/// ':' type-specifier-seq
3341///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003342/// [C++] elaborated-type-specifier:
3343/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3344///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003345void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003346 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003347 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003348 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003349 if (Tok.is(tok::code_completion)) {
3350 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003351 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003352 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003353 }
John McCallcb432fa2011-07-06 05:58:41 +00003354
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003355 // If attributes exist after tag, parse them.
3356 ParsedAttributesWithRange attrs(AttrFactory);
3357 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003358 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003359
3360 // If declspecs exist after tag, parse them.
3361 while (Tok.is(tok::kw___declspec))
3362 ParseMicrosoftDeclSpec(attrs);
3363
Richard Smith0f8ee222012-01-10 01:33:14 +00003364 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003365 bool IsScopedUsingClassTag = false;
3366
John McCallbeae29a2012-06-23 22:30:04 +00003367 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003368 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3369 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3370 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003371 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003372 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003373
Bill Wendling44426052012-12-20 19:22:21 +00003374 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003375 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003376 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003377
3378 // They are allowed afterwards, though.
3379 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003380 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003381 while (Tok.is(tok::kw___declspec))
3382 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003383 }
Richard Smith7d137e32012-03-23 03:33:32 +00003384
John McCall6347b682012-05-07 06:16:58 +00003385 // C++11 [temp.explicit]p12:
3386 // The usual access controls do not apply to names used to specify
3387 // explicit instantiations.
3388 // We extend this to also cover explicit specializations. Note that
3389 // we don't suppress if this turns out to be an elaborated type
3390 // specifier.
3391 bool shouldDelayDiagsInTag =
3392 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3393 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3394 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003395
Richard Smithbfdb1082012-03-12 08:56:40 +00003396 // Enum definitions should not be parsed in a trailing-return-type.
3397 bool AllowDeclaration = DSC != DSC_trailing;
3398
3399 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003400 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003401 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003402
Abramo Bagnarad7548482010-05-19 21:37:53 +00003403 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003404 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003405 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3406 // if a fixed underlying type is allowed.
3407 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003408
3409 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003410 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003411 return;
3412
3413 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003414 Diag(Tok, diag::err_expected_ident);
3415 if (Tok.isNot(tok::l_brace)) {
3416 // Has no name and is not a definition.
3417 // Skip the rest of this declarator, up until the comma or semicolon.
3418 SkipUntil(tok::comma, true);
3419 return;
3420 }
3421 }
3422 }
Mike Stump11289f42009-09-09 15:08:12 +00003423
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003424 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003425 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003426 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003427 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003428
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003429 // Skip the rest of this declarator, up until the comma or semicolon.
3430 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003431 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003432 }
Mike Stump11289f42009-09-09 15:08:12 +00003433
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003434 // If an identifier is present, consume and remember it.
3435 IdentifierInfo *Name = 0;
3436 SourceLocation NameLoc;
3437 if (Tok.is(tok::identifier)) {
3438 Name = Tok.getIdentifierInfo();
3439 NameLoc = ConsumeToken();
3440 }
Mike Stump11289f42009-09-09 15:08:12 +00003441
Richard Smith0f8ee222012-01-10 01:33:14 +00003442 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003443 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3444 // declaration of a scoped enumeration.
3445 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003446 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003447 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003448 }
3449
John McCall6347b682012-05-07 06:16:58 +00003450 // Okay, end the suppression area. We'll decide whether to emit the
3451 // diagnostics in a second.
3452 if (shouldDelayDiagsInTag)
3453 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003454
Douglas Gregor0bf31402010-10-08 23:50:27 +00003455 TypeResult BaseType;
3456
Douglas Gregord1f69f62010-12-01 17:42:47 +00003457 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003458 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003459 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003460 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003461 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003462 // If we're in class scope, this can either be an enum declaration with
3463 // an underlying type, or a declaration of a bitfield member. We try to
3464 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003465 // (integer literal, sizeof); if it's still ambiguous, we then consider
3466 // anything that's a simple-type-specifier followed by '(' as an
3467 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003468 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003469 EnterExpressionEvaluationContext Unevaluated(Actions,
3470 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003471 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003472 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003473 // bit-field. This is the common case.
3474 if (TPR == TPResult::True())
3475 PossibleBitfield = true;
3476 // If the next token starts a type-specifier-seq, it may be either a
3477 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003478 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003479 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003480 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003481 GetLookAheadToken(2).getKind() == tok::semi) {
3482 // Consume the ':'.
3483 ConsumeToken();
3484 } else {
3485 // We have the start of a type-specifier-seq, so we have to perform
3486 // tentative parsing to determine whether we have an expression or a
3487 // type.
3488 TentativeParsingAction TPA(*this);
3489
3490 // Consume the ':'.
3491 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003492
3493 // If we see a type specifier followed by an open-brace, we have an
3494 // ambiguity between an underlying type and a C++11 braced
3495 // function-style cast. Resolve this by always treating it as an
3496 // underlying type.
3497 // FIXME: The standard is not entirely clear on how to disambiguate in
3498 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003499 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003500 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003501 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003502 // We'll parse this as a bitfield later.
3503 PossibleBitfield = true;
3504 TPA.Revert();
3505 } else {
3506 // We have a type-specifier-seq.
3507 TPA.Commit();
3508 }
3509 }
3510 } else {
3511 // Consume the ':'.
3512 ConsumeToken();
3513 }
3514
3515 if (!PossibleBitfield) {
3516 SourceRange Range;
3517 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003518
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003519 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003520 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003521 } else if (!getLangOpts().ObjC2) {
3522 if (getLangOpts().CPlusPlus)
3523 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3524 else
3525 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3526 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003527 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003528 }
3529
Richard Smith0f8ee222012-01-10 01:33:14 +00003530 // There are four options here. If we have 'friend enum foo;' then this is a
3531 // friend declaration, and cannot have an accompanying definition. If we have
3532 // 'enum foo;', then this is a forward declaration. If we have
3533 // 'enum foo {...' then this is a definition. Otherwise we have something
3534 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003535 //
3536 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3537 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3538 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3539 //
John McCallfaf5fb42010-08-26 23:41:50 +00003540 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003541 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003542 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003543 } else if (Tok.is(tok::l_brace)) {
3544 if (DS.isFriendSpecified()) {
3545 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3546 << SourceRange(DS.getFriendSpecLoc());
3547 ConsumeBrace();
3548 SkipUntil(tok::r_brace);
3549 TUK = Sema::TUK_Friend;
3550 } else {
3551 TUK = Sema::TUK_Definition;
3552 }
Richard Smith369b9f92012-06-25 21:37:02 +00003553 } else if (DSC != DSC_type_specifier &&
3554 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003555 (Tok.isAtStartOfLine() &&
3556 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003557 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3558 if (Tok.isNot(tok::semi)) {
3559 // A semicolon was missing after this declaration. Diagnose and recover.
3560 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3561 "enum");
3562 PP.EnterToken(Tok);
3563 Tok.setKind(tok::semi);
3564 }
John McCall6347b682012-05-07 06:16:58 +00003565 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003566 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003567 }
3568
3569 // If this is an elaborated type specifier, and we delayed
3570 // diagnostics before, just merge them into the current pool.
3571 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3572 diagsFromTag.redelay();
3573 }
Richard Smith7d137e32012-03-23 03:33:32 +00003574
3575 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003576 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003577 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003578 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003579 // Skip the rest of this declarator, up until the comma or semicolon.
3580 Diag(Tok, diag::err_enum_template);
3581 SkipUntil(tok::comma, true);
3582 return;
3583 }
3584
3585 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3586 // Enumerations can't be explicitly instantiated.
3587 DS.SetTypeSpecError();
3588 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3589 return;
3590 }
3591
3592 assert(TemplateInfo.TemplateParams && "no template parameters");
3593 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3594 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003595 }
Chad Rosierc1183952012-06-26 22:30:43 +00003596
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003597 if (TUK == Sema::TUK_Reference)
3598 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003599
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003600 if (!Name && TUK != Sema::TUK_Definition) {
3601 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003602
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003603 // Skip the rest of this declarator, up until the comma or semicolon.
3604 SkipUntil(tok::comma, true);
3605 return;
3606 }
Richard Smith7d137e32012-03-23 03:33:32 +00003607
Douglas Gregord6ab8742009-05-28 23:31:59 +00003608 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003609 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003610 const char *PrevSpec = 0;
3611 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003612 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003613 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003614 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003615 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003616 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003617
Douglas Gregorba41d012010-04-24 16:38:41 +00003618 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003619 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003620 // dependent tag.
3621 if (!Name) {
3622 DS.SetTypeSpecError();
3623 Diag(Tok, diag::err_expected_type_name_after_typename);
3624 return;
3625 }
Chad Rosierc1183952012-06-26 22:30:43 +00003626
Douglas Gregor0be31a22010-07-02 17:43:08 +00003627 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003628 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003629 NameLoc);
3630 if (Type.isInvalid()) {
3631 DS.SetTypeSpecError();
3632 return;
3633 }
Chad Rosierc1183952012-06-26 22:30:43 +00003634
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003635 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3636 NameLoc.isValid() ? NameLoc : StartLoc,
3637 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003638 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003639
Douglas Gregorba41d012010-04-24 16:38:41 +00003640 return;
3641 }
Mike Stump11289f42009-09-09 15:08:12 +00003642
John McCall48871652010-08-21 09:40:31 +00003643 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003644 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003645 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003646 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003647 ConsumeBrace();
3648 SkipUntil(tok::r_brace);
3649 }
Chad Rosierc1183952012-06-26 22:30:43 +00003650
Douglas Gregorba41d012010-04-24 16:38:41 +00003651 DS.SetTypeSpecError();
3652 return;
3653 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003654
Richard Smith369b9f92012-06-25 21:37:02 +00003655 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003656 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003657
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003658 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3659 NameLoc.isValid() ? NameLoc : StartLoc,
3660 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003661 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003662}
3663
Chris Lattnerc1915e22007-01-25 07:29:02 +00003664/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3665/// enumerator-list:
3666/// enumerator
3667/// enumerator-list ',' enumerator
3668/// enumerator:
3669/// enumeration-constant
3670/// enumeration-constant '=' constant-expression
3671/// enumeration-constant:
3672/// identifier
3673///
John McCall48871652010-08-21 09:40:31 +00003674void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003675 // Enter the scope of the enum body and start the definition.
3676 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003677 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003678
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003679 BalancedDelimiterTracker T(*this, tok::l_brace);
3680 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003681
Chris Lattner37256fb2007-08-27 17:24:30 +00003682 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003683 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003684 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003685
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003686 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003687
John McCall48871652010-08-21 09:40:31 +00003688 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003689
Chris Lattnerc1915e22007-01-25 07:29:02 +00003690 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003691 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003692 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3693 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003694
John McCall811a0f52010-10-22 23:36:17 +00003695 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003696 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003697 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003698 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003699 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003700
Chris Lattnerc1915e22007-01-25 07:29:02 +00003701 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003702 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003703 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003704
Chris Lattner76c72282007-10-09 17:33:22 +00003705 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003706 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003707 AssignedVal = ParseConstantExpression();
3708 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003709 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003710 }
Mike Stump11289f42009-09-09 15:08:12 +00003711
Chris Lattnerc1915e22007-01-25 07:29:02 +00003712 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003713 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3714 LastEnumConstDecl,
3715 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003716 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003717 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003718 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003719
Chris Lattner4ef40012007-06-11 01:28:17 +00003720 EnumConstantDecls.push_back(EnumConstDecl);
3721 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003722
Douglas Gregorce66d022010-09-07 14:51:08 +00003723 if (Tok.is(tok::identifier)) {
3724 // We're missing a comma between enumerators.
3725 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003726 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003727 << FixItHint::CreateInsertion(Loc, ", ");
3728 continue;
3729 }
Chad Rosierc1183952012-06-26 22:30:43 +00003730
Chris Lattner76c72282007-10-09 17:33:22 +00003731 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003732 break;
3733 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003734
Richard Smith5d164bc2011-10-15 05:09:34 +00003735 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003736 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003737 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3738 diag::ext_enumerator_list_comma_cxx :
3739 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003740 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003741 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003742 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3743 << FixItHint::CreateRemoval(CommaLoc);
3744 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003745 }
Mike Stump11289f42009-09-09 15:08:12 +00003746
Chris Lattnerc1915e22007-01-25 07:29:02 +00003747 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003748 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003749
Chris Lattnerc1915e22007-01-25 07:29:02 +00003750 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003751 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003752 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003753
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003754 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003755 EnumDecl, EnumConstantDecls,
3756 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003757 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003758
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003759 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003760 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3761 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003762
3763 // The next token must be valid after an enum definition. If not, a ';'
3764 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003765 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3766 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003767 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3768 // Push this token back into the preprocessor and change our current token
3769 // to ';' so that the rest of the code recovers as though there were an
3770 // ';' after the definition.
3771 PP.EnterToken(Tok);
3772 Tok.setKind(tok::semi);
3773 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003774}
Chris Lattner3b561a32006-08-13 00:12:11 +00003775
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003776/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003777/// start of a type-qualifier-list.
3778bool Parser::isTypeQualifier() const {
3779 switch (Tok.getKind()) {
3780 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003781
3782 // type-qualifier only in OpenCL
3783 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003784 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003785
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003786 // type-qualifier
3787 case tok::kw_const:
3788 case tok::kw_volatile:
3789 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003790 case tok::kw___private:
3791 case tok::kw___local:
3792 case tok::kw___global:
3793 case tok::kw___constant:
3794 case tok::kw___read_only:
3795 case tok::kw___read_write:
3796 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003797 return true;
3798 }
3799}
3800
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003801/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3802/// is definitely a type-specifier. Return false if it isn't part of a type
3803/// specifier or if we're not sure.
3804bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3805 switch (Tok.getKind()) {
3806 default: return false;
3807 // type-specifiers
3808 case tok::kw_short:
3809 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003810 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003811 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003812 case tok::kw_signed:
3813 case tok::kw_unsigned:
3814 case tok::kw__Complex:
3815 case tok::kw__Imaginary:
3816 case tok::kw_void:
3817 case tok::kw_char:
3818 case tok::kw_wchar_t:
3819 case tok::kw_char16_t:
3820 case tok::kw_char32_t:
3821 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003822 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003823 case tok::kw_float:
3824 case tok::kw_double:
3825 case tok::kw_bool:
3826 case tok::kw__Bool:
3827 case tok::kw__Decimal32:
3828 case tok::kw__Decimal64:
3829 case tok::kw__Decimal128:
3830 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003831
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003832 // OpenCL specific types:
3833 case tok::kw_image1d_t:
3834 case tok::kw_image1d_array_t:
3835 case tok::kw_image1d_buffer_t:
3836 case tok::kw_image2d_t:
3837 case tok::kw_image2d_array_t:
3838 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003839 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003840 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003841
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003842 // struct-or-union-specifier (C99) or class-specifier (C++)
3843 case tok::kw_class:
3844 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003845 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003846 case tok::kw_union:
3847 // enum-specifier
3848 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003849
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003850 // typedef-name
3851 case tok::annot_typename:
3852 return true;
3853 }
3854}
3855
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003856/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003857/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003858bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003859 switch (Tok.getKind()) {
3860 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003861
Chris Lattner020bab92009-01-04 23:41:41 +00003862 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003863 if (TryAltiVecVectorToken())
3864 return true;
3865 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003866 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003867 // Annotate typenames and C++ scope specifiers. If we get one, just
3868 // recurse to handle whatever we get.
3869 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003870 return true;
3871 if (Tok.is(tok::identifier))
3872 return false;
3873 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003874
Chris Lattner020bab92009-01-04 23:41:41 +00003875 case tok::coloncolon: // ::foo::bar
3876 if (NextToken().is(tok::kw_new) || // ::new
3877 NextToken().is(tok::kw_delete)) // ::delete
3878 return false;
3879
Chris Lattner020bab92009-01-04 23:41:41 +00003880 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003881 return true;
3882 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003883
Chris Lattnere37e2332006-08-15 04:50:22 +00003884 // GNU attributes support.
3885 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003886 // GNU typeof support.
3887 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003888
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003889 // type-specifiers
3890 case tok::kw_short:
3891 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003892 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003893 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003894 case tok::kw_signed:
3895 case tok::kw_unsigned:
3896 case tok::kw__Complex:
3897 case tok::kw__Imaginary:
3898 case tok::kw_void:
3899 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003900 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003901 case tok::kw_char16_t:
3902 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003903 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003904 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003905 case tok::kw_float:
3906 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003907 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003908 case tok::kw__Bool:
3909 case tok::kw__Decimal32:
3910 case tok::kw__Decimal64:
3911 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003912 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003913
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003914 // OpenCL specific types:
3915 case tok::kw_image1d_t:
3916 case tok::kw_image1d_array_t:
3917 case tok::kw_image1d_buffer_t:
3918 case tok::kw_image2d_t:
3919 case tok::kw_image2d_array_t:
3920 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003921 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003922 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003923
Chris Lattner861a2262008-04-13 18:59:07 +00003924 // struct-or-union-specifier (C99) or class-specifier (C++)
3925 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003926 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003927 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003928 case tok::kw_union:
3929 // enum-specifier
3930 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003931
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003932 // type-qualifier
3933 case tok::kw_const:
3934 case tok::kw_volatile:
3935 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003936
John McCallea0a39e2012-11-14 00:49:39 +00003937 // Debugger support.
3938 case tok::kw___unknown_anytype:
3939
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003940 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003941 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003942 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003943
Chris Lattner409bf7d2008-10-20 00:25:30 +00003944 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3945 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003946 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003947
Steve Naroff44ac7772008-12-25 14:16:32 +00003948 case tok::kw___cdecl:
3949 case tok::kw___stdcall:
3950 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003951 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003952 case tok::kw___w64:
3953 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003954 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003955 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003956 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003957
3958 case tok::kw___private:
3959 case tok::kw___local:
3960 case tok::kw___global:
3961 case tok::kw___constant:
3962 case tok::kw___read_only:
3963 case tok::kw___read_write:
3964 case tok::kw___write_only:
3965
Eli Friedman53339e02009-06-08 23:27:34 +00003966 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003967
3968 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003969 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003970
Richard Smith8e1ac332013-03-28 01:55:44 +00003971 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00003972 case tok::kw__Atomic:
3973 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003974 }
3975}
3976
Chris Lattneracd58a32006-08-06 17:24:14 +00003977/// isDeclarationSpecifier() - Return true if the current token is part of a
3978/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003979///
3980/// \param DisambiguatingWithExpression True to indicate that the purpose of
3981/// this check is to disambiguate between an expression and a declaration.
3982bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003983 switch (Tok.getKind()) {
3984 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003985
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003986 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003987 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003988
Chris Lattner020bab92009-01-04 23:41:41 +00003989 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003990 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003991 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003992 return false;
John Thompson22334602010-02-05 00:12:22 +00003993 if (TryAltiVecVectorToken())
3994 return true;
3995 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003996 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003997 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003998 // Annotate typenames and C++ scope specifiers. If we get one, just
3999 // recurse to handle whatever we get.
4000 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004001 return true;
4002 if (Tok.is(tok::identifier))
4003 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004004
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004005 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004006 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004007 // expression is permitted, then this is probably a class message send
4008 // missing the initial '['. In this case, we won't consider this to be
4009 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004010 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004011 isStartOfObjCClassMessageMissingOpenBracket())
4012 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004013
John McCall1f476a12010-02-26 08:45:28 +00004014 return isDeclarationSpecifier();
4015
Chris Lattner020bab92009-01-04 23:41:41 +00004016 case tok::coloncolon: // ::foo::bar
4017 if (NextToken().is(tok::kw_new) || // ::new
4018 NextToken().is(tok::kw_delete)) // ::delete
4019 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004020
Chris Lattner020bab92009-01-04 23:41:41 +00004021 // Annotate typenames and C++ scope specifiers. If we get one, just
4022 // recurse to handle whatever we get.
4023 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004024 return true;
4025 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004026
Chris Lattneracd58a32006-08-06 17:24:14 +00004027 // storage-class-specifier
4028 case tok::kw_typedef:
4029 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004030 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004031 case tok::kw_static:
4032 case tok::kw_auto:
4033 case tok::kw_register:
4034 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004035 case tok::kw_thread_local:
4036 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004037
Douglas Gregor26701a42011-09-09 02:06:17 +00004038 // Modules
4039 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004040
John McCallea0a39e2012-11-14 00:49:39 +00004041 // Debugger support
4042 case tok::kw___unknown_anytype:
4043
Chris Lattneracd58a32006-08-06 17:24:14 +00004044 // type-specifiers
4045 case tok::kw_short:
4046 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004047 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004048 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004049 case tok::kw_signed:
4050 case tok::kw_unsigned:
4051 case tok::kw__Complex:
4052 case tok::kw__Imaginary:
4053 case tok::kw_void:
4054 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004055 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004056 case tok::kw_char16_t:
4057 case tok::kw_char32_t:
4058
Chris Lattneracd58a32006-08-06 17:24:14 +00004059 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004060 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004061 case tok::kw_float:
4062 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004063 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004064 case tok::kw__Bool:
4065 case tok::kw__Decimal32:
4066 case tok::kw__Decimal64:
4067 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004068 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004069
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004070 // OpenCL specific types:
4071 case tok::kw_image1d_t:
4072 case tok::kw_image1d_array_t:
4073 case tok::kw_image1d_buffer_t:
4074 case tok::kw_image2d_t:
4075 case tok::kw_image2d_array_t:
4076 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004077 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004078 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004079
Chris Lattner861a2262008-04-13 18:59:07 +00004080 // struct-or-union-specifier (C99) or class-specifier (C++)
4081 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004082 case tok::kw_struct:
4083 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004084 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004085 // enum-specifier
4086 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004087
Chris Lattneracd58a32006-08-06 17:24:14 +00004088 // type-qualifier
4089 case tok::kw_const:
4090 case tok::kw_volatile:
4091 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004092
Chris Lattneracd58a32006-08-06 17:24:14 +00004093 // function-specifier
4094 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004095 case tok::kw_virtual:
4096 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004097 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004098
Richard Smith1dba27c2013-01-29 09:02:09 +00004099 // alignment-specifier
4100 case tok::kw__Alignas:
4101
Richard Smithd16fe122012-10-25 00:00:53 +00004102 // friend keyword.
4103 case tok::kw_friend:
4104
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004105 // static_assert-declaration
4106 case tok::kw__Static_assert:
4107
Chris Lattner599e47e2007-08-09 17:01:07 +00004108 // GNU typeof support.
4109 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004110
Chris Lattner599e47e2007-08-09 17:01:07 +00004111 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004112 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004113
Richard Smithd16fe122012-10-25 00:00:53 +00004114 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004115 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004116 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004117
Richard Smith8e1ac332013-03-28 01:55:44 +00004118 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004119 case tok::kw__Atomic:
4120 return true;
4121
Chris Lattner8b2ec162008-07-26 03:38:44 +00004122 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4123 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004124 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004125
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004126 // typedef-name
4127 case tok::annot_typename:
4128 return !DisambiguatingWithExpression ||
4129 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004130
Steve Narofff192fab2009-01-06 19:34:12 +00004131 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004132 case tok::kw___cdecl:
4133 case tok::kw___stdcall:
4134 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004135 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004136 case tok::kw___w64:
4137 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004138 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004139 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004140 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004141 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004142
4143 case tok::kw___private:
4144 case tok::kw___local:
4145 case tok::kw___global:
4146 case tok::kw___constant:
4147 case tok::kw___read_only:
4148 case tok::kw___read_write:
4149 case tok::kw___write_only:
4150
Eli Friedman53339e02009-06-08 23:27:34 +00004151 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004152 }
4153}
4154
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004155bool Parser::isConstructorDeclarator() {
4156 TentativeParsingAction TPA(*this);
4157
4158 // Parse the C++ scope specifier.
4159 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004160 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004161 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004162 TPA.Revert();
4163 return false;
4164 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004165
4166 // Parse the constructor name.
4167 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4168 // We already know that we have a constructor name; just consume
4169 // the token.
4170 ConsumeToken();
4171 } else {
4172 TPA.Revert();
4173 return false;
4174 }
4175
Richard Smith43f340f2012-03-27 23:05:05 +00004176 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004177 if (Tok.isNot(tok::l_paren)) {
4178 TPA.Revert();
4179 return false;
4180 }
4181 ConsumeParen();
4182
Richard Smith43f340f2012-03-27 23:05:05 +00004183 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4184 // that we have a constructor.
4185 if (Tok.is(tok::r_paren) ||
4186 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004187 TPA.Revert();
4188 return true;
4189 }
4190
4191 // If we need to, enter the specified scope.
4192 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004193 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004194 DeclScopeObj.EnterDeclaratorScope();
4195
Francois Pichet79f3a872011-01-31 04:54:32 +00004196 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004197 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004198 MaybeParseMicrosoftAttributes(Attrs);
4199
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004200 // Check whether the next token(s) are part of a declaration
4201 // specifier, in which case we have the start of a parameter and,
4202 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004203 bool IsConstructor = false;
4204 if (isDeclarationSpecifier())
4205 IsConstructor = true;
4206 else if (Tok.is(tok::identifier) ||
4207 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4208 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4209 // This might be a parenthesized member name, but is more likely to
4210 // be a constructor declaration with an invalid argument type. Keep
4211 // looking.
4212 if (Tok.is(tok::annot_cxxscope))
4213 ConsumeToken();
4214 ConsumeToken();
4215
4216 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004217 // which must have one of the following syntactic forms (see the
4218 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004219 switch (Tok.getKind()) {
4220 case tok::l_paren:
4221 // C(X ( int));
4222 case tok::l_square:
4223 // C(X [ 5]);
4224 // C(X [ [attribute]]);
4225 case tok::coloncolon:
4226 // C(X :: Y);
4227 // C(X :: *p);
4228 case tok::r_paren:
4229 // C(X )
4230 // Assume this isn't a constructor, rather than assuming it's a
4231 // constructor with an unnamed parameter of an ill-formed type.
4232 break;
4233
4234 default:
4235 IsConstructor = true;
4236 break;
4237 }
4238 }
4239
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004240 TPA.Revert();
4241 return IsConstructor;
4242}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004243
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004244/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004245/// type-qualifier-list: [C99 6.7.5]
4246/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004247/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004248/// [ only if VendorAttributesAllowed=true ]
4249/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004250/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004251/// [ only if VendorAttributesAllowed=true ]
4252/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004253/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004254/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004255///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004256void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4257 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004258 bool CXX11AttributesAllowed,
4259 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004260 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004261 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004262 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004263 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004264 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004265 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004266
4267 SourceLocation EndLoc;
4268
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004269 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004270 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004271 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004272 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004273 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004274
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004275 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004276 case tok::code_completion:
4277 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004278 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004279
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004280 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004281 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004282 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004283 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004284 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004285 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004286 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004287 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004288 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004289 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004290 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004291 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004292 case tok::kw__Atomic:
4293 if (!AtomicAllowed)
4294 goto DoneWithTypeQuals;
4295 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4296 getLangOpts());
4297 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004298
4299 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004300 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004301 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004302 goto DoneWithTypeQuals;
4303 case tok::kw___private:
4304 case tok::kw___global:
4305 case tok::kw___local:
4306 case tok::kw___constant:
4307 case tok::kw___read_only:
4308 case tok::kw___write_only:
4309 case tok::kw___read_write:
4310 ParseOpenCLQualifiers(DS);
4311 break;
4312
Eli Friedman53339e02009-06-08 23:27:34 +00004313 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004314 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004315 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004316 case tok::kw___cdecl:
4317 case tok::kw___stdcall:
4318 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004319 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004320 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004321 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004322 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004323 continue;
4324 }
4325 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004326 case tok::kw___pascal:
4327 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004328 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004329 continue;
4330 }
4331 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004332 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004333 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004334 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004335 continue; // do *not* consume the next token!
4336 }
4337 // otherwise, FALL THROUGH!
4338 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004339 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004340 // If this is not a type-qualifier token, we're done reading type
4341 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004342 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004343 if (EndLoc.isValid())
4344 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004345 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004346 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004347
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004348 // If the specifier combination wasn't legal, issue a diagnostic.
4349 if (isInvalid) {
4350 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004351 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004352 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004353 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004354 }
4355}
4356
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004357
4358/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4359///
4360void Parser::ParseDeclarator(Declarator &D) {
4361 /// This implements the 'declarator' production in the C grammar, then checks
4362 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004363 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004364}
4365
Richard Smith0efa75c2012-03-29 01:16:42 +00004366static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4367 if (Kind == tok::star || Kind == tok::caret)
4368 return true;
4369
4370 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4371 if (!Lang.CPlusPlus)
4372 return false;
4373
4374 return Kind == tok::amp || Kind == tok::ampamp;
4375}
4376
Sebastian Redlbd150f42008-11-21 19:14:01 +00004377/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4378/// is parsed by the function passed to it. Pass null, and the direct-declarator
4379/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004380/// ptr-operator production.
4381///
Richard Smith09f76ee2011-10-19 21:33:05 +00004382/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004383/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4384/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004385///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004386/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4387/// [C] pointer[opt] direct-declarator
4388/// [C++] direct-declarator
4389/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004390///
4391/// pointer: [C99 6.7.5]
4392/// '*' type-qualifier-list[opt]
4393/// '*' type-qualifier-list[opt] pointer
4394///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004395/// ptr-operator:
4396/// '*' cv-qualifier-seq[opt]
4397/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004398/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004399/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004400/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004401/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004402void Parser::ParseDeclaratorInternal(Declarator &D,
4403 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004404 if (Diags.hasAllExtensionsSilenced())
4405 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004406
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004407 // C++ member pointers start with a '::' or a nested-name.
4408 // Member pointers get special handling, since there's no place for the
4409 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004410 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004411 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4412 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004413 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4414 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004415 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004416 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004417
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004418 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004419 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004420 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004421 if (D.mayHaveIdentifier())
4422 D.getCXXScopeSpec() = SS;
4423 else
4424 AnnotateScopeToken(SS, true);
4425
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004426 if (DirectDeclParser)
4427 (this->*DirectDeclParser)(D);
4428 return;
4429 }
4430
4431 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004432 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004433 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004434 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004435 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004436
4437 // Recurse to parse whatever is left.
4438 ParseDeclaratorInternal(D, DirectDeclParser);
4439
4440 // Sema will have to catch (syntactically invalid) pointers into global
4441 // scope. It has to catch pointers into namespace scope anyway.
4442 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004443 Loc),
4444 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004445 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004446 return;
4447 }
4448 }
4449
4450 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004451 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004452 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004453 if (DirectDeclParser)
4454 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004455 return;
4456 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004457
Sebastian Redled0f3b02009-03-15 22:02:01 +00004458 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4459 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004460 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004461 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004462
Chris Lattner9eac9312009-03-27 04:18:06 +00004463 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004464 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004465 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004466
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004467 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004468 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004469 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004470
Bill Wendling3708c182007-05-27 10:15:43 +00004471 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004472 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004473 if (Kind == tok::star)
4474 // Remember that we parsed a pointer type, and remember the type-quals.
4475 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004476 DS.getConstSpecLoc(),
4477 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004478 DS.getRestrictSpecLoc()),
4479 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004480 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004481 else
4482 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004483 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004484 Loc),
4485 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004486 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004487 } else {
4488 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004489 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004490
Sebastian Redl3b27be62009-03-23 00:00:23 +00004491 // Complain about rvalue references in C++03, but then go on and build
4492 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004493 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004494 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004495 diag::warn_cxx98_compat_rvalue_reference :
4496 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004497
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004498 // GNU-style and C++11 attributes are allowed here, as is restrict.
4499 ParseTypeQualifierListOpt(DS);
4500 D.ExtendWithDeclSpec(DS);
4501
Bill Wendling93efb222007-06-02 23:28:54 +00004502 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4503 // cv-qualifiers are introduced through the use of a typedef or of a
4504 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004505 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4506 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4507 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004508 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004509 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4510 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004511 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004512 // 'restrict' is permitted as an extension.
4513 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4514 Diag(DS.getAtomicSpecLoc(),
4515 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004516 }
Bill Wendling3708c182007-05-27 10:15:43 +00004517
4518 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004519 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004520
Douglas Gregor66583c52008-11-03 15:51:28 +00004521 if (D.getNumTypeObjects() > 0) {
4522 // C++ [dcl.ref]p4: There shall be no references to references.
4523 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4524 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004525 if (const IdentifierInfo *II = D.getIdentifier())
4526 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4527 << II;
4528 else
4529 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4530 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004531
Sebastian Redlbd150f42008-11-21 19:14:01 +00004532 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004533 // can go ahead and build the (technically ill-formed)
4534 // declarator: reference collapsing will take care of it.
4535 }
4536 }
4537
Richard Smith8e1ac332013-03-28 01:55:44 +00004538 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004539 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004540 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004541 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004542 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004543 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004544}
4545
Richard Smith0efa75c2012-03-29 01:16:42 +00004546static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4547 SourceLocation EllipsisLoc) {
4548 if (EllipsisLoc.isValid()) {
4549 FixItHint Insertion;
4550 if (!D.getEllipsisLoc().isValid()) {
4551 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4552 D.setEllipsisLoc(EllipsisLoc);
4553 }
4554 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4555 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4556 }
4557}
4558
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004559/// ParseDirectDeclarator
4560/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004561/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004562/// '(' declarator ')'
4563/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004564/// [C90] direct-declarator '[' constant-expression[opt] ']'
4565/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4566/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4567/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4568/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004569/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4570/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004571/// direct-declarator '(' parameter-type-list ')'
4572/// direct-declarator '(' identifier-list[opt] ')'
4573/// [GNU] direct-declarator '(' parameter-forward-declarations
4574/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004575/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4576/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004577/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4578/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4579/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004580/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004581/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004582///
4583/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004584/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004585/// '::'[opt] nested-name-specifier[opt] type-name
4586///
4587/// id-expression: [C++ 5.1]
4588/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004589/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004590///
4591/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004592/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004593/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004594/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004595/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004596/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004597///
Richard Smith1453e312012-03-27 01:42:32 +00004598/// Note, any additional constructs added here may need corresponding changes
4599/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004600void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004601 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004602
David Blaikiebbafb8a2012-03-11 07:00:24 +00004603 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004604 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004605 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004606 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4607 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004608 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004609 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004610 }
4611
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004612 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004613 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004614 // Change the declaration context for name lookup, until this function
4615 // is exited (and the declarator has been parsed).
4616 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004617 }
4618
Douglas Gregor27b4c162010-12-23 22:44:42 +00004619 // C++0x [dcl.fct]p14:
4620 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004621 // of a parameter-declaration-clause without a preceding comma. In
4622 // this case, the ellipsis is parsed as part of the
4623 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004624 // parameter pack that has not been expanded; otherwise, it is parsed
4625 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004626 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004627 !((D.getContext() == Declarator::PrototypeContext ||
4628 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004629 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004630 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004631 !Actions.containsUnexpandedParameterPacks(D))) {
4632 SourceLocation EllipsisLoc = ConsumeToken();
4633 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4634 // The ellipsis was put in the wrong place. Recover, and explain to
4635 // the user what they should have done.
4636 ParseDeclarator(D);
4637 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4638 return;
4639 } else
4640 D.setEllipsisLoc(EllipsisLoc);
4641
4642 // The ellipsis can't be followed by a parenthesized declarator. We
4643 // check for that in ParseParenDeclarator, after we have disambiguated
4644 // the l_paren token.
4645 }
4646
Douglas Gregor7861a802009-11-03 01:35:08 +00004647 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4648 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4649 // We found something that indicates the start of an unqualified-id.
4650 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004651 bool AllowConstructorName;
4652 if (D.getDeclSpec().hasTypeSpecifier())
4653 AllowConstructorName = false;
4654 else if (D.getCXXScopeSpec().isSet())
4655 AllowConstructorName =
4656 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004657 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004658 else
4659 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4660
Abramo Bagnara7945c982012-01-27 09:46:47 +00004661 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004662 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4663 /*EnteringContext=*/true,
4664 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004665 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004666 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004667 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004668 D.getName()) ||
4669 // Once we're past the identifier, if the scope was bad, mark the
4670 // whole declarator bad.
4671 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004672 D.SetIdentifier(0, Tok.getLocation());
4673 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004674 } else {
4675 // Parsed the unqualified-id; update range information and move along.
4676 if (D.getSourceRange().getBegin().isInvalid())
4677 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4678 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004679 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004680 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004681 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004682 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004683 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004684 "There's a C++-specific check for tok::identifier above");
4685 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4686 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4687 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004688 goto PastIdentifier;
4689 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004690
Douglas Gregor7861a802009-11-03 01:35:08 +00004691 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004692 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004693 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004694 // Example: 'char (*X)' or 'int (*XX)(void)'
4695 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004696
4697 // If the declarator was parenthesized, we entered the declarator
4698 // scope when parsing the parenthesized declarator, then exited
4699 // the scope already. Re-enter the scope, if we need to.
4700 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004701 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004702 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004703 if (!D.isInvalidType() &&
4704 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004705 // Change the declaration context for name lookup, until this function
4706 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004707 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004708 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004709 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004710 // This could be something simple like "int" (in which case the declarator
4711 // portion is empty), if an abstract-declarator is allowed.
4712 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004713
4714 // The grammar for abstract-pack-declarator does not allow grouping parens.
4715 // FIXME: Revisit this once core issue 1488 is resolved.
4716 if (D.hasEllipsis() && D.hasGroupingParens())
4717 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4718 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004719 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004720 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004721 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004722 if (D.getContext() == Declarator::MemberContext)
4723 Diag(Tok, diag::err_expected_member_name_or_semi)
4724 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004725 else if (getLangOpts().CPlusPlus) {
4726 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4727 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4728 else
4729 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4730 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004731 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004732 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004733 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004734 }
Mike Stump11289f42009-09-09 15:08:12 +00004735
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004736 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004737 assert(D.isPastIdentifier() &&
4738 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004739
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004740 // Don't parse attributes unless we have parsed an unparenthesized name.
4741 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004742 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004743
Chris Lattneracd58a32006-08-06 17:24:14 +00004744 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004745 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004746 // Enter function-declaration scope, limiting any declarators to the
4747 // function prototype scope, including parameter declarators.
4748 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004749 Scope::FunctionPrototypeScope|Scope::DeclScope|
4750 (D.isFunctionDeclaratorAFunctionDeclaration()
4751 ? Scope::FunctionDeclarationScope : 0));
4752
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004753 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4754 // In such a case, check if we actually have a function declarator; if it
4755 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004756 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004757 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4758 // The name of the declarator, if any, is tentatively declared within
4759 // a possible direct initializer.
4760 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4761 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4762 TentativelyDeclaredIdentifiers.pop_back();
4763 if (!IsFunctionDecl)
4764 break;
4765 }
John McCall084e83d2011-03-24 11:26:52 +00004766 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004767 BalancedDelimiterTracker T(*this, tok::l_paren);
4768 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004769 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004770 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004771 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004772 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004773 } else {
4774 break;
4775 }
4776 }
Chad Rosierc1183952012-06-26 22:30:43 +00004777}
Chris Lattneracd58a32006-08-06 17:24:14 +00004778
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004779/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4780/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004781/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004782/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4783///
4784/// direct-declarator:
4785/// '(' declarator ')'
4786/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004787/// direct-declarator '(' parameter-type-list ')'
4788/// direct-declarator '(' identifier-list[opt] ')'
4789/// [GNU] direct-declarator '(' parameter-forward-declarations
4790/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004791///
4792void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004793 BalancedDelimiterTracker T(*this, tok::l_paren);
4794 T.consumeOpen();
4795
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004796 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004797
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004798 // Eat any attributes before we look at whether this is a grouping or function
4799 // declarator paren. If this is a grouping paren, the attribute applies to
4800 // the type being built up, for example:
4801 // int (__attribute__(()) *x)(long y)
4802 // If this ends up not being a grouping paren, the attribute applies to the
4803 // first argument, for example:
4804 // int (__attribute__(()) int x)
4805 // In either case, we need to eat any attributes to be able to determine what
4806 // sort of paren this is.
4807 //
John McCall084e83d2011-03-24 11:26:52 +00004808 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004809 bool RequiresArg = false;
4810 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004811 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004812
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004813 // We require that the argument list (if this is a non-grouping paren) be
4814 // present even if the attribute list was empty.
4815 RequiresArg = true;
4816 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004817
Steve Naroff44ac7772008-12-25 14:16:32 +00004818 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004819 ParseMicrosoftTypeAttributes(attrs);
4820
Dawn Perchik335e16b2010-09-03 01:29:35 +00004821 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004822 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004823 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004824
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004825 // If we haven't past the identifier yet (or where the identifier would be
4826 // stored, if this is an abstract declarator), then this is probably just
4827 // grouping parens. However, if this could be an abstract-declarator, then
4828 // this could also be the start of function arguments (consider 'void()').
4829 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004830
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004831 if (!D.mayOmitIdentifier()) {
4832 // If this can't be an abstract-declarator, this *must* be a grouping
4833 // paren, because we haven't seen the identifier yet.
4834 isGrouping = true;
4835 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004836 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4837 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004838 isDeclarationSpecifier() || // 'int(int)' is a function.
4839 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004840 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4841 // considered to be a type, not a K&R identifier-list.
4842 isGrouping = false;
4843 } else {
4844 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4845 isGrouping = true;
4846 }
Mike Stump11289f42009-09-09 15:08:12 +00004847
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004848 // If this is a grouping paren, handle:
4849 // direct-declarator: '(' declarator ')'
4850 // direct-declarator: '(' attributes declarator ')'
4851 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004852 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4853 D.setEllipsisLoc(SourceLocation());
4854
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004855 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004856 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004857 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004858 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004859 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004860 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004861 T.getCloseLocation()),
4862 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004863
4864 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004865
4866 // An ellipsis cannot be placed outside parentheses.
4867 if (EllipsisLoc.isValid())
4868 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4869
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004870 return;
4871 }
Mike Stump11289f42009-09-09 15:08:12 +00004872
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004873 // Okay, if this wasn't a grouping paren, it must be the start of a function
4874 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004875 // identifier (and remember where it would have been), then call into
4876 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004877 D.SetIdentifier(0, Tok.getLocation());
4878
David Blaikie15a430a2011-12-04 05:04:18 +00004879 // Enter function-declaration scope, limiting any declarators to the
4880 // function prototype scope, including parameter declarators.
4881 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004882 Scope::FunctionPrototypeScope | Scope::DeclScope |
4883 (D.isFunctionDeclaratorAFunctionDeclaration()
4884 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004885 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004886 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004887}
4888
4889/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4890/// declarator D up to a paren, which indicates that we are parsing function
4891/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004892///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004893/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4894/// immediately after the open paren - they should be considered to be the
4895/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004896///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004897/// If RequiresArg is true, then the first argument of the function is required
4898/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004899///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004900/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4901/// (C++11) ref-qualifier[opt], exception-specification[opt],
4902/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4903///
4904/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004905/// dynamic-exception-specification
4906/// noexcept-specification
4907///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004908void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004909 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004910 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004911 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004912 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004913 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004914 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004915 // lparen is already consumed!
4916 assert(D.isPastIdentifier() && "Should not call before identifier!");
4917
4918 // This should be true when the function has typed arguments.
4919 // Otherwise, it is treated as a K&R-style function.
4920 bool HasProto = false;
4921 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004922 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004923 // Remember where we see an ellipsis, if any.
4924 SourceLocation EllipsisLoc;
4925
4926 DeclSpec DS(AttrFactory);
4927 bool RefQualifierIsLValueRef = true;
4928 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004929 SourceLocation ConstQualifierLoc;
4930 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004931 ExceptionSpecificationType ESpecType = EST_None;
4932 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004933 SmallVector<ParsedType, 2> DynamicExceptions;
4934 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004935 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004936 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004937 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004938
James Molloy6f8780b2012-02-29 10:24:19 +00004939 Actions.ActOnStartFunctionDeclarator();
4940
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004941 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4942 EndLoc is the end location for the function declarator.
4943 They differ for trailing return types. */
4944 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004945 SourceLocation LParenLoc, RParenLoc;
4946 LParenLoc = Tracker.getOpenLocation();
4947 StartLoc = LParenLoc;
4948
Douglas Gregor9e66af42011-07-05 16:44:18 +00004949 if (isFunctionDeclaratorIdentifierList()) {
4950 if (RequiresArg)
4951 Diag(Tok, diag::err_argument_required_after_attribute);
4952
4953 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4954
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004955 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004956 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004957 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004958 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004959 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004960 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004961 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004962 else if (RequiresArg)
4963 Diag(Tok, diag::err_argument_required_after_attribute);
4964
David Blaikiebbafb8a2012-03-11 07:00:24 +00004965 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004966
4967 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004968 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004969 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004970 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004971 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004972
David Blaikiebbafb8a2012-03-11 07:00:24 +00004973 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004974 // FIXME: Accept these components in any order, and produce fixits to
4975 // correct the order if the user gets it wrong. Ideally we should deal
4976 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004977
4978 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00004979 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4980 /*CXX11AttributesAllowed*/ false,
4981 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004982 if (!DS.getSourceRange().getEnd().isInvalid()) {
4983 EndLoc = DS.getSourceRange().getEnd();
4984 ConstQualifierLoc = DS.getConstSpecLoc();
4985 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4986 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004987
4988 // Parse ref-qualifier[opt].
4989 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004990 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004991 diag::warn_cxx98_compat_ref_qualifier :
4992 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004993
Douglas Gregor9e66af42011-07-05 16:44:18 +00004994 RefQualifierIsLValueRef = Tok.is(tok::amp);
4995 RefQualifierLoc = ConsumeToken();
4996 EndLoc = RefQualifierLoc;
4997 }
4998
Douglas Gregor3024f072012-04-16 07:05:22 +00004999 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005000 // If a declaration declares a member function or member function
5001 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005002 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005003 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005004 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005005 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005006 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005007 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005008 (D.getContext() == Declarator::MemberContext
5009 ? !D.getDeclSpec().isFriendSpecified()
5010 : D.getContext() == Declarator::FileContext &&
5011 D.getCXXScopeSpec().isValid() &&
5012 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005013 Sema::CXXThisScopeRAII ThisScope(Actions,
5014 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005015 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005016 (D.getDeclSpec().isConstexprSpecified() &&
5017 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005018 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005019 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005020
Douglas Gregor9e66af42011-07-05 16:44:18 +00005021 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005022 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005023 DynamicExceptions,
5024 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005025 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005026 if (ESpecType != EST_None)
5027 EndLoc = ESpecRange.getEnd();
5028
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005029 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5030 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005031 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005032
Douglas Gregor9e66af42011-07-05 16:44:18 +00005033 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005034 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005035 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005036 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005037 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5038 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005039 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005040 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005041 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005042 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005043 }
5044 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005045 }
5046
5047 // Remember that we parsed a function type, and remember the attributes.
5048 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005049 IsAmbiguous,
5050 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005051 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005052 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005053 DS.getTypeQualifiers(),
5054 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005055 RefQualifierLoc, ConstQualifierLoc,
5056 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005057 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005058 ESpecType, ESpecRange.getBegin(),
5059 DynamicExceptions.data(),
5060 DynamicExceptionRanges.data(),
5061 DynamicExceptions.size(),
5062 NoexceptExpr.isUsable() ?
5063 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005064 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005065 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005066 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005067
5068 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005069}
5070
5071/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5072/// identifier list form for a K&R-style function: void foo(a,b,c)
5073///
5074/// Note that identifier-lists are only allowed for normal declarators, not for
5075/// abstract-declarators.
5076bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005077 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005078 && Tok.is(tok::identifier)
5079 && !TryAltiVecVectorToken()
5080 // K&R identifier lists can't have typedefs as identifiers, per C99
5081 // 6.7.5.3p11.
5082 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5083 // Identifier lists follow a really simple grammar: the identifiers can
5084 // be followed *only* by a ", identifier" or ")". However, K&R
5085 // identifier lists are really rare in the brave new modern world, and
5086 // it is very common for someone to typo a type in a non-K&R style
5087 // list. If we are presented with something like: "void foo(intptr x,
5088 // float y)", we don't want to start parsing the function declarator as
5089 // though it is a K&R style declarator just because intptr is an
5090 // invalid type.
5091 //
5092 // To handle this, we check to see if the token after the first
5093 // identifier is a "," or ")". Only then do we parse it as an
5094 // identifier list.
5095 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5096}
5097
5098/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5099/// we found a K&R-style identifier list instead of a typed parameter list.
5100///
5101/// After returning, ParamInfo will hold the parsed parameters.
5102///
5103/// identifier-list: [C99 6.7.5]
5104/// identifier
5105/// identifier-list ',' identifier
5106///
5107void Parser::ParseFunctionDeclaratorIdentifierList(
5108 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005109 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005110 // If there was no identifier specified for the declarator, either we are in
5111 // an abstract-declarator, or we are in a parameter declarator which was found
5112 // to be abstract. In abstract-declarators, identifier lists are not valid:
5113 // diagnose this.
5114 if (!D.getIdentifier())
5115 Diag(Tok, diag::ext_ident_list_in_param);
5116
5117 // Maintain an efficient lookup of params we have seen so far.
5118 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5119
5120 while (1) {
5121 // If this isn't an identifier, report the error and skip until ')'.
5122 if (Tok.isNot(tok::identifier)) {
5123 Diag(Tok, diag::err_expected_ident);
5124 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5125 // Forget we parsed anything.
5126 ParamInfo.clear();
5127 return;
5128 }
5129
5130 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5131
5132 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5133 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5134 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5135
5136 // Verify that the argument identifier has not already been mentioned.
5137 if (!ParamsSoFar.insert(ParmII)) {
5138 Diag(Tok, diag::err_param_redefinition) << ParmII;
5139 } else {
5140 // Remember this identifier in ParamInfo.
5141 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5142 Tok.getLocation(),
5143 0));
5144 }
5145
5146 // Eat the identifier.
5147 ConsumeToken();
5148
5149 // The list continues if we see a comma.
5150 if (Tok.isNot(tok::comma))
5151 break;
5152 ConsumeToken();
5153 }
5154}
5155
5156/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5157/// after the opening parenthesis. This function will not parse a K&R-style
5158/// identifier list.
5159///
Richard Smith2620cd92012-04-11 04:01:28 +00005160/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5161/// caller parsed those arguments immediately after the open paren - they should
5162/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005163///
5164/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5165/// be the location of the ellipsis, if any was parsed.
5166///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005167/// parameter-type-list: [C99 6.7.5]
5168/// parameter-list
5169/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005170/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005171///
5172/// parameter-list: [C99 6.7.5]
5173/// parameter-declaration
5174/// parameter-list ',' parameter-declaration
5175///
5176/// parameter-declaration: [C99 6.7.5]
5177/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005178/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005179/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005180/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005181/// declaration-specifiers abstract-declarator[opt]
5182/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005183/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005184/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005185/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005186///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005187void Parser::ParseParameterDeclarationClause(
5188 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005189 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005190 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005191 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00005192
Chris Lattner371ed4e2008-04-06 06:57:35 +00005193 while (1) {
5194 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005195 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5196 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005197 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005198 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005199 }
Mike Stump11289f42009-09-09 15:08:12 +00005200
Chris Lattner371ed4e2008-04-06 06:57:35 +00005201 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005202 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005203 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005204
Richard Smith2620cd92012-04-11 04:01:28 +00005205 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005206 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005207
John McCall53fa7142010-12-24 02:08:15 +00005208 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005209 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005210
5211 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005212
5213 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005214 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005215 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005216 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5217 // too much hassle.
5218 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005219
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005220 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005221
Chris Lattner371ed4e2008-04-06 06:57:35 +00005222 // Parse the declarator. This is "PrototypeContext", because we must
5223 // accept either 'declarator' or 'abstract-declarator' here.
5224 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5225 ParseDeclarator(ParmDecl);
5226
5227 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005228 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005229
Chris Lattner371ed4e2008-04-06 06:57:35 +00005230 // Remember this parsed parameter in ParamInfo.
5231 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005232
Douglas Gregor4d87df52008-12-16 21:30:33 +00005233 // DefArgToks is used when the parsing of default arguments needs
5234 // to be delayed.
5235 CachedTokens *DefArgToks = 0;
5236
Chris Lattner371ed4e2008-04-06 06:57:35 +00005237 // If no parameter was specified, verify that *something* was specified,
5238 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005239 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5240 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005241 // Completely missing, emit error.
5242 Diag(DSStart, diag::err_missing_param);
5243 } else {
5244 // Otherwise, we have something. Add it and let semantic analysis try
5245 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005246
Chris Lattner371ed4e2008-04-06 06:57:35 +00005247 // Inform the actions module about the parameter declarator, so it gets
5248 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005249 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005250
5251 // Parse the default argument, if any. We parse the default
5252 // arguments in all dialects; the semantic analysis in
5253 // ActOnParamDefaultArgument will reject the default argument in
5254 // C.
5255 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005256 SourceLocation EqualLoc = Tok.getLocation();
5257
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005258 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005259 if (D.getContext() == Declarator::MemberContext) {
5260 // If we're inside a class definition, cache the tokens
5261 // corresponding to the default argument. We'll actually parse
5262 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005263 // FIXME: Can we use a smart pointer for Toks?
5264 DefArgToks = new CachedTokens;
5265
Mike Stump11289f42009-09-09 15:08:12 +00005266 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005267 /*StopAtSemi=*/true,
5268 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005269 delete DefArgToks;
5270 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005271 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005272 } else {
5273 // Mark the end of the default argument so that we know when to
5274 // stop when we parse it later on.
5275 Token DefArgEnd;
5276 DefArgEnd.startToken();
5277 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5278 DefArgEnd.setLocation(Tok.getLocation());
5279 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005280 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005281 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005282 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005283 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005284 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005285 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005286
Chad Rosierc1183952012-06-26 22:30:43 +00005287 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005288 // used.
5289 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005290 Sema::PotentiallyEvaluatedIfUsed,
5291 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005292
Sebastian Redldb63af22012-03-14 15:54:00 +00005293 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005294 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005295 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005296 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005297 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005298 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005299 if (DefArgResult.isInvalid()) {
5300 Actions.ActOnParamDefaultArgumentError(Param);
5301 SkipUntil(tok::comma, tok::r_paren, true, true);
5302 } else {
5303 // Inform the actions module about the default argument
5304 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005305 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005306 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005307 }
5308 }
Mike Stump11289f42009-09-09 15:08:12 +00005309
5310 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5311 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005312 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005313 }
5314
5315 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005316 if (Tok.isNot(tok::comma)) {
5317 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005318 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005319
David Blaikiebbafb8a2012-03-11 07:00:24 +00005320 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005321 // We have ellipsis without a preceding ',', which is ill-formed
5322 // in C. Complain and provide the fix.
5323 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005324 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005325 }
5326 }
Chad Rosierc1183952012-06-26 22:30:43 +00005327
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005328 break;
5329 }
Mike Stump11289f42009-09-09 15:08:12 +00005330
Chris Lattner371ed4e2008-04-06 06:57:35 +00005331 // Consume the comma.
5332 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005333 }
Mike Stump11289f42009-09-09 15:08:12 +00005334
Chris Lattner6c940e62008-04-06 06:34:08 +00005335}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005336
Chris Lattnere8074e62006-08-06 18:30:15 +00005337/// [C90] direct-declarator '[' constant-expression[opt] ']'
5338/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5339/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5340/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5341/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005342/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5343/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005344void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005345 if (CheckProhibitedCXX11Attribute())
5346 return;
5347
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005348 BalancedDelimiterTracker T(*this, tok::l_square);
5349 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005350
Chris Lattner84a11622008-12-18 07:27:21 +00005351 // C array syntax has many features, but by-far the most common is [] and [4].
5352 // This code does a fast path to handle some of the most obvious cases.
5353 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005354 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005355 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005356 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005357
Chris Lattner84a11622008-12-18 07:27:21 +00005358 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005359 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005360 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005361 T.getOpenLocation(),
5362 T.getCloseLocation()),
5363 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005364 return;
5365 } else if (Tok.getKind() == tok::numeric_constant &&
5366 GetLookAheadToken(1).is(tok::r_square)) {
5367 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005368 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005369 ConsumeToken();
5370
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005371 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005372 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005373 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005374
Chris Lattner84a11622008-12-18 07:27:21 +00005375 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005376 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005377 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005378 T.getOpenLocation(),
5379 T.getCloseLocation()),
5380 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005381 return;
5382 }
Mike Stump11289f42009-09-09 15:08:12 +00005383
Chris Lattnere8074e62006-08-06 18:30:15 +00005384 // If valid, this location is the position where we read the 'static' keyword.
5385 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005386 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005387 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005388
Chris Lattnere8074e62006-08-06 18:30:15 +00005389 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005390 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005391 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005392 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005393
Chris Lattnere8074e62006-08-06 18:30:15 +00005394 // If we haven't already read 'static', check to see if there is one after the
5395 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005396 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005397 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005398
Chris Lattnere8074e62006-08-06 18:30:15 +00005399 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005400 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005401 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005402
Chris Lattner521ff2b2008-04-06 05:26:30 +00005403 // Handle the case where we have '[*]' as the array size. However, a leading
5404 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005405 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005406 // infrequent, use of lookahead is not costly here.
5407 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005408 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005409
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005410 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005411 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005412 StaticLoc = SourceLocation(); // Drop the static.
5413 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005414 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005415 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005416 // Note, in C89, this production uses the constant-expr production instead
5417 // of assignment-expr. The only difference is that assignment-expr allows
5418 // things like '=' and '*='. Sema rejects these in C89 mode because they
5419 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005420
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005421 // Parse the constant-expression or assignment-expression now (depending
5422 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005423 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005424 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005425 } else {
5426 EnterExpressionEvaluationContext Unevaluated(Actions,
5427 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005428 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005429 }
Chris Lattner62591722006-08-12 18:40:58 +00005430 }
Mike Stump11289f42009-09-09 15:08:12 +00005431
Chris Lattner62591722006-08-12 18:40:58 +00005432 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005433 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005434 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005435 // If the expression was invalid, skip it.
5436 SkipUntil(tok::r_square);
5437 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005438 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005439
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005440 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005441
John McCall084e83d2011-03-24 11:26:52 +00005442 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005443 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005444
Chris Lattner84a11622008-12-18 07:27:21 +00005445 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005446 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005447 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005448 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005449 T.getOpenLocation(),
5450 T.getCloseLocation()),
5451 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005452}
5453
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005454/// [GNU] typeof-specifier:
5455/// typeof ( expressions )
5456/// typeof ( type-name )
5457/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005458///
5459void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005460 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005461 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005462 SourceLocation StartLoc = ConsumeToken();
5463
John McCalle8595032010-01-13 20:03:27 +00005464 const bool hasParens = Tok.is(tok::l_paren);
5465
Eli Friedman15681d62012-09-26 04:34:21 +00005466 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5467 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005468
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005469 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005470 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005471 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005472 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5473 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005474 if (hasParens)
5475 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005476
5477 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005478 // FIXME: Not accurate, the range gets one token more than it should.
5479 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005480 else
5481 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005482
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005483 if (isCastExpr) {
5484 if (!CastTy) {
5485 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005486 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005487 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005488
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005489 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005490 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005491 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5492 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005493 DiagID, CastTy))
5494 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005495 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005496 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005497
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005498 // If we get here, the operand to the typeof was an expresion.
5499 if (Operand.isInvalid()) {
5500 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005501 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005502 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005503
Eli Friedmane0afc982012-01-21 01:01:51 +00005504 // We might need to transform the operand if it is potentially evaluated.
5505 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5506 if (Operand.isInvalid()) {
5507 DS.SetTypeSpecError();
5508 return;
5509 }
5510
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005511 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005512 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005513 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5514 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005515 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005516 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005517}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005518
Benjamin Kramere56f3932011-12-23 17:00:35 +00005519/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005520/// _Atomic ( type-name )
5521///
5522void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005523 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5524 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005525
5526 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005527 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005528 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005529 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005530
5531 TypeResult Result = ParseTypeName();
5532 if (Result.isInvalid()) {
5533 SkipUntil(tok::r_paren);
5534 return;
5535 }
5536
5537 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005538 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005539
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005540 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005541 return;
5542
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005543 DS.setTypeofParensRange(T.getRange());
5544 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005545
5546 const char *PrevSpec = 0;
5547 unsigned DiagID;
5548 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5549 DiagID, Result.release()))
5550 Diag(StartLoc, DiagID) << PrevSpec;
5551}
5552
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005553
5554/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5555/// from TryAltiVecVectorToken.
5556bool Parser::TryAltiVecVectorTokenOutOfLine() {
5557 Token Next = NextToken();
5558 switch (Next.getKind()) {
5559 default: return false;
5560 case tok::kw_short:
5561 case tok::kw_long:
5562 case tok::kw_signed:
5563 case tok::kw_unsigned:
5564 case tok::kw_void:
5565 case tok::kw_char:
5566 case tok::kw_int:
5567 case tok::kw_float:
5568 case tok::kw_double:
5569 case tok::kw_bool:
5570 case tok::kw___pixel:
5571 Tok.setKind(tok::kw___vector);
5572 return true;
5573 case tok::identifier:
5574 if (Next.getIdentifierInfo() == Ident_pixel) {
5575 Tok.setKind(tok::kw___vector);
5576 return true;
5577 }
5578 return false;
5579 }
5580}
5581
5582bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5583 const char *&PrevSpec, unsigned &DiagID,
5584 bool &isInvalid) {
5585 if (Tok.getIdentifierInfo() == Ident_vector) {
5586 Token Next = NextToken();
5587 switch (Next.getKind()) {
5588 case tok::kw_short:
5589 case tok::kw_long:
5590 case tok::kw_signed:
5591 case tok::kw_unsigned:
5592 case tok::kw_void:
5593 case tok::kw_char:
5594 case tok::kw_int:
5595 case tok::kw_float:
5596 case tok::kw_double:
5597 case tok::kw_bool:
5598 case tok::kw___pixel:
5599 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5600 return true;
5601 case tok::identifier:
5602 if (Next.getIdentifierInfo() == Ident_pixel) {
5603 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5604 return true;
5605 }
5606 break;
5607 default:
5608 break;
5609 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005610 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005611 DS.isTypeAltiVecVector()) {
5612 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5613 return true;
5614 }
5615 return false;
5616}