blob: cf3dca20d9013734ac6277cc6d10c89d343d2969 [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);
759 }
760 LAs.clear();
761}
762
763
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000764/// \brief Finish parsing an attribute for which parsing was delayed.
765/// This will be called at the end of parsing a class declaration
766/// for each LateParsedAttribute. We consume the saved tokens and
767/// create an attribute with the arguments filled in. We add this
768/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000769void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
770 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771 // Save the current token position.
772 SourceLocation OrigLoc = Tok.getLocation();
773
774 // Append the current token at the end of the new token stream so that it
775 // doesn't get lost.
776 LA.Toks.push_back(Tok);
777 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
778 // Consume the previously pushed token.
779 ConsumeAnyToken();
780
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000781 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
782 Diag(Tok, diag::warn_attribute_on_function_definition)
783 << LA.AttrName.getName();
784 }
785
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000786 ParsedAttributes Attrs(AttrFactory);
787 SourceLocation endLoc;
788
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000789 if (LA.Decls.size() == 1) {
790 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000791
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000792 // If the Decl is templatized, add template parameters to scope.
793 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
794 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
795 if (HasTemplateScope)
796 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000797
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000798 // If the Decl is on a function, add function parameters to the scope.
799 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
800 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
801 if (HasFunctionScope)
802 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
803
804 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
805
806 if (HasFunctionScope) {
807 Actions.ActOnExitFunctionContext();
808 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
809 }
810 if (HasTemplateScope) {
811 TempScope.Exit();
812 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000813 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000814 // If there are multiple decls, then the decl cannot be within the
815 // function scope.
816 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000817 } else {
818 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000819 }
820
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
822 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
823 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000824
825 if (Tok.getLocation() != OrigLoc) {
826 // Due to a parsing error, we either went over the cached tokens or
827 // there are still cached tokens left, so we skip the leftover tokens.
828 // Since this is an uncommon situation that should be avoided, use the
829 // expensive isBeforeInTranslationUnit call.
830 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
831 OrigLoc))
832 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000833 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000834 }
835}
836
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000837/// \brief Wrapper around a case statement checking if AttrName is
838/// one of the thread safety attributes
839bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
840 return llvm::StringSwitch<bool>(AttrName)
841 .Case("guarded_by", true)
842 .Case("guarded_var", true)
843 .Case("pt_guarded_by", true)
844 .Case("pt_guarded_var", true)
845 .Case("lockable", true)
846 .Case("scoped_lockable", true)
847 .Case("no_thread_safety_analysis", true)
848 .Case("acquired_after", true)
849 .Case("acquired_before", true)
850 .Case("exclusive_lock_function", true)
851 .Case("shared_lock_function", true)
852 .Case("exclusive_trylock_function", true)
853 .Case("shared_trylock_function", true)
854 .Case("unlock_function", true)
855 .Case("lock_returned", true)
856 .Case("locks_excluded", true)
857 .Case("exclusive_locks_required", true)
858 .Case("shared_locks_required", true)
859 .Default(false);
860}
861
862/// \brief Parse the contents of thread safety attributes. These
863/// should always be parsed as an expression list.
864///
865/// We need to special case the parsing due to the fact that if the first token
866/// of the first argument is an identifier, the main parse loop will store
867/// that token as a "parameter" and the rest of
868/// the arguments will be added to a list of "arguments". However,
869/// subsequent tokens in the first argument are lost. We instead parse each
870/// argument as an expression and add all arguments to the list of "arguments".
871/// In future, we will take advantage of this special case to also
872/// deal with some argument scoping issues here (for example, referring to a
873/// function parameter in the attribute on that function).
874void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
875 SourceLocation AttrNameLoc,
876 ParsedAttributes &Attrs,
877 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000878 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000880 BalancedDelimiterTracker T(*this, tok::l_paren);
881 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882
883 ExprVector ArgExprs(Actions);
884 bool ArgExprsOk = true;
885
886 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000887 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888 ExprResult ArgExpr(ParseAssignmentExpression());
889 if (ArgExpr.isInvalid()) {
890 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000891 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000892 break;
893 } else {
894 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000896 if (Tok.isNot(tok::comma))
897 break;
898 ConsumeToken(); // Eat the comma, move to the next argument
899 }
900 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000901 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000902 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
903 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000905 if (EndLoc)
906 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000907}
908
Richard Smith6ee326a2012-04-10 01:32:12 +0000909/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
910/// of a C++11 attribute-specifier in a location where an attribute is not
911/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
912/// situation.
913///
914/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
915/// this doesn't appear to actually be an attribute-specifier, and the caller
916/// should try to parse it.
917bool Parser::DiagnoseProhibitedCXX11Attribute() {
918 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
919
920 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
921 case CAK_NotAttributeSpecifier:
922 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
923 return false;
924
925 case CAK_InvalidAttributeSpecifier:
926 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
927 return false;
928
929 case CAK_AttributeSpecifier:
930 // Parse and discard the attributes.
931 SourceLocation BeginLoc = ConsumeBracket();
932 ConsumeBracket();
933 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
934 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
935 SourceLocation EndLoc = ConsumeBracket();
936 Diag(BeginLoc, diag::err_attributes_not_allowed)
937 << SourceRange(BeginLoc, EndLoc);
938 return true;
939 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000940 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000941}
942
John McCall7f040a92010-12-24 02:08:15 +0000943void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
944 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
945 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000946}
947
Reid Spencer5f016e22007-07-11 17:01:13 +0000948/// ParseDeclaration - Parse a full 'declaration', which consists of
949/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000950/// 'Context' should be a Declarator::TheContext value. This returns the
951/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000952///
953/// declaration: [C99 6.7]
954/// block-declaration ->
955/// simple-declaration
956/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000957/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000958/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000959/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000960/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000961/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000962/// others... [FIXME]
963///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000964Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
965 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000966 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000967 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000968 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000969 // Must temporarily exit the objective-c container scope for
970 // parsing c none objective-c decls.
971 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000972
John McCalld226f652010-08-21 09:40:31 +0000973 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000974 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000975 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000976 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000977 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000978 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000979 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000980 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000981 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000982 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000983 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000984 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000985 SourceLocation InlineLoc = ConsumeToken();
986 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
987 break;
988 }
John McCall7f040a92010-12-24 02:08:15 +0000989 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000990 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000991 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000992 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000993 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000994 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000995 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000996 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000997 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000998 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000999 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001000 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001001 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001002 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001003 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001004 default:
John McCall7f040a92010-12-24 02:08:15 +00001005 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001006 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001007
Chris Lattner682bf922009-03-29 16:50:03 +00001008 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001009 // single decl, convert it now. Alias declarations can also declare a type;
1010 // include that too if it is present.
1011 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001012}
1013
1014/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1015/// declaration-specifiers init-declarator-list[opt] ';'
1016///[C90/C++]init-declarator-list ';' [TODO]
1017/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001018///
Richard Smithad762fc2011-04-14 22:09:26 +00001019/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1020/// attribute-specifier-seq[opt] type-specifier-seq declarator
1021///
Chris Lattnercd147752009-03-29 17:27:48 +00001022/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001023/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001024///
1025/// If FRI is non-null, we might be parsing a for-range-declaration instead
1026/// of a simple-declaration. If we find that we are, we also parse the
1027/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001028Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1029 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001030 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001031 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001032 bool RequireSemi,
1033 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001035 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001036 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001037
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001038 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001039 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001040
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1042 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001043 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001044 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001045 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001046 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001047 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001048 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001050
1051 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001052}
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Richard Smith0706df42011-10-19 21:33:05 +00001054/// Returns true if this might be the start of a declarator, or a common typo
1055/// for a declarator.
1056bool Parser::MightBeDeclarator(unsigned Context) {
1057 switch (Tok.getKind()) {
1058 case tok::annot_cxxscope:
1059 case tok::annot_template_id:
1060 case tok::caret:
1061 case tok::code_completion:
1062 case tok::coloncolon:
1063 case tok::ellipsis:
1064 case tok::kw___attribute:
1065 case tok::kw_operator:
1066 case tok::l_paren:
1067 case tok::star:
1068 return true;
1069
1070 case tok::amp:
1071 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001072 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001073
Richard Smith1c94c162012-01-09 22:31:44 +00001074 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001075 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001076 NextToken().is(tok::l_square);
1077
1078 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001079 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001080
Richard Smith0706df42011-10-19 21:33:05 +00001081 case tok::identifier:
1082 switch (NextToken().getKind()) {
1083 case tok::code_completion:
1084 case tok::coloncolon:
1085 case tok::comma:
1086 case tok::equal:
1087 case tok::equalequal: // Might be a typo for '='.
1088 case tok::kw_alignas:
1089 case tok::kw_asm:
1090 case tok::kw___attribute:
1091 case tok::l_brace:
1092 case tok::l_paren:
1093 case tok::l_square:
1094 case tok::less:
1095 case tok::r_brace:
1096 case tok::r_paren:
1097 case tok::r_square:
1098 case tok::semi:
1099 return true;
1100
1101 case tok::colon:
1102 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001103 // and in block scope it's probably a label. Inside a class definition,
1104 // this is a bit-field.
1105 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001106 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001107
1108 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001109 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001110
1111 default:
1112 return false;
1113 }
1114
1115 default:
1116 return false;
1117 }
1118}
1119
Richard Smith994d73f2012-04-11 20:59:20 +00001120/// Skip until we reach something which seems like a sensible place to pick
1121/// up parsing after a malformed declaration. This will sometimes stop sooner
1122/// than SkipUntil(tok::r_brace) would, but will never stop later.
1123void Parser::SkipMalformedDecl() {
1124 while (true) {
1125 switch (Tok.getKind()) {
1126 case tok::l_brace:
1127 // Skip until matching }, then stop. We've probably skipped over
1128 // a malformed class or function definition or similar.
1129 ConsumeBrace();
1130 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1131 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1132 // This declaration isn't over yet. Keep skipping.
1133 continue;
1134 }
1135 if (Tok.is(tok::semi))
1136 ConsumeToken();
1137 return;
1138
1139 case tok::l_square:
1140 ConsumeBracket();
1141 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1142 continue;
1143
1144 case tok::l_paren:
1145 ConsumeParen();
1146 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1147 continue;
1148
1149 case tok::r_brace:
1150 return;
1151
1152 case tok::semi:
1153 ConsumeToken();
1154 return;
1155
1156 case tok::kw_inline:
1157 // 'inline namespace' at the start of a line is almost certainly
1158 // a good place to pick back up parsing.
1159 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1160 return;
1161 break;
1162
1163 case tok::kw_namespace:
1164 // 'namespace' at the start of a line is almost certainly a good
1165 // place to pick back up parsing.
1166 if (Tok.isAtStartOfLine())
1167 return;
1168 break;
1169
1170 case tok::eof:
1171 return;
1172
1173 default:
1174 break;
1175 }
1176
1177 ConsumeAnyToken();
1178 }
1179}
1180
John McCalld8ac0572009-11-03 19:26:08 +00001181/// ParseDeclGroup - Having concluded that this is either a function
1182/// definition or a group of object declarations, actually parse the
1183/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001184Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1185 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001186 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001187 SourceLocation *DeclEnd,
1188 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001189 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001190 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001191 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001192
John McCalld8ac0572009-11-03 19:26:08 +00001193 // Bail out if the first declarator didn't seem well-formed.
1194 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001195 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001196 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001199 // Save late-parsed attributes for now; they need to be parsed in the
1200 // appropriate function scope after the function Decl has been constructed.
1201 LateParsedAttrList LateParsedAttrs;
1202 if (D.isFunctionDeclarator())
1203 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1204
Chris Lattnerc82daef2010-07-11 22:24:20 +00001205 // Check to see if we have a function *definition* which must have a body.
1206 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1207 // Look at the next token to make sure that this isn't a function
1208 // declaration. We have to check this because __attribute__ might be the
1209 // start of a function definition in GCC-extended K&R C.
1210 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001211
Chris Lattner004659a2010-07-11 22:42:07 +00001212 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001213 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1214 Diag(Tok, diag::err_function_declared_typedef);
1215
1216 // Recover by treating the 'typedef' as spurious.
1217 DS.ClearStorageClassSpecs();
1218 }
1219
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001220 Decl *TheDecl =
1221 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001222 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001223 }
1224
1225 if (isDeclarationSpecifier()) {
1226 // If there is an invalid declaration specifier right after the function
1227 // prototype, then we must be in a missing semicolon case where this isn't
1228 // actually a body. Just fall through into the code that handles it as a
1229 // prototype, and let the top-level code handle the erroneous declspec
1230 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001231 } else {
1232 Diag(Tok, diag::err_expected_fn_body);
1233 SkipUntil(tok::semi);
1234 return DeclGroupPtrTy();
1235 }
1236 }
1237
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001238 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001239 return DeclGroupPtrTy();
1240
1241 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1242 // must parse and analyze the for-range-initializer before the declaration is
1243 // analyzed.
1244 if (FRI && Tok.is(tok::colon)) {
1245 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001246 if (Tok.is(tok::l_brace))
1247 FRI->RangeExpr = ParseBraceInitializer();
1248 else
1249 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001250 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1251 Actions.ActOnCXXForRangeDecl(ThisDecl);
1252 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001253 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001254 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1255 }
1256
Chris Lattner5f9e2722011-07-23 10:55:15 +00001257 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001258 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001259 if (LateParsedAttrs.size() > 0)
1260 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001261 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001262 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001263 DeclsInGroup.push_back(FirstDecl);
1264
Richard Smith0706df42011-10-19 21:33:05 +00001265 bool ExpectSemi = Context != Declarator::ForContext;
1266
John McCalld8ac0572009-11-03 19:26:08 +00001267 // If we don't have a comma, it is either the end of the list (a ';') or an
1268 // error, bail out.
1269 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001270 SourceLocation CommaLoc = ConsumeToken();
1271
1272 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1273 // This comma was followed by a line-break and something which can't be
1274 // the start of a declarator. The comma was probably a typo for a
1275 // semicolon.
1276 Diag(CommaLoc, diag::err_expected_semi_declaration)
1277 << FixItHint::CreateReplacement(CommaLoc, ";");
1278 ExpectSemi = false;
1279 break;
1280 }
John McCalld8ac0572009-11-03 19:26:08 +00001281
1282 // Parse the next declarator.
1283 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001284 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001285
1286 // Accept attributes in an init-declarator. In the first declarator in a
1287 // declaration, these would be part of the declspec. In subsequent
1288 // declarators, they become part of the declarator itself, so that they
1289 // don't apply to declarators after *this* one. Examples:
1290 // short __attribute__((common)) var; -> declspec
1291 // short var __attribute__((common)); -> declarator
1292 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001293 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001294
1295 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001296 if (!D.isInvalidType()) {
1297 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1298 D.complete(ThisDecl);
1299 if (ThisDecl)
1300 DeclsInGroup.push_back(ThisDecl);
1301 }
John McCalld8ac0572009-11-03 19:26:08 +00001302 }
1303
1304 if (DeclEnd)
1305 *DeclEnd = Tok.getLocation();
1306
Richard Smith0706df42011-10-19 21:33:05 +00001307 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001308 ExpectAndConsume(tok::semi,
1309 Context == Declarator::FileContext
1310 ? diag::err_invalid_token_after_toplevel_declarator
1311 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001312 // Okay, there was no semicolon and one was expected. If we see a
1313 // declaration specifier, just assume it was missing and continue parsing.
1314 // Otherwise things are very confused and we skip to recover.
1315 if (!isDeclarationSpecifier()) {
1316 SkipUntil(tok::r_brace, true, true);
1317 if (Tok.is(tok::semi))
1318 ConsumeToken();
1319 }
John McCalld8ac0572009-11-03 19:26:08 +00001320 }
1321
Douglas Gregor23c94db2010-07-02 17:43:08 +00001322 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001323 DeclsInGroup.data(),
1324 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001325}
1326
Richard Smithad762fc2011-04-14 22:09:26 +00001327/// Parse an optional simple-asm-expr and attributes, and attach them to a
1328/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001329bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001330 // If a simple-asm-expr is present, parse it.
1331 if (Tok.is(tok::kw_asm)) {
1332 SourceLocation Loc;
1333 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1334 if (AsmLabel.isInvalid()) {
1335 SkipUntil(tok::semi, true, true);
1336 return true;
1337 }
1338
1339 D.setAsmLabel(AsmLabel.release());
1340 D.SetRangeEnd(Loc);
1341 }
1342
1343 MaybeParseGNUAttributes(D);
1344 return false;
1345}
1346
Douglas Gregor1426e532009-05-12 21:31:51 +00001347/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1348/// declarator'. This method parses the remainder of the declaration
1349/// (including any attributes or initializer, among other things) and
1350/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001351///
Reid Spencer5f016e22007-07-11 17:01:13 +00001352/// init-declarator: [C99 6.7]
1353/// declarator
1354/// declarator '=' initializer
1355/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1356/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001357/// [C++] declarator initializer[opt]
1358///
1359/// [C++] initializer:
1360/// [C++] '=' initializer-clause
1361/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001362/// [C++0x] '=' 'default' [TODO]
1363/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001364/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001365///
1366/// According to the standard grammar, =default and =delete are function
1367/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001368///
John McCalld226f652010-08-21 09:40:31 +00001369Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001370 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001371 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001372 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Richard Smithad762fc2011-04-14 22:09:26 +00001374 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1375}
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Richard Smithad762fc2011-04-14 22:09:26 +00001377Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1378 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001379 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001380 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001381 switch (TemplateInfo.Kind) {
1382 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001383 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001384 break;
1385
1386 case ParsedTemplateInfo::Template:
1387 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001388 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001389 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001390 TemplateInfo.TemplateParams->data(),
1391 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001392 D);
1393 break;
1394
1395 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001396 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001397 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001398 TemplateInfo.ExternLoc,
1399 TemplateInfo.TemplateLoc,
1400 D);
1401 if (ThisRes.isInvalid()) {
1402 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001403 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001404 }
1405
1406 ThisDecl = ThisRes.get();
1407 break;
1408 }
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Richard Smith34b41d92011-02-20 03:19:35 +00001411 bool TypeContainsAuto =
1412 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1413
Douglas Gregor1426e532009-05-12 21:31:51 +00001414 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001415 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001416 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001417 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001418 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001419 if (D.isFunctionDeclarator())
1420 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1421 << 1 /* delete */;
1422 else
1423 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001424 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001425 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001426 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1427 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001428 else
1429 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001430 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001431 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001432 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001433 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001434 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001435
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001436 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001437 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001438 cutOffParsing();
1439 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001440 }
1441
John McCall60d7b3a2010-08-24 06:29:42 +00001442 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001443
David Blaikie4e4d0842012-03-11 07:00:24 +00001444 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001445 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001446 ExitScope();
1447 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001448
Douglas Gregor1426e532009-05-12 21:31:51 +00001449 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001450 SkipUntil(tok::comma, true, true);
1451 Actions.ActOnInitializerError(ThisDecl);
1452 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001453 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1454 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001455 }
1456 } else if (Tok.is(tok::l_paren)) {
1457 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001458 BalancedDelimiterTracker T(*this, tok::l_paren);
1459 T.consumeOpen();
1460
Douglas Gregor1426e532009-05-12 21:31:51 +00001461 ExprVector Exprs(Actions);
1462 CommaLocsTy CommaLocs;
1463
David Blaikie4e4d0842012-03-11 07:00:24 +00001464 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001465 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001466 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001467 }
1468
Douglas Gregor1426e532009-05-12 21:31:51 +00001469 if (ParseExpressionList(Exprs, CommaLocs)) {
1470 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001471
David Blaikie4e4d0842012-03-11 07:00:24 +00001472 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001474 ExitScope();
1475 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001476 } else {
1477 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001478 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001479
1480 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1481 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001482
David Blaikie4e4d0842012-03-11 07:00:24 +00001483 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001484 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001485 ExitScope();
1486 }
1487
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001488 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1489 T.getCloseLocation(),
1490 move_arg(Exprs));
1491 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1492 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001493 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001494 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001495 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001496 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1497
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001498 if (D.getCXXScopeSpec().isSet()) {
1499 EnterScope(0);
1500 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1501 }
1502
1503 ExprResult Init(ParseBraceInitializer());
1504
1505 if (D.getCXXScopeSpec().isSet()) {
1506 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1507 ExitScope();
1508 }
1509
1510 if (Init.isInvalid()) {
1511 Actions.ActOnInitializerError(ThisDecl);
1512 } else
1513 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1514 /*DirectInit=*/true, TypeContainsAuto);
1515
Douglas Gregor1426e532009-05-12 21:31:51 +00001516 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001517 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001518 }
1519
Richard Smith483b9f32011-02-21 20:05:19 +00001520 Actions.FinalizeDeclaration(ThisDecl);
1521
Douglas Gregor1426e532009-05-12 21:31:51 +00001522 return ThisDecl;
1523}
1524
Reid Spencer5f016e22007-07-11 17:01:13 +00001525/// ParseSpecifierQualifierList
1526/// specifier-qualifier-list:
1527/// type-specifier specifier-qualifier-list[opt]
1528/// type-qualifier specifier-qualifier-list[opt]
1529/// [GNU] attributes specifier-qualifier-list[opt]
1530///
Richard Smith69730c12012-03-12 07:56:15 +00001531void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1532 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1534 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001535 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001536 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 // Validate declspec for type-name.
1539 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001540 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1541 Diag(Tok, diag::err_expected_type);
1542 DS.SetTypeSpecError();
1543 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1544 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001546 if (!DS.hasTypeSpecifier())
1547 DS.SetTypeSpecError();
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 // Issue diagnostic and remove storage class if present.
1551 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1552 if (DS.getStorageClassSpecLoc().isValid())
1553 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1554 else
1555 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1556 DS.ClearStorageClassSpecs();
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // Issue diagnostic and remove function specfier if present.
1560 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001561 if (DS.isInlineSpecified())
1562 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1563 if (DS.isVirtualSpecified())
1564 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1565 if (DS.isExplicitSpecified())
1566 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 DS.ClearFunctionSpecs();
1568 }
Richard Smith69730c12012-03-12 07:56:15 +00001569
1570 // Issue diagnostic and remove constexpr specfier if present.
1571 if (DS.isConstexprSpecified()) {
1572 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1573 DS.ClearConstexprSpec();
1574 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001575}
1576
Chris Lattnerc199ab32009-04-12 20:42:31 +00001577/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1578/// specified token is valid after the identifier in a declarator which
1579/// immediately follows the declspec. For example, these things are valid:
1580///
1581/// int x [ 4]; // direct-declarator
1582/// int x ( int y); // direct-declarator
1583/// int(int x ) // direct-declarator
1584/// int x ; // simple-declaration
1585/// int x = 17; // init-declarator-list
1586/// int x , y; // init-declarator-list
1587/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001588/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001589/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001590///
1591/// This is not, because 'x' does not immediately follow the declspec (though
1592/// ')' happens to be valid anyway).
1593/// int (x)
1594///
1595static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1596 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1597 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001598 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001599}
1600
Chris Lattnere40c2952009-04-14 21:34:55 +00001601
1602/// ParseImplicitInt - This method is called when we have an non-typename
1603/// identifier in a declspec (which normally terminates the decl spec) when
1604/// the declspec has no type specifier. In this case, the declspec is either
1605/// malformed or is "implicit int" (in K&R and C89).
1606///
1607/// This method handles diagnosing this prettily and returns false if the
1608/// declspec is done being processed. If it recovers and thinks there may be
1609/// other pieces of declspec after it, it returns true.
1610///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001611bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001612 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001613 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001614 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Chris Lattnere40c2952009-04-14 21:34:55 +00001616 SourceLocation Loc = Tok.getLocation();
1617 // If we see an identifier that is not a type name, we normally would
1618 // parse it as the identifer being declared. However, when a typename
1619 // is typo'd or the definition is not included, this will incorrectly
1620 // parse the typename as the identifier name and fall over misparsing
1621 // later parts of the diagnostic.
1622 //
1623 // As such, we try to do some look-ahead in cases where this would
1624 // otherwise be an "implicit-int" case to see if this is invalid. For
1625 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1626 // an identifier with implicit int, we'd get a parse error because the
1627 // next token is obviously invalid for a type. Parse these as a case
1628 // with an invalid type specifier.
1629 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Chris Lattnere40c2952009-04-14 21:34:55 +00001631 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001632 // error, do lookahead to try to do better recovery. This never applies within
1633 // a type specifier.
1634 // FIXME: Don't bail out here in languages with no implicit int (like
1635 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1636 // type or typo than a use of implicit int.
1637 if (DSC != DSC_type_specifier &&
1638 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001639 // If this token is valid for implicit int, e.g. "static x = 4", then
1640 // we just avoid eating the identifier, so it will be parsed as the
1641 // identifier in the declarator.
1642 return false;
1643 }
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Chris Lattnere40c2952009-04-14 21:34:55 +00001645 // Otherwise, if we don't consume this token, we are going to emit an
1646 // error anyway. Try to recover from various common problems. Check
1647 // to see if this was a reference to a tag name without a tag specified.
1648 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001649 //
1650 // C++ doesn't need this, and isTagName doesn't take SS.
1651 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001652 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001653 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Douglas Gregor23c94db2010-07-02 17:43:08 +00001655 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001656 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001657 case DeclSpec::TST_enum:
1658 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1659 case DeclSpec::TST_union:
1660 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1661 case DeclSpec::TST_struct:
1662 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1663 case DeclSpec::TST_class:
1664 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Chris Lattnerf4382f52009-04-14 22:17:06 +00001667 if (TagName) {
1668 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001669 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001670 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Chris Lattnerf4382f52009-04-14 22:17:06 +00001672 // Parse this as a tag as if the missing tag were present.
1673 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001674 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001675 else
Richard Smith69730c12012-03-12 07:56:15 +00001676 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1677 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001678 return true;
1679 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregora786fdb2009-10-13 23:27:22 +00001682 // This is almost certainly an invalid type name. Let the action emit a
1683 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001684 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001685 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001686 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001687 // The action emitted a diagnostic, so we don't have to.
1688 if (T) {
1689 // The action has suggested that the type T could be used. Set that as
1690 // the type in the declaration specifiers, consume the would-be type
1691 // name token, and we're done.
1692 const char *PrevSpec;
1693 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001694 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001695 DS.SetRangeEnd(Tok.getLocation());
1696 ConsumeToken();
1697
1698 // There may be other declaration specifiers after this.
1699 return true;
1700 }
1701
1702 // Fall through; the action had no suggestion for us.
1703 } else {
1704 // The action did not emit a diagnostic, so emit one now.
1705 SourceRange R;
1706 if (SS) R = SS->getRange();
1707 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Douglas Gregora786fdb2009-10-13 23:27:22 +00001710 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001711 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001712 DS.SetRangeEnd(Tok.getLocation());
1713 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Chris Lattnere40c2952009-04-14 21:34:55 +00001715 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1716 // avoid rippling error messages on subsequent uses of the same type,
1717 // could be useful if #include was forgotten.
1718 return false;
1719}
1720
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001721/// \brief Determine the declaration specifier context from the declarator
1722/// context.
1723///
1724/// \param Context the declarator context, which is one of the
1725/// Declarator::TheContext enumerator values.
1726Parser::DeclSpecContext
1727Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1728 if (Context == Declarator::MemberContext)
1729 return DSC_class;
1730 if (Context == Declarator::FileContext)
1731 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001732 if (Context == Declarator::TrailingReturnContext)
1733 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001734 return DSC_normal;
1735}
1736
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001737/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1738///
1739/// FIXME: Simply returns an alignof() expression if the argument is a
1740/// type. Ideally, the type should be propagated directly into Sema.
1741///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001742/// [C11] type-id
1743/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001744/// [C++0x] type-id ...[opt]
1745/// [C++0x] assignment-expression ...[opt]
1746ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1747 SourceLocation &EllipsisLoc) {
1748 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001749 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001750 SourceLocation TypeLoc = Tok.getLocation();
1751 ParsedType Ty = ParseTypeName().get();
1752 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001753 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1754 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001755 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001756 ER = ParseConstantExpression();
1757
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001759 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001760
1761 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001762}
1763
1764/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1765/// attribute to Attrs.
1766///
1767/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001768/// [C11] '_Alignas' '(' type-id ')'
1769/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001770/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1771/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001772void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1773 SourceLocation *endLoc) {
1774 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1775 "Not an alignment-specifier!");
1776
1777 SourceLocation KWLoc = Tok.getLocation();
1778 ConsumeToken();
1779
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001780 BalancedDelimiterTracker T(*this, tok::l_paren);
1781 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001782 return;
1783
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001784 SourceLocation EllipsisLoc;
1785 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001786 if (ArgExpr.isInvalid()) {
1787 SkipUntil(tok::r_paren);
1788 return;
1789 }
1790
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001791 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001792 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001793 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001794
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001795 // FIXME: Handle pack-expansions here.
1796 if (EllipsisLoc.isValid()) {
1797 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1798 return;
1799 }
1800
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001801 ExprVector ArgExprs(Actions);
1802 ArgExprs.push_back(ArgExpr.release());
1803 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001804 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001805}
1806
Reid Spencer5f016e22007-07-11 17:01:13 +00001807/// ParseDeclarationSpecifiers
1808/// declaration-specifiers: [C99 6.7]
1809/// storage-class-specifier declaration-specifiers[opt]
1810/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001811/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001812/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001813/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001814/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001815///
1816/// storage-class-specifier: [C99 6.7.1]
1817/// 'typedef'
1818/// 'extern'
1819/// 'static'
1820/// 'auto'
1821/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001822/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001823/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001824/// function-specifier: [C99 6.7.4]
1825/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001826/// [C++] 'virtual'
1827/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001828/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001829/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001830/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001831
Reid Spencer5f016e22007-07-11 17:01:13 +00001832///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001833void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001834 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001835 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001836 DeclSpecContext DSContext,
1837 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001838 if (DS.getSourceRange().isInvalid()) {
1839 DS.SetRangeStart(Tok.getLocation());
1840 DS.SetRangeEnd(Tok.getLocation());
1841 }
1842
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001843 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001844 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001845 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001847 unsigned DiagID = 0;
1848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001850
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001852 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001853 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001854 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1855 MaybeParseCXX0XAttributes(DS.getAttributes());
1856
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 // If this is not a declaration specifier token, we're done reading decl
1858 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001859 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001862 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001863 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001864 if (DS.hasTypeSpecifier()) {
1865 bool AllowNonIdentifiers
1866 = (getCurScope()->getFlags() & (Scope::ControlScope |
1867 Scope::BlockScope |
1868 Scope::TemplateParamScope |
1869 Scope::FunctionPrototypeScope |
1870 Scope::AtCatchScope)) == 0;
1871 bool AllowNestedNameSpecifiers
1872 = DSContext == DSC_top_level ||
1873 (DSContext == DSC_class && DS.isFriendSpecified());
1874
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001875 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1876 AllowNonIdentifiers,
1877 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001878 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001879 }
1880
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001881 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1882 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1883 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001884 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1885 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001886 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001887 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001888 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001889 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001890
1891 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001892 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001893 }
1894
Chris Lattner5e02c472009-01-05 00:07:25 +00001895 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001896 // C++ scope specifier. Annotate and loop, or bail out on error.
1897 if (TryAnnotateCXXScopeToken(true)) {
1898 if (!DS.hasTypeSpecifier())
1899 DS.SetTypeSpecError();
1900 goto DoneWithDeclSpec;
1901 }
John McCall2e0a7152010-03-01 18:20:46 +00001902 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1903 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001904 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001905
1906 case tok::annot_cxxscope: {
1907 if (DS.hasTypeSpecifier())
1908 goto DoneWithDeclSpec;
1909
John McCallaa87d332009-12-12 11:40:51 +00001910 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001911 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1912 Tok.getAnnotationRange(),
1913 SS);
John McCallaa87d332009-12-12 11:40:51 +00001914
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001915 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001916 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001917 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001918 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001919 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001920 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001921
1922 // C++ [class.qual]p2:
1923 // In a lookup in which the constructor is an acceptable lookup
1924 // result and the nested-name-specifier nominates a class C:
1925 //
1926 // - if the name specified after the
1927 // nested-name-specifier, when looked up in C, is the
1928 // injected-class-name of C (Clause 9), or
1929 //
1930 // - if the name specified after the nested-name-specifier
1931 // is the same as the identifier or the
1932 // simple-template-id's template-name in the last
1933 // component of the nested-name-specifier,
1934 //
1935 // the name is instead considered to name the constructor of
1936 // class C.
1937 //
1938 // Thus, if the template-name is actually the constructor
1939 // name, then the code is ill-formed; this interpretation is
1940 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001941 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001942 if ((DSContext == DSC_top_level ||
1943 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1944 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001945 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001946 if (isConstructorDeclarator()) {
1947 // The user meant this to be an out-of-line constructor
1948 // definition, but template arguments are not allowed
1949 // there. Just allow this as a constructor; we'll
1950 // complain about it later.
1951 goto DoneWithDeclSpec;
1952 }
1953
1954 // The user meant this to name a type, but it actually names
1955 // a constructor with some extraneous template
1956 // arguments. Complain, then parse it as a type as the user
1957 // intended.
1958 Diag(TemplateId->TemplateNameLoc,
1959 diag::err_out_of_line_template_id_names_constructor)
1960 << TemplateId->Name;
1961 }
1962
John McCallaa87d332009-12-12 11:40:51 +00001963 DS.getTypeSpecScope() = SS;
1964 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001965 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001966 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001967 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001968 continue;
1969 }
1970
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001971 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001972 DS.getTypeSpecScope() = SS;
1973 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001974 if (Tok.getAnnotationValue()) {
1975 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1977 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001978 PrevSpec, DiagID, T);
1979 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001980 else
1981 DS.SetTypeSpecError();
1982 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1983 ConsumeToken(); // The typename
1984 }
1985
Douglas Gregor9135c722009-03-25 15:40:00 +00001986 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001987 goto DoneWithDeclSpec;
1988
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001989 // If we're in a context where the identifier could be a class name,
1990 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001991 if ((DSContext == DSC_top_level ||
1992 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001993 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001994 &SS)) {
1995 if (isConstructorDeclarator())
1996 goto DoneWithDeclSpec;
1997
1998 // As noted in C++ [class.qual]p2 (cited above), when the name
1999 // of the class is qualified in a context where it could name
2000 // a constructor, its a constructor name. However, we've
2001 // looked at the declarator, and the user probably meant this
2002 // to be a type. Complain that it isn't supposed to be treated
2003 // as a type, then proceed to parse it as a type.
2004 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2005 << Next.getIdentifierInfo();
2006 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002007
John McCallb3d87482010-08-24 05:47:05 +00002008 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2009 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002010 getCurScope(), &SS,
2011 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002012 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002013 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002014
Chris Lattnerf4382f52009-04-14 22:17:06 +00002015 // If the referenced identifier is not a type, then this declspec is
2016 // erroneous: We already checked about that it has no type specifier, and
2017 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002018 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002019 if (TypeRep == 0) {
2020 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002021 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002022 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002023 }
Mike Stump1eb44332009-09-09 15:08:12 +00002024
John McCallaa87d332009-12-12 11:40:51 +00002025 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002026 ConsumeToken(); // The C++ scope.
2027
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002029 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002030 if (isInvalid)
2031 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002033 DS.SetRangeEnd(Tok.getLocation());
2034 ConsumeToken(); // The typename.
2035
2036 continue;
2037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Chris Lattner80d0c892009-01-21 19:48:37 +00002039 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002040 if (Tok.getAnnotationValue()) {
2041 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002043 DiagID, T);
2044 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002045 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002046
2047 if (isInvalid)
2048 break;
2049
Chris Lattner80d0c892009-01-21 19:48:37 +00002050 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2051 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Chris Lattner80d0c892009-01-21 19:48:37 +00002053 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2054 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002055 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002056 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002057 ParseObjCProtocolQualifiers(DS);
2058
Chris Lattner80d0c892009-01-21 19:48:37 +00002059 continue;
2060 }
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Douglas Gregorbfad9152011-04-28 15:48:45 +00002062 case tok::kw___is_signed:
2063 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2064 // typically treats it as a trait. If we see __is_signed as it appears
2065 // in libstdc++, e.g.,
2066 //
2067 // static const bool __is_signed;
2068 //
2069 // then treat __is_signed as an identifier rather than as a keyword.
2070 if (DS.getTypeSpecType() == TST_bool &&
2071 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2072 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2073 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2074 Tok.setKind(tok::identifier);
2075 }
2076
2077 // We're done with the declaration-specifiers.
2078 goto DoneWithDeclSpec;
2079
Chris Lattner3bd934a2008-07-26 01:18:38 +00002080 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002081 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002082 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002083 // In C++, check to see if this is a scope specifier like foo::bar::, if
2084 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002085 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002086 if (TryAnnotateCXXScopeToken(true)) {
2087 if (!DS.hasTypeSpecifier())
2088 DS.SetTypeSpecError();
2089 goto DoneWithDeclSpec;
2090 }
2091 if (!Tok.is(tok::identifier))
2092 continue;
2093 }
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Chris Lattner3bd934a2008-07-26 01:18:38 +00002095 // This identifier can only be a typedef name if we haven't already seen
2096 // a type-specifier. Without this check we misparse:
2097 // typedef int X; struct Y { short X; }; as 'short int'.
2098 if (DS.hasTypeSpecifier())
2099 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
John Thompson82287d12010-02-05 00:12:22 +00002101 // Check for need to substitute AltiVec keyword tokens.
2102 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2103 break;
2104
John McCallb3d87482010-08-24 05:47:05 +00002105 ParsedType TypeRep =
2106 Actions.getTypeName(*Tok.getIdentifierInfo(),
2107 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002108
Chris Lattnerc199ab32009-04-12 20:42:31 +00002109 // If this is not a typedef name, don't parse it as part of the declspec,
2110 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002111 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002112 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002113 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002114 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002115
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002116 // If we're in a context where the identifier could be a class name,
2117 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002118 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002119 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002120 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002121 goto DoneWithDeclSpec;
2122
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002124 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002125 if (isInvalid)
2126 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Chris Lattner3bd934a2008-07-26 01:18:38 +00002128 DS.SetRangeEnd(Tok.getLocation());
2129 ConsumeToken(); // The identifier
2130
2131 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2132 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002133 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002134 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002135 ParseObjCProtocolQualifiers(DS);
2136
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002137 // Need to support trailing type qualifiers (e.g. "id<p> const").
2138 // If a type specifier follows, it will be diagnosed elsewhere.
2139 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002140 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002141
2142 // type-name
2143 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002144 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002145 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002146 // This template-id does not refer to a type name, so we're
2147 // done with the type-specifiers.
2148 goto DoneWithDeclSpec;
2149 }
2150
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002151 // If we're in a context where the template-id could be a
2152 // constructor name or specialization, check whether this is a
2153 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002155 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002156 isConstructorDeclarator())
2157 goto DoneWithDeclSpec;
2158
Douglas Gregor39a8de12009-02-25 19:37:18 +00002159 // Turn the template-id annotation token into a type annotation
2160 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002161 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002162 continue;
2163 }
2164
Reid Spencer5f016e22007-07-11 17:01:13 +00002165 // GNU attributes support.
2166 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002167 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002169
2170 // Microsoft declspec support.
2171 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002172 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002173 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Steve Naroff239f0732008-12-25 14:16:32 +00002175 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002176 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002177 // FIXME: Add handling here!
2178 break;
2179
2180 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002181 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002182 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002183 case tok::kw___cdecl:
2184 case tok::kw___stdcall:
2185 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002186 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002187 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002188 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002189 continue;
2190
Dawn Perchik52fc3142010-09-03 01:29:35 +00002191 // Borland single token adornments.
2192 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002193 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002194 continue;
2195
Peter Collingbournef315fa82011-02-14 01:42:53 +00002196 // OpenCL single token adornments.
2197 case tok::kw___kernel:
2198 ParseOpenCLAttributes(DS.getAttributes());
2199 continue;
2200
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // storage-class-specifier
2202 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002203 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2204 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002205 break;
2206 case tok::kw_extern:
2207 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002208 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002209 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2210 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002212 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002213 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2214 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002216 case tok::kw_static:
2217 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002218 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002219 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2220 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 break;
2222 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002223 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002224 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002225 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2226 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002227 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002228 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002229 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002230 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002231 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2232 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002233 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002234 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2235 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002236 break;
2237 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002238 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2239 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002240 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002241 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002242 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2243 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002244 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002245 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002246 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 // function-specifier
2250 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002251 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002252 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002253 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002254 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002255 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002256 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002257 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002258 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002259
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002260 // alignment-specifier
2261 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002262 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002263 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002264 ParseAlignmentSpecifier(DS.getAttributes());
2265 continue;
2266
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002267 // friend
2268 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002269 if (DSContext == DSC_class)
2270 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2271 else {
2272 PrevSpec = ""; // not actually used by the diagnostic
2273 DiagID = diag::err_friend_invalid_in_context;
2274 isInvalid = true;
2275 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002276 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Douglas Gregor8d267c52011-09-09 02:06:17 +00002278 // Modules
2279 case tok::kw___module_private__:
2280 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2281 break;
2282
Sebastian Redl2ac67232009-11-05 15:47:02 +00002283 // constexpr
2284 case tok::kw_constexpr:
2285 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2286 break;
2287
Chris Lattner80d0c892009-01-21 19:48:37 +00002288 // type-specifier
2289 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002290 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2291 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002292 break;
2293 case tok::kw_long:
2294 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002295 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2296 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002297 else
John McCallfec54012009-08-03 20:12:06 +00002298 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2299 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002300 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002301 case tok::kw___int64:
2302 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2303 DiagID);
2304 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002305 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002306 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2307 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002308 break;
2309 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002310 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2311 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002312 break;
2313 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002314 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2315 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002316 break;
2317 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002318 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2319 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002320 break;
2321 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2323 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002324 break;
2325 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2327 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002328 break;
2329 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2331 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002332 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002333 case tok::kw___int128:
2334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2335 DiagID);
2336 break;
2337 case tok::kw_half:
2338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2339 DiagID);
2340 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002341 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2343 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002344 break;
2345 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2347 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002348 break;
2349 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2351 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002352 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002353 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2355 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002356 break;
2357 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2359 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002360 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002361 case tok::kw_bool:
2362 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002363 if (Tok.is(tok::kw_bool) &&
2364 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2365 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2366 PrevSpec = ""; // Not used by the diagnostic.
2367 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002368 // For better error recovery.
2369 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002370 isInvalid = true;
2371 } else {
2372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2373 DiagID);
2374 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002375 break;
2376 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002377 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2378 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002379 break;
2380 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2382 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 break;
2384 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2386 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002387 break;
John Thompson82287d12010-02-05 00:12:22 +00002388 case tok::kw___vector:
2389 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2390 break;
2391 case tok::kw___pixel:
2392 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2393 break;
John McCalla5fc4722011-04-09 22:50:59 +00002394 case tok::kw___unknown_anytype:
2395 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2396 PrevSpec, DiagID);
2397 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002398
2399 // class-specifier:
2400 case tok::kw_class:
2401 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002402 case tok::kw_union: {
2403 tok::TokenKind Kind = Tok.getKind();
2404 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002405 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2406 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002407 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002408 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002409
2410 // enum-specifier:
2411 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002412 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002413 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002414 continue;
2415
2416 // cv-qualifier:
2417 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002418 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002419 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002420 break;
2421 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002422 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002423 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002424 break;
2425 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002427 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002428 break;
2429
Douglas Gregord57959a2009-03-27 23:10:48 +00002430 // C++ typename-specifier:
2431 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002432 if (TryAnnotateTypeOrScopeToken()) {
2433 DS.SetTypeSpecError();
2434 goto DoneWithDeclSpec;
2435 }
2436 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002437 continue;
2438 break;
2439
Chris Lattner80d0c892009-01-21 19:48:37 +00002440 // GNU typeof support.
2441 case tok::kw_typeof:
2442 ParseTypeofSpecifier(DS);
2443 continue;
2444
David Blaikie42d6d0c2011-12-04 05:04:18 +00002445 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002446 ParseDecltypeSpecifier(DS);
2447 continue;
2448
Sean Huntdb5d44b2011-05-19 05:37:45 +00002449 case tok::kw___underlying_type:
2450 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002451 continue;
2452
2453 case tok::kw__Atomic:
2454 ParseAtomicSpecifier(DS);
2455 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002456
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002457 // OpenCL qualifiers:
2458 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002459 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002460 goto DoneWithDeclSpec;
2461 case tok::kw___private:
2462 case tok::kw___global:
2463 case tok::kw___local:
2464 case tok::kw___constant:
2465 case tok::kw___read_only:
2466 case tok::kw___write_only:
2467 case tok::kw___read_write:
2468 ParseOpenCLQualifiers(DS);
2469 break;
2470
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002471 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002472 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002473 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2474 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002475 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002476 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Douglas Gregor46f936e2010-11-19 17:10:50 +00002478 if (!ParseObjCProtocolQualifiers(DS))
2479 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2480 << FixItHint::CreateInsertion(Loc, "id")
2481 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002482
2483 // Need to support trailing type qualifiers (e.g. "id<p> const").
2484 // If a type specifier follows, it will be diagnosed elsewhere.
2485 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002486 }
John McCallfec54012009-08-03 20:12:06 +00002487 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 if (isInvalid) {
2489 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002490 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002491
2492 if (DiagID == diag::ext_duplicate_declspec)
2493 Diag(Tok, DiagID)
2494 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2495 else
2496 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002497 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002498
Chris Lattner81c018d2008-03-13 06:29:04 +00002499 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002500 if (DiagID != diag::err_bool_redeclaration)
2501 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002502 }
2503}
Douglas Gregoradcac882008-12-01 23:54:00 +00002504
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002505/// ParseStructDeclaration - Parse a struct declaration without the terminating
2506/// semicolon.
2507///
Reid Spencer5f016e22007-07-11 17:01:13 +00002508/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002509/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002510/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002511/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002512/// struct-declarator-list:
2513/// struct-declarator
2514/// struct-declarator-list ',' struct-declarator
2515/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2516/// struct-declarator:
2517/// declarator
2518/// [GNU] declarator attributes[opt]
2519/// declarator[opt] ':' constant-expression
2520/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2521///
Chris Lattnere1359422008-04-10 06:46:29 +00002522void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002523ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002524
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002525 if (Tok.is(tok::kw___extension__)) {
2526 // __extension__ silences extension warnings in the subexpression.
2527 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002528 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002529 return ParseStructDeclaration(DS, Fields);
2530 }
Mike Stump1eb44332009-09-09 15:08:12 +00002531
Steve Naroff28a7ca82007-08-20 22:28:22 +00002532 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002533 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002535 // If there are no declarators, this is a free-standing declaration
2536 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002537 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002538 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002539 return;
2540 }
2541
2542 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002543 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002544 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002545 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002546 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002547 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002548 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002549
2550 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002551 if (!FirstDeclarator)
2552 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Steve Naroff28a7ca82007-08-20 22:28:22 +00002554 /// struct-declarator: declarator
2555 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002556 if (Tok.isNot(tok::colon)) {
2557 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2558 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002559 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002560 }
Mike Stump1eb44332009-09-09 15:08:12 +00002561
Chris Lattner04d66662007-10-09 17:33:22 +00002562 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002563 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002564 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002565 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002566 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002567 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002568 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002569 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002570
Steve Naroff28a7ca82007-08-20 22:28:22 +00002571 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002572 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002573
John McCallbdd563e2009-11-03 02:38:08 +00002574 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002575 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002576 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002577
Steve Naroff28a7ca82007-08-20 22:28:22 +00002578 // If we don't have a comma, it is either the end of the list (a ';')
2579 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002580 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002581 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002582
Steve Naroff28a7ca82007-08-20 22:28:22 +00002583 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002584 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002585
John McCallbdd563e2009-11-03 02:38:08 +00002586 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002587 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002588}
2589
2590/// ParseStructUnionBody
2591/// struct-contents:
2592/// struct-declaration-list
2593/// [EXT] empty
2594/// [GNU] "struct-declaration-list" without terminatoring ';'
2595/// struct-declaration-list:
2596/// struct-declaration
2597/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002598/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002599///
Reid Spencer5f016e22007-07-11 17:01:13 +00002600void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002601 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002602 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2603 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002605 BalancedDelimiterTracker T(*this, tok::l_brace);
2606 if (T.consumeOpen())
2607 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002609 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002610 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002611
Reid Spencer5f016e22007-07-11 17:01:13 +00002612 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2613 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002614 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002615 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2616 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2617 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002618
Chris Lattner5f9e2722011-07-23 10:55:15 +00002619 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002620
Reid Spencer5f016e22007-07-11 17:01:13 +00002621 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002622 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002626 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002627 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002628 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002629 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 ConsumeToken();
2631 continue;
2632 }
Chris Lattnere1359422008-04-10 06:46:29 +00002633
2634 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002635 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002636
John McCallbdd563e2009-11-03 02:38:08 +00002637 if (!Tok.is(tok::at)) {
2638 struct CFieldCallback : FieldCallback {
2639 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002640 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002641 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002642
John McCalld226f652010-08-21 09:40:31 +00002643 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002644 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002645 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2646
John McCalld226f652010-08-21 09:40:31 +00002647 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002648 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002649 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002650 FD.D.getDeclSpec().getSourceRange().getBegin(),
2651 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002652 FieldDecls.push_back(Field);
2653 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002654 }
John McCallbdd563e2009-11-03 02:38:08 +00002655 } Callback(*this, TagDecl, FieldDecls);
2656
2657 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002658 } else { // Handle @defs
2659 ConsumeToken();
2660 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2661 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002662 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002663 continue;
2664 }
2665 ConsumeToken();
2666 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2667 if (!Tok.is(tok::identifier)) {
2668 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002669 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002670 continue;
2671 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002672 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002673 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002674 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002675 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2676 ConsumeToken();
2677 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002678 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002679
Chris Lattner04d66662007-10-09 17:33:22 +00002680 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002681 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002682 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002683 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002684 break;
2685 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002686 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2687 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002688 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002689 // If we stopped at a ';', eat it.
2690 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002691 }
2692 }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002694 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002695
John McCall0b7e6782011-03-24 11:26:52 +00002696 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002697 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002698 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002699
Douglas Gregor23c94db2010-07-02 17:43:08 +00002700 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002701 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002702 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002703 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002704 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002705 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2706 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002707}
2708
Reid Spencer5f016e22007-07-11 17:01:13 +00002709/// ParseEnumSpecifier
2710/// enum-specifier: [C99 6.7.2.2]
2711/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002712///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002713/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2714/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002715/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2716/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002717/// 'enum' identifier
2718/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002719///
Richard Smith1af83c42012-03-23 03:33:32 +00002720/// [C++11] enum-head '{' enumerator-list[opt] '}'
2721/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002722///
Richard Smith1af83c42012-03-23 03:33:32 +00002723/// enum-head: [C++11]
2724/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2725/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2726/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002727///
Richard Smith1af83c42012-03-23 03:33:32 +00002728/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002729/// 'enum'
2730/// 'enum' 'class'
2731/// 'enum' 'struct'
2732///
Richard Smith1af83c42012-03-23 03:33:32 +00002733/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002734/// ':' type-specifier-seq
2735///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002736/// [C++] elaborated-type-specifier:
2737/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2738///
Chris Lattner4c97d762009-04-12 21:49:30 +00002739void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002740 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002741 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002743 if (Tok.is(tok::code_completion)) {
2744 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002745 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002746 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002747 }
John McCall57c13002011-07-06 05:58:41 +00002748
Richard Smithbdad7a22012-01-10 01:33:14 +00002749 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002750 bool IsScopedUsingClassTag = false;
2751
David Blaikie4e4d0842012-03-11 07:00:24 +00002752 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002753 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002754 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002755 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002756 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002757 }
Richard Smith1af83c42012-03-23 03:33:32 +00002758
2759 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2760 // used to specify explicit instantiations. We extend this to also cover
2761 // explicit specializations.
2762 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2763 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2764 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2765
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002766 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002767 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002768 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002769
Aaron Ballman6454a022012-03-01 04:09:28 +00002770 // If declspecs exist after tag, parse them.
2771 while (Tok.is(tok::kw___declspec))
2772 ParseMicrosoftDeclSpec(attrs);
2773
Richard Smith7796eb52012-03-12 08:56:40 +00002774 // Enum definitions should not be parsed in a trailing-return-type.
2775 bool AllowDeclaration = DSC != DSC_trailing;
2776
2777 bool AllowFixedUnderlyingType = AllowDeclaration &&
2778 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2779 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002780
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002781 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002782 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002783 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2784 // if a fixed underlying type is allowed.
2785 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2786
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002787 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2788 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002789 return;
2790
2791 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002792 Diag(Tok, diag::err_expected_ident);
2793 if (Tok.isNot(tok::l_brace)) {
2794 // Has no name and is not a definition.
2795 // Skip the rest of this declarator, up until the comma or semicolon.
2796 SkipUntil(tok::comma, true);
2797 return;
2798 }
2799 }
2800 }
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002802 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002803 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002804 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002805 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002807 // Skip the rest of this declarator, up until the comma or semicolon.
2808 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002810 }
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002812 // If an identifier is present, consume and remember it.
2813 IdentifierInfo *Name = 0;
2814 SourceLocation NameLoc;
2815 if (Tok.is(tok::identifier)) {
2816 Name = Tok.getIdentifierInfo();
2817 NameLoc = ConsumeToken();
2818 }
Mike Stump1eb44332009-09-09 15:08:12 +00002819
Richard Smithbdad7a22012-01-10 01:33:14 +00002820 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002821 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2822 // declaration of a scoped enumeration.
2823 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002824 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002825 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002826 }
2827
Richard Smith1af83c42012-03-23 03:33:32 +00002828 // Stop suppressing access control now we've parsed the enum name.
2829 SuppressAccess.done();
2830
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002831 TypeResult BaseType;
2832
Douglas Gregora61b3e72010-12-01 17:42:47 +00002833 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002834 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002835 bool PossibleBitfield = false;
2836 if (getCurScope()->getFlags() & Scope::ClassScope) {
2837 // If we're in class scope, this can either be an enum declaration with
2838 // an underlying type, or a declaration of a bitfield member. We try to
2839 // use a simple disambiguation scheme first to catch the common cases
2840 // (integer literal, sizeof); if it's still ambiguous, we then consider
2841 // anything that's a simple-type-specifier followed by '(' as an
2842 // expression. This suffices because function types are not valid
2843 // underlying types anyway.
2844 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2845 // If the next token starts an expression, we know we're parsing a
2846 // bit-field. This is the common case.
2847 if (TPR == TPResult::True())
2848 PossibleBitfield = true;
2849 // If the next token starts a type-specifier-seq, it may be either a
2850 // a fixed underlying type or the start of a function-style cast in C++;
2851 // lookahead one more token to see if it's obvious that we have a
2852 // fixed underlying type.
2853 else if (TPR == TPResult::False() &&
2854 GetLookAheadToken(2).getKind() == tok::semi) {
2855 // Consume the ':'.
2856 ConsumeToken();
2857 } else {
2858 // We have the start of a type-specifier-seq, so we have to perform
2859 // tentative parsing to determine whether we have an expression or a
2860 // type.
2861 TentativeParsingAction TPA(*this);
2862
2863 // Consume the ':'.
2864 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002865
2866 // If we see a type specifier followed by an open-brace, we have an
2867 // ambiguity between an underlying type and a C++11 braced
2868 // function-style cast. Resolve this by always treating it as an
2869 // underlying type.
2870 // FIXME: The standard is not entirely clear on how to disambiguate in
2871 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002872 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002873 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002874 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002875 // We'll parse this as a bitfield later.
2876 PossibleBitfield = true;
2877 TPA.Revert();
2878 } else {
2879 // We have a type-specifier-seq.
2880 TPA.Commit();
2881 }
2882 }
2883 } else {
2884 // Consume the ':'.
2885 ConsumeToken();
2886 }
2887
2888 if (!PossibleBitfield) {
2889 SourceRange Range;
2890 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002891
David Blaikie4e4d0842012-03-11 07:00:24 +00002892 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002893 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2894 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002895 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002896 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002897 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002898 }
2899
Richard Smithbdad7a22012-01-10 01:33:14 +00002900 // There are four options here. If we have 'friend enum foo;' then this is a
2901 // friend declaration, and cannot have an accompanying definition. If we have
2902 // 'enum foo;', then this is a forward declaration. If we have
2903 // 'enum foo {...' then this is a definition. Otherwise we have something
2904 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002905 //
2906 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2907 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2908 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2909 //
John McCallf312b1e2010-08-26 23:41:50 +00002910 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002911 if (DS.isFriendSpecified())
2912 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002913 else if (!AllowDeclaration)
2914 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002915 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002916 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002917 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002918 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002919 else
John McCallf312b1e2010-08-26 23:41:50 +00002920 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002921
2922 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002923 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002924 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002925 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2926 // Skip the rest of this declarator, up until the comma or semicolon.
2927 Diag(Tok, diag::err_enum_template);
2928 SkipUntil(tok::comma, true);
2929 return;
2930 }
2931
2932 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2933 // Enumerations can't be explicitly instantiated.
2934 DS.SetTypeSpecError();
2935 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2936 return;
2937 }
2938
2939 assert(TemplateInfo.TemplateParams && "no template parameters");
2940 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2941 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002942 }
Richard Smith1af83c42012-03-23 03:33:32 +00002943
Douglas Gregorb9075602011-02-22 02:55:24 +00002944 if (!Name && TUK != Sema::TUK_Definition) {
2945 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002946
Douglas Gregorb9075602011-02-22 02:55:24 +00002947 // Skip the rest of this declarator, up until the comma or semicolon.
2948 SkipUntil(tok::comma, true);
2949 return;
2950 }
Richard Smith1af83c42012-03-23 03:33:32 +00002951
Douglas Gregor402abb52009-05-28 23:31:59 +00002952 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002953 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002954 const char *PrevSpec = 0;
2955 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002956 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002957 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002958 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002959 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002960 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002961
Douglas Gregor48c89f42010-04-24 16:38:41 +00002962 if (IsDependent) {
2963 // This enum has a dependent nested-name-specifier. Handle it as a
2964 // dependent tag.
2965 if (!Name) {
2966 DS.SetTypeSpecError();
2967 Diag(Tok, diag::err_expected_type_name_after_typename);
2968 return;
2969 }
2970
Douglas Gregor23c94db2010-07-02 17:43:08 +00002971 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002972 TUK, SS, Name, StartLoc,
2973 NameLoc);
2974 if (Type.isInvalid()) {
2975 DS.SetTypeSpecError();
2976 return;
2977 }
2978
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002979 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2980 NameLoc.isValid() ? NameLoc : StartLoc,
2981 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002982 Diag(StartLoc, DiagID) << PrevSpec;
2983
2984 return;
2985 }
Mike Stump1eb44332009-09-09 15:08:12 +00002986
John McCalld226f652010-08-21 09:40:31 +00002987 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002988 // The action failed to produce an enumeration tag. If this is a
2989 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002990 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002991 ConsumeBrace();
2992 SkipUntil(tok::r_brace);
2993 }
2994
2995 DS.SetTypeSpecError();
2996 return;
2997 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002998
Richard Smith7796eb52012-03-12 08:56:40 +00002999 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003000 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00003001 Diag(Tok, diag::err_friend_decl_defines_type)
3002 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00003003 ConsumeBrace();
3004 SkipUntil(tok::r_brace);
3005 } else {
3006 ParseEnumBody(StartLoc, TagDecl);
3007 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003008 }
Mike Stump1eb44332009-09-09 15:08:12 +00003009
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003010 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3011 NameLoc.isValid() ? NameLoc : StartLoc,
3012 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003013 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003014}
3015
3016/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3017/// enumerator-list:
3018/// enumerator
3019/// enumerator-list ',' enumerator
3020/// enumerator:
3021/// enumeration-constant
3022/// enumeration-constant '=' constant-expression
3023/// enumeration-constant:
3024/// identifier
3025///
John McCalld226f652010-08-21 09:40:31 +00003026void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003027 // Enter the scope of the enum body and start the definition.
3028 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003029 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003030
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003031 BalancedDelimiterTracker T(*this, tok::l_brace);
3032 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Chris Lattner7946dd32007-08-27 17:24:30 +00003034 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003035 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003036 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Chris Lattner5f9e2722011-07-23 10:55:15 +00003038 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003039
John McCalld226f652010-08-21 09:40:31 +00003040 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003041
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003043 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003044 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3045 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003046
John McCall5b629aa2010-10-22 23:36:17 +00003047 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003048 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003049 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003050
Reid Spencer5f016e22007-07-11 17:01:13 +00003051 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003052 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003053 ParsingDeclRAIIObject PD(*this);
3054
Chris Lattner04d66662007-10-09 17:33:22 +00003055 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003056 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003057 AssignedVal = ParseConstantExpression();
3058 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003059 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003060 }
Mike Stump1eb44332009-09-09 15:08:12 +00003061
Reid Spencer5f016e22007-07-11 17:01:13 +00003062 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003063 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3064 LastEnumConstDecl,
3065 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003066 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003067 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003068 PD.complete(EnumConstDecl);
3069
Reid Spencer5f016e22007-07-11 17:01:13 +00003070 EnumConstantDecls.push_back(EnumConstDecl);
3071 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Douglas Gregor751f6922010-09-07 14:51:08 +00003073 if (Tok.is(tok::identifier)) {
3074 // We're missing a comma between enumerators.
3075 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3076 Diag(Loc, diag::err_enumerator_list_missing_comma)
3077 << FixItHint::CreateInsertion(Loc, ", ");
3078 continue;
3079 }
3080
Chris Lattner04d66662007-10-09 17:33:22 +00003081 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003082 break;
3083 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003084
Richard Smith7fe62082011-10-15 05:09:34 +00003085 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003086 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003087 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003088 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003089 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003090 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003091 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3092 << FixItHint::CreateRemoval(CommaLoc);
3093 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003094 }
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Reid Spencer5f016e22007-07-11 17:01:13 +00003096 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003097 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003098
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003100 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003101 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003102
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003103 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3104 EnumDecl, EnumConstantDecls.data(),
3105 EnumConstantDecls.size(), getCurScope(),
3106 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Douglas Gregor72de6672009-01-08 20:45:30 +00003108 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003109 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3110 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003111}
3112
3113/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003114/// start of a type-qualifier-list.
3115bool Parser::isTypeQualifier() const {
3116 switch (Tok.getKind()) {
3117 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003118
3119 // type-qualifier only in OpenCL
3120 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003121 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003122
Steve Naroff5f8aa692008-02-11 23:15:56 +00003123 // type-qualifier
3124 case tok::kw_const:
3125 case tok::kw_volatile:
3126 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003127 case tok::kw___private:
3128 case tok::kw___local:
3129 case tok::kw___global:
3130 case tok::kw___constant:
3131 case tok::kw___read_only:
3132 case tok::kw___read_write:
3133 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003134 return true;
3135 }
3136}
3137
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003138/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3139/// is definitely a type-specifier. Return false if it isn't part of a type
3140/// specifier or if we're not sure.
3141bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3142 switch (Tok.getKind()) {
3143 default: return false;
3144 // type-specifiers
3145 case tok::kw_short:
3146 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003147 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003148 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003149 case tok::kw_signed:
3150 case tok::kw_unsigned:
3151 case tok::kw__Complex:
3152 case tok::kw__Imaginary:
3153 case tok::kw_void:
3154 case tok::kw_char:
3155 case tok::kw_wchar_t:
3156 case tok::kw_char16_t:
3157 case tok::kw_char32_t:
3158 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003159 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003160 case tok::kw_float:
3161 case tok::kw_double:
3162 case tok::kw_bool:
3163 case tok::kw__Bool:
3164 case tok::kw__Decimal32:
3165 case tok::kw__Decimal64:
3166 case tok::kw__Decimal128:
3167 case tok::kw___vector:
3168
3169 // struct-or-union-specifier (C99) or class-specifier (C++)
3170 case tok::kw_class:
3171 case tok::kw_struct:
3172 case tok::kw_union:
3173 // enum-specifier
3174 case tok::kw_enum:
3175
3176 // typedef-name
3177 case tok::annot_typename:
3178 return true;
3179 }
3180}
3181
Steve Naroff5f8aa692008-02-11 23:15:56 +00003182/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003183/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003184bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003185 switch (Tok.getKind()) {
3186 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Chris Lattner166a8fc2009-01-04 23:41:41 +00003188 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003189 if (TryAltiVecVectorToken())
3190 return true;
3191 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003192 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003193 // Annotate typenames and C++ scope specifiers. If we get one, just
3194 // recurse to handle whatever we get.
3195 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003196 return true;
3197 if (Tok.is(tok::identifier))
3198 return false;
3199 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003200
Chris Lattner166a8fc2009-01-04 23:41:41 +00003201 case tok::coloncolon: // ::foo::bar
3202 if (NextToken().is(tok::kw_new) || // ::new
3203 NextToken().is(tok::kw_delete)) // ::delete
3204 return false;
3205
Chris Lattner166a8fc2009-01-04 23:41:41 +00003206 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003207 return true;
3208 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003209
Reid Spencer5f016e22007-07-11 17:01:13 +00003210 // GNU attributes support.
3211 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003212 // GNU typeof support.
3213 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003214
Reid Spencer5f016e22007-07-11 17:01:13 +00003215 // type-specifiers
3216 case tok::kw_short:
3217 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003218 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003219 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003220 case tok::kw_signed:
3221 case tok::kw_unsigned:
3222 case tok::kw__Complex:
3223 case tok::kw__Imaginary:
3224 case tok::kw_void:
3225 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003226 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003227 case tok::kw_char16_t:
3228 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003229 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003230 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003231 case tok::kw_float:
3232 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003233 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 case tok::kw__Bool:
3235 case tok::kw__Decimal32:
3236 case tok::kw__Decimal64:
3237 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003238 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Chris Lattner99dc9142008-04-13 18:59:07 +00003240 // struct-or-union-specifier (C99) or class-specifier (C++)
3241 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003242 case tok::kw_struct:
3243 case tok::kw_union:
3244 // enum-specifier
3245 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Reid Spencer5f016e22007-07-11 17:01:13 +00003247 // type-qualifier
3248 case tok::kw_const:
3249 case tok::kw_volatile:
3250 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003251
3252 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003253 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003254 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Chris Lattner7c186be2008-10-20 00:25:30 +00003256 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3257 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003258 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003259
Steve Naroff239f0732008-12-25 14:16:32 +00003260 case tok::kw___cdecl:
3261 case tok::kw___stdcall:
3262 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003263 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003264 case tok::kw___w64:
3265 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003266 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003267 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003268 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003269
3270 case tok::kw___private:
3271 case tok::kw___local:
3272 case tok::kw___global:
3273 case tok::kw___constant:
3274 case tok::kw___read_only:
3275 case tok::kw___read_write:
3276 case tok::kw___write_only:
3277
Eli Friedman290eeb02009-06-08 23:27:34 +00003278 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003279
3280 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003281 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003282
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003283 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003284 case tok::kw__Atomic:
3285 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003286 }
3287}
3288
3289/// isDeclarationSpecifier() - Return true if the current token is part of a
3290/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003291///
3292/// \param DisambiguatingWithExpression True to indicate that the purpose of
3293/// this check is to disambiguate between an expression and a declaration.
3294bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003295 switch (Tok.getKind()) {
3296 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003298 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003299 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003300
Chris Lattner166a8fc2009-01-04 23:41:41 +00003301 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003302 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003303 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003304 return false;
John Thompson82287d12010-02-05 00:12:22 +00003305 if (TryAltiVecVectorToken())
3306 return true;
3307 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003308 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003309 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003310 // Annotate typenames and C++ scope specifiers. If we get one, just
3311 // recurse to handle whatever we get.
3312 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003313 return true;
3314 if (Tok.is(tok::identifier))
3315 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003316
3317 // If we're in Objective-C and we have an Objective-C class type followed
3318 // by an identifier and then either ':' or ']', in a place where an
3319 // expression is permitted, then this is probably a class message send
3320 // missing the initial '['. In this case, we won't consider this to be
3321 // the start of a declaration.
3322 if (DisambiguatingWithExpression &&
3323 isStartOfObjCClassMessageMissingOpenBracket())
3324 return false;
3325
John McCall9ba61662010-02-26 08:45:28 +00003326 return isDeclarationSpecifier();
3327
Chris Lattner166a8fc2009-01-04 23:41:41 +00003328 case tok::coloncolon: // ::foo::bar
3329 if (NextToken().is(tok::kw_new) || // ::new
3330 NextToken().is(tok::kw_delete)) // ::delete
3331 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Chris Lattner166a8fc2009-01-04 23:41:41 +00003333 // Annotate typenames and C++ scope specifiers. If we get one, just
3334 // recurse to handle whatever we get.
3335 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003336 return true;
3337 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Reid Spencer5f016e22007-07-11 17:01:13 +00003339 // storage-class-specifier
3340 case tok::kw_typedef:
3341 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003342 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003343 case tok::kw_static:
3344 case tok::kw_auto:
3345 case tok::kw_register:
3346 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Douglas Gregor8d267c52011-09-09 02:06:17 +00003348 // Modules
3349 case tok::kw___module_private__:
3350
Reid Spencer5f016e22007-07-11 17:01:13 +00003351 // type-specifiers
3352 case tok::kw_short:
3353 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003354 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003355 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003356 case tok::kw_signed:
3357 case tok::kw_unsigned:
3358 case tok::kw__Complex:
3359 case tok::kw__Imaginary:
3360 case tok::kw_void:
3361 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003362 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003363 case tok::kw_char16_t:
3364 case tok::kw_char32_t:
3365
Reid Spencer5f016e22007-07-11 17:01:13 +00003366 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003367 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003368 case tok::kw_float:
3369 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003370 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003371 case tok::kw__Bool:
3372 case tok::kw__Decimal32:
3373 case tok::kw__Decimal64:
3374 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003375 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003376
Chris Lattner99dc9142008-04-13 18:59:07 +00003377 // struct-or-union-specifier (C99) or class-specifier (C++)
3378 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003379 case tok::kw_struct:
3380 case tok::kw_union:
3381 // enum-specifier
3382 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003383
Reid Spencer5f016e22007-07-11 17:01:13 +00003384 // type-qualifier
3385 case tok::kw_const:
3386 case tok::kw_volatile:
3387 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003388
Reid Spencer5f016e22007-07-11 17:01:13 +00003389 // function-specifier
3390 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003391 case tok::kw_virtual:
3392 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003393
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003394 // static_assert-declaration
3395 case tok::kw__Static_assert:
3396
Chris Lattner1ef08762007-08-09 17:01:07 +00003397 // GNU typeof support.
3398 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003399
Chris Lattner1ef08762007-08-09 17:01:07 +00003400 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003401 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003402 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003403
Francois Pichete3d49b42011-06-19 08:02:06 +00003404 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003405 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003406 return true;
3407
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003408 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003409 case tok::kw__Atomic:
3410 return true;
3411
Chris Lattnerf3948c42008-07-26 03:38:44 +00003412 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3413 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003414 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003415
Douglas Gregord9d75e52011-04-27 05:41:15 +00003416 // typedef-name
3417 case tok::annot_typename:
3418 return !DisambiguatingWithExpression ||
3419 !isStartOfObjCClassMessageMissingOpenBracket();
3420
Steve Naroff47f52092009-01-06 19:34:12 +00003421 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003422 case tok::kw___cdecl:
3423 case tok::kw___stdcall:
3424 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003425 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003426 case tok::kw___w64:
3427 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003428 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003429 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003430 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003431 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003432
3433 case tok::kw___private:
3434 case tok::kw___local:
3435 case tok::kw___global:
3436 case tok::kw___constant:
3437 case tok::kw___read_only:
3438 case tok::kw___read_write:
3439 case tok::kw___write_only:
3440
Eli Friedman290eeb02009-06-08 23:27:34 +00003441 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 }
3443}
3444
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003445bool Parser::isConstructorDeclarator() {
3446 TentativeParsingAction TPA(*this);
3447
3448 // Parse the C++ scope specifier.
3449 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003450 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3451 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003452 TPA.Revert();
3453 return false;
3454 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003455
3456 // Parse the constructor name.
3457 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3458 // We already know that we have a constructor name; just consume
3459 // the token.
3460 ConsumeToken();
3461 } else {
3462 TPA.Revert();
3463 return false;
3464 }
3465
Richard Smith22592862012-03-27 23:05:05 +00003466 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003467 if (Tok.isNot(tok::l_paren)) {
3468 TPA.Revert();
3469 return false;
3470 }
3471 ConsumeParen();
3472
Richard Smith22592862012-03-27 23:05:05 +00003473 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3474 // that we have a constructor.
3475 if (Tok.is(tok::r_paren) ||
3476 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003477 TPA.Revert();
3478 return true;
3479 }
3480
3481 // If we need to, enter the specified scope.
3482 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003483 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003484 DeclScopeObj.EnterDeclaratorScope();
3485
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003486 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003487 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003488 MaybeParseMicrosoftAttributes(Attrs);
3489
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003490 // Check whether the next token(s) are part of a declaration
3491 // specifier, in which case we have the start of a parameter and,
3492 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003493 bool IsConstructor = false;
3494 if (isDeclarationSpecifier())
3495 IsConstructor = true;
3496 else if (Tok.is(tok::identifier) ||
3497 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3498 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3499 // This might be a parenthesized member name, but is more likely to
3500 // be a constructor declaration with an invalid argument type. Keep
3501 // looking.
3502 if (Tok.is(tok::annot_cxxscope))
3503 ConsumeToken();
3504 ConsumeToken();
3505
3506 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003507 // which must have one of the following syntactic forms (see the
3508 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003509 switch (Tok.getKind()) {
3510 case tok::l_paren:
3511 // C(X ( int));
3512 case tok::l_square:
3513 // C(X [ 5]);
3514 // C(X [ [attribute]]);
3515 case tok::coloncolon:
3516 // C(X :: Y);
3517 // C(X :: *p);
3518 case tok::r_paren:
3519 // C(X )
3520 // Assume this isn't a constructor, rather than assuming it's a
3521 // constructor with an unnamed parameter of an ill-formed type.
3522 break;
3523
3524 default:
3525 IsConstructor = true;
3526 break;
3527 }
3528 }
3529
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003530 TPA.Revert();
3531 return IsConstructor;
3532}
Reid Spencer5f016e22007-07-11 17:01:13 +00003533
3534/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003535/// type-qualifier-list: [C99 6.7.5]
3536/// type-qualifier
3537/// [vendor] attributes
3538/// [ only if VendorAttributesAllowed=true ]
3539/// type-qualifier-list type-qualifier
3540/// [vendor] type-qualifier-list attributes
3541/// [ only if VendorAttributesAllowed=true ]
3542/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3543/// [ only if CXX0XAttributesAllowed=true ]
3544/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003545///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003546void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3547 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003548 bool CXX11AttributesAllowed) {
3549 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003550 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003551 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003552 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003553 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003554 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003555
3556 SourceLocation EndLoc;
3557
Reid Spencer5f016e22007-07-11 17:01:13 +00003558 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003559 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003560 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003561 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003562 SourceLocation Loc = Tok.getLocation();
3563
3564 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003565 case tok::code_completion:
3566 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003567 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003568
Reid Spencer5f016e22007-07-11 17:01:13 +00003569 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003570 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003571 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 break;
3573 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003574 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003575 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003576 break;
3577 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003578 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003579 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003580 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003581
3582 // OpenCL qualifiers:
3583 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003584 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003585 goto DoneWithTypeQuals;
3586 case tok::kw___private:
3587 case tok::kw___global:
3588 case tok::kw___local:
3589 case tok::kw___constant:
3590 case tok::kw___read_only:
3591 case tok::kw___write_only:
3592 case tok::kw___read_write:
3593 ParseOpenCLQualifiers(DS);
3594 break;
3595
Eli Friedman290eeb02009-06-08 23:27:34 +00003596 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003597 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003598 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003599 case tok::kw___cdecl:
3600 case tok::kw___stdcall:
3601 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003602 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003603 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003604 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003605 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003606 continue;
3607 }
3608 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003609 case tok::kw___pascal:
3610 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003611 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003612 continue;
3613 }
3614 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003615 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003616 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003617 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003618 continue; // do *not* consume the next token!
3619 }
3620 // otherwise, FALL THROUGH!
3621 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003622 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003623 // If this is not a type-qualifier token, we're done reading type
3624 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003625 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003626 if (EndLoc.isValid())
3627 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003628 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003629 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003630
Reid Spencer5f016e22007-07-11 17:01:13 +00003631 // If the specifier combination wasn't legal, issue a diagnostic.
3632 if (isInvalid) {
3633 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003634 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003635 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003636 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003637 }
3638}
3639
3640
3641/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3642///
3643void Parser::ParseDeclarator(Declarator &D) {
3644 /// This implements the 'declarator' production in the C grammar, then checks
3645 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003646 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003647}
3648
Richard Smith9988f282012-03-29 01:16:42 +00003649static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3650 if (Kind == tok::star || Kind == tok::caret)
3651 return true;
3652
3653 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3654 if (!Lang.CPlusPlus)
3655 return false;
3656
3657 return Kind == tok::amp || Kind == tok::ampamp;
3658}
3659
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003660/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3661/// is parsed by the function passed to it. Pass null, and the direct-declarator
3662/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003663/// ptr-operator production.
3664///
Richard Smith0706df42011-10-19 21:33:05 +00003665/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003666/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3667/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003668///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003669/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3670/// [C] pointer[opt] direct-declarator
3671/// [C++] direct-declarator
3672/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003673///
3674/// pointer: [C99 6.7.5]
3675/// '*' type-qualifier-list[opt]
3676/// '*' type-qualifier-list[opt] pointer
3677///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003678/// ptr-operator:
3679/// '*' cv-qualifier-seq[opt]
3680/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003681/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003682/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003683/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003684/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003685void Parser::ParseDeclaratorInternal(Declarator &D,
3686 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003687 if (Diags.hasAllExtensionsSilenced())
3688 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003689
Sebastian Redlf30208a2009-01-24 21:16:55 +00003690 // C++ member pointers start with a '::' or a nested-name.
3691 // Member pointers get special handling, since there's no place for the
3692 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003693 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003694 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3695 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003696 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3697 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003698 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003699 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003700
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003701 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003702 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003703 // The scope spec really belongs to the direct-declarator.
3704 D.getCXXScopeSpec() = SS;
3705 if (DirectDeclParser)
3706 (this->*DirectDeclParser)(D);
3707 return;
3708 }
3709
3710 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003711 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003712 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003713 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003714 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003715
3716 // Recurse to parse whatever is left.
3717 ParseDeclaratorInternal(D, DirectDeclParser);
3718
3719 // Sema will have to catch (syntactically invalid) pointers into global
3720 // scope. It has to catch pointers into namespace scope anyway.
3721 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003722 Loc),
3723 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003724 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003725 return;
3726 }
3727 }
3728
3729 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003730 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003731 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003732 if (DirectDeclParser)
3733 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003734 return;
3735 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003736
Sebastian Redl05532f22009-03-15 22:02:01 +00003737 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3738 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003739 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003740 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003741
Chris Lattner9af55002009-03-27 04:18:06 +00003742 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003743 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003744 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003745
Richard Smith6ee326a2012-04-10 01:32:12 +00003746 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003748 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003749
Reid Spencer5f016e22007-07-11 17:01:13 +00003750 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003751 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003752 if (Kind == tok::star)
3753 // Remember that we parsed a pointer type, and remember the type-quals.
3754 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003755 DS.getConstSpecLoc(),
3756 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003757 DS.getRestrictSpecLoc()),
3758 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003759 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003760 else
3761 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003762 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003763 Loc),
3764 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003765 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003766 } else {
3767 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003768 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003769
Sebastian Redl743de1f2009-03-23 00:00:23 +00003770 // Complain about rvalue references in C++03, but then go on and build
3771 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003772 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003773 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003774 diag::warn_cxx98_compat_rvalue_reference :
3775 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003776
Richard Smith6ee326a2012-04-10 01:32:12 +00003777 // GNU-style and C++11 attributes are allowed here, as is restrict.
3778 ParseTypeQualifierListOpt(DS);
3779 D.ExtendWithDeclSpec(DS);
3780
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3782 // cv-qualifiers are introduced through the use of a typedef or of a
3783 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003784 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3785 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3786 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003787 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3789 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003790 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003791 }
3792
3793 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003794 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003795
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003796 if (D.getNumTypeObjects() > 0) {
3797 // C++ [dcl.ref]p4: There shall be no references to references.
3798 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3799 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003800 if (const IdentifierInfo *II = D.getIdentifier())
3801 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3802 << II;
3803 else
3804 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3805 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003806
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003807 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003808 // can go ahead and build the (technically ill-formed)
3809 // declarator: reference collapsing will take care of it.
3810 }
3811 }
3812
Reid Spencer5f016e22007-07-11 17:01:13 +00003813 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003814 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003815 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003816 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003817 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003818 }
3819}
3820
Richard Smith9988f282012-03-29 01:16:42 +00003821static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3822 SourceLocation EllipsisLoc) {
3823 if (EllipsisLoc.isValid()) {
3824 FixItHint Insertion;
3825 if (!D.getEllipsisLoc().isValid()) {
3826 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3827 D.setEllipsisLoc(EllipsisLoc);
3828 }
3829 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3830 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3831 }
3832}
3833
Reid Spencer5f016e22007-07-11 17:01:13 +00003834/// ParseDirectDeclarator
3835/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003836/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003837/// '(' declarator ')'
3838/// [GNU] '(' attributes declarator ')'
3839/// [C90] direct-declarator '[' constant-expression[opt] ']'
3840/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3841/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3842/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3843/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003844/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3845/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003846/// direct-declarator '(' parameter-type-list ')'
3847/// direct-declarator '(' identifier-list[opt] ')'
3848/// [GNU] direct-declarator '(' parameter-forward-declarations
3849/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003850/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3851/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003852/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3853/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3854/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003855/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003856/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003857///
3858/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003859/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003860/// '::'[opt] nested-name-specifier[opt] type-name
3861///
3862/// id-expression: [C++ 5.1]
3863/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003864/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003865///
3866/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003867/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003868/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003869/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003870/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003871/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003872///
Richard Smith5d8388c2012-03-27 01:42:32 +00003873/// Note, any additional constructs added here may need corresponding changes
3874/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003875void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003876 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003877
David Blaikie4e4d0842012-03-11 07:00:24 +00003878 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003879 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003880 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003881 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3882 D.getContext() == Declarator::MemberContext;
3883 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3884 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003885 }
3886
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003887 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003888 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003889 // Change the declaration context for name lookup, until this function
3890 // is exited (and the declarator has been parsed).
3891 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003892 }
3893
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003894 // C++0x [dcl.fct]p14:
3895 // There is a syntactic ambiguity when an ellipsis occurs at the end
3896 // of a parameter-declaration-clause without a preceding comma. In
3897 // this case, the ellipsis is parsed as part of the
3898 // abstract-declarator if the type of the parameter names a template
3899 // parameter pack that has not been expanded; otherwise, it is parsed
3900 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003901 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003902 !((D.getContext() == Declarator::PrototypeContext ||
3903 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003904 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003905 !Actions.containsUnexpandedParameterPacks(D))) {
3906 SourceLocation EllipsisLoc = ConsumeToken();
3907 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3908 // The ellipsis was put in the wrong place. Recover, and explain to
3909 // the user what they should have done.
3910 ParseDeclarator(D);
3911 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3912 return;
3913 } else
3914 D.setEllipsisLoc(EllipsisLoc);
3915
3916 // The ellipsis can't be followed by a parenthesized declarator. We
3917 // check for that in ParseParenDeclarator, after we have disambiguated
3918 // the l_paren token.
3919 }
3920
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003921 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3922 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3923 // We found something that indicates the start of an unqualified-id.
3924 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003925 bool AllowConstructorName;
3926 if (D.getDeclSpec().hasTypeSpecifier())
3927 AllowConstructorName = false;
3928 else if (D.getCXXScopeSpec().isSet())
3929 AllowConstructorName =
3930 (D.getContext() == Declarator::FileContext ||
3931 (D.getContext() == Declarator::MemberContext &&
3932 D.getDeclSpec().isFriendSpecified()));
3933 else
3934 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3935
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003936 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003937 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3938 /*EnteringContext=*/true,
3939 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003940 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003941 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003942 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003943 D.getName()) ||
3944 // Once we're past the identifier, if the scope was bad, mark the
3945 // whole declarator bad.
3946 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003947 D.SetIdentifier(0, Tok.getLocation());
3948 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003949 } else {
3950 // Parsed the unqualified-id; update range information and move along.
3951 if (D.getSourceRange().getBegin().isInvalid())
3952 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3953 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003954 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003955 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003956 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003957 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003958 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003959 "There's a C++-specific check for tok::identifier above");
3960 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3961 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3962 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003963 goto PastIdentifier;
3964 }
Richard Smith9988f282012-03-29 01:16:42 +00003965
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003966 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003967 // direct-declarator: '(' declarator ')'
3968 // direct-declarator: '(' attributes declarator ')'
3969 // Example: 'char (*X)' or 'int (*XX)(void)'
3970 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003971
3972 // If the declarator was parenthesized, we entered the declarator
3973 // scope when parsing the parenthesized declarator, then exited
3974 // the scope already. Re-enter the scope, if we need to.
3975 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003976 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003977 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003978 if (!D.isInvalidType() &&
3979 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003980 // Change the declaration context for name lookup, until this function
3981 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003982 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003983 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003984 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003985 // This could be something simple like "int" (in which case the declarator
3986 // portion is empty), if an abstract-declarator is allowed.
3987 D.SetIdentifier(0, Tok.getLocation());
3988 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003989 if (D.getContext() == Declarator::MemberContext)
3990 Diag(Tok, diag::err_expected_member_name_or_semi)
3991 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003992 else if (getLangOpts().CPlusPlus)
3993 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003994 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003995 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003996 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003997 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003998 }
Mike Stump1eb44332009-09-09 15:08:12 +00003999
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004000 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004001 assert(D.isPastIdentifier() &&
4002 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004003
Richard Smith6ee326a2012-04-10 01:32:12 +00004004 // Don't parse attributes unless we have parsed an unparenthesized name.
4005 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004006 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004007
Reid Spencer5f016e22007-07-11 17:01:13 +00004008 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004009 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004010 // Enter function-declaration scope, limiting any declarators to the
4011 // function prototype scope, including parameter declarators.
4012 ParseScope PrototypeScope(this,
4013 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004014 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4015 // In such a case, check if we actually have a function declarator; if it
4016 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004017 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004018 // When not in file scope, warn for ambiguous function declarators, just
4019 // in case the author intended it as a variable definition.
4020 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4021 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4022 break;
4023 }
John McCall0b7e6782011-03-24 11:26:52 +00004024 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004025 BalancedDelimiterTracker T(*this, tok::l_paren);
4026 T.consumeOpen();
4027 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004028 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004029 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004030 ParseBracketDeclarator(D);
4031 } else {
4032 break;
4033 }
4034 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004035}
Reid Spencer5f016e22007-07-11 17:01:13 +00004036
Chris Lattneref4715c2008-04-06 05:45:57 +00004037/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4038/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004039/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004040/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4041///
4042/// direct-declarator:
4043/// '(' declarator ')'
4044/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004045/// direct-declarator '(' parameter-type-list ')'
4046/// direct-declarator '(' identifier-list[opt] ')'
4047/// [GNU] direct-declarator '(' parameter-forward-declarations
4048/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004049///
4050void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004051 BalancedDelimiterTracker T(*this, tok::l_paren);
4052 T.consumeOpen();
4053
Chris Lattneref4715c2008-04-06 05:45:57 +00004054 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004055
Chris Lattner7399ee02008-10-20 02:05:46 +00004056 // Eat any attributes before we look at whether this is a grouping or function
4057 // declarator paren. If this is a grouping paren, the attribute applies to
4058 // the type being built up, for example:
4059 // int (__attribute__(()) *x)(long y)
4060 // If this ends up not being a grouping paren, the attribute applies to the
4061 // first argument, for example:
4062 // int (__attribute__(()) int x)
4063 // In either case, we need to eat any attributes to be able to determine what
4064 // sort of paren this is.
4065 //
John McCall0b7e6782011-03-24 11:26:52 +00004066 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004067 bool RequiresArg = false;
4068 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004069 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004070
Chris Lattner7399ee02008-10-20 02:05:46 +00004071 // We require that the argument list (if this is a non-grouping paren) be
4072 // present even if the attribute list was empty.
4073 RequiresArg = true;
4074 }
Steve Naroff239f0732008-12-25 14:16:32 +00004075 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004076 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004077 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004078 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004079 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004080 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004081 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004082 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004083 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004084 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004085
Chris Lattneref4715c2008-04-06 05:45:57 +00004086 // If we haven't past the identifier yet (or where the identifier would be
4087 // stored, if this is an abstract declarator), then this is probably just
4088 // grouping parens. However, if this could be an abstract-declarator, then
4089 // this could also be the start of function arguments (consider 'void()').
4090 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004091
Chris Lattneref4715c2008-04-06 05:45:57 +00004092 if (!D.mayOmitIdentifier()) {
4093 // If this can't be an abstract-declarator, this *must* be a grouping
4094 // paren, because we haven't seen the identifier yet.
4095 isGrouping = true;
4096 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004097 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4098 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004099 isDeclarationSpecifier() || // 'int(int)' is a function.
4100 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004101 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4102 // considered to be a type, not a K&R identifier-list.
4103 isGrouping = false;
4104 } else {
4105 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4106 isGrouping = true;
4107 }
Mike Stump1eb44332009-09-09 15:08:12 +00004108
Chris Lattneref4715c2008-04-06 05:45:57 +00004109 // If this is a grouping paren, handle:
4110 // direct-declarator: '(' declarator ')'
4111 // direct-declarator: '(' attributes declarator ')'
4112 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004113 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4114 D.setEllipsisLoc(SourceLocation());
4115
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004116 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004117 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004118 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004119 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004120 T.consumeClose();
4121 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4122 T.getCloseLocation()),
4123 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004124
4125 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004126
4127 // An ellipsis cannot be placed outside parentheses.
4128 if (EllipsisLoc.isValid())
4129 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4130
Chris Lattneref4715c2008-04-06 05:45:57 +00004131 return;
4132 }
Mike Stump1eb44332009-09-09 15:08:12 +00004133
Chris Lattneref4715c2008-04-06 05:45:57 +00004134 // Okay, if this wasn't a grouping paren, it must be the start of a function
4135 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004136 // identifier (and remember where it would have been), then call into
4137 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004138 D.SetIdentifier(0, Tok.getLocation());
4139
David Blaikie42d6d0c2011-12-04 05:04:18 +00004140 // Enter function-declaration scope, limiting any declarators to the
4141 // function prototype scope, including parameter declarators.
4142 ParseScope PrototypeScope(this,
4143 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004144 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004145 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004146}
4147
4148/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4149/// declarator D up to a paren, which indicates that we are parsing function
4150/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004151///
Richard Smith6ee326a2012-04-10 01:32:12 +00004152/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4153/// immediately after the open paren - they should be considered to be the
4154/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004155///
Richard Smith6ee326a2012-04-10 01:32:12 +00004156/// If RequiresArg is true, then the first argument of the function is required
4157/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004158///
Richard Smith6ee326a2012-04-10 01:32:12 +00004159/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4160/// (C++11) ref-qualifier[opt], exception-specification[opt],
4161/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4162///
4163/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004164/// dynamic-exception-specification
4165/// noexcept-specification
4166///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004167void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004168 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004169 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004170 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004171 assert(getCurScope()->isFunctionPrototypeScope() &&
4172 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004173 // lparen is already consumed!
4174 assert(D.isPastIdentifier() && "Should not call before identifier!");
4175
4176 // This should be true when the function has typed arguments.
4177 // Otherwise, it is treated as a K&R-style function.
4178 bool HasProto = false;
4179 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004180 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004181 // Remember where we see an ellipsis, if any.
4182 SourceLocation EllipsisLoc;
4183
4184 DeclSpec DS(AttrFactory);
4185 bool RefQualifierIsLValueRef = true;
4186 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004187 SourceLocation ConstQualifierLoc;
4188 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004189 ExceptionSpecificationType ESpecType = EST_None;
4190 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004191 SmallVector<ParsedType, 2> DynamicExceptions;
4192 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004193 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004194 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004195 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004196
James Molloy16f1f712012-02-29 10:24:19 +00004197 Actions.ActOnStartFunctionDeclarator();
4198
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004199 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004200 if (isFunctionDeclaratorIdentifierList()) {
4201 if (RequiresArg)
4202 Diag(Tok, diag::err_argument_required_after_attribute);
4203
4204 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4205
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004206 Tracker.consumeClose();
4207 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004208 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004209 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004210 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004211 else if (RequiresArg)
4212 Diag(Tok, diag::err_argument_required_after_attribute);
4213
David Blaikie4e4d0842012-03-11 07:00:24 +00004214 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004215
4216 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004217 Tracker.consumeClose();
4218 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004219
David Blaikie4e4d0842012-03-11 07:00:24 +00004220 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004221 // FIXME: Accept these components in any order, and produce fixits to
4222 // correct the order if the user gets it wrong. Ideally we should deal
4223 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004224
4225 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004226 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4227 if (!DS.getSourceRange().getEnd().isInvalid()) {
4228 EndLoc = DS.getSourceRange().getEnd();
4229 ConstQualifierLoc = DS.getConstSpecLoc();
4230 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4231 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004232
4233 // Parse ref-qualifier[opt].
4234 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004235 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004236 diag::warn_cxx98_compat_ref_qualifier :
4237 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004238
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004239 RefQualifierIsLValueRef = Tok.is(tok::amp);
4240 RefQualifierLoc = ConsumeToken();
4241 EndLoc = RefQualifierLoc;
4242 }
4243
4244 // Parse exception-specification[opt].
4245 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4246 DynamicExceptions,
4247 DynamicExceptionRanges,
4248 NoexceptExpr);
4249 if (ESpecType != EST_None)
4250 EndLoc = ESpecRange.getEnd();
4251
Richard Smith6ee326a2012-04-10 01:32:12 +00004252 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4253 // after the exception-specification.
4254 MaybeParseCXX0XAttributes(FnAttrs);
4255
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004256 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004257 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004258 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004259 SourceRange Range;
4260 TrailingReturnType = ParseTrailingReturnType(Range).get();
4261 if (Range.getEnd().isValid())
4262 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004263 }
4264 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004265 }
4266
4267 // Remember that we parsed a function type, and remember the attributes.
4268 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4269 /*isVariadic=*/EllipsisLoc.isValid(),
4270 EllipsisLoc,
4271 ParamInfo.data(), ParamInfo.size(),
4272 DS.getTypeQualifiers(),
4273 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004274 RefQualifierLoc, ConstQualifierLoc,
4275 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004276 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004277 ESpecType, ESpecRange.getBegin(),
4278 DynamicExceptions.data(),
4279 DynamicExceptionRanges.data(),
4280 DynamicExceptions.size(),
4281 NoexceptExpr.isUsable() ?
4282 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004283 Tracker.getOpenLocation(),
4284 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004285 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004286 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004287
4288 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004289}
4290
4291/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4292/// identifier list form for a K&R-style function: void foo(a,b,c)
4293///
4294/// Note that identifier-lists are only allowed for normal declarators, not for
4295/// abstract-declarators.
4296bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004297 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004298 && Tok.is(tok::identifier)
4299 && !TryAltiVecVectorToken()
4300 // K&R identifier lists can't have typedefs as identifiers, per C99
4301 // 6.7.5.3p11.
4302 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4303 // Identifier lists follow a really simple grammar: the identifiers can
4304 // be followed *only* by a ", identifier" or ")". However, K&R
4305 // identifier lists are really rare in the brave new modern world, and
4306 // it is very common for someone to typo a type in a non-K&R style
4307 // list. If we are presented with something like: "void foo(intptr x,
4308 // float y)", we don't want to start parsing the function declarator as
4309 // though it is a K&R style declarator just because intptr is an
4310 // invalid type.
4311 //
4312 // To handle this, we check to see if the token after the first
4313 // identifier is a "," or ")". Only then do we parse it as an
4314 // identifier list.
4315 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4316}
4317
4318/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4319/// we found a K&R-style identifier list instead of a typed parameter list.
4320///
4321/// After returning, ParamInfo will hold the parsed parameters.
4322///
4323/// identifier-list: [C99 6.7.5]
4324/// identifier
4325/// identifier-list ',' identifier
4326///
4327void Parser::ParseFunctionDeclaratorIdentifierList(
4328 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004329 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004330 // If there was no identifier specified for the declarator, either we are in
4331 // an abstract-declarator, or we are in a parameter declarator which was found
4332 // to be abstract. In abstract-declarators, identifier lists are not valid:
4333 // diagnose this.
4334 if (!D.getIdentifier())
4335 Diag(Tok, diag::ext_ident_list_in_param);
4336
4337 // Maintain an efficient lookup of params we have seen so far.
4338 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4339
4340 while (1) {
4341 // If this isn't an identifier, report the error and skip until ')'.
4342 if (Tok.isNot(tok::identifier)) {
4343 Diag(Tok, diag::err_expected_ident);
4344 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4345 // Forget we parsed anything.
4346 ParamInfo.clear();
4347 return;
4348 }
4349
4350 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4351
4352 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4353 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4354 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4355
4356 // Verify that the argument identifier has not already been mentioned.
4357 if (!ParamsSoFar.insert(ParmII)) {
4358 Diag(Tok, diag::err_param_redefinition) << ParmII;
4359 } else {
4360 // Remember this identifier in ParamInfo.
4361 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4362 Tok.getLocation(),
4363 0));
4364 }
4365
4366 // Eat the identifier.
4367 ConsumeToken();
4368
4369 // The list continues if we see a comma.
4370 if (Tok.isNot(tok::comma))
4371 break;
4372 ConsumeToken();
4373 }
4374}
4375
4376/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4377/// after the opening parenthesis. This function will not parse a K&R-style
4378/// identifier list.
4379///
Richard Smith6ce48a72012-04-11 04:01:28 +00004380/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4381/// caller parsed those arguments immediately after the open paren - they should
4382/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004383///
4384/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4385/// be the location of the ellipsis, if any was parsed.
4386///
Reid Spencer5f016e22007-07-11 17:01:13 +00004387/// parameter-type-list: [C99 6.7.5]
4388/// parameter-list
4389/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004390/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004391///
4392/// parameter-list: [C99 6.7.5]
4393/// parameter-declaration
4394/// parameter-list ',' parameter-declaration
4395///
4396/// parameter-declaration: [C99 6.7.5]
4397/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004398/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004399/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004400/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004401/// declaration-specifiers abstract-declarator[opt]
4402/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004403/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004404/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004405/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004406///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004407void Parser::ParseParameterDeclarationClause(
4408 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004409 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004410 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004411 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004412
Chris Lattnerf97409f2008-04-06 06:57:35 +00004413 while (1) {
4414 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004415 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4416 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004417 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004418 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004419 }
Mike Stump1eb44332009-09-09 15:08:12 +00004420
Chris Lattnerf97409f2008-04-06 06:57:35 +00004421 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004422 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004423 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004424
Richard Smith6ce48a72012-04-11 04:01:28 +00004425 // Parse any C++11 attributes.
4426 MaybeParseCXX0XAttributes(DS.getAttributes());
4427
John McCall7f040a92010-12-24 02:08:15 +00004428 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004429 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004430 ParseMicrosoftAttributes(DS.getAttributes());
4431
4432 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004433
4434 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004435 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004436 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004437 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4438 // too much hassle.
4439 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004440
Chris Lattnere64c5492009-02-27 18:38:20 +00004441 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004442
Chris Lattnerf97409f2008-04-06 06:57:35 +00004443 // Parse the declarator. This is "PrototypeContext", because we must
4444 // accept either 'declarator' or 'abstract-declarator' here.
4445 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4446 ParseDeclarator(ParmDecl);
4447
4448 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004449 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004450
Chris Lattnerf97409f2008-04-06 06:57:35 +00004451 // Remember this parsed parameter in ParamInfo.
4452 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004453
Douglas Gregor72b505b2008-12-16 21:30:33 +00004454 // DefArgToks is used when the parsing of default arguments needs
4455 // to be delayed.
4456 CachedTokens *DefArgToks = 0;
4457
Chris Lattnerf97409f2008-04-06 06:57:35 +00004458 // If no parameter was specified, verify that *something* was specified,
4459 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004460 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4461 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004462 // Completely missing, emit error.
4463 Diag(DSStart, diag::err_missing_param);
4464 } else {
4465 // Otherwise, we have something. Add it and let semantic analysis try
4466 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004467
Chris Lattnerf97409f2008-04-06 06:57:35 +00004468 // Inform the actions module about the parameter declarator, so it gets
4469 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004470 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004471
4472 // Parse the default argument, if any. We parse the default
4473 // arguments in all dialects; the semantic analysis in
4474 // ActOnParamDefaultArgument will reject the default argument in
4475 // C.
4476 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004477 SourceLocation EqualLoc = Tok.getLocation();
4478
Chris Lattner04421082008-04-08 04:40:51 +00004479 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004480 if (D.getContext() == Declarator::MemberContext) {
4481 // If we're inside a class definition, cache the tokens
4482 // corresponding to the default argument. We'll actually parse
4483 // them when we see the end of the class definition.
4484 // FIXME: Templates will require something similar.
4485 // FIXME: Can we use a smart pointer for Toks?
4486 DefArgToks = new CachedTokens;
4487
Mike Stump1eb44332009-09-09 15:08:12 +00004488 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004489 /*StopAtSemi=*/true,
4490 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004491 delete DefArgToks;
4492 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004493 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004494 } else {
4495 // Mark the end of the default argument so that we know when to
4496 // stop when we parse it later on.
4497 Token DefArgEnd;
4498 DefArgEnd.startToken();
4499 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4500 DefArgEnd.setLocation(Tok.getLocation());
4501 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004502 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004503 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004504 }
Chris Lattner04421082008-04-08 04:40:51 +00004505 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004506 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004507 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004508
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004509 // The argument isn't actually potentially evaluated unless it is
4510 // used.
4511 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004512 Sema::PotentiallyEvaluatedIfUsed,
4513 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004514
Sebastian Redl84407ba2012-03-14 15:54:00 +00004515 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004516 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4517 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004518 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004519 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004520 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004521 if (DefArgResult.isInvalid()) {
4522 Actions.ActOnParamDefaultArgumentError(Param);
4523 SkipUntil(tok::comma, tok::r_paren, true, true);
4524 } else {
4525 // Inform the actions module about the default argument
4526 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004527 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004528 }
Chris Lattner04421082008-04-08 04:40:51 +00004529 }
4530 }
Mike Stump1eb44332009-09-09 15:08:12 +00004531
4532 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4533 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004534 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004535 }
4536
4537 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004538 if (Tok.isNot(tok::comma)) {
4539 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004540 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4541
David Blaikie4e4d0842012-03-11 07:00:24 +00004542 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004543 // We have ellipsis without a preceding ',', which is ill-formed
4544 // in C. Complain and provide the fix.
4545 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004546 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004547 }
4548 }
4549
4550 break;
4551 }
Mike Stump1eb44332009-09-09 15:08:12 +00004552
Chris Lattnerf97409f2008-04-06 06:57:35 +00004553 // Consume the comma.
4554 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004555 }
Mike Stump1eb44332009-09-09 15:08:12 +00004556
Chris Lattner66d28652008-04-06 06:34:08 +00004557}
Chris Lattneref4715c2008-04-06 05:45:57 +00004558
Reid Spencer5f016e22007-07-11 17:01:13 +00004559/// [C90] direct-declarator '[' constant-expression[opt] ']'
4560/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4561/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4562/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4563/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004564/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4565/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004566void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004567 if (CheckProhibitedCXX11Attribute())
4568 return;
4569
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004570 BalancedDelimiterTracker T(*this, tok::l_square);
4571 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004572
Chris Lattner378c7e42008-12-18 07:27:21 +00004573 // C array syntax has many features, but by-far the most common is [] and [4].
4574 // This code does a fast path to handle some of the most obvious cases.
4575 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004576 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004577 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004578 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004579
Chris Lattner378c7e42008-12-18 07:27:21 +00004580 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004581 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004582 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004583 T.getOpenLocation(),
4584 T.getCloseLocation()),
4585 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004586 return;
4587 } else if (Tok.getKind() == tok::numeric_constant &&
4588 GetLookAheadToken(1).is(tok::r_square)) {
4589 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004590 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004591 ConsumeToken();
4592
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004593 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004594 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004595 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004596
Chris Lattner378c7e42008-12-18 07:27:21 +00004597 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004598 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004599 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004600 T.getOpenLocation(),
4601 T.getCloseLocation()),
4602 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004603 return;
4604 }
Mike Stump1eb44332009-09-09 15:08:12 +00004605
Reid Spencer5f016e22007-07-11 17:01:13 +00004606 // If valid, this location is the position where we read the 'static' keyword.
4607 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004608 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004609 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004610
Reid Spencer5f016e22007-07-11 17:01:13 +00004611 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004612 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004613 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004614 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004615
Reid Spencer5f016e22007-07-11 17:01:13 +00004616 // If we haven't already read 'static', check to see if there is one after the
4617 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004618 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004619 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004620
Reid Spencer5f016e22007-07-11 17:01:13 +00004621 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4622 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004623 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004624
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004625 // Handle the case where we have '[*]' as the array size. However, a leading
4626 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4627 // the the token after the star is a ']'. Since stars in arrays are
4628 // infrequent, use of lookahead is not costly here.
4629 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004630 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004631
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004632 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004633 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004634 StaticLoc = SourceLocation(); // Drop the static.
4635 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004636 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004637 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004638 // Note, in C89, this production uses the constant-expr production instead
4639 // of assignment-expr. The only difference is that assignment-expr allows
4640 // things like '=' and '*='. Sema rejects these in C89 mode because they
4641 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004642
Douglas Gregore0762c92009-06-19 23:52:42 +00004643 // Parse the constant-expression or assignment-expression now (depending
4644 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004645 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004646 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004647 } else {
4648 EnterExpressionEvaluationContext Unevaluated(Actions,
4649 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004650 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004651 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004652 }
Mike Stump1eb44332009-09-09 15:08:12 +00004653
Reid Spencer5f016e22007-07-11 17:01:13 +00004654 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004655 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004656 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004657 // If the expression was invalid, skip it.
4658 SkipUntil(tok::r_square);
4659 return;
4660 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004661
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004662 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004663
John McCall0b7e6782011-03-24 11:26:52 +00004664 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004665 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004666
Chris Lattner378c7e42008-12-18 07:27:21 +00004667 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004668 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004669 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004670 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004671 T.getOpenLocation(),
4672 T.getCloseLocation()),
4673 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004674}
4675
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004676/// [GNU] typeof-specifier:
4677/// typeof ( expressions )
4678/// typeof ( type-name )
4679/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004680///
4681void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004682 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004683 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004684 SourceLocation StartLoc = ConsumeToken();
4685
John McCallcfb708c2010-01-13 20:03:27 +00004686 const bool hasParens = Tok.is(tok::l_paren);
4687
Eli Friedman71b8fb52012-01-21 01:01:51 +00004688 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4689
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004690 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004691 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004692 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004693 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4694 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004695 if (hasParens)
4696 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004697
4698 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004699 // FIXME: Not accurate, the range gets one token more than it should.
4700 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004701 else
4702 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004703
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004704 if (isCastExpr) {
4705 if (!CastTy) {
4706 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004707 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004708 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004709
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004710 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004711 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004712 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4713 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004714 DiagID, CastTy))
4715 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004716 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004717 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004718
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004719 // If we get here, the operand to the typeof was an expresion.
4720 if (Operand.isInvalid()) {
4721 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004722 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004723 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004724
Eli Friedman71b8fb52012-01-21 01:01:51 +00004725 // We might need to transform the operand if it is potentially evaluated.
4726 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4727 if (Operand.isInvalid()) {
4728 DS.SetTypeSpecError();
4729 return;
4730 }
4731
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004732 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004733 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004734 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4735 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004736 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004737 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004738}
Chris Lattner1b492422010-02-28 18:33:55 +00004739
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004740/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004741/// _Atomic ( type-name )
4742///
4743void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4744 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4745
4746 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004747 BalancedDelimiterTracker T(*this, tok::l_paren);
4748 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004749 SkipUntil(tok::r_paren);
4750 return;
4751 }
4752
4753 TypeResult Result = ParseTypeName();
4754 if (Result.isInvalid()) {
4755 SkipUntil(tok::r_paren);
4756 return;
4757 }
4758
4759 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004760 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004761
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004762 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004763 return;
4764
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004765 DS.setTypeofParensRange(T.getRange());
4766 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004767
4768 const char *PrevSpec = 0;
4769 unsigned DiagID;
4770 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4771 DiagID, Result.release()))
4772 Diag(StartLoc, DiagID) << PrevSpec;
4773}
4774
Chris Lattner1b492422010-02-28 18:33:55 +00004775
4776/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4777/// from TryAltiVecVectorToken.
4778bool Parser::TryAltiVecVectorTokenOutOfLine() {
4779 Token Next = NextToken();
4780 switch (Next.getKind()) {
4781 default: return false;
4782 case tok::kw_short:
4783 case tok::kw_long:
4784 case tok::kw_signed:
4785 case tok::kw_unsigned:
4786 case tok::kw_void:
4787 case tok::kw_char:
4788 case tok::kw_int:
4789 case tok::kw_float:
4790 case tok::kw_double:
4791 case tok::kw_bool:
4792 case tok::kw___pixel:
4793 Tok.setKind(tok::kw___vector);
4794 return true;
4795 case tok::identifier:
4796 if (Next.getIdentifierInfo() == Ident_pixel) {
4797 Tok.setKind(tok::kw___vector);
4798 return true;
4799 }
4800 return false;
4801 }
4802}
4803
4804bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4805 const char *&PrevSpec, unsigned &DiagID,
4806 bool &isInvalid) {
4807 if (Tok.getIdentifierInfo() == Ident_vector) {
4808 Token Next = NextToken();
4809 switch (Next.getKind()) {
4810 case tok::kw_short:
4811 case tok::kw_long:
4812 case tok::kw_signed:
4813 case tok::kw_unsigned:
4814 case tok::kw_void:
4815 case tok::kw_char:
4816 case tok::kw_int:
4817 case tok::kw_float:
4818 case tok::kw_double:
4819 case tok::kw_bool:
4820 case tok::kw___pixel:
4821 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4822 return true;
4823 case tok::identifier:
4824 if (Next.getIdentifierInfo() == Ident_pixel) {
4825 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4826 return true;
4827 }
4828 break;
4829 default:
4830 break;
4831 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004832 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004833 DS.isTypeAltiVecVector()) {
4834 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4835 return true;
4836 }
4837 return false;
4838}