blob: 932ffb440fd271f3d2486d13496c8842854b647b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000023#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.7: Declarations.
28//===----------------------------------------------------------------------===//
29
30/// ParseTypeName
31/// type-name: [C99 6.7.6]
32/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000033///
34/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000035TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000036 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000037 AccessSpecifier AS,
38 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000039 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith7796eb52012-03-12 08:56:40 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000042 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000043 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000044 if (OwnedType)
45 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000048 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000050 if (Range)
51 *Range = DeclaratorInfo.getSourceRange();
52
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000054 return true;
55
Douglas Gregor23c94db2010-07-02 17:43:08 +000056 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000059
60/// isAttributeLateParsed - Return true if the attribute has arguments that
61/// require late parsing.
62static bool isAttributeLateParsed(const IdentifierInfo &II) {
63 return llvm::StringSwitch<bool>(II.getName())
64#include "clang/Parse/AttrLateParsed.inc"
65 .Default(false);
66}
67
68
Sean Huntbbd37c62009-11-21 08:43:09 +000069/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71/// [GNU] attributes:
72/// attribute
73/// attributes attribute
74///
75/// [GNU] attribute:
76/// '__attribute__' '(' '(' attribute-list ')' ')'
77///
78/// [GNU] attribute-list:
79/// attrib
80/// attribute_list ',' attrib
81///
82/// [GNU] attrib:
83/// empty
84/// attrib-name
85/// attrib-name '(' identifier ')'
86/// attrib-name '(' identifier ',' nonempty-expr-list ')'
87/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
88///
89/// [GNU] attrib-name:
90/// identifier
91/// typespec
92/// typequal
93/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000094///
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000096/// token lookahead. Comment from gcc: "If they start with an identifier
97/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// start with that identifier; otherwise they are an expression list."
99///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000100/// GCC does not require the ',' between attribs in an attribute-list.
101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// At the moment, I am not doing 2 token lookahead. I am also unaware of
103/// any attributes that don't work (based on my limited testing). Most
104/// attributes are very simple in practice. Until we find a bug, I don't see
105/// a pressing need to implement the 2 token lookahead.
106
John McCall7f040a92010-12-24 02:08:15 +0000107void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000108 SourceLocation *endLoc,
109 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000110 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeToken();
114 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
115 "attribute")) {
116 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000117 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000124 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
125 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
128 ConsumeToken();
129 continue;
130 }
131 // we have an identifier or declaration specifier (const, int, etc.)
132 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
133 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000135 if (Tok.is(tok::l_paren)) {
136 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000137 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141
142 // Attributes in a class are parsed at the end of the class, along
143 // with other late-parsed declarations.
144 if (!ClassStack.empty())
145 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000147 // consume everything up to and including the matching right parens
148 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 Token Eof;
151 Eof.startToken();
152 Eof.setLocation(Tok.getLocation());
153 LA->Toks.push_back(Eof);
154 } else {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000158 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
159 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 }
162 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000164 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
166 SkipUntil(tok::r_paren, false);
167 }
John McCall7f040a92010-12-24 02:08:15 +0000168 if (endLoc)
169 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000173
174/// Parse the arguments to a parameterized GNU attribute
175void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
176 SourceLocation AttrNameLoc,
177 ParsedAttributes &Attrs,
178 SourceLocation *EndLoc) {
179
180 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
181
182 // Availability attributes have their own grammar.
183 if (AttrName->isStr("availability")) {
184 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
185 return;
186 }
187 // Thread safety attributes fit into the FIXME case above, so we
188 // just parse the arguments as a list of expressions
189 if (IsThreadSafetyAttribute(AttrName->getName())) {
190 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
191 return;
192 }
193
194 ConsumeParen(); // ignore the left paren loc for now
195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 IdentifierInfo *ParmName = 0;
197 SourceLocation ParmLoc;
198 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000199
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000200 switch (Tok.getKind()) {
201 case tok::kw_char:
202 case tok::kw_wchar_t:
203 case tok::kw_char16_t:
204 case tok::kw_char32_t:
205 case tok::kw_bool:
206 case tok::kw_short:
207 case tok::kw_int:
208 case tok::kw_long:
209 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000210 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000211 case tok::kw_signed:
212 case tok::kw_unsigned:
213 case tok::kw_float:
214 case tok::kw_double:
215 case tok::kw_void:
216 case tok::kw_typeof:
217 // __attribute__(( vec_type_hint(char) ))
218 // FIXME: Don't just discard the builtin type token.
219 ConsumeToken();
220 BuiltinType = true;
221 break;
222
223 case tok::identifier:
224 ParmName = Tok.getIdentifierInfo();
225 ParmLoc = ConsumeToken();
226 break;
227
228 default:
229 break;
230 }
231
232 ExprVector ArgExprs(Actions);
233
234 if (!BuiltinType &&
235 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
236 // Eat the comma.
237 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000238 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000239
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000240 // Parse the non-empty comma-separated list of expressions.
241 while (1) {
242 ExprResult ArgExpr(ParseAssignmentExpression());
243 if (ArgExpr.isInvalid()) {
244 SkipUntil(tok::r_paren);
245 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 ArgExprs.push_back(ArgExpr.release());
248 if (Tok.isNot(tok::comma))
249 break;
250 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000252 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000253 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
254 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
255 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000256 while (Tok.is(tok::identifier)) {
257 ConsumeToken();
258 if (Tok.is(tok::greater))
259 break;
260 if (Tok.is(tok::comma)) {
261 ConsumeToken();
262 continue;
263 }
264 }
265 if (Tok.isNot(tok::greater))
266 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000267 SkipUntil(tok::r_paren, false, true); // skip until ')'
268 }
269 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270
271 SourceLocation RParen = Tok.getLocation();
272 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
273 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000274 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000275 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000276 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000277 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000278 }
279}
280
281
Eli Friedmana23b4852009-06-08 07:21:15 +0000282/// ParseMicrosoftDeclSpec - Parse an __declspec construct
283///
284/// [MS] decl-specifier:
285/// __declspec ( extended-decl-modifier-seq )
286///
287/// [MS] extended-decl-modifier-seq:
288/// extended-decl-modifier[opt]
289/// extended-decl-modifier extended-decl-modifier-seq
290
John McCall7f040a92010-12-24 02:08:15 +0000291void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000292 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000293
Steve Narofff59e17e2008-12-24 20:59:21 +0000294 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000295 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
296 "declspec")) {
297 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000298 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000299 }
Francois Pichet373197b2011-05-07 19:04:49 +0000300
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000302 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
303 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000304
305 // FIXME: Remove this when we have proper __declspec(property()) support.
306 // Just skip everything inside property().
307 if (AttrName->getName() == "property") {
308 ConsumeParen();
309 SkipUntil(tok::r_paren);
310 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (Tok.is(tok::l_paren)) {
312 ConsumeParen();
313 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
314 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000315 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000316 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000317 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000318 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
319 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000320 }
321 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
322 SkipUntil(tok::r_paren, false);
323 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
325 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000326 }
327 }
328 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
329 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000330 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000331}
332
John McCall7f040a92010-12-24 02:08:15 +0000333void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000334 // Treat these like attributes
335 // FIXME: Allow Sema to distinguish between these and real attributes!
336 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000337 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000338 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000339 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000340 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000341 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
342 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000343 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
344 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000345 // FIXME: Support these properly!
346 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000347 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
348 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000349 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000350}
351
John McCall7f040a92010-12-24 02:08:15 +0000352void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000353 // Treat these like attributes
354 while (Tok.is(tok::kw___pascal)) {
355 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
356 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000357 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
358 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000359 }
John McCall7f040a92010-12-24 02:08:15 +0000360}
361
Peter Collingbournef315fa82011-02-14 01:42:53 +0000362void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
363 // Treat these like attributes
364 while (Tok.is(tok::kw___kernel)) {
365 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000366 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
367 AttrNameLoc, 0, AttrNameLoc, 0,
368 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000369 }
370}
371
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
373 SourceLocation Loc = Tok.getLocation();
374 switch(Tok.getKind()) {
375 // OpenCL qualifiers:
376 case tok::kw___private:
377 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000378 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000379 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000380 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 break;
382
383 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000384 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000385 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000386 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000387 break;
388
389 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000390 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000391 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000392 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 break;
394
395 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000396 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000397 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000398 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 break;
400
401 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000402 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000403 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000404 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 break;
406
407 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000408 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000409 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000410 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 break;
412
413 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000414 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000415 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000416 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 break;
418 default: break;
419 }
420}
421
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000422/// \brief Parse a version number.
423///
424/// version:
425/// simple-integer
426/// simple-integer ',' simple-integer
427/// simple-integer ',' simple-integer ',' simple-integer
428VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
429 Range = Tok.getLocation();
430
431 if (!Tok.is(tok::numeric_constant)) {
432 Diag(Tok, diag::err_expected_version);
433 SkipUntil(tok::comma, tok::r_paren, true, true, true);
434 return VersionTuple();
435 }
436
437 // Parse the major (and possibly minor and subminor) versions, which
438 // are stored in the numeric constant. We utilize a quirk of the
439 // lexer, which is that it handles something like 1.2.3 as a single
440 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000441 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000442 Buffer.resize(Tok.getLength()+1);
443 const char *ThisTokBegin = &Buffer[0];
444
445 // Get the spelling of the token, which eliminates trigraphs, etc.
446 bool Invalid = false;
447 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
448 if (Invalid)
449 return VersionTuple();
450
451 // Parse the major version.
452 unsigned AfterMajor = 0;
453 unsigned Major = 0;
454 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
455 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
456 ++AfterMajor;
457 }
458
459 if (AfterMajor == 0) {
460 Diag(Tok, diag::err_expected_version);
461 SkipUntil(tok::comma, tok::r_paren, true, true, true);
462 return VersionTuple();
463 }
464
465 if (AfterMajor == ActualLength) {
466 ConsumeToken();
467
468 // We only had a single version component.
469 if (Major == 0) {
470 Diag(Tok, diag::err_zero_version);
471 return VersionTuple();
472 }
473
474 return VersionTuple(Major);
475 }
476
477 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
478 Diag(Tok, diag::err_expected_version);
479 SkipUntil(tok::comma, tok::r_paren, true, true, true);
480 return VersionTuple();
481 }
482
483 // Parse the minor version.
484 unsigned AfterMinor = AfterMajor + 1;
485 unsigned Minor = 0;
486 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
487 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
488 ++AfterMinor;
489 }
490
491 if (AfterMinor == ActualLength) {
492 ConsumeToken();
493
494 // We had major.minor.
495 if (Major == 0 && Minor == 0) {
496 Diag(Tok, diag::err_zero_version);
497 return VersionTuple();
498 }
499
500 return VersionTuple(Major, Minor);
501 }
502
503 // If what follows is not a '.', we have a problem.
504 if (ThisTokBegin[AfterMinor] != '.') {
505 Diag(Tok, diag::err_expected_version);
506 SkipUntil(tok::comma, tok::r_paren, true, true, true);
507 return VersionTuple();
508 }
509
510 // Parse the subminor version.
511 unsigned AfterSubminor = AfterMinor + 1;
512 unsigned Subminor = 0;
513 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
514 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
515 ++AfterSubminor;
516 }
517
518 if (AfterSubminor != ActualLength) {
519 Diag(Tok, diag::err_expected_version);
520 SkipUntil(tok::comma, tok::r_paren, true, true, true);
521 return VersionTuple();
522 }
523 ConsumeToken();
524 return VersionTuple(Major, Minor, Subminor);
525}
526
527/// \brief Parse the contents of the "availability" attribute.
528///
529/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000530/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000531///
532/// platform:
533/// identifier
534///
535/// version-arg-list:
536/// version-arg
537/// version-arg ',' version-arg-list
538///
539/// version-arg:
540/// 'introduced' '=' version
541/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000542/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000543/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000544/// opt-message:
545/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000546void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
547 SourceLocation AvailabilityLoc,
548 ParsedAttributes &attrs,
549 SourceLocation *endLoc) {
550 SourceLocation PlatformLoc;
551 IdentifierInfo *Platform = 0;
552
553 enum { Introduced, Deprecated, Obsoleted, Unknown };
554 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000555 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000556
557 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000558 BalancedDelimiterTracker T(*this, tok::l_paren);
559 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560 Diag(Tok, diag::err_expected_lparen);
561 return;
562 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563
564 // Parse the platform name,
565 if (Tok.isNot(tok::identifier)) {
566 Diag(Tok, diag::err_availability_expected_platform);
567 SkipUntil(tok::r_paren);
568 return;
569 }
570 Platform = Tok.getIdentifierInfo();
571 PlatformLoc = ConsumeToken();
572
573 // Parse the ',' following the platform name.
574 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
575 return;
576
577 // If we haven't grabbed the pointers for the identifiers
578 // "introduced", "deprecated", and "obsoleted", do so now.
579 if (!Ident_introduced) {
580 Ident_introduced = PP.getIdentifierInfo("introduced");
581 Ident_deprecated = PP.getIdentifierInfo("deprecated");
582 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000583 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000584 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000585 }
586
587 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000588 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000589 do {
590 if (Tok.isNot(tok::identifier)) {
591 Diag(Tok, diag::err_availability_expected_change);
592 SkipUntil(tok::r_paren);
593 return;
594 }
595 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
596 SourceLocation KeywordLoc = ConsumeToken();
597
Douglas Gregorb53e4172011-03-26 03:35:55 +0000598 if (Keyword == Ident_unavailable) {
599 if (UnavailableLoc.isValid()) {
600 Diag(KeywordLoc, diag::err_availability_redundant)
601 << Keyword << SourceRange(UnavailableLoc);
602 }
603 UnavailableLoc = KeywordLoc;
604
605 if (Tok.isNot(tok::comma))
606 break;
607
608 ConsumeToken();
609 continue;
610 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000611
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000612 if (Tok.isNot(tok::equal)) {
613 Diag(Tok, diag::err_expected_equal_after)
614 << Keyword;
615 SkipUntil(tok::r_paren);
616 return;
617 }
618 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000619 if (Keyword == Ident_message) {
620 if (!isTokenStringLiteral()) {
621 Diag(Tok, diag::err_expected_string_literal);
622 SkipUntil(tok::r_paren);
623 return;
624 }
625 MessageExpr = ParseStringLiteralExpression();
626 break;
627 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628
629 SourceRange VersionRange;
630 VersionTuple Version = ParseVersionTuple(VersionRange);
631
632 if (Version.empty()) {
633 SkipUntil(tok::r_paren);
634 return;
635 }
636
637 unsigned Index;
638 if (Keyword == Ident_introduced)
639 Index = Introduced;
640 else if (Keyword == Ident_deprecated)
641 Index = Deprecated;
642 else if (Keyword == Ident_obsoleted)
643 Index = Obsoleted;
644 else
645 Index = Unknown;
646
647 if (Index < Unknown) {
648 if (!Changes[Index].KeywordLoc.isInvalid()) {
649 Diag(KeywordLoc, diag::err_availability_redundant)
650 << Keyword
651 << SourceRange(Changes[Index].KeywordLoc,
652 Changes[Index].VersionRange.getEnd());
653 }
654
655 Changes[Index].KeywordLoc = KeywordLoc;
656 Changes[Index].Version = Version;
657 Changes[Index].VersionRange = VersionRange;
658 } else {
659 Diag(KeywordLoc, diag::err_availability_unknown_change)
660 << Keyword << VersionRange;
661 }
662
663 if (Tok.isNot(tok::comma))
664 break;
665
666 ConsumeToken();
667 } while (true);
668
669 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000670 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000671 return;
672
673 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000674 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000675
Douglas Gregorb53e4172011-03-26 03:35:55 +0000676 // The 'unavailable' availability cannot be combined with any other
677 // availability changes. Make sure that hasn't happened.
678 if (UnavailableLoc.isValid()) {
679 bool Complained = false;
680 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
681 if (Changes[Index].KeywordLoc.isValid()) {
682 if (!Complained) {
683 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
684 << SourceRange(Changes[Index].KeywordLoc,
685 Changes[Index].VersionRange.getEnd());
686 Complained = true;
687 }
688
689 // Clear out the availability.
690 Changes[Index] = AvailabilityChange();
691 }
692 }
693 }
694
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000696 attrs.addNew(&Availability,
697 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000698 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000699 Platform, PlatformLoc,
700 Changes[Introduced],
701 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000702 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000703 UnavailableLoc, MessageExpr.take(),
704 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705}
706
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000707
708// Late Parsed Attributes:
709// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
710
711void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
712
713void Parser::LateParsedClass::ParseLexedAttributes() {
714 Self->ParseLexedAttributes(*Class);
715}
716
717void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000718 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000719}
720
721/// Wrapper class which calls ParseLexedAttribute, after setting up the
722/// scope appropriately.
723void Parser::ParseLexedAttributes(ParsingClass &Class) {
724 // Deal with templates
725 // FIXME: Test cases to make sure this does the right thing for templates.
726 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
727 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
728 HasTemplateScope);
729 if (HasTemplateScope)
730 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
731
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000732 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000733 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000734 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000735 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
736 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
737
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000738 // Enter the scope of nested classes
739 if (!AlreadyHasClassScope)
740 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
741 Class.TagOrTemplate);
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000742 {
743 // Allow 'this' within late-parsed attributes.
744 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
745 /*TypeQuals=*/0);
746
747 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
748 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
749 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000750 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000751
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000752 if (!AlreadyHasClassScope)
753 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
754 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755}
756
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000757
758/// \brief Parse all attributes in LAs, and attach them to Decl D.
759void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
760 bool EnterScope, bool OnDefinition) {
761 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000762 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000763 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000764 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000765 }
766 LAs.clear();
767}
768
769
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000770/// \brief Finish parsing an attribute for which parsing was delayed.
771/// This will be called at the end of parsing a class declaration
772/// for each LateParsedAttribute. We consume the saved tokens and
773/// create an attribute with the arguments filled in. We add this
774/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000775void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
776 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000777 // Save the current token position.
778 SourceLocation OrigLoc = Tok.getLocation();
779
780 // Append the current token at the end of the new token stream so that it
781 // doesn't get lost.
782 LA.Toks.push_back(Tok);
783 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
784 // Consume the previously pushed token.
785 ConsumeAnyToken();
786
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000787 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
788 Diag(Tok, diag::warn_attribute_on_function_definition)
789 << LA.AttrName.getName();
790 }
791
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000792 ParsedAttributes Attrs(AttrFactory);
793 SourceLocation endLoc;
794
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000795 if (LA.Decls.size() == 1) {
796 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000797
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000798 // If the Decl is templatized, add template parameters to scope.
799 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
800 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
801 if (HasTemplateScope)
802 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000803
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000804 // If the Decl is on a function, add function parameters to the scope.
805 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
806 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
807 if (HasFunctionScope)
808 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
809
810 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
811
812 if (HasFunctionScope) {
813 Actions.ActOnExitFunctionContext();
814 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
815 }
816 if (HasTemplateScope) {
817 TempScope.Exit();
818 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000819 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000820 // If there are multiple decls, then the decl cannot be within the
821 // function scope.
822 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000823 } else {
824 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000825 }
826
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000827 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
828 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
829 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000830
831 if (Tok.getLocation() != OrigLoc) {
832 // Due to a parsing error, we either went over the cached tokens or
833 // there are still cached tokens left, so we skip the leftover tokens.
834 // Since this is an uncommon situation that should be avoided, use the
835 // expensive isBeforeInTranslationUnit call.
836 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
837 OrigLoc))
838 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000839 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840 }
841}
842
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000843/// \brief Wrapper around a case statement checking if AttrName is
844/// one of the thread safety attributes
845bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
846 return llvm::StringSwitch<bool>(AttrName)
847 .Case("guarded_by", true)
848 .Case("guarded_var", true)
849 .Case("pt_guarded_by", true)
850 .Case("pt_guarded_var", true)
851 .Case("lockable", true)
852 .Case("scoped_lockable", true)
853 .Case("no_thread_safety_analysis", true)
854 .Case("acquired_after", true)
855 .Case("acquired_before", true)
856 .Case("exclusive_lock_function", true)
857 .Case("shared_lock_function", true)
858 .Case("exclusive_trylock_function", true)
859 .Case("shared_trylock_function", true)
860 .Case("unlock_function", true)
861 .Case("lock_returned", true)
862 .Case("locks_excluded", true)
863 .Case("exclusive_locks_required", true)
864 .Case("shared_locks_required", true)
865 .Default(false);
866}
867
868/// \brief Parse the contents of thread safety attributes. These
869/// should always be parsed as an expression list.
870///
871/// We need to special case the parsing due to the fact that if the first token
872/// of the first argument is an identifier, the main parse loop will store
873/// that token as a "parameter" and the rest of
874/// the arguments will be added to a list of "arguments". However,
875/// subsequent tokens in the first argument are lost. We instead parse each
876/// argument as an expression and add all arguments to the list of "arguments".
877/// In future, we will take advantage of this special case to also
878/// deal with some argument scoping issues here (for example, referring to a
879/// function parameter in the attribute on that function).
880void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
881 SourceLocation AttrNameLoc,
882 ParsedAttributes &Attrs,
883 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000884 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000886 BalancedDelimiterTracker T(*this, tok::l_paren);
887 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888
889 ExprVector ArgExprs(Actions);
890 bool ArgExprsOk = true;
891
892 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000893 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894 ExprResult ArgExpr(ParseAssignmentExpression());
895 if (ArgExpr.isInvalid()) {
896 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000897 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000898 break;
899 } else {
900 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000901 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000902 if (Tok.isNot(tok::comma))
903 break;
904 ConsumeToken(); // Eat the comma, move to the next argument
905 }
906 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000907 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000908 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
909 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000910 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000911 if (EndLoc)
912 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000913}
914
Richard Smith6ee326a2012-04-10 01:32:12 +0000915/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
916/// of a C++11 attribute-specifier in a location where an attribute is not
917/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
918/// situation.
919///
920/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
921/// this doesn't appear to actually be an attribute-specifier, and the caller
922/// should try to parse it.
923bool Parser::DiagnoseProhibitedCXX11Attribute() {
924 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
925
926 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
927 case CAK_NotAttributeSpecifier:
928 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
929 return false;
930
931 case CAK_InvalidAttributeSpecifier:
932 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
933 return false;
934
935 case CAK_AttributeSpecifier:
936 // Parse and discard the attributes.
937 SourceLocation BeginLoc = ConsumeBracket();
938 ConsumeBracket();
939 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
940 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
941 SourceLocation EndLoc = ConsumeBracket();
942 Diag(BeginLoc, diag::err_attributes_not_allowed)
943 << SourceRange(BeginLoc, EndLoc);
944 return true;
945 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000946 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000947}
948
John McCall7f040a92010-12-24 02:08:15 +0000949void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
950 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
951 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000952}
953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954/// ParseDeclaration - Parse a full 'declaration', which consists of
955/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000956/// 'Context' should be a Declarator::TheContext value. This returns the
957/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000958///
959/// declaration: [C99 6.7]
960/// block-declaration ->
961/// simple-declaration
962/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000963/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000964/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000965/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000966/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000967/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000968/// others... [FIXME]
969///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000970Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
971 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000972 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000973 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000974 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000975 // Must temporarily exit the objective-c container scope for
976 // parsing c none objective-c decls.
977 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000978
John McCalld226f652010-08-21 09:40:31 +0000979 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000980 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000981 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000982 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000983 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000984 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000985 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000986 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000987 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000988 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000989 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000990 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000991 SourceLocation InlineLoc = ConsumeToken();
992 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
993 break;
994 }
John McCall7f040a92010-12-24 02:08:15 +0000995 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000996 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000997 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000998 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000999 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001000 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001001 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001002 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001003 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001004 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001005 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001006 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001007 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001008 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001009 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001010 default:
John McCall7f040a92010-12-24 02:08:15 +00001011 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001012 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001013
Chris Lattner682bf922009-03-29 16:50:03 +00001014 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001015 // single decl, convert it now. Alias declarations can also declare a type;
1016 // include that too if it is present.
1017 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001018}
1019
1020/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1021/// declaration-specifiers init-declarator-list[opt] ';'
1022///[C90/C++]init-declarator-list ';' [TODO]
1023/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001024///
Richard Smithad762fc2011-04-14 22:09:26 +00001025/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1026/// attribute-specifier-seq[opt] type-specifier-seq declarator
1027///
Chris Lattnercd147752009-03-29 17:27:48 +00001028/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001029/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001030///
1031/// If FRI is non-null, we might be parsing a for-range-declaration instead
1032/// of a simple-declaration. If we find that we are, we also parse the
1033/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001034Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1035 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001036 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001037 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001038 bool RequireSemi,
1039 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001041 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001042 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001043
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001044 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001045 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1048 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001049 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001050 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001051 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001052 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001053 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001054 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001056
1057 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001058}
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Richard Smith0706df42011-10-19 21:33:05 +00001060/// Returns true if this might be the start of a declarator, or a common typo
1061/// for a declarator.
1062bool Parser::MightBeDeclarator(unsigned Context) {
1063 switch (Tok.getKind()) {
1064 case tok::annot_cxxscope:
1065 case tok::annot_template_id:
1066 case tok::caret:
1067 case tok::code_completion:
1068 case tok::coloncolon:
1069 case tok::ellipsis:
1070 case tok::kw___attribute:
1071 case tok::kw_operator:
1072 case tok::l_paren:
1073 case tok::star:
1074 return true;
1075
1076 case tok::amp:
1077 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001078 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001079
Richard Smith1c94c162012-01-09 22:31:44 +00001080 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001081 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001082 NextToken().is(tok::l_square);
1083
1084 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001085 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001086
Richard Smith0706df42011-10-19 21:33:05 +00001087 case tok::identifier:
1088 switch (NextToken().getKind()) {
1089 case tok::code_completion:
1090 case tok::coloncolon:
1091 case tok::comma:
1092 case tok::equal:
1093 case tok::equalequal: // Might be a typo for '='.
1094 case tok::kw_alignas:
1095 case tok::kw_asm:
1096 case tok::kw___attribute:
1097 case tok::l_brace:
1098 case tok::l_paren:
1099 case tok::l_square:
1100 case tok::less:
1101 case tok::r_brace:
1102 case tok::r_paren:
1103 case tok::r_square:
1104 case tok::semi:
1105 return true;
1106
1107 case tok::colon:
1108 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001109 // and in block scope it's probably a label. Inside a class definition,
1110 // this is a bit-field.
1111 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001112 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001113
1114 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001115 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001116
1117 default:
1118 return false;
1119 }
1120
1121 default:
1122 return false;
1123 }
1124}
1125
Richard Smith994d73f2012-04-11 20:59:20 +00001126/// Skip until we reach something which seems like a sensible place to pick
1127/// up parsing after a malformed declaration. This will sometimes stop sooner
1128/// than SkipUntil(tok::r_brace) would, but will never stop later.
1129void Parser::SkipMalformedDecl() {
1130 while (true) {
1131 switch (Tok.getKind()) {
1132 case tok::l_brace:
1133 // Skip until matching }, then stop. We've probably skipped over
1134 // a malformed class or function definition or similar.
1135 ConsumeBrace();
1136 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1137 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1138 // This declaration isn't over yet. Keep skipping.
1139 continue;
1140 }
1141 if (Tok.is(tok::semi))
1142 ConsumeToken();
1143 return;
1144
1145 case tok::l_square:
1146 ConsumeBracket();
1147 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1148 continue;
1149
1150 case tok::l_paren:
1151 ConsumeParen();
1152 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1153 continue;
1154
1155 case tok::r_brace:
1156 return;
1157
1158 case tok::semi:
1159 ConsumeToken();
1160 return;
1161
1162 case tok::kw_inline:
1163 // 'inline namespace' at the start of a line is almost certainly
1164 // a good place to pick back up parsing.
1165 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1166 return;
1167 break;
1168
1169 case tok::kw_namespace:
1170 // 'namespace' at the start of a line is almost certainly a good
1171 // place to pick back up parsing.
1172 if (Tok.isAtStartOfLine())
1173 return;
1174 break;
1175
1176 case tok::eof:
1177 return;
1178
1179 default:
1180 break;
1181 }
1182
1183 ConsumeAnyToken();
1184 }
1185}
1186
John McCalld8ac0572009-11-03 19:26:08 +00001187/// ParseDeclGroup - Having concluded that this is either a function
1188/// definition or a group of object declarations, actually parse the
1189/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001190Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1191 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001192 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001193 SourceLocation *DeclEnd,
1194 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001195 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001196 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001197 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001198
John McCalld8ac0572009-11-03 19:26:08 +00001199 // Bail out if the first declarator didn't seem well-formed.
1200 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001201 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001202 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001205 // Save late-parsed attributes for now; they need to be parsed in the
1206 // appropriate function scope after the function Decl has been constructed.
1207 LateParsedAttrList LateParsedAttrs;
1208 if (D.isFunctionDeclarator())
1209 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1210
Chris Lattnerc82daef2010-07-11 22:24:20 +00001211 // Check to see if we have a function *definition* which must have a body.
1212 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1213 // Look at the next token to make sure that this isn't a function
1214 // declaration. We have to check this because __attribute__ might be the
1215 // start of a function definition in GCC-extended K&R C.
1216 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001217
Chris Lattner004659a2010-07-11 22:42:07 +00001218 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001219 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1220 Diag(Tok, diag::err_function_declared_typedef);
1221
1222 // Recover by treating the 'typedef' as spurious.
1223 DS.ClearStorageClassSpecs();
1224 }
1225
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001226 Decl *TheDecl =
1227 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001228 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001229 }
1230
1231 if (isDeclarationSpecifier()) {
1232 // If there is an invalid declaration specifier right after the function
1233 // prototype, then we must be in a missing semicolon case where this isn't
1234 // actually a body. Just fall through into the code that handles it as a
1235 // prototype, and let the top-level code handle the erroneous declspec
1236 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001237 } else {
1238 Diag(Tok, diag::err_expected_fn_body);
1239 SkipUntil(tok::semi);
1240 return DeclGroupPtrTy();
1241 }
1242 }
1243
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001244 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001245 return DeclGroupPtrTy();
1246
1247 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1248 // must parse and analyze the for-range-initializer before the declaration is
1249 // analyzed.
1250 if (FRI && Tok.is(tok::colon)) {
1251 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001252 if (Tok.is(tok::l_brace))
1253 FRI->RangeExpr = ParseBraceInitializer();
1254 else
1255 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001256 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1257 Actions.ActOnCXXForRangeDecl(ThisDecl);
1258 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001259 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001260 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1261 }
1262
Chris Lattner5f9e2722011-07-23 10:55:15 +00001263 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001264 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001265 if (LateParsedAttrs.size() > 0)
1266 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001267 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001268 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001269 DeclsInGroup.push_back(FirstDecl);
1270
Richard Smith0706df42011-10-19 21:33:05 +00001271 bool ExpectSemi = Context != Declarator::ForContext;
1272
John McCalld8ac0572009-11-03 19:26:08 +00001273 // If we don't have a comma, it is either the end of the list (a ';') or an
1274 // error, bail out.
1275 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001276 SourceLocation CommaLoc = ConsumeToken();
1277
1278 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1279 // This comma was followed by a line-break and something which can't be
1280 // the start of a declarator. The comma was probably a typo for a
1281 // semicolon.
1282 Diag(CommaLoc, diag::err_expected_semi_declaration)
1283 << FixItHint::CreateReplacement(CommaLoc, ";");
1284 ExpectSemi = false;
1285 break;
1286 }
John McCalld8ac0572009-11-03 19:26:08 +00001287
1288 // Parse the next declarator.
1289 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001290 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001291
1292 // Accept attributes in an init-declarator. In the first declarator in a
1293 // declaration, these would be part of the declspec. In subsequent
1294 // declarators, they become part of the declarator itself, so that they
1295 // don't apply to declarators after *this* one. Examples:
1296 // short __attribute__((common)) var; -> declspec
1297 // short var __attribute__((common)); -> declarator
1298 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001299 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001300
1301 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001302 if (!D.isInvalidType()) {
1303 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1304 D.complete(ThisDecl);
1305 if (ThisDecl)
1306 DeclsInGroup.push_back(ThisDecl);
1307 }
John McCalld8ac0572009-11-03 19:26:08 +00001308 }
1309
1310 if (DeclEnd)
1311 *DeclEnd = Tok.getLocation();
1312
Richard Smith0706df42011-10-19 21:33:05 +00001313 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001314 ExpectAndConsume(tok::semi,
1315 Context == Declarator::FileContext
1316 ? diag::err_invalid_token_after_toplevel_declarator
1317 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001318 // Okay, there was no semicolon and one was expected. If we see a
1319 // declaration specifier, just assume it was missing and continue parsing.
1320 // Otherwise things are very confused and we skip to recover.
1321 if (!isDeclarationSpecifier()) {
1322 SkipUntil(tok::r_brace, true, true);
1323 if (Tok.is(tok::semi))
1324 ConsumeToken();
1325 }
John McCalld8ac0572009-11-03 19:26:08 +00001326 }
1327
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001329 DeclsInGroup.data(),
1330 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
1332
Richard Smithad762fc2011-04-14 22:09:26 +00001333/// Parse an optional simple-asm-expr and attributes, and attach them to a
1334/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001335bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001336 // If a simple-asm-expr is present, parse it.
1337 if (Tok.is(tok::kw_asm)) {
1338 SourceLocation Loc;
1339 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1340 if (AsmLabel.isInvalid()) {
1341 SkipUntil(tok::semi, true, true);
1342 return true;
1343 }
1344
1345 D.setAsmLabel(AsmLabel.release());
1346 D.SetRangeEnd(Loc);
1347 }
1348
1349 MaybeParseGNUAttributes(D);
1350 return false;
1351}
1352
Douglas Gregor1426e532009-05-12 21:31:51 +00001353/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1354/// declarator'. This method parses the remainder of the declaration
1355/// (including any attributes or initializer, among other things) and
1356/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001357///
Reid Spencer5f016e22007-07-11 17:01:13 +00001358/// init-declarator: [C99 6.7]
1359/// declarator
1360/// declarator '=' initializer
1361/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1362/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001363/// [C++] declarator initializer[opt]
1364///
1365/// [C++] initializer:
1366/// [C++] '=' initializer-clause
1367/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001368/// [C++0x] '=' 'default' [TODO]
1369/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001370/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001371///
1372/// According to the standard grammar, =default and =delete are function
1373/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001374///
John McCalld226f652010-08-21 09:40:31 +00001375Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001376 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001377 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001378 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Richard Smithad762fc2011-04-14 22:09:26 +00001380 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1381}
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Richard Smithad762fc2011-04-14 22:09:26 +00001383Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1384 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001385 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001386 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001387 switch (TemplateInfo.Kind) {
1388 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001389 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001390 break;
1391
1392 case ParsedTemplateInfo::Template:
1393 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001394 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001395 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001396 TemplateInfo.TemplateParams->data(),
1397 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001398 D);
1399 break;
1400
1401 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001402 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001403 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001404 TemplateInfo.ExternLoc,
1405 TemplateInfo.TemplateLoc,
1406 D);
1407 if (ThisRes.isInvalid()) {
1408 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001409 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001410 }
1411
1412 ThisDecl = ThisRes.get();
1413 break;
1414 }
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Richard Smith34b41d92011-02-20 03:19:35 +00001417 bool TypeContainsAuto =
1418 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1419
Douglas Gregor1426e532009-05-12 21:31:51 +00001420 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001421 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001422 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001423 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001424 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001425 if (D.isFunctionDeclarator())
1426 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1427 << 1 /* delete */;
1428 else
1429 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001430 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001431 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001432 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1433 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001434 else
1435 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001436 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001437 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001438 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001439 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001440 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001441
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001442 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001443 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001444 cutOffParsing();
1445 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001446 }
1447
John McCall60d7b3a2010-08-24 06:29:42 +00001448 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001449
David Blaikie4e4d0842012-03-11 07:00:24 +00001450 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001451 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001452 ExitScope();
1453 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001454
Douglas Gregor1426e532009-05-12 21:31:51 +00001455 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001456 SkipUntil(tok::comma, true, true);
1457 Actions.ActOnInitializerError(ThisDecl);
1458 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001459 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1460 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001461 }
1462 } else if (Tok.is(tok::l_paren)) {
1463 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001464 BalancedDelimiterTracker T(*this, tok::l_paren);
1465 T.consumeOpen();
1466
Douglas Gregor1426e532009-05-12 21:31:51 +00001467 ExprVector Exprs(Actions);
1468 CommaLocsTy CommaLocs;
1469
David Blaikie4e4d0842012-03-11 07:00:24 +00001470 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001471 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001472 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001473 }
1474
Douglas Gregor1426e532009-05-12 21:31:51 +00001475 if (ParseExpressionList(Exprs, CommaLocs)) {
1476 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001477
David Blaikie4e4d0842012-03-11 07:00:24 +00001478 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001479 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001480 ExitScope();
1481 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001482 } else {
1483 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001484 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001485
1486 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1487 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001488
David Blaikie4e4d0842012-03-11 07:00:24 +00001489 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001490 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001491 ExitScope();
1492 }
1493
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001494 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1495 T.getCloseLocation(),
1496 move_arg(Exprs));
1497 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1498 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001499 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001500 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001501 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001502 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1503
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001504 if (D.getCXXScopeSpec().isSet()) {
1505 EnterScope(0);
1506 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1507 }
1508
1509 ExprResult Init(ParseBraceInitializer());
1510
1511 if (D.getCXXScopeSpec().isSet()) {
1512 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1513 ExitScope();
1514 }
1515
1516 if (Init.isInvalid()) {
1517 Actions.ActOnInitializerError(ThisDecl);
1518 } else
1519 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1520 /*DirectInit=*/true, TypeContainsAuto);
1521
Douglas Gregor1426e532009-05-12 21:31:51 +00001522 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001523 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001524 }
1525
Richard Smith483b9f32011-02-21 20:05:19 +00001526 Actions.FinalizeDeclaration(ThisDecl);
1527
Douglas Gregor1426e532009-05-12 21:31:51 +00001528 return ThisDecl;
1529}
1530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531/// ParseSpecifierQualifierList
1532/// specifier-qualifier-list:
1533/// type-specifier specifier-qualifier-list[opt]
1534/// type-qualifier specifier-qualifier-list[opt]
1535/// [GNU] attributes specifier-qualifier-list[opt]
1536///
Richard Smith69730c12012-03-12 07:56:15 +00001537void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1538 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1540 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001541 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001542 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 // Validate declspec for type-name.
1545 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001546 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1547 Diag(Tok, diag::err_expected_type);
1548 DS.SetTypeSpecError();
1549 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1550 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001552 if (!DS.hasTypeSpecifier())
1553 DS.SetTypeSpecError();
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 // Issue diagnostic and remove storage class if present.
1557 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1558 if (DS.getStorageClassSpecLoc().isValid())
1559 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1560 else
1561 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1562 DS.ClearStorageClassSpecs();
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 // Issue diagnostic and remove function specfier if present.
1566 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001567 if (DS.isInlineSpecified())
1568 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1569 if (DS.isVirtualSpecified())
1570 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1571 if (DS.isExplicitSpecified())
1572 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 DS.ClearFunctionSpecs();
1574 }
Richard Smith69730c12012-03-12 07:56:15 +00001575
1576 // Issue diagnostic and remove constexpr specfier if present.
1577 if (DS.isConstexprSpecified()) {
1578 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1579 DS.ClearConstexprSpec();
1580 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Chris Lattnerc199ab32009-04-12 20:42:31 +00001583/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1584/// specified token is valid after the identifier in a declarator which
1585/// immediately follows the declspec. For example, these things are valid:
1586///
1587/// int x [ 4]; // direct-declarator
1588/// int x ( int y); // direct-declarator
1589/// int(int x ) // direct-declarator
1590/// int x ; // simple-declaration
1591/// int x = 17; // init-declarator-list
1592/// int x , y; // init-declarator-list
1593/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001594/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001595/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001596///
1597/// This is not, because 'x' does not immediately follow the declspec (though
1598/// ')' happens to be valid anyway).
1599/// int (x)
1600///
1601static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1602 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1603 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001604 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001605}
1606
Chris Lattnere40c2952009-04-14 21:34:55 +00001607
1608/// ParseImplicitInt - This method is called when we have an non-typename
1609/// identifier in a declspec (which normally terminates the decl spec) when
1610/// the declspec has no type specifier. In this case, the declspec is either
1611/// malformed or is "implicit int" (in K&R and C89).
1612///
1613/// This method handles diagnosing this prettily and returns false if the
1614/// declspec is done being processed. If it recovers and thinks there may be
1615/// other pieces of declspec after it, it returns true.
1616///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001617bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001618 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001619 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001620 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Chris Lattnere40c2952009-04-14 21:34:55 +00001622 SourceLocation Loc = Tok.getLocation();
1623 // If we see an identifier that is not a type name, we normally would
1624 // parse it as the identifer being declared. However, when a typename
1625 // is typo'd or the definition is not included, this will incorrectly
1626 // parse the typename as the identifier name and fall over misparsing
1627 // later parts of the diagnostic.
1628 //
1629 // As such, we try to do some look-ahead in cases where this would
1630 // otherwise be an "implicit-int" case to see if this is invalid. For
1631 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1632 // an identifier with implicit int, we'd get a parse error because the
1633 // next token is obviously invalid for a type. Parse these as a case
1634 // with an invalid type specifier.
1635 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattnere40c2952009-04-14 21:34:55 +00001637 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001638 // error, do lookahead to try to do better recovery. This never applies within
1639 // a type specifier.
1640 // FIXME: Don't bail out here in languages with no implicit int (like
1641 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1642 // type or typo than a use of implicit int.
1643 if (DSC != DSC_type_specifier &&
1644 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001645 // If this token is valid for implicit int, e.g. "static x = 4", then
1646 // we just avoid eating the identifier, so it will be parsed as the
1647 // identifier in the declarator.
1648 return false;
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Chris Lattnere40c2952009-04-14 21:34:55 +00001651 // Otherwise, if we don't consume this token, we are going to emit an
1652 // error anyway. Try to recover from various common problems. Check
1653 // to see if this was a reference to a tag name without a tag specified.
1654 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001655 //
1656 // C++ doesn't need this, and isTagName doesn't take SS.
1657 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001658 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001659 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Douglas Gregor23c94db2010-07-02 17:43:08 +00001661 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001662 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001663 case DeclSpec::TST_enum:
1664 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1665 case DeclSpec::TST_union:
1666 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1667 case DeclSpec::TST_struct:
1668 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1669 case DeclSpec::TST_class:
1670 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Chris Lattnerf4382f52009-04-14 22:17:06 +00001673 if (TagName) {
1674 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001675 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001676 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Chris Lattnerf4382f52009-04-14 22:17:06 +00001678 // Parse this as a tag as if the missing tag were present.
1679 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001680 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001681 else
Richard Smith69730c12012-03-12 07:56:15 +00001682 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1683 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001684 return true;
1685 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001686 }
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregora786fdb2009-10-13 23:27:22 +00001688 // This is almost certainly an invalid type name. Let the action emit a
1689 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001690 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001691 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001692 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001693 // The action emitted a diagnostic, so we don't have to.
1694 if (T) {
1695 // The action has suggested that the type T could be used. Set that as
1696 // the type in the declaration specifiers, consume the would-be type
1697 // name token, and we're done.
1698 const char *PrevSpec;
1699 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001700 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001701 DS.SetRangeEnd(Tok.getLocation());
1702 ConsumeToken();
1703
1704 // There may be other declaration specifiers after this.
1705 return true;
1706 }
1707
1708 // Fall through; the action had no suggestion for us.
1709 } else {
1710 // The action did not emit a diagnostic, so emit one now.
1711 SourceRange R;
1712 if (SS) R = SS->getRange();
1713 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Douglas Gregora786fdb2009-10-13 23:27:22 +00001716 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001717 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001718 DS.SetRangeEnd(Tok.getLocation());
1719 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Chris Lattnere40c2952009-04-14 21:34:55 +00001721 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1722 // avoid rippling error messages on subsequent uses of the same type,
1723 // could be useful if #include was forgotten.
1724 return false;
1725}
1726
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001727/// \brief Determine the declaration specifier context from the declarator
1728/// context.
1729///
1730/// \param Context the declarator context, which is one of the
1731/// Declarator::TheContext enumerator values.
1732Parser::DeclSpecContext
1733Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1734 if (Context == Declarator::MemberContext)
1735 return DSC_class;
1736 if (Context == Declarator::FileContext)
1737 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001738 if (Context == Declarator::TrailingReturnContext)
1739 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001740 return DSC_normal;
1741}
1742
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001743/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1744///
1745/// FIXME: Simply returns an alignof() expression if the argument is a
1746/// type. Ideally, the type should be propagated directly into Sema.
1747///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001748/// [C11] type-id
1749/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001750/// [C++0x] type-id ...[opt]
1751/// [C++0x] assignment-expression ...[opt]
1752ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1753 SourceLocation &EllipsisLoc) {
1754 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001755 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001756 SourceLocation TypeLoc = Tok.getLocation();
1757 ParsedType Ty = ParseTypeName().get();
1758 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001759 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1760 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001761 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001762 ER = ParseConstantExpression();
1763
David Blaikie4e4d0842012-03-11 07:00:24 +00001764 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001765 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001766
1767 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001768}
1769
1770/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1771/// attribute to Attrs.
1772///
1773/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001774/// [C11] '_Alignas' '(' type-id ')'
1775/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001776/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1777/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001778void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1779 SourceLocation *endLoc) {
1780 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1781 "Not an alignment-specifier!");
1782
1783 SourceLocation KWLoc = Tok.getLocation();
1784 ConsumeToken();
1785
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001786 BalancedDelimiterTracker T(*this, tok::l_paren);
1787 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001788 return;
1789
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001790 SourceLocation EllipsisLoc;
1791 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001792 if (ArgExpr.isInvalid()) {
1793 SkipUntil(tok::r_paren);
1794 return;
1795 }
1796
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001797 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001798 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001799 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001800
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001801 // FIXME: Handle pack-expansions here.
1802 if (EllipsisLoc.isValid()) {
1803 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1804 return;
1805 }
1806
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001807 ExprVector ArgExprs(Actions);
1808 ArgExprs.push_back(ArgExpr.release());
1809 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001810 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001811}
1812
Reid Spencer5f016e22007-07-11 17:01:13 +00001813/// ParseDeclarationSpecifiers
1814/// declaration-specifiers: [C99 6.7]
1815/// storage-class-specifier declaration-specifiers[opt]
1816/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001817/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001818/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001819/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001820/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001821///
1822/// storage-class-specifier: [C99 6.7.1]
1823/// 'typedef'
1824/// 'extern'
1825/// 'static'
1826/// 'auto'
1827/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001828/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001829/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001830/// function-specifier: [C99 6.7.4]
1831/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001832/// [C++] 'virtual'
1833/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001834/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001835/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001836/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001837
Reid Spencer5f016e22007-07-11 17:01:13 +00001838///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001839void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001840 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001841 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001842 DeclSpecContext DSContext,
1843 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001844 if (DS.getSourceRange().isInvalid()) {
1845 DS.SetRangeStart(Tok.getLocation());
1846 DS.SetRangeEnd(Tok.getLocation());
1847 }
1848
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001849 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001851 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001853 unsigned DiagID = 0;
1854
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001856
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001858 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001859 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001860 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1861 MaybeParseCXX0XAttributes(DS.getAttributes());
1862
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 // If this is not a declaration specifier token, we're done reading decl
1864 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001865 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001868 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001869 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001870 if (DS.hasTypeSpecifier()) {
1871 bool AllowNonIdentifiers
1872 = (getCurScope()->getFlags() & (Scope::ControlScope |
1873 Scope::BlockScope |
1874 Scope::TemplateParamScope |
1875 Scope::FunctionPrototypeScope |
1876 Scope::AtCatchScope)) == 0;
1877 bool AllowNestedNameSpecifiers
1878 = DSContext == DSC_top_level ||
1879 (DSContext == DSC_class && DS.isFriendSpecified());
1880
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001881 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1882 AllowNonIdentifiers,
1883 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001884 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001885 }
1886
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001887 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1888 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1889 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001890 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1891 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001892 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001893 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001894 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001895 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001896
1897 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001898 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001899 }
1900
Chris Lattner5e02c472009-01-05 00:07:25 +00001901 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001902 // C++ scope specifier. Annotate and loop, or bail out on error.
1903 if (TryAnnotateCXXScopeToken(true)) {
1904 if (!DS.hasTypeSpecifier())
1905 DS.SetTypeSpecError();
1906 goto DoneWithDeclSpec;
1907 }
John McCall2e0a7152010-03-01 18:20:46 +00001908 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1909 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001910 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001911
1912 case tok::annot_cxxscope: {
1913 if (DS.hasTypeSpecifier())
1914 goto DoneWithDeclSpec;
1915
John McCallaa87d332009-12-12 11:40:51 +00001916 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001917 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1918 Tok.getAnnotationRange(),
1919 SS);
John McCallaa87d332009-12-12 11:40:51 +00001920
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001921 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001922 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001923 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001924 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001925 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001926 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001927
1928 // C++ [class.qual]p2:
1929 // In a lookup in which the constructor is an acceptable lookup
1930 // result and the nested-name-specifier nominates a class C:
1931 //
1932 // - if the name specified after the
1933 // nested-name-specifier, when looked up in C, is the
1934 // injected-class-name of C (Clause 9), or
1935 //
1936 // - if the name specified after the nested-name-specifier
1937 // is the same as the identifier or the
1938 // simple-template-id's template-name in the last
1939 // component of the nested-name-specifier,
1940 //
1941 // the name is instead considered to name the constructor of
1942 // class C.
1943 //
1944 // Thus, if the template-name is actually the constructor
1945 // name, then the code is ill-formed; this interpretation is
1946 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001947 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001948 if ((DSContext == DSC_top_level ||
1949 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1950 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001951 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001952 if (isConstructorDeclarator()) {
1953 // The user meant this to be an out-of-line constructor
1954 // definition, but template arguments are not allowed
1955 // there. Just allow this as a constructor; we'll
1956 // complain about it later.
1957 goto DoneWithDeclSpec;
1958 }
1959
1960 // The user meant this to name a type, but it actually names
1961 // a constructor with some extraneous template
1962 // arguments. Complain, then parse it as a type as the user
1963 // intended.
1964 Diag(TemplateId->TemplateNameLoc,
1965 diag::err_out_of_line_template_id_names_constructor)
1966 << TemplateId->Name;
1967 }
1968
John McCallaa87d332009-12-12 11:40:51 +00001969 DS.getTypeSpecScope() = SS;
1970 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001971 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001972 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001973 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001974 continue;
1975 }
1976
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001977 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001978 DS.getTypeSpecScope() = SS;
1979 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001980 if (Tok.getAnnotationValue()) {
1981 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1983 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001984 PrevSpec, DiagID, T);
1985 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001986 else
1987 DS.SetTypeSpecError();
1988 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1989 ConsumeToken(); // The typename
1990 }
1991
Douglas Gregor9135c722009-03-25 15:40:00 +00001992 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001993 goto DoneWithDeclSpec;
1994
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001995 // If we're in a context where the identifier could be a class name,
1996 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001997 if ((DSContext == DSC_top_level ||
1998 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001999 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002000 &SS)) {
2001 if (isConstructorDeclarator())
2002 goto DoneWithDeclSpec;
2003
2004 // As noted in C++ [class.qual]p2 (cited above), when the name
2005 // of the class is qualified in a context where it could name
2006 // a constructor, its a constructor name. However, we've
2007 // looked at the declarator, and the user probably meant this
2008 // to be a type. Complain that it isn't supposed to be treated
2009 // as a type, then proceed to parse it as a type.
2010 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2011 << Next.getIdentifierInfo();
2012 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002013
John McCallb3d87482010-08-24 05:47:05 +00002014 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2015 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002016 getCurScope(), &SS,
2017 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002018 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002019 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002020
Chris Lattnerf4382f52009-04-14 22:17:06 +00002021 // If the referenced identifier is not a type, then this declspec is
2022 // erroneous: We already checked about that it has no type specifier, and
2023 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002024 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002025 if (TypeRep == 0) {
2026 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002027 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002028 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002029 }
Mike Stump1eb44332009-09-09 15:08:12 +00002030
John McCallaa87d332009-12-12 11:40:51 +00002031 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002032 ConsumeToken(); // The C++ scope.
2033
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002035 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002036 if (isInvalid)
2037 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002039 DS.SetRangeEnd(Tok.getLocation());
2040 ConsumeToken(); // The typename.
2041
2042 continue;
2043 }
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Chris Lattner80d0c892009-01-21 19:48:37 +00002045 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002046 if (Tok.getAnnotationValue()) {
2047 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002049 DiagID, T);
2050 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002051 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002052
2053 if (isInvalid)
2054 break;
2055
Chris Lattner80d0c892009-01-21 19:48:37 +00002056 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2057 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Chris Lattner80d0c892009-01-21 19:48:37 +00002059 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2060 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002061 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002063 ParseObjCProtocolQualifiers(DS);
2064
Chris Lattner80d0c892009-01-21 19:48:37 +00002065 continue;
2066 }
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregorbfad9152011-04-28 15:48:45 +00002068 case tok::kw___is_signed:
2069 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2070 // typically treats it as a trait. If we see __is_signed as it appears
2071 // in libstdc++, e.g.,
2072 //
2073 // static const bool __is_signed;
2074 //
2075 // then treat __is_signed as an identifier rather than as a keyword.
2076 if (DS.getTypeSpecType() == TST_bool &&
2077 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2078 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2079 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2080 Tok.setKind(tok::identifier);
2081 }
2082
2083 // We're done with the declaration-specifiers.
2084 goto DoneWithDeclSpec;
2085
Chris Lattner3bd934a2008-07-26 01:18:38 +00002086 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002087 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002088 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002089 // In C++, check to see if this is a scope specifier like foo::bar::, if
2090 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002091 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002092 if (TryAnnotateCXXScopeToken(true)) {
2093 if (!DS.hasTypeSpecifier())
2094 DS.SetTypeSpecError();
2095 goto DoneWithDeclSpec;
2096 }
2097 if (!Tok.is(tok::identifier))
2098 continue;
2099 }
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Chris Lattner3bd934a2008-07-26 01:18:38 +00002101 // This identifier can only be a typedef name if we haven't already seen
2102 // a type-specifier. Without this check we misparse:
2103 // typedef int X; struct Y { short X; }; as 'short int'.
2104 if (DS.hasTypeSpecifier())
2105 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002106
John Thompson82287d12010-02-05 00:12:22 +00002107 // Check for need to substitute AltiVec keyword tokens.
2108 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2109 break;
2110
John McCallb3d87482010-08-24 05:47:05 +00002111 ParsedType TypeRep =
2112 Actions.getTypeName(*Tok.getIdentifierInfo(),
2113 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002114
Chris Lattnerc199ab32009-04-12 20:42:31 +00002115 // If this is not a typedef name, don't parse it as part of the declspec,
2116 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002117 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002118 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002119 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002120 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002121
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002122 // If we're in a context where the identifier could be a class name,
2123 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002124 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002125 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002126 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002127 goto DoneWithDeclSpec;
2128
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002130 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002131 if (isInvalid)
2132 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Chris Lattner3bd934a2008-07-26 01:18:38 +00002134 DS.SetRangeEnd(Tok.getLocation());
2135 ConsumeToken(); // The identifier
2136
2137 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2138 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002139 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002140 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002141 ParseObjCProtocolQualifiers(DS);
2142
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002143 // Need to support trailing type qualifiers (e.g. "id<p> const").
2144 // If a type specifier follows, it will be diagnosed elsewhere.
2145 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002146 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002147
2148 // type-name
2149 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002150 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002151 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002152 // This template-id does not refer to a type name, so we're
2153 // done with the type-specifiers.
2154 goto DoneWithDeclSpec;
2155 }
2156
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002157 // If we're in a context where the template-id could be a
2158 // constructor name or specialization, check whether this is a
2159 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002160 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002161 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002162 isConstructorDeclarator())
2163 goto DoneWithDeclSpec;
2164
Douglas Gregor39a8de12009-02-25 19:37:18 +00002165 // Turn the template-id annotation token into a type annotation
2166 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002167 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002168 continue;
2169 }
2170
Reid Spencer5f016e22007-07-11 17:01:13 +00002171 // GNU attributes support.
2172 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002173 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002175
2176 // Microsoft declspec support.
2177 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002178 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002179 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Steve Naroff239f0732008-12-25 14:16:32 +00002181 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002182 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002183 // FIXME: Add handling here!
2184 break;
2185
2186 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002187 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002188 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002189 case tok::kw___cdecl:
2190 case tok::kw___stdcall:
2191 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002192 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002193 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002194 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002195 continue;
2196
Dawn Perchik52fc3142010-09-03 01:29:35 +00002197 // Borland single token adornments.
2198 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002199 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002200 continue;
2201
Peter Collingbournef315fa82011-02-14 01:42:53 +00002202 // OpenCL single token adornments.
2203 case tok::kw___kernel:
2204 ParseOpenCLAttributes(DS.getAttributes());
2205 continue;
2206
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 // storage-class-specifier
2208 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002209 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2210 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 break;
2212 case tok::kw_extern:
2213 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002214 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002215 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2216 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002218 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002219 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2220 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002221 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 case tok::kw_static:
2223 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002224 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002225 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2226 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 break;
2228 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002229 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002230 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002231 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2232 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002233 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002234 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002235 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002236 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2238 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002239 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002240 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2241 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 break;
2243 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002244 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2245 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002247 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002248 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2249 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002250 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002252 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002253 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Reid Spencer5f016e22007-07-11 17:01:13 +00002255 // function-specifier
2256 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002257 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002259 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002260 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002261 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002262 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002263 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002264 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002265
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002266 // alignment-specifier
2267 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002268 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002269 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002270 ParseAlignmentSpecifier(DS.getAttributes());
2271 continue;
2272
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002273 // friend
2274 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002275 if (DSContext == DSC_class)
2276 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2277 else {
2278 PrevSpec = ""; // not actually used by the diagnostic
2279 DiagID = diag::err_friend_invalid_in_context;
2280 isInvalid = true;
2281 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002282 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Douglas Gregor8d267c52011-09-09 02:06:17 +00002284 // Modules
2285 case tok::kw___module_private__:
2286 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2287 break;
2288
Sebastian Redl2ac67232009-11-05 15:47:02 +00002289 // constexpr
2290 case tok::kw_constexpr:
2291 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2292 break;
2293
Chris Lattner80d0c892009-01-21 19:48:37 +00002294 // type-specifier
2295 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002296 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002298 break;
2299 case tok::kw_long:
2300 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002301 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2302 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002303 else
John McCallfec54012009-08-03 20:12:06 +00002304 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2305 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002306 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002307 case tok::kw___int64:
2308 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2309 DiagID);
2310 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002311 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002312 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002314 break;
2315 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002316 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002318 break;
2319 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002322 break;
2323 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002324 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2325 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002326 break;
2327 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002330 break;
2331 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2333 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002334 break;
2335 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2337 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002338 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002339 case tok::kw___int128:
2340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2341 DiagID);
2342 break;
2343 case tok::kw_half:
2344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2345 DiagID);
2346 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002347 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2349 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002350 break;
2351 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2353 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002354 break;
2355 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2357 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002358 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002359 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2361 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002362 break;
2363 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002364 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2365 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002366 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002367 case tok::kw_bool:
2368 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002369 if (Tok.is(tok::kw_bool) &&
2370 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2371 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2372 PrevSpec = ""; // Not used by the diagnostic.
2373 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002374 // For better error recovery.
2375 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002376 isInvalid = true;
2377 } else {
2378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2379 DiagID);
2380 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002381 break;
2382 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002383 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2384 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002385 break;
2386 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2388 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002389 break;
2390 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002391 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2392 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 break;
John Thompson82287d12010-02-05 00:12:22 +00002394 case tok::kw___vector:
2395 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2396 break;
2397 case tok::kw___pixel:
2398 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2399 break;
John McCalla5fc4722011-04-09 22:50:59 +00002400 case tok::kw___unknown_anytype:
2401 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2402 PrevSpec, DiagID);
2403 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002404
2405 // class-specifier:
2406 case tok::kw_class:
2407 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002408 case tok::kw_union: {
2409 tok::TokenKind Kind = Tok.getKind();
2410 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002411 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2412 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002413 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002414 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002415
2416 // enum-specifier:
2417 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002418 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002419 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002420 continue;
2421
2422 // cv-qualifier:
2423 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002425 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002426 break;
2427 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002428 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002429 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002430 break;
2431 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002432 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002433 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002434 break;
2435
Douglas Gregord57959a2009-03-27 23:10:48 +00002436 // C++ typename-specifier:
2437 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002438 if (TryAnnotateTypeOrScopeToken()) {
2439 DS.SetTypeSpecError();
2440 goto DoneWithDeclSpec;
2441 }
2442 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002443 continue;
2444 break;
2445
Chris Lattner80d0c892009-01-21 19:48:37 +00002446 // GNU typeof support.
2447 case tok::kw_typeof:
2448 ParseTypeofSpecifier(DS);
2449 continue;
2450
David Blaikie42d6d0c2011-12-04 05:04:18 +00002451 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002452 ParseDecltypeSpecifier(DS);
2453 continue;
2454
Sean Huntdb5d44b2011-05-19 05:37:45 +00002455 case tok::kw___underlying_type:
2456 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002457 continue;
2458
2459 case tok::kw__Atomic:
2460 ParseAtomicSpecifier(DS);
2461 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002462
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002463 // OpenCL qualifiers:
2464 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002465 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002466 goto DoneWithDeclSpec;
2467 case tok::kw___private:
2468 case tok::kw___global:
2469 case tok::kw___local:
2470 case tok::kw___constant:
2471 case tok::kw___read_only:
2472 case tok::kw___write_only:
2473 case tok::kw___read_write:
2474 ParseOpenCLQualifiers(DS);
2475 break;
2476
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002477 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002478 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002479 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2480 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002481 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002482 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor46f936e2010-11-19 17:10:50 +00002484 if (!ParseObjCProtocolQualifiers(DS))
2485 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2486 << FixItHint::CreateInsertion(Loc, "id")
2487 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002488
2489 // Need to support trailing type qualifiers (e.g. "id<p> const").
2490 // If a type specifier follows, it will be diagnosed elsewhere.
2491 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002492 }
John McCallfec54012009-08-03 20:12:06 +00002493 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002494 if (isInvalid) {
2495 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002496 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002497
2498 if (DiagID == diag::ext_duplicate_declspec)
2499 Diag(Tok, DiagID)
2500 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2501 else
2502 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002503 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002504
Chris Lattner81c018d2008-03-13 06:29:04 +00002505 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002506 if (DiagID != diag::err_bool_redeclaration)
2507 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002508 }
2509}
Douglas Gregoradcac882008-12-01 23:54:00 +00002510
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002511/// ParseStructDeclaration - Parse a struct declaration without the terminating
2512/// semicolon.
2513///
Reid Spencer5f016e22007-07-11 17:01:13 +00002514/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002515/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002516/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002517/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002518/// struct-declarator-list:
2519/// struct-declarator
2520/// struct-declarator-list ',' struct-declarator
2521/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2522/// struct-declarator:
2523/// declarator
2524/// [GNU] declarator attributes[opt]
2525/// declarator[opt] ':' constant-expression
2526/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2527///
Chris Lattnere1359422008-04-10 06:46:29 +00002528void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002529ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002530
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002531 if (Tok.is(tok::kw___extension__)) {
2532 // __extension__ silences extension warnings in the subexpression.
2533 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002534 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002535 return ParseStructDeclaration(DS, Fields);
2536 }
Mike Stump1eb44332009-09-09 15:08:12 +00002537
Steve Naroff28a7ca82007-08-20 22:28:22 +00002538 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002539 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002540
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002541 // If there are no declarators, this is a free-standing declaration
2542 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002543 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002544 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002545 return;
2546 }
2547
2548 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002549 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002550 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002551 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002552 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002553 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002554 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002555
2556 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002557 if (!FirstDeclarator)
2558 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Steve Naroff28a7ca82007-08-20 22:28:22 +00002560 /// struct-declarator: declarator
2561 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002562 if (Tok.isNot(tok::colon)) {
2563 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2564 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002565 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002566 }
Mike Stump1eb44332009-09-09 15:08:12 +00002567
Chris Lattner04d66662007-10-09 17:33:22 +00002568 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002569 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002570 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002571 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002572 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002573 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002574 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002575 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002576
Steve Naroff28a7ca82007-08-20 22:28:22 +00002577 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002578 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002579
John McCallbdd563e2009-11-03 02:38:08 +00002580 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002581 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002582 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002583
Steve Naroff28a7ca82007-08-20 22:28:22 +00002584 // If we don't have a comma, it is either the end of the list (a ';')
2585 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002586 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002587 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002588
Steve Naroff28a7ca82007-08-20 22:28:22 +00002589 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002590 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002591
John McCallbdd563e2009-11-03 02:38:08 +00002592 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002593 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002594}
2595
2596/// ParseStructUnionBody
2597/// struct-contents:
2598/// struct-declaration-list
2599/// [EXT] empty
2600/// [GNU] "struct-declaration-list" without terminatoring ';'
2601/// struct-declaration-list:
2602/// struct-declaration
2603/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002604/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002605///
Reid Spencer5f016e22007-07-11 17:01:13 +00002606void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002607 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002608 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2609 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002611 BalancedDelimiterTracker T(*this, tok::l_brace);
2612 if (T.consumeOpen())
2613 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002615 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002616 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002617
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2619 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002620 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002621 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2622 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2623 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002624
Chris Lattner5f9e2722011-07-23 10:55:15 +00002625 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002626
Reid Spencer5f016e22007-07-11 17:01:13 +00002627 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002628 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Reid Spencer5f016e22007-07-11 17:01:13 +00002631 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002632 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002633 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002634 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002635 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 ConsumeToken();
2637 continue;
2638 }
Chris Lattnere1359422008-04-10 06:46:29 +00002639
2640 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002641 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002642
John McCallbdd563e2009-11-03 02:38:08 +00002643 if (!Tok.is(tok::at)) {
2644 struct CFieldCallback : FieldCallback {
2645 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002646 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002647 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002648
John McCalld226f652010-08-21 09:40:31 +00002649 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002650 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002651 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2652
John McCalld226f652010-08-21 09:40:31 +00002653 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002654 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002655 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002656 FD.D.getDeclSpec().getSourceRange().getBegin(),
2657 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002658 FieldDecls.push_back(Field);
2659 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002660 }
John McCallbdd563e2009-11-03 02:38:08 +00002661 } Callback(*this, TagDecl, FieldDecls);
2662
2663 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002664 } else { // Handle @defs
2665 ConsumeToken();
2666 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2667 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002668 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002669 continue;
2670 }
2671 ConsumeToken();
2672 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2673 if (!Tok.is(tok::identifier)) {
2674 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002675 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002676 continue;
2677 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002678 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002679 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002680 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002681 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2682 ConsumeToken();
2683 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002684 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002685
Chris Lattner04d66662007-10-09 17:33:22 +00002686 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002688 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002689 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002690 break;
2691 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002692 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2693 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002694 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002695 // If we stopped at a ';', eat it.
2696 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002697 }
2698 }
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002700 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002701
John McCall0b7e6782011-03-24 11:26:52 +00002702 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002704 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002705
Douglas Gregor23c94db2010-07-02 17:43:08 +00002706 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002707 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002708 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002709 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002710 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002711 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2712 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002713}
2714
Reid Spencer5f016e22007-07-11 17:01:13 +00002715/// ParseEnumSpecifier
2716/// enum-specifier: [C99 6.7.2.2]
2717/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002718///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002719/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2720/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002721/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2722/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002723/// 'enum' identifier
2724/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002725///
Richard Smith1af83c42012-03-23 03:33:32 +00002726/// [C++11] enum-head '{' enumerator-list[opt] '}'
2727/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002728///
Richard Smith1af83c42012-03-23 03:33:32 +00002729/// enum-head: [C++11]
2730/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2731/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2732/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002733///
Richard Smith1af83c42012-03-23 03:33:32 +00002734/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002735/// 'enum'
2736/// 'enum' 'class'
2737/// 'enum' 'struct'
2738///
Richard Smith1af83c42012-03-23 03:33:32 +00002739/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002740/// ':' type-specifier-seq
2741///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002742/// [C++] elaborated-type-specifier:
2743/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2744///
Chris Lattner4c97d762009-04-12 21:49:30 +00002745void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002746 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002747 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002748 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002749 if (Tok.is(tok::code_completion)) {
2750 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002751 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002752 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002753 }
John McCall57c13002011-07-06 05:58:41 +00002754
Richard Smithbdad7a22012-01-10 01:33:14 +00002755 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002756 bool IsScopedUsingClassTag = false;
2757
David Blaikie4e4d0842012-03-11 07:00:24 +00002758 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002759 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002760 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002761 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002762 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002763 }
Richard Smith1af83c42012-03-23 03:33:32 +00002764
2765 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2766 // used to specify explicit instantiations. We extend this to also cover
2767 // explicit specializations.
2768 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2769 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2770 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2771
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002772 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002773 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002774 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002775
Aaron Ballman6454a022012-03-01 04:09:28 +00002776 // If declspecs exist after tag, parse them.
2777 while (Tok.is(tok::kw___declspec))
2778 ParseMicrosoftDeclSpec(attrs);
2779
Richard Smith7796eb52012-03-12 08:56:40 +00002780 // Enum definitions should not be parsed in a trailing-return-type.
2781 bool AllowDeclaration = DSC != DSC_trailing;
2782
2783 bool AllowFixedUnderlyingType = AllowDeclaration &&
2784 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2785 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002786
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002787 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002788 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002789 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2790 // if a fixed underlying type is allowed.
2791 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2792
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002793 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2794 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002795 return;
2796
2797 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002798 Diag(Tok, diag::err_expected_ident);
2799 if (Tok.isNot(tok::l_brace)) {
2800 // Has no name and is not a definition.
2801 // Skip the rest of this declarator, up until the comma or semicolon.
2802 SkipUntil(tok::comma, true);
2803 return;
2804 }
2805 }
2806 }
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002808 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002809 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002810 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002811 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002813 // Skip the rest of this declarator, up until the comma or semicolon.
2814 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002815 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002816 }
Mike Stump1eb44332009-09-09 15:08:12 +00002817
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002818 // If an identifier is present, consume and remember it.
2819 IdentifierInfo *Name = 0;
2820 SourceLocation NameLoc;
2821 if (Tok.is(tok::identifier)) {
2822 Name = Tok.getIdentifierInfo();
2823 NameLoc = ConsumeToken();
2824 }
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Richard Smithbdad7a22012-01-10 01:33:14 +00002826 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002827 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2828 // declaration of a scoped enumeration.
2829 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002830 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002831 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002832 }
2833
Richard Smith1af83c42012-03-23 03:33:32 +00002834 // Stop suppressing access control now we've parsed the enum name.
2835 SuppressAccess.done();
2836
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002837 TypeResult BaseType;
2838
Douglas Gregora61b3e72010-12-01 17:42:47 +00002839 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002840 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002841 bool PossibleBitfield = false;
2842 if (getCurScope()->getFlags() & Scope::ClassScope) {
2843 // If we're in class scope, this can either be an enum declaration with
2844 // an underlying type, or a declaration of a bitfield member. We try to
2845 // use a simple disambiguation scheme first to catch the common cases
2846 // (integer literal, sizeof); if it's still ambiguous, we then consider
2847 // anything that's a simple-type-specifier followed by '(' as an
2848 // expression. This suffices because function types are not valid
2849 // underlying types anyway.
2850 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2851 // If the next token starts an expression, we know we're parsing a
2852 // bit-field. This is the common case.
2853 if (TPR == TPResult::True())
2854 PossibleBitfield = true;
2855 // If the next token starts a type-specifier-seq, it may be either a
2856 // a fixed underlying type or the start of a function-style cast in C++;
2857 // lookahead one more token to see if it's obvious that we have a
2858 // fixed underlying type.
2859 else if (TPR == TPResult::False() &&
2860 GetLookAheadToken(2).getKind() == tok::semi) {
2861 // Consume the ':'.
2862 ConsumeToken();
2863 } else {
2864 // We have the start of a type-specifier-seq, so we have to perform
2865 // tentative parsing to determine whether we have an expression or a
2866 // type.
2867 TentativeParsingAction TPA(*this);
2868
2869 // Consume the ':'.
2870 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002871
2872 // If we see a type specifier followed by an open-brace, we have an
2873 // ambiguity between an underlying type and a C++11 braced
2874 // function-style cast. Resolve this by always treating it as an
2875 // underlying type.
2876 // FIXME: The standard is not entirely clear on how to disambiguate in
2877 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002878 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002879 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002880 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002881 // We'll parse this as a bitfield later.
2882 PossibleBitfield = true;
2883 TPA.Revert();
2884 } else {
2885 // We have a type-specifier-seq.
2886 TPA.Commit();
2887 }
2888 }
2889 } else {
2890 // Consume the ':'.
2891 ConsumeToken();
2892 }
2893
2894 if (!PossibleBitfield) {
2895 SourceRange Range;
2896 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002897
David Blaikie4e4d0842012-03-11 07:00:24 +00002898 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002899 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2900 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002901 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002902 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002903 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002904 }
2905
Richard Smithbdad7a22012-01-10 01:33:14 +00002906 // There are four options here. If we have 'friend enum foo;' then this is a
2907 // friend declaration, and cannot have an accompanying definition. If we have
2908 // 'enum foo;', then this is a forward declaration. If we have
2909 // 'enum foo {...' then this is a definition. Otherwise we have something
2910 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002911 //
2912 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2913 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2914 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2915 //
John McCallf312b1e2010-08-26 23:41:50 +00002916 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002917 if (DS.isFriendSpecified())
2918 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002919 else if (!AllowDeclaration)
2920 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002921 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002922 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002923 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002924 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002925 else
John McCallf312b1e2010-08-26 23:41:50 +00002926 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002927
2928 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002929 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002930 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002931 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2932 // Skip the rest of this declarator, up until the comma or semicolon.
2933 Diag(Tok, diag::err_enum_template);
2934 SkipUntil(tok::comma, true);
2935 return;
2936 }
2937
2938 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2939 // Enumerations can't be explicitly instantiated.
2940 DS.SetTypeSpecError();
2941 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2942 return;
2943 }
2944
2945 assert(TemplateInfo.TemplateParams && "no template parameters");
2946 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2947 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002948 }
Richard Smith1af83c42012-03-23 03:33:32 +00002949
Douglas Gregorb9075602011-02-22 02:55:24 +00002950 if (!Name && TUK != Sema::TUK_Definition) {
2951 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002952
Douglas Gregorb9075602011-02-22 02:55:24 +00002953 // Skip the rest of this declarator, up until the comma or semicolon.
2954 SkipUntil(tok::comma, true);
2955 return;
2956 }
Richard Smith1af83c42012-03-23 03:33:32 +00002957
Douglas Gregor402abb52009-05-28 23:31:59 +00002958 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002959 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002960 const char *PrevSpec = 0;
2961 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002962 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002963 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002964 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002965 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002966 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002967
Douglas Gregor48c89f42010-04-24 16:38:41 +00002968 if (IsDependent) {
2969 // This enum has a dependent nested-name-specifier. Handle it as a
2970 // dependent tag.
2971 if (!Name) {
2972 DS.SetTypeSpecError();
2973 Diag(Tok, diag::err_expected_type_name_after_typename);
2974 return;
2975 }
2976
Douglas Gregor23c94db2010-07-02 17:43:08 +00002977 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002978 TUK, SS, Name, StartLoc,
2979 NameLoc);
2980 if (Type.isInvalid()) {
2981 DS.SetTypeSpecError();
2982 return;
2983 }
2984
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002985 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2986 NameLoc.isValid() ? NameLoc : StartLoc,
2987 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002988 Diag(StartLoc, DiagID) << PrevSpec;
2989
2990 return;
2991 }
Mike Stump1eb44332009-09-09 15:08:12 +00002992
John McCalld226f652010-08-21 09:40:31 +00002993 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002994 // The action failed to produce an enumeration tag. If this is a
2995 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002996 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002997 ConsumeBrace();
2998 SkipUntil(tok::r_brace);
2999 }
3000
3001 DS.SetTypeSpecError();
3002 return;
3003 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003004
Richard Smith7796eb52012-03-12 08:56:40 +00003005 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003006 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00003007 Diag(Tok, diag::err_friend_decl_defines_type)
3008 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00003009 ConsumeBrace();
3010 SkipUntil(tok::r_brace);
3011 } else {
3012 ParseEnumBody(StartLoc, TagDecl);
3013 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003016 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3017 NameLoc.isValid() ? NameLoc : StartLoc,
3018 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003019 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003020}
3021
3022/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3023/// enumerator-list:
3024/// enumerator
3025/// enumerator-list ',' enumerator
3026/// enumerator:
3027/// enumeration-constant
3028/// enumeration-constant '=' constant-expression
3029/// enumeration-constant:
3030/// identifier
3031///
John McCalld226f652010-08-21 09:40:31 +00003032void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003033 // Enter the scope of the enum body and start the definition.
3034 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003035 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003036
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003037 BalancedDelimiterTracker T(*this, tok::l_brace);
3038 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003039
Chris Lattner7946dd32007-08-27 17:24:30 +00003040 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003041 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003042 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Chris Lattner5f9e2722011-07-23 10:55:15 +00003044 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003045
John McCalld226f652010-08-21 09:40:31 +00003046 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Reid Spencer5f016e22007-07-11 17:01:13 +00003048 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003049 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003050 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3051 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003052
John McCall5b629aa2010-10-22 23:36:17 +00003053 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003054 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003055 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003056
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003058 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003059 ParsingDeclRAIIObject PD(*this);
3060
Chris Lattner04d66662007-10-09 17:33:22 +00003061 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003062 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003063 AssignedVal = ParseConstantExpression();
3064 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003065 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003066 }
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Reid Spencer5f016e22007-07-11 17:01:13 +00003068 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003069 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3070 LastEnumConstDecl,
3071 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003072 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003073 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003074 PD.complete(EnumConstDecl);
3075
Reid Spencer5f016e22007-07-11 17:01:13 +00003076 EnumConstantDecls.push_back(EnumConstDecl);
3077 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003078
Douglas Gregor751f6922010-09-07 14:51:08 +00003079 if (Tok.is(tok::identifier)) {
3080 // We're missing a comma between enumerators.
3081 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3082 Diag(Loc, diag::err_enumerator_list_missing_comma)
3083 << FixItHint::CreateInsertion(Loc, ", ");
3084 continue;
3085 }
3086
Chris Lattner04d66662007-10-09 17:33:22 +00003087 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003088 break;
3089 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Richard Smith7fe62082011-10-15 05:09:34 +00003091 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003092 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003093 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003094 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003095 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003096 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003097 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3098 << FixItHint::CreateRemoval(CommaLoc);
3099 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 }
Mike Stump1eb44332009-09-09 15:08:12 +00003101
Reid Spencer5f016e22007-07-11 17:01:13 +00003102 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003103 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003104
Reid Spencer5f016e22007-07-11 17:01:13 +00003105 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003106 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003107 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003108
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003109 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3110 EnumDecl, EnumConstantDecls.data(),
3111 EnumConstantDecls.size(), getCurScope(),
3112 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Douglas Gregor72de6672009-01-08 20:45:30 +00003114 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003115 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3116 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003117}
3118
3119/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003120/// start of a type-qualifier-list.
3121bool Parser::isTypeQualifier() const {
3122 switch (Tok.getKind()) {
3123 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003124
3125 // type-qualifier only in OpenCL
3126 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003127 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003128
Steve Naroff5f8aa692008-02-11 23:15:56 +00003129 // type-qualifier
3130 case tok::kw_const:
3131 case tok::kw_volatile:
3132 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003133 case tok::kw___private:
3134 case tok::kw___local:
3135 case tok::kw___global:
3136 case tok::kw___constant:
3137 case tok::kw___read_only:
3138 case tok::kw___read_write:
3139 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003140 return true;
3141 }
3142}
3143
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003144/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3145/// is definitely a type-specifier. Return false if it isn't part of a type
3146/// specifier or if we're not sure.
3147bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3148 switch (Tok.getKind()) {
3149 default: return false;
3150 // type-specifiers
3151 case tok::kw_short:
3152 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003153 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003154 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003155 case tok::kw_signed:
3156 case tok::kw_unsigned:
3157 case tok::kw__Complex:
3158 case tok::kw__Imaginary:
3159 case tok::kw_void:
3160 case tok::kw_char:
3161 case tok::kw_wchar_t:
3162 case tok::kw_char16_t:
3163 case tok::kw_char32_t:
3164 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003165 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003166 case tok::kw_float:
3167 case tok::kw_double:
3168 case tok::kw_bool:
3169 case tok::kw__Bool:
3170 case tok::kw__Decimal32:
3171 case tok::kw__Decimal64:
3172 case tok::kw__Decimal128:
3173 case tok::kw___vector:
3174
3175 // struct-or-union-specifier (C99) or class-specifier (C++)
3176 case tok::kw_class:
3177 case tok::kw_struct:
3178 case tok::kw_union:
3179 // enum-specifier
3180 case tok::kw_enum:
3181
3182 // typedef-name
3183 case tok::annot_typename:
3184 return true;
3185 }
3186}
3187
Steve Naroff5f8aa692008-02-11 23:15:56 +00003188/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003189/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003190bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003191 switch (Tok.getKind()) {
3192 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003193
Chris Lattner166a8fc2009-01-04 23:41:41 +00003194 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003195 if (TryAltiVecVectorToken())
3196 return true;
3197 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003198 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003199 // Annotate typenames and C++ scope specifiers. If we get one, just
3200 // recurse to handle whatever we get.
3201 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003202 return true;
3203 if (Tok.is(tok::identifier))
3204 return false;
3205 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003206
Chris Lattner166a8fc2009-01-04 23:41:41 +00003207 case tok::coloncolon: // ::foo::bar
3208 if (NextToken().is(tok::kw_new) || // ::new
3209 NextToken().is(tok::kw_delete)) // ::delete
3210 return false;
3211
Chris Lattner166a8fc2009-01-04 23:41:41 +00003212 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003213 return true;
3214 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 // GNU attributes support.
3217 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003218 // GNU typeof support.
3219 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 // type-specifiers
3222 case tok::kw_short:
3223 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003224 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003225 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003226 case tok::kw_signed:
3227 case tok::kw_unsigned:
3228 case tok::kw__Complex:
3229 case tok::kw__Imaginary:
3230 case tok::kw_void:
3231 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003232 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003233 case tok::kw_char16_t:
3234 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003236 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 case tok::kw_float:
3238 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003239 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003240 case tok::kw__Bool:
3241 case tok::kw__Decimal32:
3242 case tok::kw__Decimal64:
3243 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003244 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Chris Lattner99dc9142008-04-13 18:59:07 +00003246 // struct-or-union-specifier (C99) or class-specifier (C++)
3247 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 case tok::kw_struct:
3249 case tok::kw_union:
3250 // enum-specifier
3251 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Reid Spencer5f016e22007-07-11 17:01:13 +00003253 // type-qualifier
3254 case tok::kw_const:
3255 case tok::kw_volatile:
3256 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003257
3258 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003259 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003260 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Chris Lattner7c186be2008-10-20 00:25:30 +00003262 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3263 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003264 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003265
Steve Naroff239f0732008-12-25 14:16:32 +00003266 case tok::kw___cdecl:
3267 case tok::kw___stdcall:
3268 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003269 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003270 case tok::kw___w64:
3271 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003272 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003273 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003274 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003275
3276 case tok::kw___private:
3277 case tok::kw___local:
3278 case tok::kw___global:
3279 case tok::kw___constant:
3280 case tok::kw___read_only:
3281 case tok::kw___read_write:
3282 case tok::kw___write_only:
3283
Eli Friedman290eeb02009-06-08 23:27:34 +00003284 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003285
3286 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003287 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003288
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003289 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003290 case tok::kw__Atomic:
3291 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 }
3293}
3294
3295/// isDeclarationSpecifier() - Return true if the current token is part of a
3296/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003297///
3298/// \param DisambiguatingWithExpression True to indicate that the purpose of
3299/// this check is to disambiguate between an expression and a declaration.
3300bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 switch (Tok.getKind()) {
3302 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003304 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003305 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003306
Chris Lattner166a8fc2009-01-04 23:41:41 +00003307 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003308 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003309 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003310 return false;
John Thompson82287d12010-02-05 00:12:22 +00003311 if (TryAltiVecVectorToken())
3312 return true;
3313 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003314 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003315 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003316 // Annotate typenames and C++ scope specifiers. If we get one, just
3317 // recurse to handle whatever we get.
3318 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003319 return true;
3320 if (Tok.is(tok::identifier))
3321 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003322
3323 // If we're in Objective-C and we have an Objective-C class type followed
3324 // by an identifier and then either ':' or ']', in a place where an
3325 // expression is permitted, then this is probably a class message send
3326 // missing the initial '['. In this case, we won't consider this to be
3327 // the start of a declaration.
3328 if (DisambiguatingWithExpression &&
3329 isStartOfObjCClassMessageMissingOpenBracket())
3330 return false;
3331
John McCall9ba61662010-02-26 08:45:28 +00003332 return isDeclarationSpecifier();
3333
Chris Lattner166a8fc2009-01-04 23:41:41 +00003334 case tok::coloncolon: // ::foo::bar
3335 if (NextToken().is(tok::kw_new) || // ::new
3336 NextToken().is(tok::kw_delete)) // ::delete
3337 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Chris Lattner166a8fc2009-01-04 23:41:41 +00003339 // Annotate typenames and C++ scope specifiers. If we get one, just
3340 // recurse to handle whatever we get.
3341 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003342 return true;
3343 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 // storage-class-specifier
3346 case tok::kw_typedef:
3347 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003348 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 case tok::kw_static:
3350 case tok::kw_auto:
3351 case tok::kw_register:
3352 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Douglas Gregor8d267c52011-09-09 02:06:17 +00003354 // Modules
3355 case tok::kw___module_private__:
3356
Reid Spencer5f016e22007-07-11 17:01:13 +00003357 // type-specifiers
3358 case tok::kw_short:
3359 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003360 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003361 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003362 case tok::kw_signed:
3363 case tok::kw_unsigned:
3364 case tok::kw__Complex:
3365 case tok::kw__Imaginary:
3366 case tok::kw_void:
3367 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003368 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003369 case tok::kw_char16_t:
3370 case tok::kw_char32_t:
3371
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003373 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003374 case tok::kw_float:
3375 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003376 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003377 case tok::kw__Bool:
3378 case tok::kw__Decimal32:
3379 case tok::kw__Decimal64:
3380 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003381 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003382
Chris Lattner99dc9142008-04-13 18:59:07 +00003383 // struct-or-union-specifier (C99) or class-specifier (C++)
3384 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003385 case tok::kw_struct:
3386 case tok::kw_union:
3387 // enum-specifier
3388 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003389
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 // type-qualifier
3391 case tok::kw_const:
3392 case tok::kw_volatile:
3393 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003394
Reid Spencer5f016e22007-07-11 17:01:13 +00003395 // function-specifier
3396 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003397 case tok::kw_virtual:
3398 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003399
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003400 // static_assert-declaration
3401 case tok::kw__Static_assert:
3402
Chris Lattner1ef08762007-08-09 17:01:07 +00003403 // GNU typeof support.
3404 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003405
Chris Lattner1ef08762007-08-09 17:01:07 +00003406 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003407 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003408 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003409
Francois Pichete3d49b42011-06-19 08:02:06 +00003410 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003411 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003412 return true;
3413
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003414 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003415 case tok::kw__Atomic:
3416 return true;
3417
Chris Lattnerf3948c42008-07-26 03:38:44 +00003418 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3419 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003420 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Douglas Gregord9d75e52011-04-27 05:41:15 +00003422 // typedef-name
3423 case tok::annot_typename:
3424 return !DisambiguatingWithExpression ||
3425 !isStartOfObjCClassMessageMissingOpenBracket();
3426
Steve Naroff47f52092009-01-06 19:34:12 +00003427 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003428 case tok::kw___cdecl:
3429 case tok::kw___stdcall:
3430 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003431 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003432 case tok::kw___w64:
3433 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003434 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003435 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003436 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003437 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003438
3439 case tok::kw___private:
3440 case tok::kw___local:
3441 case tok::kw___global:
3442 case tok::kw___constant:
3443 case tok::kw___read_only:
3444 case tok::kw___read_write:
3445 case tok::kw___write_only:
3446
Eli Friedman290eeb02009-06-08 23:27:34 +00003447 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003448 }
3449}
3450
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003451bool Parser::isConstructorDeclarator() {
3452 TentativeParsingAction TPA(*this);
3453
3454 // Parse the C++ scope specifier.
3455 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003456 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3457 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003458 TPA.Revert();
3459 return false;
3460 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003461
3462 // Parse the constructor name.
3463 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3464 // We already know that we have a constructor name; just consume
3465 // the token.
3466 ConsumeToken();
3467 } else {
3468 TPA.Revert();
3469 return false;
3470 }
3471
Richard Smith22592862012-03-27 23:05:05 +00003472 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003473 if (Tok.isNot(tok::l_paren)) {
3474 TPA.Revert();
3475 return false;
3476 }
3477 ConsumeParen();
3478
Richard Smith22592862012-03-27 23:05:05 +00003479 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3480 // that we have a constructor.
3481 if (Tok.is(tok::r_paren) ||
3482 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003483 TPA.Revert();
3484 return true;
3485 }
3486
3487 // If we need to, enter the specified scope.
3488 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003489 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003490 DeclScopeObj.EnterDeclaratorScope();
3491
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003492 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003493 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003494 MaybeParseMicrosoftAttributes(Attrs);
3495
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003496 // Check whether the next token(s) are part of a declaration
3497 // specifier, in which case we have the start of a parameter and,
3498 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003499 bool IsConstructor = false;
3500 if (isDeclarationSpecifier())
3501 IsConstructor = true;
3502 else if (Tok.is(tok::identifier) ||
3503 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3504 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3505 // This might be a parenthesized member name, but is more likely to
3506 // be a constructor declaration with an invalid argument type. Keep
3507 // looking.
3508 if (Tok.is(tok::annot_cxxscope))
3509 ConsumeToken();
3510 ConsumeToken();
3511
3512 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003513 // which must have one of the following syntactic forms (see the
3514 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003515 switch (Tok.getKind()) {
3516 case tok::l_paren:
3517 // C(X ( int));
3518 case tok::l_square:
3519 // C(X [ 5]);
3520 // C(X [ [attribute]]);
3521 case tok::coloncolon:
3522 // C(X :: Y);
3523 // C(X :: *p);
3524 case tok::r_paren:
3525 // C(X )
3526 // Assume this isn't a constructor, rather than assuming it's a
3527 // constructor with an unnamed parameter of an ill-formed type.
3528 break;
3529
3530 default:
3531 IsConstructor = true;
3532 break;
3533 }
3534 }
3535
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003536 TPA.Revert();
3537 return IsConstructor;
3538}
Reid Spencer5f016e22007-07-11 17:01:13 +00003539
3540/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003541/// type-qualifier-list: [C99 6.7.5]
3542/// type-qualifier
3543/// [vendor] attributes
3544/// [ only if VendorAttributesAllowed=true ]
3545/// type-qualifier-list type-qualifier
3546/// [vendor] type-qualifier-list attributes
3547/// [ only if VendorAttributesAllowed=true ]
3548/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3549/// [ only if CXX0XAttributesAllowed=true ]
3550/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003551///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003552void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3553 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003554 bool CXX11AttributesAllowed) {
3555 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003556 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003557 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003558 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003559 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003560 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003561
3562 SourceLocation EndLoc;
3563
Reid Spencer5f016e22007-07-11 17:01:13 +00003564 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003565 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003566 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003567 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003568 SourceLocation Loc = Tok.getLocation();
3569
3570 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003571 case tok::code_completion:
3572 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003573 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003574
Reid Spencer5f016e22007-07-11 17:01:13 +00003575 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003576 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003577 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 break;
3579 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003580 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003581 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003582 break;
3583 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003585 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003586 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003587
3588 // OpenCL qualifiers:
3589 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003590 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003591 goto DoneWithTypeQuals;
3592 case tok::kw___private:
3593 case tok::kw___global:
3594 case tok::kw___local:
3595 case tok::kw___constant:
3596 case tok::kw___read_only:
3597 case tok::kw___write_only:
3598 case tok::kw___read_write:
3599 ParseOpenCLQualifiers(DS);
3600 break;
3601
Eli Friedman290eeb02009-06-08 23:27:34 +00003602 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003603 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003604 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003605 case tok::kw___cdecl:
3606 case tok::kw___stdcall:
3607 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003608 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003609 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003610 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003611 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003612 continue;
3613 }
3614 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003615 case tok::kw___pascal:
3616 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003617 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003618 continue;
3619 }
3620 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003622 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003623 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003624 continue; // do *not* consume the next token!
3625 }
3626 // otherwise, FALL THROUGH!
3627 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003628 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003629 // If this is not a type-qualifier token, we're done reading type
3630 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003631 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003632 if (EndLoc.isValid())
3633 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003634 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003635 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003636
Reid Spencer5f016e22007-07-11 17:01:13 +00003637 // If the specifier combination wasn't legal, issue a diagnostic.
3638 if (isInvalid) {
3639 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003640 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003641 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003642 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003643 }
3644}
3645
3646
3647/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3648///
3649void Parser::ParseDeclarator(Declarator &D) {
3650 /// This implements the 'declarator' production in the C grammar, then checks
3651 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003652 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003653}
3654
Richard Smith9988f282012-03-29 01:16:42 +00003655static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3656 if (Kind == tok::star || Kind == tok::caret)
3657 return true;
3658
3659 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3660 if (!Lang.CPlusPlus)
3661 return false;
3662
3663 return Kind == tok::amp || Kind == tok::ampamp;
3664}
3665
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003666/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3667/// is parsed by the function passed to it. Pass null, and the direct-declarator
3668/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003669/// ptr-operator production.
3670///
Richard Smith0706df42011-10-19 21:33:05 +00003671/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003672/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3673/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003674///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003675/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3676/// [C] pointer[opt] direct-declarator
3677/// [C++] direct-declarator
3678/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003679///
3680/// pointer: [C99 6.7.5]
3681/// '*' type-qualifier-list[opt]
3682/// '*' type-qualifier-list[opt] pointer
3683///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003684/// ptr-operator:
3685/// '*' cv-qualifier-seq[opt]
3686/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003687/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003688/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003689/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003690/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003691void Parser::ParseDeclaratorInternal(Declarator &D,
3692 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003693 if (Diags.hasAllExtensionsSilenced())
3694 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003695
Sebastian Redlf30208a2009-01-24 21:16:55 +00003696 // C++ member pointers start with a '::' or a nested-name.
3697 // Member pointers get special handling, since there's no place for the
3698 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003699 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003700 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3701 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003702 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3703 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003704 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003705 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003706
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003707 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003708 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003709 // The scope spec really belongs to the direct-declarator.
3710 D.getCXXScopeSpec() = SS;
3711 if (DirectDeclParser)
3712 (this->*DirectDeclParser)(D);
3713 return;
3714 }
3715
3716 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003717 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003718 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003719 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003720 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003721
3722 // Recurse to parse whatever is left.
3723 ParseDeclaratorInternal(D, DirectDeclParser);
3724
3725 // Sema will have to catch (syntactically invalid) pointers into global
3726 // scope. It has to catch pointers into namespace scope anyway.
3727 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003728 Loc),
3729 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003730 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003731 return;
3732 }
3733 }
3734
3735 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003736 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003737 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003738 if (DirectDeclParser)
3739 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003740 return;
3741 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003742
Sebastian Redl05532f22009-03-15 22:02:01 +00003743 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3744 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003745 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003746 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003747
Chris Lattner9af55002009-03-27 04:18:06 +00003748 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003749 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003750 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003751
Richard Smith6ee326a2012-04-10 01:32:12 +00003752 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003753 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003754 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003755
Reid Spencer5f016e22007-07-11 17:01:13 +00003756 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003757 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003758 if (Kind == tok::star)
3759 // Remember that we parsed a pointer type, and remember the type-quals.
3760 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003761 DS.getConstSpecLoc(),
3762 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003763 DS.getRestrictSpecLoc()),
3764 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003765 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003766 else
3767 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003768 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003769 Loc),
3770 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003771 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003772 } else {
3773 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003774 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003775
Sebastian Redl743de1f2009-03-23 00:00:23 +00003776 // Complain about rvalue references in C++03, but then go on and build
3777 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003778 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003779 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003780 diag::warn_cxx98_compat_rvalue_reference :
3781 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003782
Richard Smith6ee326a2012-04-10 01:32:12 +00003783 // GNU-style and C++11 attributes are allowed here, as is restrict.
3784 ParseTypeQualifierListOpt(DS);
3785 D.ExtendWithDeclSpec(DS);
3786
Reid Spencer5f016e22007-07-11 17:01:13 +00003787 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3788 // cv-qualifiers are introduced through the use of a typedef or of a
3789 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003790 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3791 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3792 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003793 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003794 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3795 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003796 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003797 }
3798
3799 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003800 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003801
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003802 if (D.getNumTypeObjects() > 0) {
3803 // C++ [dcl.ref]p4: There shall be no references to references.
3804 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3805 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003806 if (const IdentifierInfo *II = D.getIdentifier())
3807 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3808 << II;
3809 else
3810 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3811 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003812
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003813 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003814 // can go ahead and build the (technically ill-formed)
3815 // declarator: reference collapsing will take care of it.
3816 }
3817 }
3818
Reid Spencer5f016e22007-07-11 17:01:13 +00003819 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003820 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003821 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003822 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003823 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003824 }
3825}
3826
Richard Smith9988f282012-03-29 01:16:42 +00003827static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3828 SourceLocation EllipsisLoc) {
3829 if (EllipsisLoc.isValid()) {
3830 FixItHint Insertion;
3831 if (!D.getEllipsisLoc().isValid()) {
3832 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3833 D.setEllipsisLoc(EllipsisLoc);
3834 }
3835 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3836 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3837 }
3838}
3839
Reid Spencer5f016e22007-07-11 17:01:13 +00003840/// ParseDirectDeclarator
3841/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003842/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003843/// '(' declarator ')'
3844/// [GNU] '(' attributes declarator ')'
3845/// [C90] direct-declarator '[' constant-expression[opt] ']'
3846/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3847/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3848/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3849/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003850/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3851/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003852/// direct-declarator '(' parameter-type-list ')'
3853/// direct-declarator '(' identifier-list[opt] ')'
3854/// [GNU] direct-declarator '(' parameter-forward-declarations
3855/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003856/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3857/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003858/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3859/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3860/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003861/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003862/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003863///
3864/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003865/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003866/// '::'[opt] nested-name-specifier[opt] type-name
3867///
3868/// id-expression: [C++ 5.1]
3869/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003870/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003871///
3872/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003873/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003874/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003875/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003876/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003877/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003878///
Richard Smith5d8388c2012-03-27 01:42:32 +00003879/// Note, any additional constructs added here may need corresponding changes
3880/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003881void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003882 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003883
David Blaikie4e4d0842012-03-11 07:00:24 +00003884 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003885 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003886 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003887 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3888 D.getContext() == Declarator::MemberContext;
3889 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3890 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003891 }
3892
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003893 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003894 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003895 // Change the declaration context for name lookup, until this function
3896 // is exited (and the declarator has been parsed).
3897 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003898 }
3899
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003900 // C++0x [dcl.fct]p14:
3901 // There is a syntactic ambiguity when an ellipsis occurs at the end
3902 // of a parameter-declaration-clause without a preceding comma. In
3903 // this case, the ellipsis is parsed as part of the
3904 // abstract-declarator if the type of the parameter names a template
3905 // parameter pack that has not been expanded; otherwise, it is parsed
3906 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003907 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003908 !((D.getContext() == Declarator::PrototypeContext ||
3909 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003910 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003911 !Actions.containsUnexpandedParameterPacks(D))) {
3912 SourceLocation EllipsisLoc = ConsumeToken();
3913 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3914 // The ellipsis was put in the wrong place. Recover, and explain to
3915 // the user what they should have done.
3916 ParseDeclarator(D);
3917 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3918 return;
3919 } else
3920 D.setEllipsisLoc(EllipsisLoc);
3921
3922 // The ellipsis can't be followed by a parenthesized declarator. We
3923 // check for that in ParseParenDeclarator, after we have disambiguated
3924 // the l_paren token.
3925 }
3926
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003927 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3928 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3929 // We found something that indicates the start of an unqualified-id.
3930 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003931 bool AllowConstructorName;
3932 if (D.getDeclSpec().hasTypeSpecifier())
3933 AllowConstructorName = false;
3934 else if (D.getCXXScopeSpec().isSet())
3935 AllowConstructorName =
3936 (D.getContext() == Declarator::FileContext ||
3937 (D.getContext() == Declarator::MemberContext &&
3938 D.getDeclSpec().isFriendSpecified()));
3939 else
3940 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3941
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003942 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003943 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3944 /*EnteringContext=*/true,
3945 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003946 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003947 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003948 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003949 D.getName()) ||
3950 // Once we're past the identifier, if the scope was bad, mark the
3951 // whole declarator bad.
3952 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003953 D.SetIdentifier(0, Tok.getLocation());
3954 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003955 } else {
3956 // Parsed the unqualified-id; update range information and move along.
3957 if (D.getSourceRange().getBegin().isInvalid())
3958 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3959 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003960 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003961 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003962 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003963 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003964 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003965 "There's a C++-specific check for tok::identifier above");
3966 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3967 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3968 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003969 goto PastIdentifier;
3970 }
Richard Smith9988f282012-03-29 01:16:42 +00003971
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003972 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003973 // direct-declarator: '(' declarator ')'
3974 // direct-declarator: '(' attributes declarator ')'
3975 // Example: 'char (*X)' or 'int (*XX)(void)'
3976 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003977
3978 // If the declarator was parenthesized, we entered the declarator
3979 // scope when parsing the parenthesized declarator, then exited
3980 // the scope already. Re-enter the scope, if we need to.
3981 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003982 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003983 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003984 if (!D.isInvalidType() &&
3985 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003986 // Change the declaration context for name lookup, until this function
3987 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003988 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003989 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003990 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003991 // This could be something simple like "int" (in which case the declarator
3992 // portion is empty), if an abstract-declarator is allowed.
3993 D.SetIdentifier(0, Tok.getLocation());
3994 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003995 if (D.getContext() == Declarator::MemberContext)
3996 Diag(Tok, diag::err_expected_member_name_or_semi)
3997 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003998 else if (getLangOpts().CPlusPlus)
3999 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004000 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004001 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004002 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004003 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004004 }
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004006 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004007 assert(D.isPastIdentifier() &&
4008 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004009
Richard Smith6ee326a2012-04-10 01:32:12 +00004010 // Don't parse attributes unless we have parsed an unparenthesized name.
4011 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004012 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004013
Reid Spencer5f016e22007-07-11 17:01:13 +00004014 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004015 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004016 // Enter function-declaration scope, limiting any declarators to the
4017 // function prototype scope, including parameter declarators.
4018 ParseScope PrototypeScope(this,
4019 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004020 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4021 // In such a case, check if we actually have a function declarator; if it
4022 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004023 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004024 // When not in file scope, warn for ambiguous function declarators, just
4025 // in case the author intended it as a variable definition.
4026 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4027 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4028 break;
4029 }
John McCall0b7e6782011-03-24 11:26:52 +00004030 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004031 BalancedDelimiterTracker T(*this, tok::l_paren);
4032 T.consumeOpen();
4033 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004034 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004035 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004036 ParseBracketDeclarator(D);
4037 } else {
4038 break;
4039 }
4040 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004041}
Reid Spencer5f016e22007-07-11 17:01:13 +00004042
Chris Lattneref4715c2008-04-06 05:45:57 +00004043/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4044/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004045/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004046/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4047///
4048/// direct-declarator:
4049/// '(' declarator ')'
4050/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004051/// direct-declarator '(' parameter-type-list ')'
4052/// direct-declarator '(' identifier-list[opt] ')'
4053/// [GNU] direct-declarator '(' parameter-forward-declarations
4054/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004055///
4056void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004057 BalancedDelimiterTracker T(*this, tok::l_paren);
4058 T.consumeOpen();
4059
Chris Lattneref4715c2008-04-06 05:45:57 +00004060 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004061
Chris Lattner7399ee02008-10-20 02:05:46 +00004062 // Eat any attributes before we look at whether this is a grouping or function
4063 // declarator paren. If this is a grouping paren, the attribute applies to
4064 // the type being built up, for example:
4065 // int (__attribute__(()) *x)(long y)
4066 // If this ends up not being a grouping paren, the attribute applies to the
4067 // first argument, for example:
4068 // int (__attribute__(()) int x)
4069 // In either case, we need to eat any attributes to be able to determine what
4070 // sort of paren this is.
4071 //
John McCall0b7e6782011-03-24 11:26:52 +00004072 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004073 bool RequiresArg = false;
4074 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004075 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004076
Chris Lattner7399ee02008-10-20 02:05:46 +00004077 // We require that the argument list (if this is a non-grouping paren) be
4078 // present even if the attribute list was empty.
4079 RequiresArg = true;
4080 }
Steve Naroff239f0732008-12-25 14:16:32 +00004081 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004082 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004083 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004084 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004085 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004086 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004087 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004088 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004089 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004090 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004091
Chris Lattneref4715c2008-04-06 05:45:57 +00004092 // If we haven't past the identifier yet (or where the identifier would be
4093 // stored, if this is an abstract declarator), then this is probably just
4094 // grouping parens. However, if this could be an abstract-declarator, then
4095 // this could also be the start of function arguments (consider 'void()').
4096 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004097
Chris Lattneref4715c2008-04-06 05:45:57 +00004098 if (!D.mayOmitIdentifier()) {
4099 // If this can't be an abstract-declarator, this *must* be a grouping
4100 // paren, because we haven't seen the identifier yet.
4101 isGrouping = true;
4102 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004103 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4104 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004105 isDeclarationSpecifier() || // 'int(int)' is a function.
4106 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004107 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4108 // considered to be a type, not a K&R identifier-list.
4109 isGrouping = false;
4110 } else {
4111 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4112 isGrouping = true;
4113 }
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Chris Lattneref4715c2008-04-06 05:45:57 +00004115 // If this is a grouping paren, handle:
4116 // direct-declarator: '(' declarator ')'
4117 // direct-declarator: '(' attributes declarator ')'
4118 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004119 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4120 D.setEllipsisLoc(SourceLocation());
4121
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004122 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004123 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004124 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004125 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004126 T.consumeClose();
4127 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4128 T.getCloseLocation()),
4129 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004130
4131 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004132
4133 // An ellipsis cannot be placed outside parentheses.
4134 if (EllipsisLoc.isValid())
4135 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4136
Chris Lattneref4715c2008-04-06 05:45:57 +00004137 return;
4138 }
Mike Stump1eb44332009-09-09 15:08:12 +00004139
Chris Lattneref4715c2008-04-06 05:45:57 +00004140 // Okay, if this wasn't a grouping paren, it must be the start of a function
4141 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004142 // identifier (and remember where it would have been), then call into
4143 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004144 D.SetIdentifier(0, Tok.getLocation());
4145
David Blaikie42d6d0c2011-12-04 05:04:18 +00004146 // Enter function-declaration scope, limiting any declarators to the
4147 // function prototype scope, including parameter declarators.
4148 ParseScope PrototypeScope(this,
4149 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004150 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004151 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004152}
4153
4154/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4155/// declarator D up to a paren, which indicates that we are parsing function
4156/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004157///
Richard Smith6ee326a2012-04-10 01:32:12 +00004158/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4159/// immediately after the open paren - they should be considered to be the
4160/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004161///
Richard Smith6ee326a2012-04-10 01:32:12 +00004162/// If RequiresArg is true, then the first argument of the function is required
4163/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004164///
Richard Smith6ee326a2012-04-10 01:32:12 +00004165/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4166/// (C++11) ref-qualifier[opt], exception-specification[opt],
4167/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4168///
4169/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004170/// dynamic-exception-specification
4171/// noexcept-specification
4172///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004173void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004174 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004175 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004176 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004177 assert(getCurScope()->isFunctionPrototypeScope() &&
4178 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004179 // lparen is already consumed!
4180 assert(D.isPastIdentifier() && "Should not call before identifier!");
4181
4182 // This should be true when the function has typed arguments.
4183 // Otherwise, it is treated as a K&R-style function.
4184 bool HasProto = false;
4185 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004186 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004187 // Remember where we see an ellipsis, if any.
4188 SourceLocation EllipsisLoc;
4189
4190 DeclSpec DS(AttrFactory);
4191 bool RefQualifierIsLValueRef = true;
4192 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004193 SourceLocation ConstQualifierLoc;
4194 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004195 ExceptionSpecificationType ESpecType = EST_None;
4196 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004197 SmallVector<ParsedType, 2> DynamicExceptions;
4198 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004199 ExprResult NoexceptExpr;
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004200 CachedTokens *ExceptionSpecTokens = 0;
Richard Smith6ee326a2012-04-10 01:32:12 +00004201 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004202 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004203
James Molloy16f1f712012-02-29 10:24:19 +00004204 Actions.ActOnStartFunctionDeclarator();
4205
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004206 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004207 if (isFunctionDeclaratorIdentifierList()) {
4208 if (RequiresArg)
4209 Diag(Tok, diag::err_argument_required_after_attribute);
4210
4211 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4212
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004213 Tracker.consumeClose();
4214 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004215 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004216 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004217 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004218 else if (RequiresArg)
4219 Diag(Tok, diag::err_argument_required_after_attribute);
4220
David Blaikie4e4d0842012-03-11 07:00:24 +00004221 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004222
4223 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004224 Tracker.consumeClose();
4225 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004226
David Blaikie4e4d0842012-03-11 07:00:24 +00004227 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004228 // FIXME: Accept these components in any order, and produce fixits to
4229 // correct the order if the user gets it wrong. Ideally we should deal
4230 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004231
4232 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004233 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4234 if (!DS.getSourceRange().getEnd().isInvalid()) {
4235 EndLoc = DS.getSourceRange().getEnd();
4236 ConstQualifierLoc = DS.getConstSpecLoc();
4237 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4238 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004239
4240 // Parse ref-qualifier[opt].
4241 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004242 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004243 diag::warn_cxx98_compat_ref_qualifier :
4244 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004245
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004246 RefQualifierIsLValueRef = Tok.is(tok::amp);
4247 RefQualifierLoc = ConsumeToken();
4248 EndLoc = RefQualifierLoc;
4249 }
4250
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004251 // C++11 [expr.prim.general]p3:
4252 // If a declaration declares a member function or member function
4253 // template of a class X, the expression this is a prvalue of type
4254 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4255 // and the end of the function-definition, member-declarator, or
4256 // declarator.
4257 bool IsCXX11MemberFunction =
4258 getLangOpts().CPlusPlus0x &&
4259 (D.getContext() == Declarator::MemberContext ||
4260 (D.getContext() == Declarator::FileContext &&
4261 D.getCXXScopeSpec().isValid() &&
4262 Actions.CurContext->isRecord()));
4263 Sema::CXXThisScopeRAII ThisScope(Actions,
4264 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4265 DS.getTypeQualifiers(),
4266 IsCXX11MemberFunction);
4267
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004268 // Parse exception-specification[opt].
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004269 bool Delayed = (D.getContext() == Declarator::MemberContext &&
4270 D.getDeclSpec().getStorageClassSpec()
4271 != DeclSpec::SCS_typedef &&
4272 !D.getDeclSpec().isFriendSpecified());
4273 ESpecType = tryParseExceptionSpecification(Delayed,
4274 ESpecRange,
4275 DynamicExceptions,
4276 DynamicExceptionRanges,
4277 NoexceptExpr,
4278 ExceptionSpecTokens);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004279 if (ESpecType != EST_None)
4280 EndLoc = ESpecRange.getEnd();
4281
Richard Smith6ee326a2012-04-10 01:32:12 +00004282 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4283 // after the exception-specification.
4284 MaybeParseCXX0XAttributes(FnAttrs);
4285
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004286 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004287 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004288 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004289 SourceRange Range;
4290 TrailingReturnType = ParseTrailingReturnType(Range).get();
4291 if (Range.getEnd().isValid())
4292 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004293 }
4294 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004295 }
4296
4297 // Remember that we parsed a function type, and remember the attributes.
4298 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4299 /*isVariadic=*/EllipsisLoc.isValid(),
4300 EllipsisLoc,
4301 ParamInfo.data(), ParamInfo.size(),
4302 DS.getTypeQualifiers(),
4303 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004304 RefQualifierLoc, ConstQualifierLoc,
4305 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004306 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004307 ESpecType, ESpecRange.getBegin(),
4308 DynamicExceptions.data(),
4309 DynamicExceptionRanges.data(),
4310 DynamicExceptions.size(),
4311 NoexceptExpr.isUsable() ?
4312 NoexceptExpr.get() : 0,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004313 ExceptionSpecTokens,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004314 Tracker.getOpenLocation(),
4315 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004316 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004317 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004318
4319 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004320}
4321
4322/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4323/// identifier list form for a K&R-style function: void foo(a,b,c)
4324///
4325/// Note that identifier-lists are only allowed for normal declarators, not for
4326/// abstract-declarators.
4327bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004328 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004329 && Tok.is(tok::identifier)
4330 && !TryAltiVecVectorToken()
4331 // K&R identifier lists can't have typedefs as identifiers, per C99
4332 // 6.7.5.3p11.
4333 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4334 // Identifier lists follow a really simple grammar: the identifiers can
4335 // be followed *only* by a ", identifier" or ")". However, K&R
4336 // identifier lists are really rare in the brave new modern world, and
4337 // it is very common for someone to typo a type in a non-K&R style
4338 // list. If we are presented with something like: "void foo(intptr x,
4339 // float y)", we don't want to start parsing the function declarator as
4340 // though it is a K&R style declarator just because intptr is an
4341 // invalid type.
4342 //
4343 // To handle this, we check to see if the token after the first
4344 // identifier is a "," or ")". Only then do we parse it as an
4345 // identifier list.
4346 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4347}
4348
4349/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4350/// we found a K&R-style identifier list instead of a typed parameter list.
4351///
4352/// After returning, ParamInfo will hold the parsed parameters.
4353///
4354/// identifier-list: [C99 6.7.5]
4355/// identifier
4356/// identifier-list ',' identifier
4357///
4358void Parser::ParseFunctionDeclaratorIdentifierList(
4359 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004360 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004361 // If there was no identifier specified for the declarator, either we are in
4362 // an abstract-declarator, or we are in a parameter declarator which was found
4363 // to be abstract. In abstract-declarators, identifier lists are not valid:
4364 // diagnose this.
4365 if (!D.getIdentifier())
4366 Diag(Tok, diag::ext_ident_list_in_param);
4367
4368 // Maintain an efficient lookup of params we have seen so far.
4369 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4370
4371 while (1) {
4372 // If this isn't an identifier, report the error and skip until ')'.
4373 if (Tok.isNot(tok::identifier)) {
4374 Diag(Tok, diag::err_expected_ident);
4375 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4376 // Forget we parsed anything.
4377 ParamInfo.clear();
4378 return;
4379 }
4380
4381 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4382
4383 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4384 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4385 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4386
4387 // Verify that the argument identifier has not already been mentioned.
4388 if (!ParamsSoFar.insert(ParmII)) {
4389 Diag(Tok, diag::err_param_redefinition) << ParmII;
4390 } else {
4391 // Remember this identifier in ParamInfo.
4392 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4393 Tok.getLocation(),
4394 0));
4395 }
4396
4397 // Eat the identifier.
4398 ConsumeToken();
4399
4400 // The list continues if we see a comma.
4401 if (Tok.isNot(tok::comma))
4402 break;
4403 ConsumeToken();
4404 }
4405}
4406
4407/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4408/// after the opening parenthesis. This function will not parse a K&R-style
4409/// identifier list.
4410///
Richard Smith6ce48a72012-04-11 04:01:28 +00004411/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4412/// caller parsed those arguments immediately after the open paren - they should
4413/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004414///
4415/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4416/// be the location of the ellipsis, if any was parsed.
4417///
Reid Spencer5f016e22007-07-11 17:01:13 +00004418/// parameter-type-list: [C99 6.7.5]
4419/// parameter-list
4420/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004421/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004422///
4423/// parameter-list: [C99 6.7.5]
4424/// parameter-declaration
4425/// parameter-list ',' parameter-declaration
4426///
4427/// parameter-declaration: [C99 6.7.5]
4428/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004429/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004430/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004431/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004432/// declaration-specifiers abstract-declarator[opt]
4433/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004434/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004435/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004436/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004437///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004438void Parser::ParseParameterDeclarationClause(
4439 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004440 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004441 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004442 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004443
Chris Lattnerf97409f2008-04-06 06:57:35 +00004444 while (1) {
4445 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004446 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4447 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004448 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004449 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004450 }
Mike Stump1eb44332009-09-09 15:08:12 +00004451
Chris Lattnerf97409f2008-04-06 06:57:35 +00004452 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004453 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004454 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004455
Richard Smith6ce48a72012-04-11 04:01:28 +00004456 // Parse any C++11 attributes.
4457 MaybeParseCXX0XAttributes(DS.getAttributes());
4458
John McCall7f040a92010-12-24 02:08:15 +00004459 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004460 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004461 ParseMicrosoftAttributes(DS.getAttributes());
4462
4463 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004464
4465 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004466 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004467 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004468 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4469 // too much hassle.
4470 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004471
Chris Lattnere64c5492009-02-27 18:38:20 +00004472 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004473
Chris Lattnerf97409f2008-04-06 06:57:35 +00004474 // Parse the declarator. This is "PrototypeContext", because we must
4475 // accept either 'declarator' or 'abstract-declarator' here.
4476 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4477 ParseDeclarator(ParmDecl);
4478
4479 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004480 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004481
Chris Lattnerf97409f2008-04-06 06:57:35 +00004482 // Remember this parsed parameter in ParamInfo.
4483 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004484
Douglas Gregor72b505b2008-12-16 21:30:33 +00004485 // DefArgToks is used when the parsing of default arguments needs
4486 // to be delayed.
4487 CachedTokens *DefArgToks = 0;
4488
Chris Lattnerf97409f2008-04-06 06:57:35 +00004489 // If no parameter was specified, verify that *something* was specified,
4490 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004491 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4492 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004493 // Completely missing, emit error.
4494 Diag(DSStart, diag::err_missing_param);
4495 } else {
4496 // Otherwise, we have something. Add it and let semantic analysis try
4497 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004498
Chris Lattnerf97409f2008-04-06 06:57:35 +00004499 // Inform the actions module about the parameter declarator, so it gets
4500 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004501 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004502
4503 // Parse the default argument, if any. We parse the default
4504 // arguments in all dialects; the semantic analysis in
4505 // ActOnParamDefaultArgument will reject the default argument in
4506 // C.
4507 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004508 SourceLocation EqualLoc = Tok.getLocation();
4509
Chris Lattner04421082008-04-08 04:40:51 +00004510 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004511 if (D.getContext() == Declarator::MemberContext) {
4512 // If we're inside a class definition, cache the tokens
4513 // corresponding to the default argument. We'll actually parse
4514 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004515 // FIXME: Can we use a smart pointer for Toks?
4516 DefArgToks = new CachedTokens;
4517
Mike Stump1eb44332009-09-09 15:08:12 +00004518 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004519 /*StopAtSemi=*/true,
4520 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004521 delete DefArgToks;
4522 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004523 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004524 } else {
4525 // Mark the end of the default argument so that we know when to
4526 // stop when we parse it later on.
4527 Token DefArgEnd;
4528 DefArgEnd.startToken();
4529 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4530 DefArgEnd.setLocation(Tok.getLocation());
4531 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004532 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004533 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004534 }
Chris Lattner04421082008-04-08 04:40:51 +00004535 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004536 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004537 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004538
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004539 // The argument isn't actually potentially evaluated unless it is
4540 // used.
4541 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004542 Sema::PotentiallyEvaluatedIfUsed,
4543 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004544
Sebastian Redl84407ba2012-03-14 15:54:00 +00004545 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004546 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4547 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004548 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004549 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004550 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004551 if (DefArgResult.isInvalid()) {
4552 Actions.ActOnParamDefaultArgumentError(Param);
4553 SkipUntil(tok::comma, tok::r_paren, true, true);
4554 } else {
4555 // Inform the actions module about the default argument
4556 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004557 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004558 }
Chris Lattner04421082008-04-08 04:40:51 +00004559 }
4560 }
Mike Stump1eb44332009-09-09 15:08:12 +00004561
4562 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4563 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004564 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004565 }
4566
4567 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004568 if (Tok.isNot(tok::comma)) {
4569 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004570 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4571
David Blaikie4e4d0842012-03-11 07:00:24 +00004572 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004573 // We have ellipsis without a preceding ',', which is ill-formed
4574 // in C. Complain and provide the fix.
4575 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004576 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004577 }
4578 }
4579
4580 break;
4581 }
Mike Stump1eb44332009-09-09 15:08:12 +00004582
Chris Lattnerf97409f2008-04-06 06:57:35 +00004583 // Consume the comma.
4584 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004585 }
Mike Stump1eb44332009-09-09 15:08:12 +00004586
Chris Lattner66d28652008-04-06 06:34:08 +00004587}
Chris Lattneref4715c2008-04-06 05:45:57 +00004588
Reid Spencer5f016e22007-07-11 17:01:13 +00004589/// [C90] direct-declarator '[' constant-expression[opt] ']'
4590/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4591/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4592/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4593/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004594/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4595/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004596void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004597 if (CheckProhibitedCXX11Attribute())
4598 return;
4599
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004600 BalancedDelimiterTracker T(*this, tok::l_square);
4601 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004602
Chris Lattner378c7e42008-12-18 07:27:21 +00004603 // C array syntax has many features, but by-far the most common is [] and [4].
4604 // This code does a fast path to handle some of the most obvious cases.
4605 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004606 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004607 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004608 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004609
Chris Lattner378c7e42008-12-18 07:27:21 +00004610 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004611 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004612 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004613 T.getOpenLocation(),
4614 T.getCloseLocation()),
4615 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004616 return;
4617 } else if (Tok.getKind() == tok::numeric_constant &&
4618 GetLookAheadToken(1).is(tok::r_square)) {
4619 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004620 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004621 ConsumeToken();
4622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004623 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004624 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004625 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004626
Chris Lattner378c7e42008-12-18 07:27:21 +00004627 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004628 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004629 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004630 T.getOpenLocation(),
4631 T.getCloseLocation()),
4632 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004633 return;
4634 }
Mike Stump1eb44332009-09-09 15:08:12 +00004635
Reid Spencer5f016e22007-07-11 17:01:13 +00004636 // If valid, this location is the position where we read the 'static' keyword.
4637 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004638 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004639 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004640
Reid Spencer5f016e22007-07-11 17:01:13 +00004641 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004642 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004643 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004644 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004645
Reid Spencer5f016e22007-07-11 17:01:13 +00004646 // If we haven't already read 'static', check to see if there is one after the
4647 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004648 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004649 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004650
Reid Spencer5f016e22007-07-11 17:01:13 +00004651 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4652 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004653 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004654
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004655 // Handle the case where we have '[*]' as the array size. However, a leading
4656 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4657 // the the token after the star is a ']'. Since stars in arrays are
4658 // infrequent, use of lookahead is not costly here.
4659 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004660 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004661
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004662 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004663 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004664 StaticLoc = SourceLocation(); // Drop the static.
4665 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004666 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004667 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004668 // Note, in C89, this production uses the constant-expr production instead
4669 // of assignment-expr. The only difference is that assignment-expr allows
4670 // things like '=' and '*='. Sema rejects these in C89 mode because they
4671 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004672
Douglas Gregore0762c92009-06-19 23:52:42 +00004673 // Parse the constant-expression or assignment-expression now (depending
4674 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004675 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004676 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004677 } else {
4678 EnterExpressionEvaluationContext Unevaluated(Actions,
4679 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004680 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004681 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004682 }
Mike Stump1eb44332009-09-09 15:08:12 +00004683
Reid Spencer5f016e22007-07-11 17:01:13 +00004684 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004685 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004686 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004687 // If the expression was invalid, skip it.
4688 SkipUntil(tok::r_square);
4689 return;
4690 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004691
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004692 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004693
John McCall0b7e6782011-03-24 11:26:52 +00004694 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004695 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004696
Chris Lattner378c7e42008-12-18 07:27:21 +00004697 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004698 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004699 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004700 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004701 T.getOpenLocation(),
4702 T.getCloseLocation()),
4703 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004704}
4705
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004706/// [GNU] typeof-specifier:
4707/// typeof ( expressions )
4708/// typeof ( type-name )
4709/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004710///
4711void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004712 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004713 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004714 SourceLocation StartLoc = ConsumeToken();
4715
John McCallcfb708c2010-01-13 20:03:27 +00004716 const bool hasParens = Tok.is(tok::l_paren);
4717
Eli Friedman71b8fb52012-01-21 01:01:51 +00004718 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4719
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004720 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004721 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004722 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004723 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4724 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004725 if (hasParens)
4726 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004727
4728 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004729 // FIXME: Not accurate, the range gets one token more than it should.
4730 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004731 else
4732 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004733
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004734 if (isCastExpr) {
4735 if (!CastTy) {
4736 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004737 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004738 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004739
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004740 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004741 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004742 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4743 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004744 DiagID, CastTy))
4745 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004746 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004747 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004748
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004749 // If we get here, the operand to the typeof was an expresion.
4750 if (Operand.isInvalid()) {
4751 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004752 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004753 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004754
Eli Friedman71b8fb52012-01-21 01:01:51 +00004755 // We might need to transform the operand if it is potentially evaluated.
4756 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4757 if (Operand.isInvalid()) {
4758 DS.SetTypeSpecError();
4759 return;
4760 }
4761
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004762 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004763 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004764 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4765 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004766 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004767 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004768}
Chris Lattner1b492422010-02-28 18:33:55 +00004769
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004770/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004771/// _Atomic ( type-name )
4772///
4773void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4774 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4775
4776 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004777 BalancedDelimiterTracker T(*this, tok::l_paren);
4778 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004779 SkipUntil(tok::r_paren);
4780 return;
4781 }
4782
4783 TypeResult Result = ParseTypeName();
4784 if (Result.isInvalid()) {
4785 SkipUntil(tok::r_paren);
4786 return;
4787 }
4788
4789 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004790 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004791
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004792 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004793 return;
4794
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004795 DS.setTypeofParensRange(T.getRange());
4796 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004797
4798 const char *PrevSpec = 0;
4799 unsigned DiagID;
4800 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4801 DiagID, Result.release()))
4802 Diag(StartLoc, DiagID) << PrevSpec;
4803}
4804
Chris Lattner1b492422010-02-28 18:33:55 +00004805
4806/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4807/// from TryAltiVecVectorToken.
4808bool Parser::TryAltiVecVectorTokenOutOfLine() {
4809 Token Next = NextToken();
4810 switch (Next.getKind()) {
4811 default: return false;
4812 case tok::kw_short:
4813 case tok::kw_long:
4814 case tok::kw_signed:
4815 case tok::kw_unsigned:
4816 case tok::kw_void:
4817 case tok::kw_char:
4818 case tok::kw_int:
4819 case tok::kw_float:
4820 case tok::kw_double:
4821 case tok::kw_bool:
4822 case tok::kw___pixel:
4823 Tok.setKind(tok::kw___vector);
4824 return true;
4825 case tok::identifier:
4826 if (Next.getIdentifierInfo() == Ident_pixel) {
4827 Tok.setKind(tok::kw___vector);
4828 return true;
4829 }
4830 return false;
4831 }
4832}
4833
4834bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4835 const char *&PrevSpec, unsigned &DiagID,
4836 bool &isInvalid) {
4837 if (Tok.getIdentifierInfo() == Ident_vector) {
4838 Token Next = NextToken();
4839 switch (Next.getKind()) {
4840 case tok::kw_short:
4841 case tok::kw_long:
4842 case tok::kw_signed:
4843 case tok::kw_unsigned:
4844 case tok::kw_void:
4845 case tok::kw_char:
4846 case tok::kw_int:
4847 case tok::kw_float:
4848 case tok::kw_double:
4849 case tok::kw_bool:
4850 case tok::kw___pixel:
4851 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4852 return true;
4853 case tok::identifier:
4854 if (Next.getIdentifierInfo() == Ident_pixel) {
4855 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4856 return true;
4857 }
4858 break;
4859 default:
4860 break;
4861 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004862 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004863 DS.isTypeAltiVecVector()) {
4864 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4865 return true;
4866 }
4867 return false;
4868}