blob: 884820e0238fa00b2df9249ff9779008f73821be [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
732 // Set or update the scope flags to include Scope::ThisScope.
733 bool AlreadyHasClassScope = Class.TopLevelClass;
734 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
735 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);
742
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000743 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
744 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
745 }
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000746
747 if (!AlreadyHasClassScope)
748 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
749 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000750}
751
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000752
753/// \brief Parse all attributes in LAs, and attach them to Decl D.
754void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
755 bool EnterScope, bool OnDefinition) {
756 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000757 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000759 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000760 }
761 LAs.clear();
762}
763
764
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000765/// \brief Finish parsing an attribute for which parsing was delayed.
766/// This will be called at the end of parsing a class declaration
767/// for each LateParsedAttribute. We consume the saved tokens and
768/// create an attribute with the arguments filled in. We add this
769/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000770void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
771 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000772 // Save the current token position.
773 SourceLocation OrigLoc = Tok.getLocation();
774
775 // Append the current token at the end of the new token stream so that it
776 // doesn't get lost.
777 LA.Toks.push_back(Tok);
778 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
779 // Consume the previously pushed token.
780 ConsumeAnyToken();
781
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000782 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
783 Diag(Tok, diag::warn_attribute_on_function_definition)
784 << LA.AttrName.getName();
785 }
786
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000787 ParsedAttributes Attrs(AttrFactory);
788 SourceLocation endLoc;
789
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000790 if (LA.Decls.size() == 1) {
791 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000792
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000793 // If the Decl is templatized, add template parameters to scope.
794 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
795 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
796 if (HasTemplateScope)
797 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000798
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000799 // If the Decl is on a function, add function parameters to the scope.
800 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
801 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
802 if (HasFunctionScope)
803 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
804
805 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
806
807 if (HasFunctionScope) {
808 Actions.ActOnExitFunctionContext();
809 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
810 }
811 if (HasTemplateScope) {
812 TempScope.Exit();
813 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000814 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000815 // If there are multiple decls, then the decl cannot be within the
816 // function scope.
817 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000818 } else {
819 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000820 }
821
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000822 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
823 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
824 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000825
826 if (Tok.getLocation() != OrigLoc) {
827 // Due to a parsing error, we either went over the cached tokens or
828 // there are still cached tokens left, so we skip the leftover tokens.
829 // Since this is an uncommon situation that should be avoided, use the
830 // expensive isBeforeInTranslationUnit call.
831 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
832 OrigLoc))
833 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000834 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000835 }
836}
837
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838/// \brief Wrapper around a case statement checking if AttrName is
839/// one of the thread safety attributes
840bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
841 return llvm::StringSwitch<bool>(AttrName)
842 .Case("guarded_by", true)
843 .Case("guarded_var", true)
844 .Case("pt_guarded_by", true)
845 .Case("pt_guarded_var", true)
846 .Case("lockable", true)
847 .Case("scoped_lockable", true)
848 .Case("no_thread_safety_analysis", true)
849 .Case("acquired_after", true)
850 .Case("acquired_before", true)
851 .Case("exclusive_lock_function", true)
852 .Case("shared_lock_function", true)
853 .Case("exclusive_trylock_function", true)
854 .Case("shared_trylock_function", true)
855 .Case("unlock_function", true)
856 .Case("lock_returned", true)
857 .Case("locks_excluded", true)
858 .Case("exclusive_locks_required", true)
859 .Case("shared_locks_required", true)
860 .Default(false);
861}
862
863/// \brief Parse the contents of thread safety attributes. These
864/// should always be parsed as an expression list.
865///
866/// We need to special case the parsing due to the fact that if the first token
867/// of the first argument is an identifier, the main parse loop will store
868/// that token as a "parameter" and the rest of
869/// the arguments will be added to a list of "arguments". However,
870/// subsequent tokens in the first argument are lost. We instead parse each
871/// argument as an expression and add all arguments to the list of "arguments".
872/// In future, we will take advantage of this special case to also
873/// deal with some argument scoping issues here (for example, referring to a
874/// function parameter in the attribute on that function).
875void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
876 SourceLocation AttrNameLoc,
877 ParsedAttributes &Attrs,
878 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000879 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000880
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000881 BalancedDelimiterTracker T(*this, tok::l_paren);
882 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000883
884 ExprVector ArgExprs(Actions);
885 bool ArgExprsOk = true;
886
887 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000888 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000889 ExprResult ArgExpr(ParseAssignmentExpression());
890 if (ArgExpr.isInvalid()) {
891 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000892 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893 break;
894 } else {
895 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000896 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897 if (Tok.isNot(tok::comma))
898 break;
899 ConsumeToken(); // Eat the comma, move to the next argument
900 }
901 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000902 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000903 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
904 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000906 if (EndLoc)
907 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000908}
909
Richard Smith6ee326a2012-04-10 01:32:12 +0000910/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
911/// of a C++11 attribute-specifier in a location where an attribute is not
912/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
913/// situation.
914///
915/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
916/// this doesn't appear to actually be an attribute-specifier, and the caller
917/// should try to parse it.
918bool Parser::DiagnoseProhibitedCXX11Attribute() {
919 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
920
921 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
922 case CAK_NotAttributeSpecifier:
923 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
924 return false;
925
926 case CAK_InvalidAttributeSpecifier:
927 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
928 return false;
929
930 case CAK_AttributeSpecifier:
931 // Parse and discard the attributes.
932 SourceLocation BeginLoc = ConsumeBracket();
933 ConsumeBracket();
934 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
935 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
936 SourceLocation EndLoc = ConsumeBracket();
937 Diag(BeginLoc, diag::err_attributes_not_allowed)
938 << SourceRange(BeginLoc, EndLoc);
939 return true;
940 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000941 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000942}
943
John McCall7f040a92010-12-24 02:08:15 +0000944void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
945 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
946 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000947}
948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949/// ParseDeclaration - Parse a full 'declaration', which consists of
950/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000951/// 'Context' should be a Declarator::TheContext value. This returns the
952/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000953///
954/// declaration: [C99 6.7]
955/// block-declaration ->
956/// simple-declaration
957/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000958/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000959/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000960/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000961/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000962/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000963/// others... [FIXME]
964///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000965Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
966 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000967 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000968 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000969 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000970 // Must temporarily exit the objective-c container scope for
971 // parsing c none objective-c decls.
972 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000973
John McCalld226f652010-08-21 09:40:31 +0000974 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000975 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000976 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000977 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000978 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000979 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000980 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000981 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000982 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000983 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000984 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000985 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000986 SourceLocation InlineLoc = ConsumeToken();
987 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
988 break;
989 }
John McCall7f040a92010-12-24 02:08:15 +0000990 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000991 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000992 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000993 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000994 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000995 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000996 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000997 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000998 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000999 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001000 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001001 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001002 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001003 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001004 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001005 default:
John McCall7f040a92010-12-24 02:08:15 +00001006 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001007 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001008
Chris Lattner682bf922009-03-29 16:50:03 +00001009 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001010 // single decl, convert it now. Alias declarations can also declare a type;
1011 // include that too if it is present.
1012 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001013}
1014
1015/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1016/// declaration-specifiers init-declarator-list[opt] ';'
1017///[C90/C++]init-declarator-list ';' [TODO]
1018/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001019///
Richard Smithad762fc2011-04-14 22:09:26 +00001020/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1021/// attribute-specifier-seq[opt] type-specifier-seq declarator
1022///
Chris Lattnercd147752009-03-29 17:27:48 +00001023/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001024/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001025///
1026/// If FRI is non-null, we might be parsing a for-range-declaration instead
1027/// of a simple-declaration. If we find that we are, we also parse the
1028/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001029Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1030 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001031 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001032 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001033 bool RequireSemi,
1034 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001036 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001037 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001038
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001039 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001040 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1043 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001044 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001045 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001046 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001047 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001048 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001049 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001051
1052 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001053}
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Richard Smith0706df42011-10-19 21:33:05 +00001055/// Returns true if this might be the start of a declarator, or a common typo
1056/// for a declarator.
1057bool Parser::MightBeDeclarator(unsigned Context) {
1058 switch (Tok.getKind()) {
1059 case tok::annot_cxxscope:
1060 case tok::annot_template_id:
1061 case tok::caret:
1062 case tok::code_completion:
1063 case tok::coloncolon:
1064 case tok::ellipsis:
1065 case tok::kw___attribute:
1066 case tok::kw_operator:
1067 case tok::l_paren:
1068 case tok::star:
1069 return true;
1070
1071 case tok::amp:
1072 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001073 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001074
Richard Smith1c94c162012-01-09 22:31:44 +00001075 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001076 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001077 NextToken().is(tok::l_square);
1078
1079 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001080 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001081
Richard Smith0706df42011-10-19 21:33:05 +00001082 case tok::identifier:
1083 switch (NextToken().getKind()) {
1084 case tok::code_completion:
1085 case tok::coloncolon:
1086 case tok::comma:
1087 case tok::equal:
1088 case tok::equalequal: // Might be a typo for '='.
1089 case tok::kw_alignas:
1090 case tok::kw_asm:
1091 case tok::kw___attribute:
1092 case tok::l_brace:
1093 case tok::l_paren:
1094 case tok::l_square:
1095 case tok::less:
1096 case tok::r_brace:
1097 case tok::r_paren:
1098 case tok::r_square:
1099 case tok::semi:
1100 return true;
1101
1102 case tok::colon:
1103 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001104 // and in block scope it's probably a label. Inside a class definition,
1105 // this is a bit-field.
1106 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001107 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001108
1109 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001110 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001111
1112 default:
1113 return false;
1114 }
1115
1116 default:
1117 return false;
1118 }
1119}
1120
Richard Smith994d73f2012-04-11 20:59:20 +00001121/// Skip until we reach something which seems like a sensible place to pick
1122/// up parsing after a malformed declaration. This will sometimes stop sooner
1123/// than SkipUntil(tok::r_brace) would, but will never stop later.
1124void Parser::SkipMalformedDecl() {
1125 while (true) {
1126 switch (Tok.getKind()) {
1127 case tok::l_brace:
1128 // Skip until matching }, then stop. We've probably skipped over
1129 // a malformed class or function definition or similar.
1130 ConsumeBrace();
1131 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1132 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1133 // This declaration isn't over yet. Keep skipping.
1134 continue;
1135 }
1136 if (Tok.is(tok::semi))
1137 ConsumeToken();
1138 return;
1139
1140 case tok::l_square:
1141 ConsumeBracket();
1142 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1143 continue;
1144
1145 case tok::l_paren:
1146 ConsumeParen();
1147 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1148 continue;
1149
1150 case tok::r_brace:
1151 return;
1152
1153 case tok::semi:
1154 ConsumeToken();
1155 return;
1156
1157 case tok::kw_inline:
1158 // 'inline namespace' at the start of a line is almost certainly
1159 // a good place to pick back up parsing.
1160 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1161 return;
1162 break;
1163
1164 case tok::kw_namespace:
1165 // 'namespace' at the start of a line is almost certainly a good
1166 // place to pick back up parsing.
1167 if (Tok.isAtStartOfLine())
1168 return;
1169 break;
1170
1171 case tok::eof:
1172 return;
1173
1174 default:
1175 break;
1176 }
1177
1178 ConsumeAnyToken();
1179 }
1180}
1181
John McCalld8ac0572009-11-03 19:26:08 +00001182/// ParseDeclGroup - Having concluded that this is either a function
1183/// definition or a group of object declarations, actually parse the
1184/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001185Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1186 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001187 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001188 SourceLocation *DeclEnd,
1189 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001190 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001191 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001192 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001193
John McCalld8ac0572009-11-03 19:26:08 +00001194 // Bail out if the first declarator didn't seem well-formed.
1195 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001196 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001197 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001200 // Save late-parsed attributes for now; they need to be parsed in the
1201 // appropriate function scope after the function Decl has been constructed.
1202 LateParsedAttrList LateParsedAttrs;
1203 if (D.isFunctionDeclarator())
1204 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1205
Chris Lattnerc82daef2010-07-11 22:24:20 +00001206 // Check to see if we have a function *definition* which must have a body.
1207 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1208 // Look at the next token to make sure that this isn't a function
1209 // declaration. We have to check this because __attribute__ might be the
1210 // start of a function definition in GCC-extended K&R C.
1211 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001212
Chris Lattner004659a2010-07-11 22:42:07 +00001213 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001214 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1215 Diag(Tok, diag::err_function_declared_typedef);
1216
1217 // Recover by treating the 'typedef' as spurious.
1218 DS.ClearStorageClassSpecs();
1219 }
1220
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001221 Decl *TheDecl =
1222 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001223 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001224 }
1225
1226 if (isDeclarationSpecifier()) {
1227 // If there is an invalid declaration specifier right after the function
1228 // prototype, then we must be in a missing semicolon case where this isn't
1229 // actually a body. Just fall through into the code that handles it as a
1230 // prototype, and let the top-level code handle the erroneous declspec
1231 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001232 } else {
1233 Diag(Tok, diag::err_expected_fn_body);
1234 SkipUntil(tok::semi);
1235 return DeclGroupPtrTy();
1236 }
1237 }
1238
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001239 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001240 return DeclGroupPtrTy();
1241
1242 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1243 // must parse and analyze the for-range-initializer before the declaration is
1244 // analyzed.
1245 if (FRI && Tok.is(tok::colon)) {
1246 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001247 if (Tok.is(tok::l_brace))
1248 FRI->RangeExpr = ParseBraceInitializer();
1249 else
1250 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001251 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1252 Actions.ActOnCXXForRangeDecl(ThisDecl);
1253 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001254 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001255 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1256 }
1257
Chris Lattner5f9e2722011-07-23 10:55:15 +00001258 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001259 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001260 if (LateParsedAttrs.size() > 0)
1261 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001262 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001263 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001264 DeclsInGroup.push_back(FirstDecl);
1265
Richard Smith0706df42011-10-19 21:33:05 +00001266 bool ExpectSemi = Context != Declarator::ForContext;
1267
John McCalld8ac0572009-11-03 19:26:08 +00001268 // If we don't have a comma, it is either the end of the list (a ';') or an
1269 // error, bail out.
1270 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001271 SourceLocation CommaLoc = ConsumeToken();
1272
1273 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1274 // This comma was followed by a line-break and something which can't be
1275 // the start of a declarator. The comma was probably a typo for a
1276 // semicolon.
1277 Diag(CommaLoc, diag::err_expected_semi_declaration)
1278 << FixItHint::CreateReplacement(CommaLoc, ";");
1279 ExpectSemi = false;
1280 break;
1281 }
John McCalld8ac0572009-11-03 19:26:08 +00001282
1283 // Parse the next declarator.
1284 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001285 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001286
1287 // Accept attributes in an init-declarator. In the first declarator in a
1288 // declaration, these would be part of the declspec. In subsequent
1289 // declarators, they become part of the declarator itself, so that they
1290 // don't apply to declarators after *this* one. Examples:
1291 // short __attribute__((common)) var; -> declspec
1292 // short var __attribute__((common)); -> declarator
1293 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001294 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001295
1296 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001297 if (!D.isInvalidType()) {
1298 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1299 D.complete(ThisDecl);
1300 if (ThisDecl)
1301 DeclsInGroup.push_back(ThisDecl);
1302 }
John McCalld8ac0572009-11-03 19:26:08 +00001303 }
1304
1305 if (DeclEnd)
1306 *DeclEnd = Tok.getLocation();
1307
Richard Smith0706df42011-10-19 21:33:05 +00001308 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001309 ExpectAndConsume(tok::semi,
1310 Context == Declarator::FileContext
1311 ? diag::err_invalid_token_after_toplevel_declarator
1312 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001313 // Okay, there was no semicolon and one was expected. If we see a
1314 // declaration specifier, just assume it was missing and continue parsing.
1315 // Otherwise things are very confused and we skip to recover.
1316 if (!isDeclarationSpecifier()) {
1317 SkipUntil(tok::r_brace, true, true);
1318 if (Tok.is(tok::semi))
1319 ConsumeToken();
1320 }
John McCalld8ac0572009-11-03 19:26:08 +00001321 }
1322
Douglas Gregor23c94db2010-07-02 17:43:08 +00001323 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001324 DeclsInGroup.data(),
1325 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001326}
1327
Richard Smithad762fc2011-04-14 22:09:26 +00001328/// Parse an optional simple-asm-expr and attributes, and attach them to a
1329/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001330bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001331 // If a simple-asm-expr is present, parse it.
1332 if (Tok.is(tok::kw_asm)) {
1333 SourceLocation Loc;
1334 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1335 if (AsmLabel.isInvalid()) {
1336 SkipUntil(tok::semi, true, true);
1337 return true;
1338 }
1339
1340 D.setAsmLabel(AsmLabel.release());
1341 D.SetRangeEnd(Loc);
1342 }
1343
1344 MaybeParseGNUAttributes(D);
1345 return false;
1346}
1347
Douglas Gregor1426e532009-05-12 21:31:51 +00001348/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1349/// declarator'. This method parses the remainder of the declaration
1350/// (including any attributes or initializer, among other things) and
1351/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001352///
Reid Spencer5f016e22007-07-11 17:01:13 +00001353/// init-declarator: [C99 6.7]
1354/// declarator
1355/// declarator '=' initializer
1356/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1357/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001358/// [C++] declarator initializer[opt]
1359///
1360/// [C++] initializer:
1361/// [C++] '=' initializer-clause
1362/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001363/// [C++0x] '=' 'default' [TODO]
1364/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001365/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001366///
1367/// According to the standard grammar, =default and =delete are function
1368/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001369///
John McCalld226f652010-08-21 09:40:31 +00001370Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001371 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001372 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001373 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Richard Smithad762fc2011-04-14 22:09:26 +00001375 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1376}
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Richard Smithad762fc2011-04-14 22:09:26 +00001378Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1379 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001380 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001381 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001382 switch (TemplateInfo.Kind) {
1383 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001384 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001385 break;
1386
1387 case ParsedTemplateInfo::Template:
1388 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001389 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001390 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001391 TemplateInfo.TemplateParams->data(),
1392 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001393 D);
1394 break;
1395
1396 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001397 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001398 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001399 TemplateInfo.ExternLoc,
1400 TemplateInfo.TemplateLoc,
1401 D);
1402 if (ThisRes.isInvalid()) {
1403 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001404 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001405 }
1406
1407 ThisDecl = ThisRes.get();
1408 break;
1409 }
1410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Richard Smith34b41d92011-02-20 03:19:35 +00001412 bool TypeContainsAuto =
1413 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1414
Douglas Gregor1426e532009-05-12 21:31:51 +00001415 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001416 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001417 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001418 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001419 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001420 if (D.isFunctionDeclarator())
1421 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1422 << 1 /* delete */;
1423 else
1424 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001425 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001426 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001427 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1428 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001429 else
1430 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001431 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001432 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001433 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001434 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001435 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001436
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001437 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001438 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001439 cutOffParsing();
1440 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001441 }
1442
John McCall60d7b3a2010-08-24 06:29:42 +00001443 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001444
David Blaikie4e4d0842012-03-11 07:00:24 +00001445 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001446 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001447 ExitScope();
1448 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001449
Douglas Gregor1426e532009-05-12 21:31:51 +00001450 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001451 SkipUntil(tok::comma, true, true);
1452 Actions.ActOnInitializerError(ThisDecl);
1453 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001454 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1455 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001456 }
1457 } else if (Tok.is(tok::l_paren)) {
1458 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001459 BalancedDelimiterTracker T(*this, tok::l_paren);
1460 T.consumeOpen();
1461
Douglas Gregor1426e532009-05-12 21:31:51 +00001462 ExprVector Exprs(Actions);
1463 CommaLocsTy CommaLocs;
1464
David Blaikie4e4d0842012-03-11 07:00:24 +00001465 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001466 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001467 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001468 }
1469
Douglas Gregor1426e532009-05-12 21:31:51 +00001470 if (ParseExpressionList(Exprs, CommaLocs)) {
1471 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001472
David Blaikie4e4d0842012-03-11 07:00:24 +00001473 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001474 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001475 ExitScope();
1476 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001477 } else {
1478 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001479 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001480
1481 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1482 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001483
David Blaikie4e4d0842012-03-11 07:00:24 +00001484 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001485 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001486 ExitScope();
1487 }
1488
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001489 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1490 T.getCloseLocation(),
1491 move_arg(Exprs));
1492 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1493 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001494 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001495 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001496 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001497 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1498
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001499 if (D.getCXXScopeSpec().isSet()) {
1500 EnterScope(0);
1501 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1502 }
1503
1504 ExprResult Init(ParseBraceInitializer());
1505
1506 if (D.getCXXScopeSpec().isSet()) {
1507 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1508 ExitScope();
1509 }
1510
1511 if (Init.isInvalid()) {
1512 Actions.ActOnInitializerError(ThisDecl);
1513 } else
1514 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1515 /*DirectInit=*/true, TypeContainsAuto);
1516
Douglas Gregor1426e532009-05-12 21:31:51 +00001517 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001518 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001519 }
1520
Richard Smith483b9f32011-02-21 20:05:19 +00001521 Actions.FinalizeDeclaration(ThisDecl);
1522
Douglas Gregor1426e532009-05-12 21:31:51 +00001523 return ThisDecl;
1524}
1525
Reid Spencer5f016e22007-07-11 17:01:13 +00001526/// ParseSpecifierQualifierList
1527/// specifier-qualifier-list:
1528/// type-specifier specifier-qualifier-list[opt]
1529/// type-qualifier specifier-qualifier-list[opt]
1530/// [GNU] attributes specifier-qualifier-list[opt]
1531///
Richard Smith69730c12012-03-12 07:56:15 +00001532void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1533 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1535 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001536 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001537 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 // Validate declspec for type-name.
1540 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001541 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1542 Diag(Tok, diag::err_expected_type);
1543 DS.SetTypeSpecError();
1544 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1545 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001547 if (!DS.hasTypeSpecifier())
1548 DS.SetTypeSpecError();
1549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 // Issue diagnostic and remove storage class if present.
1552 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1553 if (DS.getStorageClassSpecLoc().isValid())
1554 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1555 else
1556 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1557 DS.ClearStorageClassSpecs();
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 // Issue diagnostic and remove function specfier if present.
1561 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001562 if (DS.isInlineSpecified())
1563 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1564 if (DS.isVirtualSpecified())
1565 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1566 if (DS.isExplicitSpecified())
1567 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001568 DS.ClearFunctionSpecs();
1569 }
Richard Smith69730c12012-03-12 07:56:15 +00001570
1571 // Issue diagnostic and remove constexpr specfier if present.
1572 if (DS.isConstexprSpecified()) {
1573 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1574 DS.ClearConstexprSpec();
1575 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001576}
1577
Chris Lattnerc199ab32009-04-12 20:42:31 +00001578/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1579/// specified token is valid after the identifier in a declarator which
1580/// immediately follows the declspec. For example, these things are valid:
1581///
1582/// int x [ 4]; // direct-declarator
1583/// int x ( int y); // direct-declarator
1584/// int(int x ) // direct-declarator
1585/// int x ; // simple-declaration
1586/// int x = 17; // init-declarator-list
1587/// int x , y; // init-declarator-list
1588/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001589/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001590/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001591///
1592/// This is not, because 'x' does not immediately follow the declspec (though
1593/// ')' happens to be valid anyway).
1594/// int (x)
1595///
1596static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1597 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1598 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001599 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001600}
1601
Chris Lattnere40c2952009-04-14 21:34:55 +00001602
1603/// ParseImplicitInt - This method is called when we have an non-typename
1604/// identifier in a declspec (which normally terminates the decl spec) when
1605/// the declspec has no type specifier. In this case, the declspec is either
1606/// malformed or is "implicit int" (in K&R and C89).
1607///
1608/// This method handles diagnosing this prettily and returns false if the
1609/// declspec is done being processed. If it recovers and thinks there may be
1610/// other pieces of declspec after it, it returns true.
1611///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001612bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001613 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001614 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001615 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Chris Lattnere40c2952009-04-14 21:34:55 +00001617 SourceLocation Loc = Tok.getLocation();
1618 // If we see an identifier that is not a type name, we normally would
1619 // parse it as the identifer being declared. However, when a typename
1620 // is typo'd or the definition is not included, this will incorrectly
1621 // parse the typename as the identifier name and fall over misparsing
1622 // later parts of the diagnostic.
1623 //
1624 // As such, we try to do some look-ahead in cases where this would
1625 // otherwise be an "implicit-int" case to see if this is invalid. For
1626 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1627 // an identifier with implicit int, we'd get a parse error because the
1628 // next token is obviously invalid for a type. Parse these as a case
1629 // with an invalid type specifier.
1630 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Chris Lattnere40c2952009-04-14 21:34:55 +00001632 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001633 // error, do lookahead to try to do better recovery. This never applies within
1634 // a type specifier.
1635 // FIXME: Don't bail out here in languages with no implicit int (like
1636 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1637 // type or typo than a use of implicit int.
1638 if (DSC != DSC_type_specifier &&
1639 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001640 // If this token is valid for implicit int, e.g. "static x = 4", then
1641 // we just avoid eating the identifier, so it will be parsed as the
1642 // identifier in the declarator.
1643 return false;
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Chris Lattnere40c2952009-04-14 21:34:55 +00001646 // Otherwise, if we don't consume this token, we are going to emit an
1647 // error anyway. Try to recover from various common problems. Check
1648 // to see if this was a reference to a tag name without a tag specified.
1649 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001650 //
1651 // C++ doesn't need this, and isTagName doesn't take SS.
1652 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001653 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001654 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Douglas Gregor23c94db2010-07-02 17:43:08 +00001656 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001657 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001658 case DeclSpec::TST_enum:
1659 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1660 case DeclSpec::TST_union:
1661 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1662 case DeclSpec::TST_struct:
1663 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1664 case DeclSpec::TST_class:
1665 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001666 }
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Chris Lattnerf4382f52009-04-14 22:17:06 +00001668 if (TagName) {
1669 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001670 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001671 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Chris Lattnerf4382f52009-04-14 22:17:06 +00001673 // Parse this as a tag as if the missing tag were present.
1674 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001675 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001676 else
Richard Smith69730c12012-03-12 07:56:15 +00001677 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1678 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001679 return true;
1680 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001681 }
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Douglas Gregora786fdb2009-10-13 23:27:22 +00001683 // This is almost certainly an invalid type name. Let the action emit a
1684 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001685 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001686 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001687 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001688 // The action emitted a diagnostic, so we don't have to.
1689 if (T) {
1690 // The action has suggested that the type T could be used. Set that as
1691 // the type in the declaration specifiers, consume the would-be type
1692 // name token, and we're done.
1693 const char *PrevSpec;
1694 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001695 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001696 DS.SetRangeEnd(Tok.getLocation());
1697 ConsumeToken();
1698
1699 // There may be other declaration specifiers after this.
1700 return true;
1701 }
1702
1703 // Fall through; the action had no suggestion for us.
1704 } else {
1705 // The action did not emit a diagnostic, so emit one now.
1706 SourceRange R;
1707 if (SS) R = SS->getRange();
1708 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1709 }
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Douglas Gregora786fdb2009-10-13 23:27:22 +00001711 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001712 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001713 DS.SetRangeEnd(Tok.getLocation());
1714 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Chris Lattnere40c2952009-04-14 21:34:55 +00001716 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1717 // avoid rippling error messages on subsequent uses of the same type,
1718 // could be useful if #include was forgotten.
1719 return false;
1720}
1721
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001722/// \brief Determine the declaration specifier context from the declarator
1723/// context.
1724///
1725/// \param Context the declarator context, which is one of the
1726/// Declarator::TheContext enumerator values.
1727Parser::DeclSpecContext
1728Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1729 if (Context == Declarator::MemberContext)
1730 return DSC_class;
1731 if (Context == Declarator::FileContext)
1732 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001733 if (Context == Declarator::TrailingReturnContext)
1734 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001735 return DSC_normal;
1736}
1737
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001738/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1739///
1740/// FIXME: Simply returns an alignof() expression if the argument is a
1741/// type. Ideally, the type should be propagated directly into Sema.
1742///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001743/// [C11] type-id
1744/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001745/// [C++0x] type-id ...[opt]
1746/// [C++0x] assignment-expression ...[opt]
1747ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1748 SourceLocation &EllipsisLoc) {
1749 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001750 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001751 SourceLocation TypeLoc = Tok.getLocation();
1752 ParsedType Ty = ParseTypeName().get();
1753 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001754 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1755 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001756 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001757 ER = ParseConstantExpression();
1758
David Blaikie4e4d0842012-03-11 07:00:24 +00001759 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001760 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001761
1762 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001763}
1764
1765/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1766/// attribute to Attrs.
1767///
1768/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001769/// [C11] '_Alignas' '(' type-id ')'
1770/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001771/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1772/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001773void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1774 SourceLocation *endLoc) {
1775 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1776 "Not an alignment-specifier!");
1777
1778 SourceLocation KWLoc = Tok.getLocation();
1779 ConsumeToken();
1780
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001781 BalancedDelimiterTracker T(*this, tok::l_paren);
1782 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001783 return;
1784
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001785 SourceLocation EllipsisLoc;
1786 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001787 if (ArgExpr.isInvalid()) {
1788 SkipUntil(tok::r_paren);
1789 return;
1790 }
1791
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001792 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001793 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001794 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001795
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001796 // FIXME: Handle pack-expansions here.
1797 if (EllipsisLoc.isValid()) {
1798 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1799 return;
1800 }
1801
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001802 ExprVector ArgExprs(Actions);
1803 ArgExprs.push_back(ArgExpr.release());
1804 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001805 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001806}
1807
Reid Spencer5f016e22007-07-11 17:01:13 +00001808/// ParseDeclarationSpecifiers
1809/// declaration-specifiers: [C99 6.7]
1810/// storage-class-specifier declaration-specifiers[opt]
1811/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001812/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001813/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001814/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001815/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001816///
1817/// storage-class-specifier: [C99 6.7.1]
1818/// 'typedef'
1819/// 'extern'
1820/// 'static'
1821/// 'auto'
1822/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001823/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001824/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001825/// function-specifier: [C99 6.7.4]
1826/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001827/// [C++] 'virtual'
1828/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001829/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001830/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001831/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001834void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001835 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001836 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001837 DeclSpecContext DSContext,
1838 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001839 if (DS.getSourceRange().isInvalid()) {
1840 DS.SetRangeStart(Tok.getLocation());
1841 DS.SetRangeEnd(Tok.getLocation());
1842 }
1843
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001844 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001846 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001848 unsigned DiagID = 0;
1849
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001851
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001853 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001854 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001855 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1856 MaybeParseCXX0XAttributes(DS.getAttributes());
1857
Reid Spencer5f016e22007-07-11 17:01:13 +00001858 // If this is not a declaration specifier token, we're done reading decl
1859 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001860 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001861 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001863 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001864 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001865 if (DS.hasTypeSpecifier()) {
1866 bool AllowNonIdentifiers
1867 = (getCurScope()->getFlags() & (Scope::ControlScope |
1868 Scope::BlockScope |
1869 Scope::TemplateParamScope |
1870 Scope::FunctionPrototypeScope |
1871 Scope::AtCatchScope)) == 0;
1872 bool AllowNestedNameSpecifiers
1873 = DSContext == DSC_top_level ||
1874 (DSContext == DSC_class && DS.isFriendSpecified());
1875
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001876 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1877 AllowNonIdentifiers,
1878 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001879 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001880 }
1881
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001882 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1883 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1884 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001885 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1886 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001887 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001888 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001889 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001890 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001891
1892 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001893 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001894 }
1895
Chris Lattner5e02c472009-01-05 00:07:25 +00001896 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001897 // C++ scope specifier. Annotate and loop, or bail out on error.
1898 if (TryAnnotateCXXScopeToken(true)) {
1899 if (!DS.hasTypeSpecifier())
1900 DS.SetTypeSpecError();
1901 goto DoneWithDeclSpec;
1902 }
John McCall2e0a7152010-03-01 18:20:46 +00001903 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1904 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001905 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001906
1907 case tok::annot_cxxscope: {
1908 if (DS.hasTypeSpecifier())
1909 goto DoneWithDeclSpec;
1910
John McCallaa87d332009-12-12 11:40:51 +00001911 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001912 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1913 Tok.getAnnotationRange(),
1914 SS);
John McCallaa87d332009-12-12 11:40:51 +00001915
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001916 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001917 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001918 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001919 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001920 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001921 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001922
1923 // C++ [class.qual]p2:
1924 // In a lookup in which the constructor is an acceptable lookup
1925 // result and the nested-name-specifier nominates a class C:
1926 //
1927 // - if the name specified after the
1928 // nested-name-specifier, when looked up in C, is the
1929 // injected-class-name of C (Clause 9), or
1930 //
1931 // - if the name specified after the nested-name-specifier
1932 // is the same as the identifier or the
1933 // simple-template-id's template-name in the last
1934 // component of the nested-name-specifier,
1935 //
1936 // the name is instead considered to name the constructor of
1937 // class C.
1938 //
1939 // Thus, if the template-name is actually the constructor
1940 // name, then the code is ill-formed; this interpretation is
1941 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001942 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001943 if ((DSContext == DSC_top_level ||
1944 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1945 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001946 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001947 if (isConstructorDeclarator()) {
1948 // The user meant this to be an out-of-line constructor
1949 // definition, but template arguments are not allowed
1950 // there. Just allow this as a constructor; we'll
1951 // complain about it later.
1952 goto DoneWithDeclSpec;
1953 }
1954
1955 // The user meant this to name a type, but it actually names
1956 // a constructor with some extraneous template
1957 // arguments. Complain, then parse it as a type as the user
1958 // intended.
1959 Diag(TemplateId->TemplateNameLoc,
1960 diag::err_out_of_line_template_id_names_constructor)
1961 << TemplateId->Name;
1962 }
1963
John McCallaa87d332009-12-12 11:40:51 +00001964 DS.getTypeSpecScope() = SS;
1965 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001966 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001967 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001968 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001969 continue;
1970 }
1971
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001972 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001973 DS.getTypeSpecScope() = SS;
1974 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001975 if (Tok.getAnnotationValue()) {
1976 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1978 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001979 PrevSpec, DiagID, T);
1980 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001981 else
1982 DS.SetTypeSpecError();
1983 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1984 ConsumeToken(); // The typename
1985 }
1986
Douglas Gregor9135c722009-03-25 15:40:00 +00001987 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001988 goto DoneWithDeclSpec;
1989
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001990 // If we're in a context where the identifier could be a class name,
1991 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001992 if ((DSContext == DSC_top_level ||
1993 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001994 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001995 &SS)) {
1996 if (isConstructorDeclarator())
1997 goto DoneWithDeclSpec;
1998
1999 // As noted in C++ [class.qual]p2 (cited above), when the name
2000 // of the class is qualified in a context where it could name
2001 // a constructor, its a constructor name. However, we've
2002 // looked at the declarator, and the user probably meant this
2003 // to be a type. Complain that it isn't supposed to be treated
2004 // as a type, then proceed to parse it as a type.
2005 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2006 << Next.getIdentifierInfo();
2007 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002008
John McCallb3d87482010-08-24 05:47:05 +00002009 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2010 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002011 getCurScope(), &SS,
2012 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002013 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002014 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002015
Chris Lattnerf4382f52009-04-14 22:17:06 +00002016 // If the referenced identifier is not a type, then this declspec is
2017 // erroneous: We already checked about that it has no type specifier, and
2018 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002019 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002020 if (TypeRep == 0) {
2021 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002022 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002023 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002024 }
Mike Stump1eb44332009-09-09 15:08:12 +00002025
John McCallaa87d332009-12-12 11:40:51 +00002026 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002027 ConsumeToken(); // The C++ scope.
2028
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002030 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002031 if (isInvalid)
2032 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002034 DS.SetRangeEnd(Tok.getLocation());
2035 ConsumeToken(); // The typename.
2036
2037 continue;
2038 }
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Chris Lattner80d0c892009-01-21 19:48:37 +00002040 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002041 if (Tok.getAnnotationValue()) {
2042 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002044 DiagID, T);
2045 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002046 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002047
2048 if (isInvalid)
2049 break;
2050
Chris Lattner80d0c892009-01-21 19:48:37 +00002051 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2052 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Chris Lattner80d0c892009-01-21 19:48:37 +00002054 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2055 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002056 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002057 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002058 ParseObjCProtocolQualifiers(DS);
2059
Chris Lattner80d0c892009-01-21 19:48:37 +00002060 continue;
2061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregorbfad9152011-04-28 15:48:45 +00002063 case tok::kw___is_signed:
2064 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2065 // typically treats it as a trait. If we see __is_signed as it appears
2066 // in libstdc++, e.g.,
2067 //
2068 // static const bool __is_signed;
2069 //
2070 // then treat __is_signed as an identifier rather than as a keyword.
2071 if (DS.getTypeSpecType() == TST_bool &&
2072 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2073 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2074 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2075 Tok.setKind(tok::identifier);
2076 }
2077
2078 // We're done with the declaration-specifiers.
2079 goto DoneWithDeclSpec;
2080
Chris Lattner3bd934a2008-07-26 01:18:38 +00002081 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002082 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002083 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002084 // In C++, check to see if this is a scope specifier like foo::bar::, if
2085 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002086 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002087 if (TryAnnotateCXXScopeToken(true)) {
2088 if (!DS.hasTypeSpecifier())
2089 DS.SetTypeSpecError();
2090 goto DoneWithDeclSpec;
2091 }
2092 if (!Tok.is(tok::identifier))
2093 continue;
2094 }
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Chris Lattner3bd934a2008-07-26 01:18:38 +00002096 // This identifier can only be a typedef name if we haven't already seen
2097 // a type-specifier. Without this check we misparse:
2098 // typedef int X; struct Y { short X; }; as 'short int'.
2099 if (DS.hasTypeSpecifier())
2100 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002101
John Thompson82287d12010-02-05 00:12:22 +00002102 // Check for need to substitute AltiVec keyword tokens.
2103 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2104 break;
2105
John McCallb3d87482010-08-24 05:47:05 +00002106 ParsedType TypeRep =
2107 Actions.getTypeName(*Tok.getIdentifierInfo(),
2108 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002109
Chris Lattnerc199ab32009-04-12 20:42:31 +00002110 // If this is not a typedef name, don't parse it as part of the declspec,
2111 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002112 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002113 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002114 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002115 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002116
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002117 // If we're in a context where the identifier could be a class name,
2118 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002119 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002120 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002121 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002122 goto DoneWithDeclSpec;
2123
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002125 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002126 if (isInvalid)
2127 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Chris Lattner3bd934a2008-07-26 01:18:38 +00002129 DS.SetRangeEnd(Tok.getLocation());
2130 ConsumeToken(); // The identifier
2131
2132 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2133 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002134 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002135 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002136 ParseObjCProtocolQualifiers(DS);
2137
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002138 // Need to support trailing type qualifiers (e.g. "id<p> const").
2139 // If a type specifier follows, it will be diagnosed elsewhere.
2140 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002141 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002142
2143 // type-name
2144 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002145 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002146 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002147 // This template-id does not refer to a type name, so we're
2148 // done with the type-specifiers.
2149 goto DoneWithDeclSpec;
2150 }
2151
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002152 // If we're in a context where the template-id could be a
2153 // constructor name or specialization, check whether this is a
2154 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002155 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002156 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002157 isConstructorDeclarator())
2158 goto DoneWithDeclSpec;
2159
Douglas Gregor39a8de12009-02-25 19:37:18 +00002160 // Turn the template-id annotation token into a type annotation
2161 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002162 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002163 continue;
2164 }
2165
Reid Spencer5f016e22007-07-11 17:01:13 +00002166 // GNU attributes support.
2167 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002168 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002169 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002170
2171 // Microsoft declspec support.
2172 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002173 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002174 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Steve Naroff239f0732008-12-25 14:16:32 +00002176 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002177 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002178 // FIXME: Add handling here!
2179 break;
2180
2181 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002182 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002183 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002184 case tok::kw___cdecl:
2185 case tok::kw___stdcall:
2186 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002187 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002188 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002189 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002190 continue;
2191
Dawn Perchik52fc3142010-09-03 01:29:35 +00002192 // Borland single token adornments.
2193 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002194 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002195 continue;
2196
Peter Collingbournef315fa82011-02-14 01:42:53 +00002197 // OpenCL single token adornments.
2198 case tok::kw___kernel:
2199 ParseOpenCLAttributes(DS.getAttributes());
2200 continue;
2201
Reid Spencer5f016e22007-07-11 17:01:13 +00002202 // storage-class-specifier
2203 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002204 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2205 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 break;
2207 case tok::kw_extern:
2208 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002209 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002210 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2211 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002213 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002214 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2215 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002216 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 case tok::kw_static:
2218 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002219 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002220 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2221 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 break;
2223 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002224 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002225 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002226 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2227 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002228 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002229 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002230 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002231 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002232 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2233 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002234 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002235 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2236 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002237 break;
2238 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002239 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2240 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002241 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002242 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002243 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2244 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002245 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002247 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002248 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Reid Spencer5f016e22007-07-11 17:01:13 +00002250 // function-specifier
2251 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002252 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002253 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002254 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002255 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002256 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002257 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002258 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002259 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002260
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002261 // alignment-specifier
2262 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002263 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002264 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002265 ParseAlignmentSpecifier(DS.getAttributes());
2266 continue;
2267
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002268 // friend
2269 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002270 if (DSContext == DSC_class)
2271 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2272 else {
2273 PrevSpec = ""; // not actually used by the diagnostic
2274 DiagID = diag::err_friend_invalid_in_context;
2275 isInvalid = true;
2276 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002277 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor8d267c52011-09-09 02:06:17 +00002279 // Modules
2280 case tok::kw___module_private__:
2281 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2282 break;
2283
Sebastian Redl2ac67232009-11-05 15:47:02 +00002284 // constexpr
2285 case tok::kw_constexpr:
2286 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2287 break;
2288
Chris Lattner80d0c892009-01-21 19:48:37 +00002289 // type-specifier
2290 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002291 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2292 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002293 break;
2294 case tok::kw_long:
2295 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002296 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002298 else
John McCallfec54012009-08-03 20:12:06 +00002299 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2300 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002301 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002302 case tok::kw___int64:
2303 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2304 DiagID);
2305 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002306 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002307 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2308 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002309 break;
2310 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002311 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2312 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002313 break;
2314 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002315 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2316 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002317 break;
2318 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002319 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2320 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002321 break;
2322 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2324 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002325 break;
2326 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002327 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2328 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002329 break;
2330 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2332 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002333 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002334 case tok::kw___int128:
2335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2336 DiagID);
2337 break;
2338 case tok::kw_half:
2339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2340 DiagID);
2341 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002342 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2344 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002345 break;
2346 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2348 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002349 break;
2350 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2352 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002353 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002354 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002355 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2356 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002357 break;
2358 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002359 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2360 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002361 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002362 case tok::kw_bool:
2363 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002364 if (Tok.is(tok::kw_bool) &&
2365 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2366 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2367 PrevSpec = ""; // Not used by the diagnostic.
2368 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002369 // For better error recovery.
2370 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002371 isInvalid = true;
2372 } else {
2373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2374 DiagID);
2375 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002376 break;
2377 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2379 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002380 break;
2381 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2383 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002384 break;
2385 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2387 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002388 break;
John Thompson82287d12010-02-05 00:12:22 +00002389 case tok::kw___vector:
2390 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2391 break;
2392 case tok::kw___pixel:
2393 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2394 break;
John McCalla5fc4722011-04-09 22:50:59 +00002395 case tok::kw___unknown_anytype:
2396 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2397 PrevSpec, DiagID);
2398 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002399
2400 // class-specifier:
2401 case tok::kw_class:
2402 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002403 case tok::kw_union: {
2404 tok::TokenKind Kind = Tok.getKind();
2405 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002406 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2407 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002408 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002409 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002410
2411 // enum-specifier:
2412 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002413 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002414 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002415 continue;
2416
2417 // cv-qualifier:
2418 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002419 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002420 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002421 break;
2422 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002423 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002424 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002425 break;
2426 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002427 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002428 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002429 break;
2430
Douglas Gregord57959a2009-03-27 23:10:48 +00002431 // C++ typename-specifier:
2432 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002433 if (TryAnnotateTypeOrScopeToken()) {
2434 DS.SetTypeSpecError();
2435 goto DoneWithDeclSpec;
2436 }
2437 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002438 continue;
2439 break;
2440
Chris Lattner80d0c892009-01-21 19:48:37 +00002441 // GNU typeof support.
2442 case tok::kw_typeof:
2443 ParseTypeofSpecifier(DS);
2444 continue;
2445
David Blaikie42d6d0c2011-12-04 05:04:18 +00002446 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002447 ParseDecltypeSpecifier(DS);
2448 continue;
2449
Sean Huntdb5d44b2011-05-19 05:37:45 +00002450 case tok::kw___underlying_type:
2451 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002452 continue;
2453
2454 case tok::kw__Atomic:
2455 ParseAtomicSpecifier(DS);
2456 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002457
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002458 // OpenCL qualifiers:
2459 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002460 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002461 goto DoneWithDeclSpec;
2462 case tok::kw___private:
2463 case tok::kw___global:
2464 case tok::kw___local:
2465 case tok::kw___constant:
2466 case tok::kw___read_only:
2467 case tok::kw___write_only:
2468 case tok::kw___read_write:
2469 ParseOpenCLQualifiers(DS);
2470 break;
2471
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002472 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002473 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002474 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2475 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002476 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002477 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Douglas Gregor46f936e2010-11-19 17:10:50 +00002479 if (!ParseObjCProtocolQualifiers(DS))
2480 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2481 << FixItHint::CreateInsertion(Loc, "id")
2482 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002483
2484 // Need to support trailing type qualifiers (e.g. "id<p> const").
2485 // If a type specifier follows, it will be diagnosed elsewhere.
2486 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002487 }
John McCallfec54012009-08-03 20:12:06 +00002488 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002489 if (isInvalid) {
2490 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002491 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002492
2493 if (DiagID == diag::ext_duplicate_declspec)
2494 Diag(Tok, DiagID)
2495 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2496 else
2497 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002498 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002499
Chris Lattner81c018d2008-03-13 06:29:04 +00002500 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002501 if (DiagID != diag::err_bool_redeclaration)
2502 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002503 }
2504}
Douglas Gregoradcac882008-12-01 23:54:00 +00002505
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002506/// ParseStructDeclaration - Parse a struct declaration without the terminating
2507/// semicolon.
2508///
Reid Spencer5f016e22007-07-11 17:01:13 +00002509/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002510/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002511/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002512/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002513/// struct-declarator-list:
2514/// struct-declarator
2515/// struct-declarator-list ',' struct-declarator
2516/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2517/// struct-declarator:
2518/// declarator
2519/// [GNU] declarator attributes[opt]
2520/// declarator[opt] ':' constant-expression
2521/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2522///
Chris Lattnere1359422008-04-10 06:46:29 +00002523void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002524ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002525
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002526 if (Tok.is(tok::kw___extension__)) {
2527 // __extension__ silences extension warnings in the subexpression.
2528 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002529 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002530 return ParseStructDeclaration(DS, Fields);
2531 }
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Steve Naroff28a7ca82007-08-20 22:28:22 +00002533 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002534 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002536 // If there are no declarators, this is a free-standing declaration
2537 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002538 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002539 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002540 return;
2541 }
2542
2543 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002544 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002545 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002546 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002547 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002548 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002549 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002550
2551 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002552 if (!FirstDeclarator)
2553 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Steve Naroff28a7ca82007-08-20 22:28:22 +00002555 /// struct-declarator: declarator
2556 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002557 if (Tok.isNot(tok::colon)) {
2558 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2559 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002560 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002561 }
Mike Stump1eb44332009-09-09 15:08:12 +00002562
Chris Lattner04d66662007-10-09 17:33:22 +00002563 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002564 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002565 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002566 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002567 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002568 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002569 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002570 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002571
Steve Naroff28a7ca82007-08-20 22:28:22 +00002572 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002573 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002574
John McCallbdd563e2009-11-03 02:38:08 +00002575 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002576 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002577 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002578
Steve Naroff28a7ca82007-08-20 22:28:22 +00002579 // If we don't have a comma, it is either the end of the list (a ';')
2580 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002581 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002582 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002583
Steve Naroff28a7ca82007-08-20 22:28:22 +00002584 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002585 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002586
John McCallbdd563e2009-11-03 02:38:08 +00002587 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002588 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002589}
2590
2591/// ParseStructUnionBody
2592/// struct-contents:
2593/// struct-declaration-list
2594/// [EXT] empty
2595/// [GNU] "struct-declaration-list" without terminatoring ';'
2596/// struct-declaration-list:
2597/// struct-declaration
2598/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002599/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002600///
Reid Spencer5f016e22007-07-11 17:01:13 +00002601void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002602 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002603 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2604 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002606 BalancedDelimiterTracker T(*this, tok::l_brace);
2607 if (T.consumeOpen())
2608 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002610 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002611 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002612
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2614 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002615 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002616 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2617 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2618 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002619
Chris Lattner5f9e2722011-07-23 10:55:15 +00002620 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002621
Reid Spencer5f016e22007-07-11 17:01:13 +00002622 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002623 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002624 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002625
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002627 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002628 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002629 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002630 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002631 ConsumeToken();
2632 continue;
2633 }
Chris Lattnere1359422008-04-10 06:46:29 +00002634
2635 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002636 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002637
John McCallbdd563e2009-11-03 02:38:08 +00002638 if (!Tok.is(tok::at)) {
2639 struct CFieldCallback : FieldCallback {
2640 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002641 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002642 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002643
John McCalld226f652010-08-21 09:40:31 +00002644 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002645 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002646 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2647
John McCalld226f652010-08-21 09:40:31 +00002648 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002649 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002650 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002651 FD.D.getDeclSpec().getSourceRange().getBegin(),
2652 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002653 FieldDecls.push_back(Field);
2654 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002655 }
John McCallbdd563e2009-11-03 02:38:08 +00002656 } Callback(*this, TagDecl, FieldDecls);
2657
2658 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002659 } else { // Handle @defs
2660 ConsumeToken();
2661 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2662 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002663 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002664 continue;
2665 }
2666 ConsumeToken();
2667 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2668 if (!Tok.is(tok::identifier)) {
2669 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002670 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002671 continue;
2672 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002673 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002674 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002675 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002676 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2677 ConsumeToken();
2678 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002679 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
Chris Lattner04d66662007-10-09 17:33:22 +00002681 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002682 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002683 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002684 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 break;
2686 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002687 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2688 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002690 // If we stopped at a ';', eat it.
2691 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 }
2693 }
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002695 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002696
John McCall0b7e6782011-03-24 11:26:52 +00002697 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002699 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002700
Douglas Gregor23c94db2010-07-02 17:43:08 +00002701 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002702 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002703 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002704 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002705 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002706 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2707 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002708}
2709
Reid Spencer5f016e22007-07-11 17:01:13 +00002710/// ParseEnumSpecifier
2711/// enum-specifier: [C99 6.7.2.2]
2712/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002713///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002714/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2715/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002716/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2717/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002718/// 'enum' identifier
2719/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002720///
Richard Smith1af83c42012-03-23 03:33:32 +00002721/// [C++11] enum-head '{' enumerator-list[opt] '}'
2722/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002723///
Richard Smith1af83c42012-03-23 03:33:32 +00002724/// enum-head: [C++11]
2725/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2726/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2727/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002728///
Richard Smith1af83c42012-03-23 03:33:32 +00002729/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002730/// 'enum'
2731/// 'enum' 'class'
2732/// 'enum' 'struct'
2733///
Richard Smith1af83c42012-03-23 03:33:32 +00002734/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002735/// ':' type-specifier-seq
2736///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002737/// [C++] elaborated-type-specifier:
2738/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2739///
Chris Lattner4c97d762009-04-12 21:49:30 +00002740void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002741 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002742 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002743 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002744 if (Tok.is(tok::code_completion)) {
2745 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002746 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002747 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002748 }
John McCall57c13002011-07-06 05:58:41 +00002749
Richard Smithbdad7a22012-01-10 01:33:14 +00002750 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002751 bool IsScopedUsingClassTag = false;
2752
David Blaikie4e4d0842012-03-11 07:00:24 +00002753 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002754 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002755 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002756 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002757 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002758 }
Richard Smith1af83c42012-03-23 03:33:32 +00002759
2760 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2761 // used to specify explicit instantiations. We extend this to also cover
2762 // explicit specializations.
2763 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2764 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2765 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2766
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002767 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002768 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002769 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002770
Aaron Ballman6454a022012-03-01 04:09:28 +00002771 // If declspecs exist after tag, parse them.
2772 while (Tok.is(tok::kw___declspec))
2773 ParseMicrosoftDeclSpec(attrs);
2774
Richard Smith7796eb52012-03-12 08:56:40 +00002775 // Enum definitions should not be parsed in a trailing-return-type.
2776 bool AllowDeclaration = DSC != DSC_trailing;
2777
2778 bool AllowFixedUnderlyingType = AllowDeclaration &&
2779 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2780 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002781
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002782 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002783 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002784 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2785 // if a fixed underlying type is allowed.
2786 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2787
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002788 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2789 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002790 return;
2791
2792 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002793 Diag(Tok, diag::err_expected_ident);
2794 if (Tok.isNot(tok::l_brace)) {
2795 // Has no name and is not a definition.
2796 // Skip the rest of this declarator, up until the comma or semicolon.
2797 SkipUntil(tok::comma, true);
2798 return;
2799 }
2800 }
2801 }
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002803 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002804 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002805 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002806 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002808 // Skip the rest of this declarator, up until the comma or semicolon.
2809 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002811 }
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002813 // If an identifier is present, consume and remember it.
2814 IdentifierInfo *Name = 0;
2815 SourceLocation NameLoc;
2816 if (Tok.is(tok::identifier)) {
2817 Name = Tok.getIdentifierInfo();
2818 NameLoc = ConsumeToken();
2819 }
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Richard Smithbdad7a22012-01-10 01:33:14 +00002821 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002822 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2823 // declaration of a scoped enumeration.
2824 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002825 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002826 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002827 }
2828
Richard Smith1af83c42012-03-23 03:33:32 +00002829 // Stop suppressing access control now we've parsed the enum name.
2830 SuppressAccess.done();
2831
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002832 TypeResult BaseType;
2833
Douglas Gregora61b3e72010-12-01 17:42:47 +00002834 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002835 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002836 bool PossibleBitfield = false;
2837 if (getCurScope()->getFlags() & Scope::ClassScope) {
2838 // If we're in class scope, this can either be an enum declaration with
2839 // an underlying type, or a declaration of a bitfield member. We try to
2840 // use a simple disambiguation scheme first to catch the common cases
2841 // (integer literal, sizeof); if it's still ambiguous, we then consider
2842 // anything that's a simple-type-specifier followed by '(' as an
2843 // expression. This suffices because function types are not valid
2844 // underlying types anyway.
2845 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2846 // If the next token starts an expression, we know we're parsing a
2847 // bit-field. This is the common case.
2848 if (TPR == TPResult::True())
2849 PossibleBitfield = true;
2850 // If the next token starts a type-specifier-seq, it may be either a
2851 // a fixed underlying type or the start of a function-style cast in C++;
2852 // lookahead one more token to see if it's obvious that we have a
2853 // fixed underlying type.
2854 else if (TPR == TPResult::False() &&
2855 GetLookAheadToken(2).getKind() == tok::semi) {
2856 // Consume the ':'.
2857 ConsumeToken();
2858 } else {
2859 // We have the start of a type-specifier-seq, so we have to perform
2860 // tentative parsing to determine whether we have an expression or a
2861 // type.
2862 TentativeParsingAction TPA(*this);
2863
2864 // Consume the ':'.
2865 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002866
2867 // If we see a type specifier followed by an open-brace, we have an
2868 // ambiguity between an underlying type and a C++11 braced
2869 // function-style cast. Resolve this by always treating it as an
2870 // underlying type.
2871 // FIXME: The standard is not entirely clear on how to disambiguate in
2872 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002873 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002874 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002875 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002876 // We'll parse this as a bitfield later.
2877 PossibleBitfield = true;
2878 TPA.Revert();
2879 } else {
2880 // We have a type-specifier-seq.
2881 TPA.Commit();
2882 }
2883 }
2884 } else {
2885 // Consume the ':'.
2886 ConsumeToken();
2887 }
2888
2889 if (!PossibleBitfield) {
2890 SourceRange Range;
2891 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002892
David Blaikie4e4d0842012-03-11 07:00:24 +00002893 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002894 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2895 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002896 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002897 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002898 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002899 }
2900
Richard Smithbdad7a22012-01-10 01:33:14 +00002901 // There are four options here. If we have 'friend enum foo;' then this is a
2902 // friend declaration, and cannot have an accompanying definition. If we have
2903 // 'enum foo;', then this is a forward declaration. If we have
2904 // 'enum foo {...' then this is a definition. Otherwise we have something
2905 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002906 //
2907 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2908 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2909 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2910 //
John McCallf312b1e2010-08-26 23:41:50 +00002911 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002912 if (DS.isFriendSpecified())
2913 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002914 else if (!AllowDeclaration)
2915 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002916 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002917 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002918 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002919 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002920 else
John McCallf312b1e2010-08-26 23:41:50 +00002921 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002922
2923 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002924 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002925 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002926 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2927 // Skip the rest of this declarator, up until the comma or semicolon.
2928 Diag(Tok, diag::err_enum_template);
2929 SkipUntil(tok::comma, true);
2930 return;
2931 }
2932
2933 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2934 // Enumerations can't be explicitly instantiated.
2935 DS.SetTypeSpecError();
2936 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2937 return;
2938 }
2939
2940 assert(TemplateInfo.TemplateParams && "no template parameters");
2941 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2942 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002943 }
Richard Smith1af83c42012-03-23 03:33:32 +00002944
Douglas Gregorb9075602011-02-22 02:55:24 +00002945 if (!Name && TUK != Sema::TUK_Definition) {
2946 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002947
Douglas Gregorb9075602011-02-22 02:55:24 +00002948 // Skip the rest of this declarator, up until the comma or semicolon.
2949 SkipUntil(tok::comma, true);
2950 return;
2951 }
Richard Smith1af83c42012-03-23 03:33:32 +00002952
Douglas Gregor402abb52009-05-28 23:31:59 +00002953 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002954 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002955 const char *PrevSpec = 0;
2956 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002957 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002958 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002959 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002960 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002961 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002962
Douglas Gregor48c89f42010-04-24 16:38:41 +00002963 if (IsDependent) {
2964 // This enum has a dependent nested-name-specifier. Handle it as a
2965 // dependent tag.
2966 if (!Name) {
2967 DS.SetTypeSpecError();
2968 Diag(Tok, diag::err_expected_type_name_after_typename);
2969 return;
2970 }
2971
Douglas Gregor23c94db2010-07-02 17:43:08 +00002972 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002973 TUK, SS, Name, StartLoc,
2974 NameLoc);
2975 if (Type.isInvalid()) {
2976 DS.SetTypeSpecError();
2977 return;
2978 }
2979
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002980 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2981 NameLoc.isValid() ? NameLoc : StartLoc,
2982 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002983 Diag(StartLoc, DiagID) << PrevSpec;
2984
2985 return;
2986 }
Mike Stump1eb44332009-09-09 15:08:12 +00002987
John McCalld226f652010-08-21 09:40:31 +00002988 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002989 // The action failed to produce an enumeration tag. If this is a
2990 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002991 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002992 ConsumeBrace();
2993 SkipUntil(tok::r_brace);
2994 }
2995
2996 DS.SetTypeSpecError();
2997 return;
2998 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002999
Richard Smith7796eb52012-03-12 08:56:40 +00003000 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003001 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00003002 Diag(Tok, diag::err_friend_decl_defines_type)
3003 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00003004 ConsumeBrace();
3005 SkipUntil(tok::r_brace);
3006 } else {
3007 ParseEnumBody(StartLoc, TagDecl);
3008 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003009 }
Mike Stump1eb44332009-09-09 15:08:12 +00003010
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003011 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3012 NameLoc.isValid() ? NameLoc : StartLoc,
3013 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003014 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003015}
3016
3017/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3018/// enumerator-list:
3019/// enumerator
3020/// enumerator-list ',' enumerator
3021/// enumerator:
3022/// enumeration-constant
3023/// enumeration-constant '=' constant-expression
3024/// enumeration-constant:
3025/// identifier
3026///
John McCalld226f652010-08-21 09:40:31 +00003027void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003028 // Enter the scope of the enum body and start the definition.
3029 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003030 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003031
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003032 BalancedDelimiterTracker T(*this, tok::l_brace);
3033 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Chris Lattner7946dd32007-08-27 17:24:30 +00003035 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003036 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003037 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Chris Lattner5f9e2722011-07-23 10:55:15 +00003039 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003040
John McCalld226f652010-08-21 09:40:31 +00003041 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Reid Spencer5f016e22007-07-11 17:01:13 +00003043 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003044 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003045 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3046 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003047
John McCall5b629aa2010-10-22 23:36:17 +00003048 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003049 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003050 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003051
Reid Spencer5f016e22007-07-11 17:01:13 +00003052 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003053 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003054 ParsingDeclRAIIObject PD(*this);
3055
Chris Lattner04d66662007-10-09 17:33:22 +00003056 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003058 AssignedVal = ParseConstantExpression();
3059 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003060 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003061 }
Mike Stump1eb44332009-09-09 15:08:12 +00003062
Reid Spencer5f016e22007-07-11 17:01:13 +00003063 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003064 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3065 LastEnumConstDecl,
3066 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003067 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003068 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003069 PD.complete(EnumConstDecl);
3070
Reid Spencer5f016e22007-07-11 17:01:13 +00003071 EnumConstantDecls.push_back(EnumConstDecl);
3072 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003073
Douglas Gregor751f6922010-09-07 14:51:08 +00003074 if (Tok.is(tok::identifier)) {
3075 // We're missing a comma between enumerators.
3076 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3077 Diag(Loc, diag::err_enumerator_list_missing_comma)
3078 << FixItHint::CreateInsertion(Loc, ", ");
3079 continue;
3080 }
3081
Chris Lattner04d66662007-10-09 17:33:22 +00003082 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003083 break;
3084 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003085
Richard Smith7fe62082011-10-15 05:09:34 +00003086 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003087 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003088 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003089 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003090 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003091 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003092 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3093 << FixItHint::CreateRemoval(CommaLoc);
3094 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 }
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003098 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003099
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003101 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003102 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003103
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003104 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3105 EnumDecl, EnumConstantDecls.data(),
3106 EnumConstantDecls.size(), getCurScope(),
3107 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003108
Douglas Gregor72de6672009-01-08 20:45:30 +00003109 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003110 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3111 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003112}
3113
3114/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003115/// start of a type-qualifier-list.
3116bool Parser::isTypeQualifier() const {
3117 switch (Tok.getKind()) {
3118 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003119
3120 // type-qualifier only in OpenCL
3121 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003122 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003123
Steve Naroff5f8aa692008-02-11 23:15:56 +00003124 // type-qualifier
3125 case tok::kw_const:
3126 case tok::kw_volatile:
3127 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003128 case tok::kw___private:
3129 case tok::kw___local:
3130 case tok::kw___global:
3131 case tok::kw___constant:
3132 case tok::kw___read_only:
3133 case tok::kw___read_write:
3134 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003135 return true;
3136 }
3137}
3138
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003139/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3140/// is definitely a type-specifier. Return false if it isn't part of a type
3141/// specifier or if we're not sure.
3142bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3143 switch (Tok.getKind()) {
3144 default: return false;
3145 // type-specifiers
3146 case tok::kw_short:
3147 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003148 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003149 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003150 case tok::kw_signed:
3151 case tok::kw_unsigned:
3152 case tok::kw__Complex:
3153 case tok::kw__Imaginary:
3154 case tok::kw_void:
3155 case tok::kw_char:
3156 case tok::kw_wchar_t:
3157 case tok::kw_char16_t:
3158 case tok::kw_char32_t:
3159 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003160 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003161 case tok::kw_float:
3162 case tok::kw_double:
3163 case tok::kw_bool:
3164 case tok::kw__Bool:
3165 case tok::kw__Decimal32:
3166 case tok::kw__Decimal64:
3167 case tok::kw__Decimal128:
3168 case tok::kw___vector:
3169
3170 // struct-or-union-specifier (C99) or class-specifier (C++)
3171 case tok::kw_class:
3172 case tok::kw_struct:
3173 case tok::kw_union:
3174 // enum-specifier
3175 case tok::kw_enum:
3176
3177 // typedef-name
3178 case tok::annot_typename:
3179 return true;
3180 }
3181}
3182
Steve Naroff5f8aa692008-02-11 23:15:56 +00003183/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003184/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003185bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003186 switch (Tok.getKind()) {
3187 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Chris Lattner166a8fc2009-01-04 23:41:41 +00003189 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003190 if (TryAltiVecVectorToken())
3191 return true;
3192 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003193 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003194 // Annotate typenames and C++ scope specifiers. If we get one, just
3195 // recurse to handle whatever we get.
3196 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003197 return true;
3198 if (Tok.is(tok::identifier))
3199 return false;
3200 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003201
Chris Lattner166a8fc2009-01-04 23:41:41 +00003202 case tok::coloncolon: // ::foo::bar
3203 if (NextToken().is(tok::kw_new) || // ::new
3204 NextToken().is(tok::kw_delete)) // ::delete
3205 return false;
3206
Chris Lattner166a8fc2009-01-04 23:41:41 +00003207 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003208 return true;
3209 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Reid Spencer5f016e22007-07-11 17:01:13 +00003211 // GNU attributes support.
3212 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003213 // GNU typeof support.
3214 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 // type-specifiers
3217 case tok::kw_short:
3218 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003219 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003220 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 case tok::kw_signed:
3222 case tok::kw_unsigned:
3223 case tok::kw__Complex:
3224 case tok::kw__Imaginary:
3225 case tok::kw_void:
3226 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003227 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003228 case tok::kw_char16_t:
3229 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003230 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003231 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003232 case tok::kw_float:
3233 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003234 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 case tok::kw__Bool:
3236 case tok::kw__Decimal32:
3237 case tok::kw__Decimal64:
3238 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003239 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003240
Chris Lattner99dc9142008-04-13 18:59:07 +00003241 // struct-or-union-specifier (C99) or class-specifier (C++)
3242 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003243 case tok::kw_struct:
3244 case tok::kw_union:
3245 // enum-specifier
3246 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 // type-qualifier
3249 case tok::kw_const:
3250 case tok::kw_volatile:
3251 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003252
3253 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003254 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003256
Chris Lattner7c186be2008-10-20 00:25:30 +00003257 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3258 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003259 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003260
Steve Naroff239f0732008-12-25 14:16:32 +00003261 case tok::kw___cdecl:
3262 case tok::kw___stdcall:
3263 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003264 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003265 case tok::kw___w64:
3266 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003267 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003268 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003269 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003270
3271 case tok::kw___private:
3272 case tok::kw___local:
3273 case tok::kw___global:
3274 case tok::kw___constant:
3275 case tok::kw___read_only:
3276 case tok::kw___read_write:
3277 case tok::kw___write_only:
3278
Eli Friedman290eeb02009-06-08 23:27:34 +00003279 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003280
3281 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003282 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003283
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003284 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003285 case tok::kw__Atomic:
3286 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003287 }
3288}
3289
3290/// isDeclarationSpecifier() - Return true if the current token is part of a
3291/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003292///
3293/// \param DisambiguatingWithExpression True to indicate that the purpose of
3294/// this check is to disambiguate between an expression and a declaration.
3295bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 switch (Tok.getKind()) {
3297 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003299 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003300 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003301
Chris Lattner166a8fc2009-01-04 23:41:41 +00003302 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003303 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003304 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003305 return false;
John Thompson82287d12010-02-05 00:12:22 +00003306 if (TryAltiVecVectorToken())
3307 return true;
3308 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003309 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003310 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003311 // Annotate typenames and C++ scope specifiers. If we get one, just
3312 // recurse to handle whatever we get.
3313 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003314 return true;
3315 if (Tok.is(tok::identifier))
3316 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003317
3318 // If we're in Objective-C and we have an Objective-C class type followed
3319 // by an identifier and then either ':' or ']', in a place where an
3320 // expression is permitted, then this is probably a class message send
3321 // missing the initial '['. In this case, we won't consider this to be
3322 // the start of a declaration.
3323 if (DisambiguatingWithExpression &&
3324 isStartOfObjCClassMessageMissingOpenBracket())
3325 return false;
3326
John McCall9ba61662010-02-26 08:45:28 +00003327 return isDeclarationSpecifier();
3328
Chris Lattner166a8fc2009-01-04 23:41:41 +00003329 case tok::coloncolon: // ::foo::bar
3330 if (NextToken().is(tok::kw_new) || // ::new
3331 NextToken().is(tok::kw_delete)) // ::delete
3332 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Chris Lattner166a8fc2009-01-04 23:41:41 +00003334 // Annotate typenames and C++ scope specifiers. If we get one, just
3335 // recurse to handle whatever we get.
3336 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003337 return true;
3338 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 // storage-class-specifier
3341 case tok::kw_typedef:
3342 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003343 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003344 case tok::kw_static:
3345 case tok::kw_auto:
3346 case tok::kw_register:
3347 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003348
Douglas Gregor8d267c52011-09-09 02:06:17 +00003349 // Modules
3350 case tok::kw___module_private__:
3351
Reid Spencer5f016e22007-07-11 17:01:13 +00003352 // type-specifiers
3353 case tok::kw_short:
3354 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003355 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003356 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003357 case tok::kw_signed:
3358 case tok::kw_unsigned:
3359 case tok::kw__Complex:
3360 case tok::kw__Imaginary:
3361 case tok::kw_void:
3362 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003363 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003364 case tok::kw_char16_t:
3365 case tok::kw_char32_t:
3366
Reid Spencer5f016e22007-07-11 17:01:13 +00003367 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003368 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003369 case tok::kw_float:
3370 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003371 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 case tok::kw__Bool:
3373 case tok::kw__Decimal32:
3374 case tok::kw__Decimal64:
3375 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003376 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003377
Chris Lattner99dc9142008-04-13 18:59:07 +00003378 // struct-or-union-specifier (C99) or class-specifier (C++)
3379 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003380 case tok::kw_struct:
3381 case tok::kw_union:
3382 // enum-specifier
3383 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003384
Reid Spencer5f016e22007-07-11 17:01:13 +00003385 // type-qualifier
3386 case tok::kw_const:
3387 case tok::kw_volatile:
3388 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003389
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 // function-specifier
3391 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003392 case tok::kw_virtual:
3393 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003394
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003395 // static_assert-declaration
3396 case tok::kw__Static_assert:
3397
Chris Lattner1ef08762007-08-09 17:01:07 +00003398 // GNU typeof support.
3399 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003400
Chris Lattner1ef08762007-08-09 17:01:07 +00003401 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003402 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003403 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003404
Francois Pichete3d49b42011-06-19 08:02:06 +00003405 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003406 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003407 return true;
3408
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003409 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003410 case tok::kw__Atomic:
3411 return true;
3412
Chris Lattnerf3948c42008-07-26 03:38:44 +00003413 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3414 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003415 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003416
Douglas Gregord9d75e52011-04-27 05:41:15 +00003417 // typedef-name
3418 case tok::annot_typename:
3419 return !DisambiguatingWithExpression ||
3420 !isStartOfObjCClassMessageMissingOpenBracket();
3421
Steve Naroff47f52092009-01-06 19:34:12 +00003422 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003423 case tok::kw___cdecl:
3424 case tok::kw___stdcall:
3425 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003426 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003427 case tok::kw___w64:
3428 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003429 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003430 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003431 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003432 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003433
3434 case tok::kw___private:
3435 case tok::kw___local:
3436 case tok::kw___global:
3437 case tok::kw___constant:
3438 case tok::kw___read_only:
3439 case tok::kw___read_write:
3440 case tok::kw___write_only:
3441
Eli Friedman290eeb02009-06-08 23:27:34 +00003442 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003443 }
3444}
3445
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003446bool Parser::isConstructorDeclarator() {
3447 TentativeParsingAction TPA(*this);
3448
3449 // Parse the C++ scope specifier.
3450 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003451 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3452 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003453 TPA.Revert();
3454 return false;
3455 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003456
3457 // Parse the constructor name.
3458 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3459 // We already know that we have a constructor name; just consume
3460 // the token.
3461 ConsumeToken();
3462 } else {
3463 TPA.Revert();
3464 return false;
3465 }
3466
Richard Smith22592862012-03-27 23:05:05 +00003467 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003468 if (Tok.isNot(tok::l_paren)) {
3469 TPA.Revert();
3470 return false;
3471 }
3472 ConsumeParen();
3473
Richard Smith22592862012-03-27 23:05:05 +00003474 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3475 // that we have a constructor.
3476 if (Tok.is(tok::r_paren) ||
3477 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003478 TPA.Revert();
3479 return true;
3480 }
3481
3482 // If we need to, enter the specified scope.
3483 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003484 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003485 DeclScopeObj.EnterDeclaratorScope();
3486
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003487 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003488 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003489 MaybeParseMicrosoftAttributes(Attrs);
3490
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003491 // Check whether the next token(s) are part of a declaration
3492 // specifier, in which case we have the start of a parameter and,
3493 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003494 bool IsConstructor = false;
3495 if (isDeclarationSpecifier())
3496 IsConstructor = true;
3497 else if (Tok.is(tok::identifier) ||
3498 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3499 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3500 // This might be a parenthesized member name, but is more likely to
3501 // be a constructor declaration with an invalid argument type. Keep
3502 // looking.
3503 if (Tok.is(tok::annot_cxxscope))
3504 ConsumeToken();
3505 ConsumeToken();
3506
3507 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003508 // which must have one of the following syntactic forms (see the
3509 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003510 switch (Tok.getKind()) {
3511 case tok::l_paren:
3512 // C(X ( int));
3513 case tok::l_square:
3514 // C(X [ 5]);
3515 // C(X [ [attribute]]);
3516 case tok::coloncolon:
3517 // C(X :: Y);
3518 // C(X :: *p);
3519 case tok::r_paren:
3520 // C(X )
3521 // Assume this isn't a constructor, rather than assuming it's a
3522 // constructor with an unnamed parameter of an ill-formed type.
3523 break;
3524
3525 default:
3526 IsConstructor = true;
3527 break;
3528 }
3529 }
3530
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003531 TPA.Revert();
3532 return IsConstructor;
3533}
Reid Spencer5f016e22007-07-11 17:01:13 +00003534
3535/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003536/// type-qualifier-list: [C99 6.7.5]
3537/// type-qualifier
3538/// [vendor] attributes
3539/// [ only if VendorAttributesAllowed=true ]
3540/// type-qualifier-list type-qualifier
3541/// [vendor] type-qualifier-list attributes
3542/// [ only if VendorAttributesAllowed=true ]
3543/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3544/// [ only if CXX0XAttributesAllowed=true ]
3545/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003546///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003547void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3548 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003549 bool CXX11AttributesAllowed) {
3550 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003551 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003552 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003553 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003554 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003555 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003556
3557 SourceLocation EndLoc;
3558
Reid Spencer5f016e22007-07-11 17:01:13 +00003559 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003560 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003561 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003562 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003563 SourceLocation Loc = Tok.getLocation();
3564
3565 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003566 case tok::code_completion:
3567 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003568 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003569
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003571 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003572 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003573 break;
3574 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003575 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003576 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 break;
3578 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003579 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003580 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003581 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003582
3583 // OpenCL qualifiers:
3584 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003585 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003586 goto DoneWithTypeQuals;
3587 case tok::kw___private:
3588 case tok::kw___global:
3589 case tok::kw___local:
3590 case tok::kw___constant:
3591 case tok::kw___read_only:
3592 case tok::kw___write_only:
3593 case tok::kw___read_write:
3594 ParseOpenCLQualifiers(DS);
3595 break;
3596
Eli Friedman290eeb02009-06-08 23:27:34 +00003597 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003598 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003599 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003600 case tok::kw___cdecl:
3601 case tok::kw___stdcall:
3602 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003603 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003604 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003605 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003606 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003607 continue;
3608 }
3609 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003610 case tok::kw___pascal:
3611 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003612 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003613 continue;
3614 }
3615 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003616 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003617 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003618 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003619 continue; // do *not* consume the next token!
3620 }
3621 // otherwise, FALL THROUGH!
3622 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003623 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003624 // If this is not a type-qualifier token, we're done reading type
3625 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003626 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003627 if (EndLoc.isValid())
3628 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003629 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003630 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003631
Reid Spencer5f016e22007-07-11 17:01:13 +00003632 // If the specifier combination wasn't legal, issue a diagnostic.
3633 if (isInvalid) {
3634 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003635 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003636 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003637 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003638 }
3639}
3640
3641
3642/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3643///
3644void Parser::ParseDeclarator(Declarator &D) {
3645 /// This implements the 'declarator' production in the C grammar, then checks
3646 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003647 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003648}
3649
Richard Smith9988f282012-03-29 01:16:42 +00003650static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3651 if (Kind == tok::star || Kind == tok::caret)
3652 return true;
3653
3654 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3655 if (!Lang.CPlusPlus)
3656 return false;
3657
3658 return Kind == tok::amp || Kind == tok::ampamp;
3659}
3660
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003661/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3662/// is parsed by the function passed to it. Pass null, and the direct-declarator
3663/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003664/// ptr-operator production.
3665///
Richard Smith0706df42011-10-19 21:33:05 +00003666/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003667/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3668/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003669///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003670/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3671/// [C] pointer[opt] direct-declarator
3672/// [C++] direct-declarator
3673/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003674///
3675/// pointer: [C99 6.7.5]
3676/// '*' type-qualifier-list[opt]
3677/// '*' type-qualifier-list[opt] pointer
3678///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003679/// ptr-operator:
3680/// '*' cv-qualifier-seq[opt]
3681/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003682/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003683/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003684/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003685/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003686void Parser::ParseDeclaratorInternal(Declarator &D,
3687 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003688 if (Diags.hasAllExtensionsSilenced())
3689 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003690
Sebastian Redlf30208a2009-01-24 21:16:55 +00003691 // C++ member pointers start with a '::' or a nested-name.
3692 // Member pointers get special handling, since there's no place for the
3693 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003694 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003695 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3696 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003697 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3698 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003699 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003700 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003701
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003702 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003703 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003704 // The scope spec really belongs to the direct-declarator.
3705 D.getCXXScopeSpec() = SS;
3706 if (DirectDeclParser)
3707 (this->*DirectDeclParser)(D);
3708 return;
3709 }
3710
3711 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003712 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003713 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003714 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003715 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003716
3717 // Recurse to parse whatever is left.
3718 ParseDeclaratorInternal(D, DirectDeclParser);
3719
3720 // Sema will have to catch (syntactically invalid) pointers into global
3721 // scope. It has to catch pointers into namespace scope anyway.
3722 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003723 Loc),
3724 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003725 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003726 return;
3727 }
3728 }
3729
3730 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003731 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003732 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003733 if (DirectDeclParser)
3734 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003735 return;
3736 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003737
Sebastian Redl05532f22009-03-15 22:02:01 +00003738 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3739 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003740 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003741 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003742
Chris Lattner9af55002009-03-27 04:18:06 +00003743 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003744 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003745 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003746
Richard Smith6ee326a2012-04-10 01:32:12 +00003747 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003748 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003749 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003750
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003752 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003753 if (Kind == tok::star)
3754 // Remember that we parsed a pointer type, and remember the type-quals.
3755 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003756 DS.getConstSpecLoc(),
3757 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003758 DS.getRestrictSpecLoc()),
3759 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003760 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003761 else
3762 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003763 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003764 Loc),
3765 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003766 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003767 } else {
3768 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003769 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003770
Sebastian Redl743de1f2009-03-23 00:00:23 +00003771 // Complain about rvalue references in C++03, but then go on and build
3772 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003773 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003774 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003775 diag::warn_cxx98_compat_rvalue_reference :
3776 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003777
Richard Smith6ee326a2012-04-10 01:32:12 +00003778 // GNU-style and C++11 attributes are allowed here, as is restrict.
3779 ParseTypeQualifierListOpt(DS);
3780 D.ExtendWithDeclSpec(DS);
3781
Reid Spencer5f016e22007-07-11 17:01:13 +00003782 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3783 // cv-qualifiers are introduced through the use of a typedef or of a
3784 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003785 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3786 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3787 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003788 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3790 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003791 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003792 }
3793
3794 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003795 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003796
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003797 if (D.getNumTypeObjects() > 0) {
3798 // C++ [dcl.ref]p4: There shall be no references to references.
3799 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3800 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003801 if (const IdentifierInfo *II = D.getIdentifier())
3802 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3803 << II;
3804 else
3805 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3806 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003807
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003808 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003809 // can go ahead and build the (technically ill-formed)
3810 // declarator: reference collapsing will take care of it.
3811 }
3812 }
3813
Reid Spencer5f016e22007-07-11 17:01:13 +00003814 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003815 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003816 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003817 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003818 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003819 }
3820}
3821
Richard Smith9988f282012-03-29 01:16:42 +00003822static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3823 SourceLocation EllipsisLoc) {
3824 if (EllipsisLoc.isValid()) {
3825 FixItHint Insertion;
3826 if (!D.getEllipsisLoc().isValid()) {
3827 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3828 D.setEllipsisLoc(EllipsisLoc);
3829 }
3830 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3831 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3832 }
3833}
3834
Reid Spencer5f016e22007-07-11 17:01:13 +00003835/// ParseDirectDeclarator
3836/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003837/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003838/// '(' declarator ')'
3839/// [GNU] '(' attributes declarator ')'
3840/// [C90] direct-declarator '[' constant-expression[opt] ']'
3841/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3842/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3843/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3844/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003845/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3846/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003847/// direct-declarator '(' parameter-type-list ')'
3848/// direct-declarator '(' identifier-list[opt] ')'
3849/// [GNU] direct-declarator '(' parameter-forward-declarations
3850/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003851/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3852/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003853/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3854/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3855/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003856/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003857/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003858///
3859/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003860/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003861/// '::'[opt] nested-name-specifier[opt] type-name
3862///
3863/// id-expression: [C++ 5.1]
3864/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003865/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003866///
3867/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003868/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003869/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003870/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003871/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003872/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003873///
Richard Smith5d8388c2012-03-27 01:42:32 +00003874/// Note, any additional constructs added here may need corresponding changes
3875/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003876void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003877 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003878
David Blaikie4e4d0842012-03-11 07:00:24 +00003879 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003880 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003881 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003882 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3883 D.getContext() == Declarator::MemberContext;
3884 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3885 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003886 }
3887
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003888 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003889 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003890 // Change the declaration context for name lookup, until this function
3891 // is exited (and the declarator has been parsed).
3892 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003893 }
3894
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003895 // C++0x [dcl.fct]p14:
3896 // There is a syntactic ambiguity when an ellipsis occurs at the end
3897 // of a parameter-declaration-clause without a preceding comma. In
3898 // this case, the ellipsis is parsed as part of the
3899 // abstract-declarator if the type of the parameter names a template
3900 // parameter pack that has not been expanded; otherwise, it is parsed
3901 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003902 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003903 !((D.getContext() == Declarator::PrototypeContext ||
3904 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003905 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003906 !Actions.containsUnexpandedParameterPacks(D))) {
3907 SourceLocation EllipsisLoc = ConsumeToken();
3908 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3909 // The ellipsis was put in the wrong place. Recover, and explain to
3910 // the user what they should have done.
3911 ParseDeclarator(D);
3912 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3913 return;
3914 } else
3915 D.setEllipsisLoc(EllipsisLoc);
3916
3917 // The ellipsis can't be followed by a parenthesized declarator. We
3918 // check for that in ParseParenDeclarator, after we have disambiguated
3919 // the l_paren token.
3920 }
3921
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003922 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3923 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3924 // We found something that indicates the start of an unqualified-id.
3925 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003926 bool AllowConstructorName;
3927 if (D.getDeclSpec().hasTypeSpecifier())
3928 AllowConstructorName = false;
3929 else if (D.getCXXScopeSpec().isSet())
3930 AllowConstructorName =
3931 (D.getContext() == Declarator::FileContext ||
3932 (D.getContext() == Declarator::MemberContext &&
3933 D.getDeclSpec().isFriendSpecified()));
3934 else
3935 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3936
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003937 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003938 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3939 /*EnteringContext=*/true,
3940 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003941 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003942 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003943 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003944 D.getName()) ||
3945 // Once we're past the identifier, if the scope was bad, mark the
3946 // whole declarator bad.
3947 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003948 D.SetIdentifier(0, Tok.getLocation());
3949 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003950 } else {
3951 // Parsed the unqualified-id; update range information and move along.
3952 if (D.getSourceRange().getBegin().isInvalid())
3953 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3954 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003955 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003956 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003957 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003958 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003959 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003960 "There's a C++-specific check for tok::identifier above");
3961 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3962 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3963 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003964 goto PastIdentifier;
3965 }
Richard Smith9988f282012-03-29 01:16:42 +00003966
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003967 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003968 // direct-declarator: '(' declarator ')'
3969 // direct-declarator: '(' attributes declarator ')'
3970 // Example: 'char (*X)' or 'int (*XX)(void)'
3971 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003972
3973 // If the declarator was parenthesized, we entered the declarator
3974 // scope when parsing the parenthesized declarator, then exited
3975 // the scope already. Re-enter the scope, if we need to.
3976 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003977 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003978 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003979 if (!D.isInvalidType() &&
3980 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003981 // Change the declaration context for name lookup, until this function
3982 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003983 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003984 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003985 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003986 // This could be something simple like "int" (in which case the declarator
3987 // portion is empty), if an abstract-declarator is allowed.
3988 D.SetIdentifier(0, Tok.getLocation());
3989 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003990 if (D.getContext() == Declarator::MemberContext)
3991 Diag(Tok, diag::err_expected_member_name_or_semi)
3992 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003993 else if (getLangOpts().CPlusPlus)
3994 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003995 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003996 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003997 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003998 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003999 }
Mike Stump1eb44332009-09-09 15:08:12 +00004000
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004001 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004002 assert(D.isPastIdentifier() &&
4003 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004004
Richard Smith6ee326a2012-04-10 01:32:12 +00004005 // Don't parse attributes unless we have parsed an unparenthesized name.
4006 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004007 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004008
Reid Spencer5f016e22007-07-11 17:01:13 +00004009 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004010 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004011 // Enter function-declaration scope, limiting any declarators to the
4012 // function prototype scope, including parameter declarators.
4013 ParseScope PrototypeScope(this,
4014 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004015 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4016 // In such a case, check if we actually have a function declarator; if it
4017 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004018 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004019 // When not in file scope, warn for ambiguous function declarators, just
4020 // in case the author intended it as a variable definition.
4021 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4022 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4023 break;
4024 }
John McCall0b7e6782011-03-24 11:26:52 +00004025 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004026 BalancedDelimiterTracker T(*this, tok::l_paren);
4027 T.consumeOpen();
4028 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004029 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004030 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004031 ParseBracketDeclarator(D);
4032 } else {
4033 break;
4034 }
4035 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004036}
Reid Spencer5f016e22007-07-11 17:01:13 +00004037
Chris Lattneref4715c2008-04-06 05:45:57 +00004038/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4039/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004040/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004041/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4042///
4043/// direct-declarator:
4044/// '(' declarator ')'
4045/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004046/// direct-declarator '(' parameter-type-list ')'
4047/// direct-declarator '(' identifier-list[opt] ')'
4048/// [GNU] direct-declarator '(' parameter-forward-declarations
4049/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004050///
4051void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004052 BalancedDelimiterTracker T(*this, tok::l_paren);
4053 T.consumeOpen();
4054
Chris Lattneref4715c2008-04-06 05:45:57 +00004055 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004056
Chris Lattner7399ee02008-10-20 02:05:46 +00004057 // Eat any attributes before we look at whether this is a grouping or function
4058 // declarator paren. If this is a grouping paren, the attribute applies to
4059 // the type being built up, for example:
4060 // int (__attribute__(()) *x)(long y)
4061 // If this ends up not being a grouping paren, the attribute applies to the
4062 // first argument, for example:
4063 // int (__attribute__(()) int x)
4064 // In either case, we need to eat any attributes to be able to determine what
4065 // sort of paren this is.
4066 //
John McCall0b7e6782011-03-24 11:26:52 +00004067 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004068 bool RequiresArg = false;
4069 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004070 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Chris Lattner7399ee02008-10-20 02:05:46 +00004072 // We require that the argument list (if this is a non-grouping paren) be
4073 // present even if the attribute list was empty.
4074 RequiresArg = true;
4075 }
Steve Naroff239f0732008-12-25 14:16:32 +00004076 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004077 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004078 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004079 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004080 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004081 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004082 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004083 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004084 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004085 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004086
Chris Lattneref4715c2008-04-06 05:45:57 +00004087 // If we haven't past the identifier yet (or where the identifier would be
4088 // stored, if this is an abstract declarator), then this is probably just
4089 // grouping parens. However, if this could be an abstract-declarator, then
4090 // this could also be the start of function arguments (consider 'void()').
4091 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004092
Chris Lattneref4715c2008-04-06 05:45:57 +00004093 if (!D.mayOmitIdentifier()) {
4094 // If this can't be an abstract-declarator, this *must* be a grouping
4095 // paren, because we haven't seen the identifier yet.
4096 isGrouping = true;
4097 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004098 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4099 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004100 isDeclarationSpecifier() || // 'int(int)' is a function.
4101 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004102 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4103 // considered to be a type, not a K&R identifier-list.
4104 isGrouping = false;
4105 } else {
4106 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4107 isGrouping = true;
4108 }
Mike Stump1eb44332009-09-09 15:08:12 +00004109
Chris Lattneref4715c2008-04-06 05:45:57 +00004110 // If this is a grouping paren, handle:
4111 // direct-declarator: '(' declarator ')'
4112 // direct-declarator: '(' attributes declarator ')'
4113 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004114 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4115 D.setEllipsisLoc(SourceLocation());
4116
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004117 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004118 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004119 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004120 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004121 T.consumeClose();
4122 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4123 T.getCloseLocation()),
4124 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004125
4126 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004127
4128 // An ellipsis cannot be placed outside parentheses.
4129 if (EllipsisLoc.isValid())
4130 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4131
Chris Lattneref4715c2008-04-06 05:45:57 +00004132 return;
4133 }
Mike Stump1eb44332009-09-09 15:08:12 +00004134
Chris Lattneref4715c2008-04-06 05:45:57 +00004135 // Okay, if this wasn't a grouping paren, it must be the start of a function
4136 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004137 // identifier (and remember where it would have been), then call into
4138 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004139 D.SetIdentifier(0, Tok.getLocation());
4140
David Blaikie42d6d0c2011-12-04 05:04:18 +00004141 // Enter function-declaration scope, limiting any declarators to the
4142 // function prototype scope, including parameter declarators.
4143 ParseScope PrototypeScope(this,
4144 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004145 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004146 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004147}
4148
4149/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4150/// declarator D up to a paren, which indicates that we are parsing function
4151/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004152///
Richard Smith6ee326a2012-04-10 01:32:12 +00004153/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4154/// immediately after the open paren - they should be considered to be the
4155/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004156///
Richard Smith6ee326a2012-04-10 01:32:12 +00004157/// If RequiresArg is true, then the first argument of the function is required
4158/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004159///
Richard Smith6ee326a2012-04-10 01:32:12 +00004160/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4161/// (C++11) ref-qualifier[opt], exception-specification[opt],
4162/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4163///
4164/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004165/// dynamic-exception-specification
4166/// noexcept-specification
4167///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004168void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004169 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004170 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004171 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004172 assert(getCurScope()->isFunctionPrototypeScope() &&
4173 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004174 // lparen is already consumed!
4175 assert(D.isPastIdentifier() && "Should not call before identifier!");
4176
4177 // This should be true when the function has typed arguments.
4178 // Otherwise, it is treated as a K&R-style function.
4179 bool HasProto = false;
4180 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004181 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004182 // Remember where we see an ellipsis, if any.
4183 SourceLocation EllipsisLoc;
4184
4185 DeclSpec DS(AttrFactory);
4186 bool RefQualifierIsLValueRef = true;
4187 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004188 SourceLocation ConstQualifierLoc;
4189 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004190 ExceptionSpecificationType ESpecType = EST_None;
4191 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004192 SmallVector<ParsedType, 2> DynamicExceptions;
4193 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004194 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004195 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004196 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004197
James Molloy16f1f712012-02-29 10:24:19 +00004198 Actions.ActOnStartFunctionDeclarator();
4199
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004200 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004201 if (isFunctionDeclaratorIdentifierList()) {
4202 if (RequiresArg)
4203 Diag(Tok, diag::err_argument_required_after_attribute);
4204
4205 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4206
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004207 Tracker.consumeClose();
4208 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004209 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004210 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004211 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004212 else if (RequiresArg)
4213 Diag(Tok, diag::err_argument_required_after_attribute);
4214
David Blaikie4e4d0842012-03-11 07:00:24 +00004215 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004216
4217 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004218 Tracker.consumeClose();
4219 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004220
David Blaikie4e4d0842012-03-11 07:00:24 +00004221 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004222 // FIXME: Accept these components in any order, and produce fixits to
4223 // correct the order if the user gets it wrong. Ideally we should deal
4224 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004225
4226 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004227 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4228 if (!DS.getSourceRange().getEnd().isInvalid()) {
4229 EndLoc = DS.getSourceRange().getEnd();
4230 ConstQualifierLoc = DS.getConstSpecLoc();
4231 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4232 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004233
4234 // Parse ref-qualifier[opt].
4235 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004236 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004237 diag::warn_cxx98_compat_ref_qualifier :
4238 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004239
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004240 RefQualifierIsLValueRef = Tok.is(tok::amp);
4241 RefQualifierLoc = ConsumeToken();
4242 EndLoc = RefQualifierLoc;
4243 }
4244
4245 // Parse exception-specification[opt].
4246 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4247 DynamicExceptions,
4248 DynamicExceptionRanges,
4249 NoexceptExpr);
4250 if (ESpecType != EST_None)
4251 EndLoc = ESpecRange.getEnd();
4252
Richard Smith6ee326a2012-04-10 01:32:12 +00004253 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4254 // after the exception-specification.
4255 MaybeParseCXX0XAttributes(FnAttrs);
4256
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004257 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004258 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004259 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004260 SourceRange Range;
4261 TrailingReturnType = ParseTrailingReturnType(Range).get();
4262 if (Range.getEnd().isValid())
4263 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004264 }
4265 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004266 }
4267
4268 // Remember that we parsed a function type, and remember the attributes.
4269 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4270 /*isVariadic=*/EllipsisLoc.isValid(),
4271 EllipsisLoc,
4272 ParamInfo.data(), ParamInfo.size(),
4273 DS.getTypeQualifiers(),
4274 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004275 RefQualifierLoc, ConstQualifierLoc,
4276 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004277 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004278 ESpecType, ESpecRange.getBegin(),
4279 DynamicExceptions.data(),
4280 DynamicExceptionRanges.data(),
4281 DynamicExceptions.size(),
4282 NoexceptExpr.isUsable() ?
4283 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004284 Tracker.getOpenLocation(),
4285 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004286 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004287 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004288
4289 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004290}
4291
4292/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4293/// identifier list form for a K&R-style function: void foo(a,b,c)
4294///
4295/// Note that identifier-lists are only allowed for normal declarators, not for
4296/// abstract-declarators.
4297bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004298 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004299 && Tok.is(tok::identifier)
4300 && !TryAltiVecVectorToken()
4301 // K&R identifier lists can't have typedefs as identifiers, per C99
4302 // 6.7.5.3p11.
4303 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4304 // Identifier lists follow a really simple grammar: the identifiers can
4305 // be followed *only* by a ", identifier" or ")". However, K&R
4306 // identifier lists are really rare in the brave new modern world, and
4307 // it is very common for someone to typo a type in a non-K&R style
4308 // list. If we are presented with something like: "void foo(intptr x,
4309 // float y)", we don't want to start parsing the function declarator as
4310 // though it is a K&R style declarator just because intptr is an
4311 // invalid type.
4312 //
4313 // To handle this, we check to see if the token after the first
4314 // identifier is a "," or ")". Only then do we parse it as an
4315 // identifier list.
4316 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4317}
4318
4319/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4320/// we found a K&R-style identifier list instead of a typed parameter list.
4321///
4322/// After returning, ParamInfo will hold the parsed parameters.
4323///
4324/// identifier-list: [C99 6.7.5]
4325/// identifier
4326/// identifier-list ',' identifier
4327///
4328void Parser::ParseFunctionDeclaratorIdentifierList(
4329 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004330 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004331 // If there was no identifier specified for the declarator, either we are in
4332 // an abstract-declarator, or we are in a parameter declarator which was found
4333 // to be abstract. In abstract-declarators, identifier lists are not valid:
4334 // diagnose this.
4335 if (!D.getIdentifier())
4336 Diag(Tok, diag::ext_ident_list_in_param);
4337
4338 // Maintain an efficient lookup of params we have seen so far.
4339 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4340
4341 while (1) {
4342 // If this isn't an identifier, report the error and skip until ')'.
4343 if (Tok.isNot(tok::identifier)) {
4344 Diag(Tok, diag::err_expected_ident);
4345 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4346 // Forget we parsed anything.
4347 ParamInfo.clear();
4348 return;
4349 }
4350
4351 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4352
4353 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4354 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4355 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4356
4357 // Verify that the argument identifier has not already been mentioned.
4358 if (!ParamsSoFar.insert(ParmII)) {
4359 Diag(Tok, diag::err_param_redefinition) << ParmII;
4360 } else {
4361 // Remember this identifier in ParamInfo.
4362 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4363 Tok.getLocation(),
4364 0));
4365 }
4366
4367 // Eat the identifier.
4368 ConsumeToken();
4369
4370 // The list continues if we see a comma.
4371 if (Tok.isNot(tok::comma))
4372 break;
4373 ConsumeToken();
4374 }
4375}
4376
4377/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4378/// after the opening parenthesis. This function will not parse a K&R-style
4379/// identifier list.
4380///
Richard Smith6ce48a72012-04-11 04:01:28 +00004381/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4382/// caller parsed those arguments immediately after the open paren - they should
4383/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004384///
4385/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4386/// be the location of the ellipsis, if any was parsed.
4387///
Reid Spencer5f016e22007-07-11 17:01:13 +00004388/// parameter-type-list: [C99 6.7.5]
4389/// parameter-list
4390/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004391/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004392///
4393/// parameter-list: [C99 6.7.5]
4394/// parameter-declaration
4395/// parameter-list ',' parameter-declaration
4396///
4397/// parameter-declaration: [C99 6.7.5]
4398/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004399/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004400/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004401/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004402/// declaration-specifiers abstract-declarator[opt]
4403/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004404/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004405/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004406/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004407///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004408void Parser::ParseParameterDeclarationClause(
4409 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004410 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004411 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004412 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004413
Chris Lattnerf97409f2008-04-06 06:57:35 +00004414 while (1) {
4415 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004416 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4417 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004418 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004419 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004420 }
Mike Stump1eb44332009-09-09 15:08:12 +00004421
Chris Lattnerf97409f2008-04-06 06:57:35 +00004422 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004423 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004424 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004425
Richard Smith6ce48a72012-04-11 04:01:28 +00004426 // Parse any C++11 attributes.
4427 MaybeParseCXX0XAttributes(DS.getAttributes());
4428
John McCall7f040a92010-12-24 02:08:15 +00004429 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004430 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004431 ParseMicrosoftAttributes(DS.getAttributes());
4432
4433 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004434
4435 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004436 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004437 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004438 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4439 // too much hassle.
4440 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004441
Chris Lattnere64c5492009-02-27 18:38:20 +00004442 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004443
Chris Lattnerf97409f2008-04-06 06:57:35 +00004444 // Parse the declarator. This is "PrototypeContext", because we must
4445 // accept either 'declarator' or 'abstract-declarator' here.
4446 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4447 ParseDeclarator(ParmDecl);
4448
4449 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004450 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004451
Chris Lattnerf97409f2008-04-06 06:57:35 +00004452 // Remember this parsed parameter in ParamInfo.
4453 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004454
Douglas Gregor72b505b2008-12-16 21:30:33 +00004455 // DefArgToks is used when the parsing of default arguments needs
4456 // to be delayed.
4457 CachedTokens *DefArgToks = 0;
4458
Chris Lattnerf97409f2008-04-06 06:57:35 +00004459 // If no parameter was specified, verify that *something* was specified,
4460 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004461 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4462 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004463 // Completely missing, emit error.
4464 Diag(DSStart, diag::err_missing_param);
4465 } else {
4466 // Otherwise, we have something. Add it and let semantic analysis try
4467 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004468
Chris Lattnerf97409f2008-04-06 06:57:35 +00004469 // Inform the actions module about the parameter declarator, so it gets
4470 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004471 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004472
4473 // Parse the default argument, if any. We parse the default
4474 // arguments in all dialects; the semantic analysis in
4475 // ActOnParamDefaultArgument will reject the default argument in
4476 // C.
4477 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004478 SourceLocation EqualLoc = Tok.getLocation();
4479
Chris Lattner04421082008-04-08 04:40:51 +00004480 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004481 if (D.getContext() == Declarator::MemberContext) {
4482 // If we're inside a class definition, cache the tokens
4483 // corresponding to the default argument. We'll actually parse
4484 // them when we see the end of the class definition.
4485 // FIXME: Templates will require something similar.
4486 // FIXME: Can we use a smart pointer for Toks?
4487 DefArgToks = new CachedTokens;
4488
Mike Stump1eb44332009-09-09 15:08:12 +00004489 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004490 /*StopAtSemi=*/true,
4491 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004492 delete DefArgToks;
4493 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004494 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004495 } else {
4496 // Mark the end of the default argument so that we know when to
4497 // stop when we parse it later on.
4498 Token DefArgEnd;
4499 DefArgEnd.startToken();
4500 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4501 DefArgEnd.setLocation(Tok.getLocation());
4502 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004503 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004504 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004505 }
Chris Lattner04421082008-04-08 04:40:51 +00004506 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004507 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004508 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004509
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004510 // The argument isn't actually potentially evaluated unless it is
4511 // used.
4512 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004513 Sema::PotentiallyEvaluatedIfUsed,
4514 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004515
Sebastian Redl84407ba2012-03-14 15:54:00 +00004516 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004517 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4518 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004519 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004520 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004521 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004522 if (DefArgResult.isInvalid()) {
4523 Actions.ActOnParamDefaultArgumentError(Param);
4524 SkipUntil(tok::comma, tok::r_paren, true, true);
4525 } else {
4526 // Inform the actions module about the default argument
4527 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004528 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004529 }
Chris Lattner04421082008-04-08 04:40:51 +00004530 }
4531 }
Mike Stump1eb44332009-09-09 15:08:12 +00004532
4533 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4534 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004535 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004536 }
4537
4538 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004539 if (Tok.isNot(tok::comma)) {
4540 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004541 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4542
David Blaikie4e4d0842012-03-11 07:00:24 +00004543 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004544 // We have ellipsis without a preceding ',', which is ill-formed
4545 // in C. Complain and provide the fix.
4546 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004547 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004548 }
4549 }
4550
4551 break;
4552 }
Mike Stump1eb44332009-09-09 15:08:12 +00004553
Chris Lattnerf97409f2008-04-06 06:57:35 +00004554 // Consume the comma.
4555 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004556 }
Mike Stump1eb44332009-09-09 15:08:12 +00004557
Chris Lattner66d28652008-04-06 06:34:08 +00004558}
Chris Lattneref4715c2008-04-06 05:45:57 +00004559
Reid Spencer5f016e22007-07-11 17:01:13 +00004560/// [C90] direct-declarator '[' constant-expression[opt] ']'
4561/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4562/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4563/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4564/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004565/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4566/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004567void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004568 if (CheckProhibitedCXX11Attribute())
4569 return;
4570
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004571 BalancedDelimiterTracker T(*this, tok::l_square);
4572 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004573
Chris Lattner378c7e42008-12-18 07:27:21 +00004574 // C array syntax has many features, but by-far the most common is [] and [4].
4575 // This code does a fast path to handle some of the most obvious cases.
4576 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004577 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004578 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004579 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004580
Chris Lattner378c7e42008-12-18 07:27:21 +00004581 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004582 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004583 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004584 T.getOpenLocation(),
4585 T.getCloseLocation()),
4586 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004587 return;
4588 } else if (Tok.getKind() == tok::numeric_constant &&
4589 GetLookAheadToken(1).is(tok::r_square)) {
4590 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004591 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004592 ConsumeToken();
4593
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004594 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004595 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004596 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004597
Chris Lattner378c7e42008-12-18 07:27:21 +00004598 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004599 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004600 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004601 T.getOpenLocation(),
4602 T.getCloseLocation()),
4603 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004604 return;
4605 }
Mike Stump1eb44332009-09-09 15:08:12 +00004606
Reid Spencer5f016e22007-07-11 17:01:13 +00004607 // If valid, this location is the position where we read the 'static' keyword.
4608 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004609 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004610 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004611
Reid Spencer5f016e22007-07-11 17:01:13 +00004612 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004613 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004614 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004615 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004616
Reid Spencer5f016e22007-07-11 17:01:13 +00004617 // If we haven't already read 'static', check to see if there is one after the
4618 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004619 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004620 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004621
Reid Spencer5f016e22007-07-11 17:01:13 +00004622 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4623 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004624 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004625
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004626 // Handle the case where we have '[*]' as the array size. However, a leading
4627 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4628 // the the token after the star is a ']'. Since stars in arrays are
4629 // infrequent, use of lookahead is not costly here.
4630 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004631 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004632
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004633 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004634 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004635 StaticLoc = SourceLocation(); // Drop the static.
4636 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004637 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004638 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004639 // Note, in C89, this production uses the constant-expr production instead
4640 // of assignment-expr. The only difference is that assignment-expr allows
4641 // things like '=' and '*='. Sema rejects these in C89 mode because they
4642 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004643
Douglas Gregore0762c92009-06-19 23:52:42 +00004644 // Parse the constant-expression or assignment-expression now (depending
4645 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004646 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004647 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004648 } else {
4649 EnterExpressionEvaluationContext Unevaluated(Actions,
4650 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004651 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004652 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004653 }
Mike Stump1eb44332009-09-09 15:08:12 +00004654
Reid Spencer5f016e22007-07-11 17:01:13 +00004655 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004656 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004657 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004658 // If the expression was invalid, skip it.
4659 SkipUntil(tok::r_square);
4660 return;
4661 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004662
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004663 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004664
John McCall0b7e6782011-03-24 11:26:52 +00004665 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004666 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004667
Chris Lattner378c7e42008-12-18 07:27:21 +00004668 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004669 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004670 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004671 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004672 T.getOpenLocation(),
4673 T.getCloseLocation()),
4674 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004675}
4676
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004677/// [GNU] typeof-specifier:
4678/// typeof ( expressions )
4679/// typeof ( type-name )
4680/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004681///
4682void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004683 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004684 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004685 SourceLocation StartLoc = ConsumeToken();
4686
John McCallcfb708c2010-01-13 20:03:27 +00004687 const bool hasParens = Tok.is(tok::l_paren);
4688
Eli Friedman71b8fb52012-01-21 01:01:51 +00004689 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4690
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004691 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004692 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004693 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004694 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4695 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004696 if (hasParens)
4697 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004698
4699 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004700 // FIXME: Not accurate, the range gets one token more than it should.
4701 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004702 else
4703 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004704
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004705 if (isCastExpr) {
4706 if (!CastTy) {
4707 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004708 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004709 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004710
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004711 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004712 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004713 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4714 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004715 DiagID, CastTy))
4716 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004717 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004718 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004719
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004720 // If we get here, the operand to the typeof was an expresion.
4721 if (Operand.isInvalid()) {
4722 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004723 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004724 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004725
Eli Friedman71b8fb52012-01-21 01:01:51 +00004726 // We might need to transform the operand if it is potentially evaluated.
4727 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4728 if (Operand.isInvalid()) {
4729 DS.SetTypeSpecError();
4730 return;
4731 }
4732
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004733 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004734 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004735 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4736 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004737 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004738 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004739}
Chris Lattner1b492422010-02-28 18:33:55 +00004740
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004741/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004742/// _Atomic ( type-name )
4743///
4744void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4745 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4746
4747 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004748 BalancedDelimiterTracker T(*this, tok::l_paren);
4749 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004750 SkipUntil(tok::r_paren);
4751 return;
4752 }
4753
4754 TypeResult Result = ParseTypeName();
4755 if (Result.isInvalid()) {
4756 SkipUntil(tok::r_paren);
4757 return;
4758 }
4759
4760 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004761 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004762
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004763 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004764 return;
4765
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004766 DS.setTypeofParensRange(T.getRange());
4767 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004768
4769 const char *PrevSpec = 0;
4770 unsigned DiagID;
4771 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4772 DiagID, Result.release()))
4773 Diag(StartLoc, DiagID) << PrevSpec;
4774}
4775
Chris Lattner1b492422010-02-28 18:33:55 +00004776
4777/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4778/// from TryAltiVecVectorToken.
4779bool Parser::TryAltiVecVectorTokenOutOfLine() {
4780 Token Next = NextToken();
4781 switch (Next.getKind()) {
4782 default: return false;
4783 case tok::kw_short:
4784 case tok::kw_long:
4785 case tok::kw_signed:
4786 case tok::kw_unsigned:
4787 case tok::kw_void:
4788 case tok::kw_char:
4789 case tok::kw_int:
4790 case tok::kw_float:
4791 case tok::kw_double:
4792 case tok::kw_bool:
4793 case tok::kw___pixel:
4794 Tok.setKind(tok::kw___vector);
4795 return true;
4796 case tok::identifier:
4797 if (Next.getIdentifierInfo() == Ident_pixel) {
4798 Tok.setKind(tok::kw___vector);
4799 return true;
4800 }
4801 return false;
4802 }
4803}
4804
4805bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4806 const char *&PrevSpec, unsigned &DiagID,
4807 bool &isInvalid) {
4808 if (Tok.getIdentifierInfo() == Ident_vector) {
4809 Token Next = NextToken();
4810 switch (Next.getKind()) {
4811 case tok::kw_short:
4812 case tok::kw_long:
4813 case tok::kw_signed:
4814 case tok::kw_unsigned:
4815 case tok::kw_void:
4816 case tok::kw_char:
4817 case tok::kw_int:
4818 case tok::kw_float:
4819 case tok::kw_double:
4820 case tok::kw_bool:
4821 case tok::kw___pixel:
4822 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4823 return true;
4824 case tok::identifier:
4825 if (Next.getIdentifierInfo() == Ident_pixel) {
4826 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4827 return true;
4828 }
4829 break;
4830 default:
4831 break;
4832 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004833 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004834 DS.isTypeAltiVecVector()) {
4835 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4836 return true;
4837 }
4838 return false;
4839}