blob: 9216b23d316b2b1f54ef60e04fc246cb0ef322c5 [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
738 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
739 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
740 }
741}
742
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000743
744/// \brief Parse all attributes in LAs, and attach them to Decl D.
745void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
746 bool EnterScope, bool OnDefinition) {
747 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000748 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000749 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
750 }
751 LAs.clear();
752}
753
754
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755/// \brief Finish parsing an attribute for which parsing was delayed.
756/// This will be called at the end of parsing a class declaration
757/// for each LateParsedAttribute. We consume the saved tokens and
758/// create an attribute with the arguments filled in. We add this
759/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000760void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
761 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000762 // Save the current token position.
763 SourceLocation OrigLoc = Tok.getLocation();
764
765 // Append the current token at the end of the new token stream so that it
766 // doesn't get lost.
767 LA.Toks.push_back(Tok);
768 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
769 // Consume the previously pushed token.
770 ConsumeAnyToken();
771
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000772 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
773 Diag(Tok, diag::warn_attribute_on_function_definition)
774 << LA.AttrName.getName();
775 }
776
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000777 ParsedAttributes Attrs(AttrFactory);
778 SourceLocation endLoc;
779
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000780 if (LA.Decls.size() == 1) {
781 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000782
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000783 // If the Decl is templatized, add template parameters to scope.
784 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
785 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
786 if (HasTemplateScope)
787 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000788
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000789 // If the Decl is on a function, add function parameters to the scope.
790 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
791 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
792 if (HasFunctionScope)
793 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
794
795 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
796
797 if (HasFunctionScope) {
798 Actions.ActOnExitFunctionContext();
799 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
800 }
801 if (HasTemplateScope) {
802 TempScope.Exit();
803 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000804 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000805 // If there are multiple decls, then the decl cannot be within the
806 // function scope.
807 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000808 } else {
809 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000810 }
811
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000812 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
813 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
814 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000815
816 if (Tok.getLocation() != OrigLoc) {
817 // Due to a parsing error, we either went over the cached tokens or
818 // there are still cached tokens left, so we skip the leftover tokens.
819 // Since this is an uncommon situation that should be avoided, use the
820 // expensive isBeforeInTranslationUnit call.
821 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
822 OrigLoc))
823 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000824 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000825 }
826}
827
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000828/// \brief Wrapper around a case statement checking if AttrName is
829/// one of the thread safety attributes
830bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
831 return llvm::StringSwitch<bool>(AttrName)
832 .Case("guarded_by", true)
833 .Case("guarded_var", true)
834 .Case("pt_guarded_by", true)
835 .Case("pt_guarded_var", true)
836 .Case("lockable", true)
837 .Case("scoped_lockable", true)
838 .Case("no_thread_safety_analysis", true)
839 .Case("acquired_after", true)
840 .Case("acquired_before", true)
841 .Case("exclusive_lock_function", true)
842 .Case("shared_lock_function", true)
843 .Case("exclusive_trylock_function", true)
844 .Case("shared_trylock_function", true)
845 .Case("unlock_function", true)
846 .Case("lock_returned", true)
847 .Case("locks_excluded", true)
848 .Case("exclusive_locks_required", true)
849 .Case("shared_locks_required", true)
850 .Default(false);
851}
852
853/// \brief Parse the contents of thread safety attributes. These
854/// should always be parsed as an expression list.
855///
856/// We need to special case the parsing due to the fact that if the first token
857/// of the first argument is an identifier, the main parse loop will store
858/// that token as a "parameter" and the rest of
859/// the arguments will be added to a list of "arguments". However,
860/// subsequent tokens in the first argument are lost. We instead parse each
861/// argument as an expression and add all arguments to the list of "arguments".
862/// In future, we will take advantage of this special case to also
863/// deal with some argument scoping issues here (for example, referring to a
864/// function parameter in the attribute on that function).
865void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
866 SourceLocation AttrNameLoc,
867 ParsedAttributes &Attrs,
868 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000869 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000871 BalancedDelimiterTracker T(*this, tok::l_paren);
872 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000873
874 ExprVector ArgExprs(Actions);
875 bool ArgExprsOk = true;
876
877 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000878 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000879 ExprResult ArgExpr(ParseAssignmentExpression());
880 if (ArgExpr.isInvalid()) {
881 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000882 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000883 break;
884 } else {
885 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887 if (Tok.isNot(tok::comma))
888 break;
889 ConsumeToken(); // Eat the comma, move to the next argument
890 }
891 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000892 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
894 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000896 if (EndLoc)
897 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000898}
899
John McCall7f040a92010-12-24 02:08:15 +0000900void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
901 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
902 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000903}
904
Reid Spencer5f016e22007-07-11 17:01:13 +0000905/// ParseDeclaration - Parse a full 'declaration', which consists of
906/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000907/// 'Context' should be a Declarator::TheContext value. This returns the
908/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000909///
910/// declaration: [C99 6.7]
911/// block-declaration ->
912/// simple-declaration
913/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000914/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000915/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000916/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000917/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000918/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000919/// others... [FIXME]
920///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000921Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
922 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000923 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000924 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000925 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000926 // Must temporarily exit the objective-c container scope for
927 // parsing c none objective-c decls.
928 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000929
John McCalld226f652010-08-21 09:40:31 +0000930 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000931 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000932 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000933 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000934 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000935 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000936 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000937 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000938 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000939 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000940 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000941 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000942 SourceLocation InlineLoc = ConsumeToken();
943 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
944 break;
945 }
John McCall7f040a92010-12-24 02:08:15 +0000946 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000947 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000948 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000949 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000950 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000951 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000952 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000953 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000954 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000955 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000956 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000957 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000958 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000959 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000960 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000961 default:
John McCall7f040a92010-12-24 02:08:15 +0000962 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000963 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000964
Chris Lattner682bf922009-03-29 16:50:03 +0000965 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000966 // single decl, convert it now. Alias declarations can also declare a type;
967 // include that too if it is present.
968 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000969}
970
971/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
972/// declaration-specifiers init-declarator-list[opt] ';'
973///[C90/C++]init-declarator-list ';' [TODO]
974/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000975///
Richard Smithad762fc2011-04-14 22:09:26 +0000976/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
977/// attribute-specifier-seq[opt] type-specifier-seq declarator
978///
Chris Lattnercd147752009-03-29 17:27:48 +0000979/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000980/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000981///
982/// If FRI is non-null, we might be parsing a for-range-declaration instead
983/// of a simple-declaration. If we find that we are, we also parse the
984/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000985Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
986 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000987 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000988 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000989 bool RequireSemi,
990 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000992 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000993 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000994
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000995 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000996 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
999 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001000 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001001 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001002 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001003 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001004 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001007
1008 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001009}
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Richard Smith0706df42011-10-19 21:33:05 +00001011/// Returns true if this might be the start of a declarator, or a common typo
1012/// for a declarator.
1013bool Parser::MightBeDeclarator(unsigned Context) {
1014 switch (Tok.getKind()) {
1015 case tok::annot_cxxscope:
1016 case tok::annot_template_id:
1017 case tok::caret:
1018 case tok::code_completion:
1019 case tok::coloncolon:
1020 case tok::ellipsis:
1021 case tok::kw___attribute:
1022 case tok::kw_operator:
1023 case tok::l_paren:
1024 case tok::star:
1025 return true;
1026
1027 case tok::amp:
1028 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001029 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001030
Richard Smith1c94c162012-01-09 22:31:44 +00001031 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001032 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001033 NextToken().is(tok::l_square);
1034
1035 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001036 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001037
Richard Smith0706df42011-10-19 21:33:05 +00001038 case tok::identifier:
1039 switch (NextToken().getKind()) {
1040 case tok::code_completion:
1041 case tok::coloncolon:
1042 case tok::comma:
1043 case tok::equal:
1044 case tok::equalequal: // Might be a typo for '='.
1045 case tok::kw_alignas:
1046 case tok::kw_asm:
1047 case tok::kw___attribute:
1048 case tok::l_brace:
1049 case tok::l_paren:
1050 case tok::l_square:
1051 case tok::less:
1052 case tok::r_brace:
1053 case tok::r_paren:
1054 case tok::r_square:
1055 case tok::semi:
1056 return true;
1057
1058 case tok::colon:
1059 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001060 // and in block scope it's probably a label. Inside a class definition,
1061 // this is a bit-field.
1062 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001063 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001064
1065 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001066 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001067
1068 default:
1069 return false;
1070 }
1071
1072 default:
1073 return false;
1074 }
1075}
1076
John McCalld8ac0572009-11-03 19:26:08 +00001077/// ParseDeclGroup - Having concluded that this is either a function
1078/// definition or a group of object declarations, actually parse the
1079/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001080Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1081 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001082 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001083 SourceLocation *DeclEnd,
1084 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001085 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001086 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001087 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001088
John McCalld8ac0572009-11-03 19:26:08 +00001089 // Bail out if the first declarator didn't seem well-formed.
1090 if (!D.hasName() && !D.mayOmitIdentifier()) {
1091 // Skip until ; or }.
1092 SkipUntil(tok::r_brace, true, true);
1093 if (Tok.is(tok::semi))
1094 ConsumeToken();
1095 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001096 }
Mike Stump1eb44332009-09-09 15:08:12 +00001097
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001098 // Save late-parsed attributes for now; they need to be parsed in the
1099 // appropriate function scope after the function Decl has been constructed.
1100 LateParsedAttrList LateParsedAttrs;
1101 if (D.isFunctionDeclarator())
1102 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1103
Chris Lattnerc82daef2010-07-11 22:24:20 +00001104 // Check to see if we have a function *definition* which must have a body.
1105 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1106 // Look at the next token to make sure that this isn't a function
1107 // declaration. We have to check this because __attribute__ might be the
1108 // start of a function definition in GCC-extended K&R C.
1109 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001110
Chris Lattner004659a2010-07-11 22:42:07 +00001111 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001112 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1113 Diag(Tok, diag::err_function_declared_typedef);
1114
1115 // Recover by treating the 'typedef' as spurious.
1116 DS.ClearStorageClassSpecs();
1117 }
1118
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001119 Decl *TheDecl =
1120 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001121 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001122 }
1123
1124 if (isDeclarationSpecifier()) {
1125 // If there is an invalid declaration specifier right after the function
1126 // prototype, then we must be in a missing semicolon case where this isn't
1127 // actually a body. Just fall through into the code that handles it as a
1128 // prototype, and let the top-level code handle the erroneous declspec
1129 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001130 } else {
1131 Diag(Tok, diag::err_expected_fn_body);
1132 SkipUntil(tok::semi);
1133 return DeclGroupPtrTy();
1134 }
1135 }
1136
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001137 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001138 return DeclGroupPtrTy();
1139
1140 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1141 // must parse and analyze the for-range-initializer before the declaration is
1142 // analyzed.
1143 if (FRI && Tok.is(tok::colon)) {
1144 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001145 if (Tok.is(tok::l_brace))
1146 FRI->RangeExpr = ParseBraceInitializer();
1147 else
1148 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001149 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1150 Actions.ActOnCXXForRangeDecl(ThisDecl);
1151 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001152 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001153 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1154 }
1155
Chris Lattner5f9e2722011-07-23 10:55:15 +00001156 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001157 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001158 if (LateParsedAttrs.size() > 0)
1159 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001160 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001161 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001162 DeclsInGroup.push_back(FirstDecl);
1163
Richard Smith0706df42011-10-19 21:33:05 +00001164 bool ExpectSemi = Context != Declarator::ForContext;
1165
John McCalld8ac0572009-11-03 19:26:08 +00001166 // If we don't have a comma, it is either the end of the list (a ';') or an
1167 // error, bail out.
1168 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001169 SourceLocation CommaLoc = ConsumeToken();
1170
1171 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1172 // This comma was followed by a line-break and something which can't be
1173 // the start of a declarator. The comma was probably a typo for a
1174 // semicolon.
1175 Diag(CommaLoc, diag::err_expected_semi_declaration)
1176 << FixItHint::CreateReplacement(CommaLoc, ";");
1177 ExpectSemi = false;
1178 break;
1179 }
John McCalld8ac0572009-11-03 19:26:08 +00001180
1181 // Parse the next declarator.
1182 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001183 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001184
1185 // Accept attributes in an init-declarator. In the first declarator in a
1186 // declaration, these would be part of the declspec. In subsequent
1187 // declarators, they become part of the declarator itself, so that they
1188 // don't apply to declarators after *this* one. Examples:
1189 // short __attribute__((common)) var; -> declspec
1190 // short var __attribute__((common)); -> declarator
1191 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001192 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001193
1194 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001195 if (!D.isInvalidType()) {
1196 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1197 D.complete(ThisDecl);
1198 if (ThisDecl)
1199 DeclsInGroup.push_back(ThisDecl);
1200 }
John McCalld8ac0572009-11-03 19:26:08 +00001201 }
1202
1203 if (DeclEnd)
1204 *DeclEnd = Tok.getLocation();
1205
Richard Smith0706df42011-10-19 21:33:05 +00001206 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001207 ExpectAndConsume(tok::semi,
1208 Context == Declarator::FileContext
1209 ? diag::err_invalid_token_after_toplevel_declarator
1210 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001211 // Okay, there was no semicolon and one was expected. If we see a
1212 // declaration specifier, just assume it was missing and continue parsing.
1213 // Otherwise things are very confused and we skip to recover.
1214 if (!isDeclarationSpecifier()) {
1215 SkipUntil(tok::r_brace, true, true);
1216 if (Tok.is(tok::semi))
1217 ConsumeToken();
1218 }
John McCalld8ac0572009-11-03 19:26:08 +00001219 }
1220
Douglas Gregor23c94db2010-07-02 17:43:08 +00001221 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001222 DeclsInGroup.data(),
1223 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
Richard Smithad762fc2011-04-14 22:09:26 +00001226/// Parse an optional simple-asm-expr and attributes, and attach them to a
1227/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001228bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001229 // If a simple-asm-expr is present, parse it.
1230 if (Tok.is(tok::kw_asm)) {
1231 SourceLocation Loc;
1232 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1233 if (AsmLabel.isInvalid()) {
1234 SkipUntil(tok::semi, true, true);
1235 return true;
1236 }
1237
1238 D.setAsmLabel(AsmLabel.release());
1239 D.SetRangeEnd(Loc);
1240 }
1241
1242 MaybeParseGNUAttributes(D);
1243 return false;
1244}
1245
Douglas Gregor1426e532009-05-12 21:31:51 +00001246/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1247/// declarator'. This method parses the remainder of the declaration
1248/// (including any attributes or initializer, among other things) and
1249/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001250///
Reid Spencer5f016e22007-07-11 17:01:13 +00001251/// init-declarator: [C99 6.7]
1252/// declarator
1253/// declarator '=' initializer
1254/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1255/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001256/// [C++] declarator initializer[opt]
1257///
1258/// [C++] initializer:
1259/// [C++] '=' initializer-clause
1260/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001261/// [C++0x] '=' 'default' [TODO]
1262/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001263/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001264///
1265/// According to the standard grammar, =default and =delete are function
1266/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001267///
John McCalld226f652010-08-21 09:40:31 +00001268Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001269 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001270 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001271 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Richard Smithad762fc2011-04-14 22:09:26 +00001273 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1274}
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Richard Smithad762fc2011-04-14 22:09:26 +00001276Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1277 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001278 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001279 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001280 switch (TemplateInfo.Kind) {
1281 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001282 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001283 break;
1284
1285 case ParsedTemplateInfo::Template:
1286 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001287 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001288 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001289 TemplateInfo.TemplateParams->data(),
1290 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001291 D);
1292 break;
1293
1294 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001295 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001296 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001297 TemplateInfo.ExternLoc,
1298 TemplateInfo.TemplateLoc,
1299 D);
1300 if (ThisRes.isInvalid()) {
1301 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001302 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001303 }
1304
1305 ThisDecl = ThisRes.get();
1306 break;
1307 }
1308 }
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Richard Smith34b41d92011-02-20 03:19:35 +00001310 bool TypeContainsAuto =
1311 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1312
Douglas Gregor1426e532009-05-12 21:31:51 +00001313 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001314 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001315 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001316 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001317 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001318 if (D.isFunctionDeclarator())
1319 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1320 << 1 /* delete */;
1321 else
1322 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001323 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001324 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001325 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1326 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001327 else
1328 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001329 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001330 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001331 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001332 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001333 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001334
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001335 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001336 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001337 cutOffParsing();
1338 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001339 }
1340
John McCall60d7b3a2010-08-24 06:29:42 +00001341 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001342
David Blaikie4e4d0842012-03-11 07:00:24 +00001343 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001344 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001345 ExitScope();
1346 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001347
Douglas Gregor1426e532009-05-12 21:31:51 +00001348 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001349 SkipUntil(tok::comma, true, true);
1350 Actions.ActOnInitializerError(ThisDecl);
1351 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001352 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1353 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001354 }
1355 } else if (Tok.is(tok::l_paren)) {
1356 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001357 BalancedDelimiterTracker T(*this, tok::l_paren);
1358 T.consumeOpen();
1359
Douglas Gregor1426e532009-05-12 21:31:51 +00001360 ExprVector Exprs(Actions);
1361 CommaLocsTy CommaLocs;
1362
David Blaikie4e4d0842012-03-11 07:00:24 +00001363 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001364 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001365 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001366 }
1367
Douglas Gregor1426e532009-05-12 21:31:51 +00001368 if (ParseExpressionList(Exprs, CommaLocs)) {
1369 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001370
David Blaikie4e4d0842012-03-11 07:00:24 +00001371 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001372 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001373 ExitScope();
1374 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001375 } else {
1376 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001377 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001378
1379 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1380 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001381
David Blaikie4e4d0842012-03-11 07:00:24 +00001382 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001383 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001384 ExitScope();
1385 }
1386
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001387 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1388 T.getCloseLocation(),
1389 move_arg(Exprs));
1390 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1391 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001392 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001393 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001394 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001395 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1396
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001397 if (D.getCXXScopeSpec().isSet()) {
1398 EnterScope(0);
1399 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1400 }
1401
1402 ExprResult Init(ParseBraceInitializer());
1403
1404 if (D.getCXXScopeSpec().isSet()) {
1405 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1406 ExitScope();
1407 }
1408
1409 if (Init.isInvalid()) {
1410 Actions.ActOnInitializerError(ThisDecl);
1411 } else
1412 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1413 /*DirectInit=*/true, TypeContainsAuto);
1414
Douglas Gregor1426e532009-05-12 21:31:51 +00001415 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001416 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001417 }
1418
Richard Smith483b9f32011-02-21 20:05:19 +00001419 Actions.FinalizeDeclaration(ThisDecl);
1420
Douglas Gregor1426e532009-05-12 21:31:51 +00001421 return ThisDecl;
1422}
1423
Reid Spencer5f016e22007-07-11 17:01:13 +00001424/// ParseSpecifierQualifierList
1425/// specifier-qualifier-list:
1426/// type-specifier specifier-qualifier-list[opt]
1427/// type-qualifier specifier-qualifier-list[opt]
1428/// [GNU] attributes specifier-qualifier-list[opt]
1429///
Richard Smith69730c12012-03-12 07:56:15 +00001430void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1431 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1433 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001434 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001435 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Reid Spencer5f016e22007-07-11 17:01:13 +00001437 // Validate declspec for type-name.
1438 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001439 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1440 Diag(Tok, diag::err_expected_type);
1441 DS.SetTypeSpecError();
1442 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1443 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001445 if (!DS.hasTypeSpecifier())
1446 DS.SetTypeSpecError();
1447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 // Issue diagnostic and remove storage class if present.
1450 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1451 if (DS.getStorageClassSpecLoc().isValid())
1452 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1453 else
1454 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1455 DS.ClearStorageClassSpecs();
1456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 // Issue diagnostic and remove function specfier if present.
1459 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001460 if (DS.isInlineSpecified())
1461 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1462 if (DS.isVirtualSpecified())
1463 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1464 if (DS.isExplicitSpecified())
1465 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 DS.ClearFunctionSpecs();
1467 }
Richard Smith69730c12012-03-12 07:56:15 +00001468
1469 // Issue diagnostic and remove constexpr specfier if present.
1470 if (DS.isConstexprSpecified()) {
1471 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1472 DS.ClearConstexprSpec();
1473 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001474}
1475
Chris Lattnerc199ab32009-04-12 20:42:31 +00001476/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1477/// specified token is valid after the identifier in a declarator which
1478/// immediately follows the declspec. For example, these things are valid:
1479///
1480/// int x [ 4]; // direct-declarator
1481/// int x ( int y); // direct-declarator
1482/// int(int x ) // direct-declarator
1483/// int x ; // simple-declaration
1484/// int x = 17; // init-declarator-list
1485/// int x , y; // init-declarator-list
1486/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001487/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001488/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001489///
1490/// This is not, because 'x' does not immediately follow the declspec (though
1491/// ')' happens to be valid anyway).
1492/// int (x)
1493///
1494static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1495 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1496 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001497 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001498}
1499
Chris Lattnere40c2952009-04-14 21:34:55 +00001500
1501/// ParseImplicitInt - This method is called when we have an non-typename
1502/// identifier in a declspec (which normally terminates the decl spec) when
1503/// the declspec has no type specifier. In this case, the declspec is either
1504/// malformed or is "implicit int" (in K&R and C89).
1505///
1506/// This method handles diagnosing this prettily and returns false if the
1507/// declspec is done being processed. If it recovers and thinks there may be
1508/// other pieces of declspec after it, it returns true.
1509///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001510bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001511 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001512 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001513 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Chris Lattnere40c2952009-04-14 21:34:55 +00001515 SourceLocation Loc = Tok.getLocation();
1516 // If we see an identifier that is not a type name, we normally would
1517 // parse it as the identifer being declared. However, when a typename
1518 // is typo'd or the definition is not included, this will incorrectly
1519 // parse the typename as the identifier name and fall over misparsing
1520 // later parts of the diagnostic.
1521 //
1522 // As such, we try to do some look-ahead in cases where this would
1523 // otherwise be an "implicit-int" case to see if this is invalid. For
1524 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1525 // an identifier with implicit int, we'd get a parse error because the
1526 // next token is obviously invalid for a type. Parse these as a case
1527 // with an invalid type specifier.
1528 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattnere40c2952009-04-14 21:34:55 +00001530 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001531 // error, do lookahead to try to do better recovery. This never applies within
1532 // a type specifier.
1533 // FIXME: Don't bail out here in languages with no implicit int (like
1534 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1535 // type or typo than a use of implicit int.
1536 if (DSC != DSC_type_specifier &&
1537 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001538 // If this token is valid for implicit int, e.g. "static x = 4", then
1539 // we just avoid eating the identifier, so it will be parsed as the
1540 // identifier in the declarator.
1541 return false;
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Chris Lattnere40c2952009-04-14 21:34:55 +00001544 // Otherwise, if we don't consume this token, we are going to emit an
1545 // error anyway. Try to recover from various common problems. Check
1546 // to see if this was a reference to a tag name without a tag specified.
1547 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001548 //
1549 // C++ doesn't need this, and isTagName doesn't take SS.
1550 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001551 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001552 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Douglas Gregor23c94db2010-07-02 17:43:08 +00001554 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001555 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001556 case DeclSpec::TST_enum:
1557 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1558 case DeclSpec::TST_union:
1559 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1560 case DeclSpec::TST_struct:
1561 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1562 case DeclSpec::TST_class:
1563 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Chris Lattnerf4382f52009-04-14 22:17:06 +00001566 if (TagName) {
1567 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001568 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001569 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Chris Lattnerf4382f52009-04-14 22:17:06 +00001571 // Parse this as a tag as if the missing tag were present.
1572 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001573 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001574 else
Richard Smith69730c12012-03-12 07:56:15 +00001575 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1576 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001577 return true;
1578 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Douglas Gregora786fdb2009-10-13 23:27:22 +00001581 // This is almost certainly an invalid type name. Let the action emit a
1582 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001583 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001584 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001585 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001586 // The action emitted a diagnostic, so we don't have to.
1587 if (T) {
1588 // The action has suggested that the type T could be used. Set that as
1589 // the type in the declaration specifiers, consume the would-be type
1590 // name token, and we're done.
1591 const char *PrevSpec;
1592 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001593 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001594 DS.SetRangeEnd(Tok.getLocation());
1595 ConsumeToken();
1596
1597 // There may be other declaration specifiers after this.
1598 return true;
1599 }
1600
1601 // Fall through; the action had no suggestion for us.
1602 } else {
1603 // The action did not emit a diagnostic, so emit one now.
1604 SourceRange R;
1605 if (SS) R = SS->getRange();
1606 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregora786fdb2009-10-13 23:27:22 +00001609 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001610 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001611 DS.SetRangeEnd(Tok.getLocation());
1612 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Chris Lattnere40c2952009-04-14 21:34:55 +00001614 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1615 // avoid rippling error messages on subsequent uses of the same type,
1616 // could be useful if #include was forgotten.
1617 return false;
1618}
1619
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001620/// \brief Determine the declaration specifier context from the declarator
1621/// context.
1622///
1623/// \param Context the declarator context, which is one of the
1624/// Declarator::TheContext enumerator values.
1625Parser::DeclSpecContext
1626Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1627 if (Context == Declarator::MemberContext)
1628 return DSC_class;
1629 if (Context == Declarator::FileContext)
1630 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001631 if (Context == Declarator::TrailingReturnContext)
1632 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001633 return DSC_normal;
1634}
1635
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001636/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1637///
1638/// FIXME: Simply returns an alignof() expression if the argument is a
1639/// type. Ideally, the type should be propagated directly into Sema.
1640///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001641/// [C11] type-id
1642/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001643/// [C++0x] type-id ...[opt]
1644/// [C++0x] assignment-expression ...[opt]
1645ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1646 SourceLocation &EllipsisLoc) {
1647 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001648 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001649 SourceLocation TypeLoc = Tok.getLocation();
1650 ParsedType Ty = ParseTypeName().get();
1651 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001652 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1653 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001654 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001655 ER = ParseConstantExpression();
1656
David Blaikie4e4d0842012-03-11 07:00:24 +00001657 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001658 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001659
1660 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001661}
1662
1663/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1664/// attribute to Attrs.
1665///
1666/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001667/// [C11] '_Alignas' '(' type-id ')'
1668/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001669/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1670/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001671void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1672 SourceLocation *endLoc) {
1673 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1674 "Not an alignment-specifier!");
1675
1676 SourceLocation KWLoc = Tok.getLocation();
1677 ConsumeToken();
1678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001679 BalancedDelimiterTracker T(*this, tok::l_paren);
1680 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001681 return;
1682
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001683 SourceLocation EllipsisLoc;
1684 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001685 if (ArgExpr.isInvalid()) {
1686 SkipUntil(tok::r_paren);
1687 return;
1688 }
1689
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001690 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001691 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001692 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001693
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001694 // FIXME: Handle pack-expansions here.
1695 if (EllipsisLoc.isValid()) {
1696 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1697 return;
1698 }
1699
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001700 ExprVector ArgExprs(Actions);
1701 ArgExprs.push_back(ArgExpr.release());
1702 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001703 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001704}
1705
Reid Spencer5f016e22007-07-11 17:01:13 +00001706/// ParseDeclarationSpecifiers
1707/// declaration-specifiers: [C99 6.7]
1708/// storage-class-specifier declaration-specifiers[opt]
1709/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001710/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001711/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001712/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001713/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001714///
1715/// storage-class-specifier: [C99 6.7.1]
1716/// 'typedef'
1717/// 'extern'
1718/// 'static'
1719/// 'auto'
1720/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001721/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001722/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001723/// function-specifier: [C99 6.7.4]
1724/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001725/// [C++] 'virtual'
1726/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001727/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001728/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001729/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001730
Reid Spencer5f016e22007-07-11 17:01:13 +00001731///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001732void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001733 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001734 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001735 DeclSpecContext DSContext,
1736 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001737 if (DS.getSourceRange().isInvalid()) {
1738 DS.SetRangeStart(Tok.getLocation());
1739 DS.SetRangeEnd(Tok.getLocation());
1740 }
1741
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001742 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001744 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001746 unsigned DiagID = 0;
1747
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001749
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001751 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001752 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001753 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1754 MaybeParseCXX0XAttributes(DS.getAttributes());
1755
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 // If this is not a declaration specifier token, we're done reading decl
1757 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001758 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001761 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001762 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001763 if (DS.hasTypeSpecifier()) {
1764 bool AllowNonIdentifiers
1765 = (getCurScope()->getFlags() & (Scope::ControlScope |
1766 Scope::BlockScope |
1767 Scope::TemplateParamScope |
1768 Scope::FunctionPrototypeScope |
1769 Scope::AtCatchScope)) == 0;
1770 bool AllowNestedNameSpecifiers
1771 = DSContext == DSC_top_level ||
1772 (DSContext == DSC_class && DS.isFriendSpecified());
1773
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001774 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1775 AllowNonIdentifiers,
1776 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001777 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001778 }
1779
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001780 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1781 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1782 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001783 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1784 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001785 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001786 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001787 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001788 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001789
1790 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001791 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001792 }
1793
Chris Lattner5e02c472009-01-05 00:07:25 +00001794 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001795 // C++ scope specifier. Annotate and loop, or bail out on error.
1796 if (TryAnnotateCXXScopeToken(true)) {
1797 if (!DS.hasTypeSpecifier())
1798 DS.SetTypeSpecError();
1799 goto DoneWithDeclSpec;
1800 }
John McCall2e0a7152010-03-01 18:20:46 +00001801 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1802 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001803 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001804
1805 case tok::annot_cxxscope: {
1806 if (DS.hasTypeSpecifier())
1807 goto DoneWithDeclSpec;
1808
John McCallaa87d332009-12-12 11:40:51 +00001809 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001810 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1811 Tok.getAnnotationRange(),
1812 SS);
John McCallaa87d332009-12-12 11:40:51 +00001813
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001814 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001815 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001816 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001817 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001818 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001819 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001820
1821 // C++ [class.qual]p2:
1822 // In a lookup in which the constructor is an acceptable lookup
1823 // result and the nested-name-specifier nominates a class C:
1824 //
1825 // - if the name specified after the
1826 // nested-name-specifier, when looked up in C, is the
1827 // injected-class-name of C (Clause 9), or
1828 //
1829 // - if the name specified after the nested-name-specifier
1830 // is the same as the identifier or the
1831 // simple-template-id's template-name in the last
1832 // component of the nested-name-specifier,
1833 //
1834 // the name is instead considered to name the constructor of
1835 // class C.
1836 //
1837 // Thus, if the template-name is actually the constructor
1838 // name, then the code is ill-formed; this interpretation is
1839 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001840 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001841 if ((DSContext == DSC_top_level ||
1842 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1843 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001844 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001845 if (isConstructorDeclarator()) {
1846 // The user meant this to be an out-of-line constructor
1847 // definition, but template arguments are not allowed
1848 // there. Just allow this as a constructor; we'll
1849 // complain about it later.
1850 goto DoneWithDeclSpec;
1851 }
1852
1853 // The user meant this to name a type, but it actually names
1854 // a constructor with some extraneous template
1855 // arguments. Complain, then parse it as a type as the user
1856 // intended.
1857 Diag(TemplateId->TemplateNameLoc,
1858 diag::err_out_of_line_template_id_names_constructor)
1859 << TemplateId->Name;
1860 }
1861
John McCallaa87d332009-12-12 11:40:51 +00001862 DS.getTypeSpecScope() = SS;
1863 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001864 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001865 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001866 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001867 continue;
1868 }
1869
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001870 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001871 DS.getTypeSpecScope() = SS;
1872 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001873 if (Tok.getAnnotationValue()) {
1874 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1876 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001877 PrevSpec, DiagID, T);
1878 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001879 else
1880 DS.SetTypeSpecError();
1881 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1882 ConsumeToken(); // The typename
1883 }
1884
Douglas Gregor9135c722009-03-25 15:40:00 +00001885 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001886 goto DoneWithDeclSpec;
1887
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001888 // If we're in a context where the identifier could be a class name,
1889 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001890 if ((DSContext == DSC_top_level ||
1891 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001892 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001893 &SS)) {
1894 if (isConstructorDeclarator())
1895 goto DoneWithDeclSpec;
1896
1897 // As noted in C++ [class.qual]p2 (cited above), when the name
1898 // of the class is qualified in a context where it could name
1899 // a constructor, its a constructor name. However, we've
1900 // looked at the declarator, and the user probably meant this
1901 // to be a type. Complain that it isn't supposed to be treated
1902 // as a type, then proceed to parse it as a type.
1903 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1904 << Next.getIdentifierInfo();
1905 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001906
John McCallb3d87482010-08-24 05:47:05 +00001907 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1908 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001909 getCurScope(), &SS,
1910 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001911 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001912 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001913
Chris Lattnerf4382f52009-04-14 22:17:06 +00001914 // If the referenced identifier is not a type, then this declspec is
1915 // erroneous: We already checked about that it has no type specifier, and
1916 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001917 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001918 if (TypeRep == 0) {
1919 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001920 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001921 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923
John McCallaa87d332009-12-12 11:40:51 +00001924 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001925 ConsumeToken(); // The C++ scope.
1926
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001927 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001928 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001929 if (isInvalid)
1930 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001932 DS.SetRangeEnd(Tok.getLocation());
1933 ConsumeToken(); // The typename.
1934
1935 continue;
1936 }
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Chris Lattner80d0c892009-01-21 19:48:37 +00001938 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001939 if (Tok.getAnnotationValue()) {
1940 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001942 DiagID, T);
1943 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001944 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001945
1946 if (isInvalid)
1947 break;
1948
Chris Lattner80d0c892009-01-21 19:48:37 +00001949 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1950 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Chris Lattner80d0c892009-01-21 19:48:37 +00001952 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1953 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001954 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001955 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001956 ParseObjCProtocolQualifiers(DS);
1957
Chris Lattner80d0c892009-01-21 19:48:37 +00001958 continue;
1959 }
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Douglas Gregorbfad9152011-04-28 15:48:45 +00001961 case tok::kw___is_signed:
1962 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1963 // typically treats it as a trait. If we see __is_signed as it appears
1964 // in libstdc++, e.g.,
1965 //
1966 // static const bool __is_signed;
1967 //
1968 // then treat __is_signed as an identifier rather than as a keyword.
1969 if (DS.getTypeSpecType() == TST_bool &&
1970 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1971 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1972 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1973 Tok.setKind(tok::identifier);
1974 }
1975
1976 // We're done with the declaration-specifiers.
1977 goto DoneWithDeclSpec;
1978
Chris Lattner3bd934a2008-07-26 01:18:38 +00001979 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001980 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001981 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001982 // In C++, check to see if this is a scope specifier like foo::bar::, if
1983 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00001984 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00001985 if (TryAnnotateCXXScopeToken(true)) {
1986 if (!DS.hasTypeSpecifier())
1987 DS.SetTypeSpecError();
1988 goto DoneWithDeclSpec;
1989 }
1990 if (!Tok.is(tok::identifier))
1991 continue;
1992 }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Chris Lattner3bd934a2008-07-26 01:18:38 +00001994 // This identifier can only be a typedef name if we haven't already seen
1995 // a type-specifier. Without this check we misparse:
1996 // typedef int X; struct Y { short X; }; as 'short int'.
1997 if (DS.hasTypeSpecifier())
1998 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001999
John Thompson82287d12010-02-05 00:12:22 +00002000 // Check for need to substitute AltiVec keyword tokens.
2001 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2002 break;
2003
John McCallb3d87482010-08-24 05:47:05 +00002004 ParsedType TypeRep =
2005 Actions.getTypeName(*Tok.getIdentifierInfo(),
2006 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002007
Chris Lattnerc199ab32009-04-12 20:42:31 +00002008 // If this is not a typedef name, don't parse it as part of the declspec,
2009 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002010 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002011 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002012 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002013 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002014
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002015 // If we're in a context where the identifier could be a class name,
2016 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002017 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002019 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002020 goto DoneWithDeclSpec;
2021
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002023 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002024 if (isInvalid)
2025 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Chris Lattner3bd934a2008-07-26 01:18:38 +00002027 DS.SetRangeEnd(Tok.getLocation());
2028 ConsumeToken(); // The identifier
2029
2030 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2031 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002032 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002033 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002034 ParseObjCProtocolQualifiers(DS);
2035
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002036 // Need to support trailing type qualifiers (e.g. "id<p> const").
2037 // If a type specifier follows, it will be diagnosed elsewhere.
2038 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002039 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002040
2041 // type-name
2042 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002043 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002044 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002045 // This template-id does not refer to a type name, so we're
2046 // done with the type-specifiers.
2047 goto DoneWithDeclSpec;
2048 }
2049
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002050 // If we're in a context where the template-id could be a
2051 // constructor name or specialization, check whether this is a
2052 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002053 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002054 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002055 isConstructorDeclarator())
2056 goto DoneWithDeclSpec;
2057
Douglas Gregor39a8de12009-02-25 19:37:18 +00002058 // Turn the template-id annotation token into a type annotation
2059 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002060 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002061 continue;
2062 }
2063
Reid Spencer5f016e22007-07-11 17:01:13 +00002064 // GNU attributes support.
2065 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002066 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002067 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002068
2069 // Microsoft declspec support.
2070 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002071 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002072 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Steve Naroff239f0732008-12-25 14:16:32 +00002074 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002075 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002076 // FIXME: Add handling here!
2077 break;
2078
2079 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002080 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002081 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002082 case tok::kw___cdecl:
2083 case tok::kw___stdcall:
2084 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002085 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002086 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002087 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002088 continue;
2089
Dawn Perchik52fc3142010-09-03 01:29:35 +00002090 // Borland single token adornments.
2091 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002092 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002093 continue;
2094
Peter Collingbournef315fa82011-02-14 01:42:53 +00002095 // OpenCL single token adornments.
2096 case tok::kw___kernel:
2097 ParseOpenCLAttributes(DS.getAttributes());
2098 continue;
2099
Reid Spencer5f016e22007-07-11 17:01:13 +00002100 // storage-class-specifier
2101 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002102 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2103 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002104 break;
2105 case tok::kw_extern:
2106 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002107 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002108 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2109 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002111 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002112 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2113 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002114 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 case tok::kw_static:
2116 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002117 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002118 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2119 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 break;
2121 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002122 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002123 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002124 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2125 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002126 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002127 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002128 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002129 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2131 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002132 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002133 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2134 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 break;
2136 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002137 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2138 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002140 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002141 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2142 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002143 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002145 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 // function-specifier
2149 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002150 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002151 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002152 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002153 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002154 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002155 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002157 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002158
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002159 // alignment-specifier
2160 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002161 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002162 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002163 ParseAlignmentSpecifier(DS.getAttributes());
2164 continue;
2165
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002166 // friend
2167 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002168 if (DSContext == DSC_class)
2169 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2170 else {
2171 PrevSpec = ""; // not actually used by the diagnostic
2172 DiagID = diag::err_friend_invalid_in_context;
2173 isInvalid = true;
2174 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002175 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Douglas Gregor8d267c52011-09-09 02:06:17 +00002177 // Modules
2178 case tok::kw___module_private__:
2179 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2180 break;
2181
Sebastian Redl2ac67232009-11-05 15:47:02 +00002182 // constexpr
2183 case tok::kw_constexpr:
2184 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2185 break;
2186
Chris Lattner80d0c892009-01-21 19:48:37 +00002187 // type-specifier
2188 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002189 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2190 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002191 break;
2192 case tok::kw_long:
2193 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002194 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2195 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002196 else
John McCallfec54012009-08-03 20:12:06 +00002197 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2198 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002199 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002200 case tok::kw___int64:
2201 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2202 DiagID);
2203 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002204 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002205 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2206 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002207 break;
2208 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002209 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2210 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002211 break;
2212 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002213 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2214 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002215 break;
2216 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002217 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2218 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002219 break;
2220 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002221 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2222 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002223 break;
2224 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002225 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2226 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002227 break;
2228 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002229 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2230 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002231 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002232 case tok::kw___int128:
2233 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2234 DiagID);
2235 break;
2236 case tok::kw_half:
2237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2238 DiagID);
2239 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002240 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2242 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002243 break;
2244 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2246 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002247 break;
2248 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2250 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002251 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002252 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2254 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002255 break;
2256 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2258 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002259 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002260 case tok::kw_bool:
2261 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002262 if (Tok.is(tok::kw_bool) &&
2263 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2264 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2265 PrevSpec = ""; // Not used by the diagnostic.
2266 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002267 // For better error recovery.
2268 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002269 isInvalid = true;
2270 } else {
2271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2272 DiagID);
2273 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002274 break;
2275 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2277 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002278 break;
2279 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2281 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002282 break;
2283 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2285 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002286 break;
John Thompson82287d12010-02-05 00:12:22 +00002287 case tok::kw___vector:
2288 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2289 break;
2290 case tok::kw___pixel:
2291 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2292 break;
John McCalla5fc4722011-04-09 22:50:59 +00002293 case tok::kw___unknown_anytype:
2294 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2295 PrevSpec, DiagID);
2296 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002297
2298 // class-specifier:
2299 case tok::kw_class:
2300 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002301 case tok::kw_union: {
2302 tok::TokenKind Kind = Tok.getKind();
2303 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002304 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2305 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002306 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002307 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002308
2309 // enum-specifier:
2310 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002311 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002312 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002313 continue;
2314
2315 // cv-qualifier:
2316 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002317 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002318 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002319 break;
2320 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002321 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002322 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002323 break;
2324 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002326 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002327 break;
2328
Douglas Gregord57959a2009-03-27 23:10:48 +00002329 // C++ typename-specifier:
2330 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002331 if (TryAnnotateTypeOrScopeToken()) {
2332 DS.SetTypeSpecError();
2333 goto DoneWithDeclSpec;
2334 }
2335 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002336 continue;
2337 break;
2338
Chris Lattner80d0c892009-01-21 19:48:37 +00002339 // GNU typeof support.
2340 case tok::kw_typeof:
2341 ParseTypeofSpecifier(DS);
2342 continue;
2343
David Blaikie42d6d0c2011-12-04 05:04:18 +00002344 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002345 ParseDecltypeSpecifier(DS);
2346 continue;
2347
Sean Huntdb5d44b2011-05-19 05:37:45 +00002348 case tok::kw___underlying_type:
2349 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002350 continue;
2351
2352 case tok::kw__Atomic:
2353 ParseAtomicSpecifier(DS);
2354 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002355
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002356 // OpenCL qualifiers:
2357 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002358 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002359 goto DoneWithDeclSpec;
2360 case tok::kw___private:
2361 case tok::kw___global:
2362 case tok::kw___local:
2363 case tok::kw___constant:
2364 case tok::kw___read_only:
2365 case tok::kw___write_only:
2366 case tok::kw___read_write:
2367 ParseOpenCLQualifiers(DS);
2368 break;
2369
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002370 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002371 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002372 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2373 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002374 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002375 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Douglas Gregor46f936e2010-11-19 17:10:50 +00002377 if (!ParseObjCProtocolQualifiers(DS))
2378 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2379 << FixItHint::CreateInsertion(Loc, "id")
2380 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002381
2382 // Need to support trailing type qualifiers (e.g. "id<p> const").
2383 // If a type specifier follows, it will be diagnosed elsewhere.
2384 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002385 }
John McCallfec54012009-08-03 20:12:06 +00002386 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 if (isInvalid) {
2388 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002389 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002390
2391 if (DiagID == diag::ext_duplicate_declspec)
2392 Diag(Tok, DiagID)
2393 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2394 else
2395 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002397
Chris Lattner81c018d2008-03-13 06:29:04 +00002398 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002399 if (DiagID != diag::err_bool_redeclaration)
2400 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002401 }
2402}
Douglas Gregoradcac882008-12-01 23:54:00 +00002403
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002404/// ParseStructDeclaration - Parse a struct declaration without the terminating
2405/// semicolon.
2406///
Reid Spencer5f016e22007-07-11 17:01:13 +00002407/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002408/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002409/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002410/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002411/// struct-declarator-list:
2412/// struct-declarator
2413/// struct-declarator-list ',' struct-declarator
2414/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2415/// struct-declarator:
2416/// declarator
2417/// [GNU] declarator attributes[opt]
2418/// declarator[opt] ':' constant-expression
2419/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2420///
Chris Lattnere1359422008-04-10 06:46:29 +00002421void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002422ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002423
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002424 if (Tok.is(tok::kw___extension__)) {
2425 // __extension__ silences extension warnings in the subexpression.
2426 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002427 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002428 return ParseStructDeclaration(DS, Fields);
2429 }
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Steve Naroff28a7ca82007-08-20 22:28:22 +00002431 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002432 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002434 // If there are no declarators, this is a free-standing declaration
2435 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002436 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002437 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002438 return;
2439 }
2440
2441 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002442 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002443 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002444 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002445 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002446 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002447 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002448
2449 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002450 if (!FirstDeclarator)
2451 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Steve Naroff28a7ca82007-08-20 22:28:22 +00002453 /// struct-declarator: declarator
2454 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002455 if (Tok.isNot(tok::colon)) {
2456 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2457 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002458 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002459 }
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Chris Lattner04d66662007-10-09 17:33:22 +00002461 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002462 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002463 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002464 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002465 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002466 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002467 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002468 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002469
Steve Naroff28a7ca82007-08-20 22:28:22 +00002470 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002471 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002472
John McCallbdd563e2009-11-03 02:38:08 +00002473 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002474 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002475 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002476
Steve Naroff28a7ca82007-08-20 22:28:22 +00002477 // If we don't have a comma, it is either the end of the list (a ';')
2478 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002479 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002480 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002481
Steve Naroff28a7ca82007-08-20 22:28:22 +00002482 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002483 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002484
John McCallbdd563e2009-11-03 02:38:08 +00002485 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002486 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002487}
2488
2489/// ParseStructUnionBody
2490/// struct-contents:
2491/// struct-declaration-list
2492/// [EXT] empty
2493/// [GNU] "struct-declaration-list" without terminatoring ';'
2494/// struct-declaration-list:
2495/// struct-declaration
2496/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002497/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002498///
Reid Spencer5f016e22007-07-11 17:01:13 +00002499void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002500 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002501 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2502 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002504 BalancedDelimiterTracker T(*this, tok::l_brace);
2505 if (T.consumeOpen())
2506 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002508 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002509 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002510
Reid Spencer5f016e22007-07-11 17:01:13 +00002511 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2512 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002513 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002514 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2515 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2516 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002517
Chris Lattner5f9e2722011-07-23 10:55:15 +00002518 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002519
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002521 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002522 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002525 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002526 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002527 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002528 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002529 ConsumeToken();
2530 continue;
2531 }
Chris Lattnere1359422008-04-10 06:46:29 +00002532
2533 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002534 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002535
John McCallbdd563e2009-11-03 02:38:08 +00002536 if (!Tok.is(tok::at)) {
2537 struct CFieldCallback : FieldCallback {
2538 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002539 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002540 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002541
John McCalld226f652010-08-21 09:40:31 +00002542 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002543 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002544 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2545
John McCalld226f652010-08-21 09:40:31 +00002546 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002547 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002548 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002549 FD.D.getDeclSpec().getSourceRange().getBegin(),
2550 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002551 FieldDecls.push_back(Field);
2552 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002553 }
John McCallbdd563e2009-11-03 02:38:08 +00002554 } Callback(*this, TagDecl, FieldDecls);
2555
2556 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002557 } else { // Handle @defs
2558 ConsumeToken();
2559 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2560 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002561 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002562 continue;
2563 }
2564 ConsumeToken();
2565 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2566 if (!Tok.is(tok::identifier)) {
2567 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002568 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002569 continue;
2570 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002571 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002572 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002573 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002574 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2575 ConsumeToken();
2576 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002577 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002578
Chris Lattner04d66662007-10-09 17:33:22 +00002579 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002580 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002581 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002582 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002583 break;
2584 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002585 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2586 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002587 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002588 // If we stopped at a ';', eat it.
2589 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002590 }
2591 }
Mike Stump1eb44332009-09-09 15:08:12 +00002592
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002593 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002594
John McCall0b7e6782011-03-24 11:26:52 +00002595 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002597 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002598
Douglas Gregor23c94db2010-07-02 17:43:08 +00002599 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002600 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002601 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002602 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002603 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002604 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2605 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002606}
2607
Reid Spencer5f016e22007-07-11 17:01:13 +00002608/// ParseEnumSpecifier
2609/// enum-specifier: [C99 6.7.2.2]
2610/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002611///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002612/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2613/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002614/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2615/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002616/// 'enum' identifier
2617/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002618///
Richard Smith1af83c42012-03-23 03:33:32 +00002619/// [C++11] enum-head '{' enumerator-list[opt] '}'
2620/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002621///
Richard Smith1af83c42012-03-23 03:33:32 +00002622/// enum-head: [C++11]
2623/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2624/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2625/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002626///
Richard Smith1af83c42012-03-23 03:33:32 +00002627/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002628/// 'enum'
2629/// 'enum' 'class'
2630/// 'enum' 'struct'
2631///
Richard Smith1af83c42012-03-23 03:33:32 +00002632/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002633/// ':' type-specifier-seq
2634///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002635/// [C++] elaborated-type-specifier:
2636/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2637///
Chris Lattner4c97d762009-04-12 21:49:30 +00002638void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002639 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002640 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002641 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002642 if (Tok.is(tok::code_completion)) {
2643 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002644 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002645 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002646 }
John McCall57c13002011-07-06 05:58:41 +00002647
Richard Smithbdad7a22012-01-10 01:33:14 +00002648 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002649 bool IsScopedUsingClassTag = false;
2650
David Blaikie4e4d0842012-03-11 07:00:24 +00002651 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002652 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002653 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002654 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002655 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002656 }
Richard Smith1af83c42012-03-23 03:33:32 +00002657
2658 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2659 // used to specify explicit instantiations. We extend this to also cover
2660 // explicit specializations.
2661 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2662 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2663 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2664
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002665 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002666 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002667 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002668
Aaron Ballman6454a022012-03-01 04:09:28 +00002669 // If declspecs exist after tag, parse them.
2670 while (Tok.is(tok::kw___declspec))
2671 ParseMicrosoftDeclSpec(attrs);
2672
Richard Smith7796eb52012-03-12 08:56:40 +00002673 // Enum definitions should not be parsed in a trailing-return-type.
2674 bool AllowDeclaration = DSC != DSC_trailing;
2675
2676 bool AllowFixedUnderlyingType = AllowDeclaration &&
2677 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2678 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002679
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002680 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002681 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002682 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2683 // if a fixed underlying type is allowed.
2684 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2685
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002686 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2687 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002688 return;
2689
2690 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002691 Diag(Tok, diag::err_expected_ident);
2692 if (Tok.isNot(tok::l_brace)) {
2693 // Has no name and is not a definition.
2694 // Skip the rest of this declarator, up until the comma or semicolon.
2695 SkipUntil(tok::comma, true);
2696 return;
2697 }
2698 }
2699 }
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002701 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002702 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002703 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002704 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002706 // Skip the rest of this declarator, up until the comma or semicolon.
2707 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002708 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002711 // If an identifier is present, consume and remember it.
2712 IdentifierInfo *Name = 0;
2713 SourceLocation NameLoc;
2714 if (Tok.is(tok::identifier)) {
2715 Name = Tok.getIdentifierInfo();
2716 NameLoc = ConsumeToken();
2717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Richard Smithbdad7a22012-01-10 01:33:14 +00002719 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002720 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2721 // declaration of a scoped enumeration.
2722 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002723 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002724 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002725 }
2726
Richard Smith1af83c42012-03-23 03:33:32 +00002727 // Stop suppressing access control now we've parsed the enum name.
2728 SuppressAccess.done();
2729
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002730 TypeResult BaseType;
2731
Douglas Gregora61b3e72010-12-01 17:42:47 +00002732 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002733 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002734 bool PossibleBitfield = false;
2735 if (getCurScope()->getFlags() & Scope::ClassScope) {
2736 // If we're in class scope, this can either be an enum declaration with
2737 // an underlying type, or a declaration of a bitfield member. We try to
2738 // use a simple disambiguation scheme first to catch the common cases
2739 // (integer literal, sizeof); if it's still ambiguous, we then consider
2740 // anything that's a simple-type-specifier followed by '(' as an
2741 // expression. This suffices because function types are not valid
2742 // underlying types anyway.
2743 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2744 // If the next token starts an expression, we know we're parsing a
2745 // bit-field. This is the common case.
2746 if (TPR == TPResult::True())
2747 PossibleBitfield = true;
2748 // If the next token starts a type-specifier-seq, it may be either a
2749 // a fixed underlying type or the start of a function-style cast in C++;
2750 // lookahead one more token to see if it's obvious that we have a
2751 // fixed underlying type.
2752 else if (TPR == TPResult::False() &&
2753 GetLookAheadToken(2).getKind() == tok::semi) {
2754 // Consume the ':'.
2755 ConsumeToken();
2756 } else {
2757 // We have the start of a type-specifier-seq, so we have to perform
2758 // tentative parsing to determine whether we have an expression or a
2759 // type.
2760 TentativeParsingAction TPA(*this);
2761
2762 // Consume the ':'.
2763 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002764
2765 // If we see a type specifier followed by an open-brace, we have an
2766 // ambiguity between an underlying type and a C++11 braced
2767 // function-style cast. Resolve this by always treating it as an
2768 // underlying type.
2769 // FIXME: The standard is not entirely clear on how to disambiguate in
2770 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002771 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002772 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002773 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002774 // We'll parse this as a bitfield later.
2775 PossibleBitfield = true;
2776 TPA.Revert();
2777 } else {
2778 // We have a type-specifier-seq.
2779 TPA.Commit();
2780 }
2781 }
2782 } else {
2783 // Consume the ':'.
2784 ConsumeToken();
2785 }
2786
2787 if (!PossibleBitfield) {
2788 SourceRange Range;
2789 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002790
David Blaikie4e4d0842012-03-11 07:00:24 +00002791 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002792 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2793 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002794 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002795 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002796 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002797 }
2798
Richard Smithbdad7a22012-01-10 01:33:14 +00002799 // There are four options here. If we have 'friend enum foo;' then this is a
2800 // friend declaration, and cannot have an accompanying definition. If we have
2801 // 'enum foo;', then this is a forward declaration. If we have
2802 // 'enum foo {...' then this is a definition. Otherwise we have something
2803 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002804 //
2805 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2806 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2807 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2808 //
John McCallf312b1e2010-08-26 23:41:50 +00002809 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002810 if (DS.isFriendSpecified())
2811 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002812 else if (!AllowDeclaration)
2813 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002814 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002815 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002816 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002817 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002818 else
John McCallf312b1e2010-08-26 23:41:50 +00002819 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002820
2821 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002822 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002823 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002824 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2825 // Skip the rest of this declarator, up until the comma or semicolon.
2826 Diag(Tok, diag::err_enum_template);
2827 SkipUntil(tok::comma, true);
2828 return;
2829 }
2830
2831 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2832 // Enumerations can't be explicitly instantiated.
2833 DS.SetTypeSpecError();
2834 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2835 return;
2836 }
2837
2838 assert(TemplateInfo.TemplateParams && "no template parameters");
2839 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2840 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002841 }
Richard Smith1af83c42012-03-23 03:33:32 +00002842
Douglas Gregorb9075602011-02-22 02:55:24 +00002843 if (!Name && TUK != Sema::TUK_Definition) {
2844 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002845
Douglas Gregorb9075602011-02-22 02:55:24 +00002846 // Skip the rest of this declarator, up until the comma or semicolon.
2847 SkipUntil(tok::comma, true);
2848 return;
2849 }
Richard Smith1af83c42012-03-23 03:33:32 +00002850
Douglas Gregor402abb52009-05-28 23:31:59 +00002851 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002852 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002853 const char *PrevSpec = 0;
2854 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002855 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002856 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002857 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002858 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002859 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002860
Douglas Gregor48c89f42010-04-24 16:38:41 +00002861 if (IsDependent) {
2862 // This enum has a dependent nested-name-specifier. Handle it as a
2863 // dependent tag.
2864 if (!Name) {
2865 DS.SetTypeSpecError();
2866 Diag(Tok, diag::err_expected_type_name_after_typename);
2867 return;
2868 }
2869
Douglas Gregor23c94db2010-07-02 17:43:08 +00002870 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002871 TUK, SS, Name, StartLoc,
2872 NameLoc);
2873 if (Type.isInvalid()) {
2874 DS.SetTypeSpecError();
2875 return;
2876 }
2877
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002878 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2879 NameLoc.isValid() ? NameLoc : StartLoc,
2880 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002881 Diag(StartLoc, DiagID) << PrevSpec;
2882
2883 return;
2884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
John McCalld226f652010-08-21 09:40:31 +00002886 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002887 // The action failed to produce an enumeration tag. If this is a
2888 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002889 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002890 ConsumeBrace();
2891 SkipUntil(tok::r_brace);
2892 }
2893
2894 DS.SetTypeSpecError();
2895 return;
2896 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002897
Richard Smith7796eb52012-03-12 08:56:40 +00002898 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002899 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002900 Diag(Tok, diag::err_friend_decl_defines_type)
2901 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00002902 ConsumeBrace();
2903 SkipUntil(tok::r_brace);
2904 } else {
2905 ParseEnumBody(StartLoc, TagDecl);
2906 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002909 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2910 NameLoc.isValid() ? NameLoc : StartLoc,
2911 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002912 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002913}
2914
2915/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2916/// enumerator-list:
2917/// enumerator
2918/// enumerator-list ',' enumerator
2919/// enumerator:
2920/// enumeration-constant
2921/// enumeration-constant '=' constant-expression
2922/// enumeration-constant:
2923/// identifier
2924///
John McCalld226f652010-08-21 09:40:31 +00002925void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002926 // Enter the scope of the enum body and start the definition.
2927 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002928 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002929
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002930 BalancedDelimiterTracker T(*this, tok::l_brace);
2931 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002932
Chris Lattner7946dd32007-08-27 17:24:30 +00002933 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002934 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002935 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Chris Lattner5f9e2722011-07-23 10:55:15 +00002937 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002938
John McCalld226f652010-08-21 09:40:31 +00002939 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Reid Spencer5f016e22007-07-11 17:01:13 +00002941 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002942 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002943 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2944 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002945
John McCall5b629aa2010-10-22 23:36:17 +00002946 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002947 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002948 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002949
Reid Spencer5f016e22007-07-11 17:01:13 +00002950 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002951 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002952 ParsingDeclRAIIObject PD(*this);
2953
Chris Lattner04d66662007-10-09 17:33:22 +00002954 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002955 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002956 AssignedVal = ParseConstantExpression();
2957 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002958 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Reid Spencer5f016e22007-07-11 17:01:13 +00002961 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002962 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2963 LastEnumConstDecl,
2964 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002965 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002966 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002967 PD.complete(EnumConstDecl);
2968
Reid Spencer5f016e22007-07-11 17:01:13 +00002969 EnumConstantDecls.push_back(EnumConstDecl);
2970 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Douglas Gregor751f6922010-09-07 14:51:08 +00002972 if (Tok.is(tok::identifier)) {
2973 // We're missing a comma between enumerators.
2974 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2975 Diag(Loc, diag::err_enumerator_list_missing_comma)
2976 << FixItHint::CreateInsertion(Loc, ", ");
2977 continue;
2978 }
2979
Chris Lattner04d66662007-10-09 17:33:22 +00002980 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002981 break;
2982 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002983
Richard Smith7fe62082011-10-15 05:09:34 +00002984 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002985 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002986 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00002987 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00002988 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00002989 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002990 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
2991 << FixItHint::CreateRemoval(CommaLoc);
2992 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002993 }
Mike Stump1eb44332009-09-09 15:08:12 +00002994
Reid Spencer5f016e22007-07-11 17:01:13 +00002995 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002996 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00002997
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002999 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003000 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003001
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003002 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3003 EnumDecl, EnumConstantDecls.data(),
3004 EnumConstantDecls.size(), getCurScope(),
3005 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Douglas Gregor72de6672009-01-08 20:45:30 +00003007 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003008 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3009 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003010}
3011
3012/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003013/// start of a type-qualifier-list.
3014bool Parser::isTypeQualifier() const {
3015 switch (Tok.getKind()) {
3016 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003017
3018 // type-qualifier only in OpenCL
3019 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003020 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003021
Steve Naroff5f8aa692008-02-11 23:15:56 +00003022 // type-qualifier
3023 case tok::kw_const:
3024 case tok::kw_volatile:
3025 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003026 case tok::kw___private:
3027 case tok::kw___local:
3028 case tok::kw___global:
3029 case tok::kw___constant:
3030 case tok::kw___read_only:
3031 case tok::kw___read_write:
3032 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003033 return true;
3034 }
3035}
3036
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003037/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3038/// is definitely a type-specifier. Return false if it isn't part of a type
3039/// specifier or if we're not sure.
3040bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3041 switch (Tok.getKind()) {
3042 default: return false;
3043 // type-specifiers
3044 case tok::kw_short:
3045 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003046 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003047 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003048 case tok::kw_signed:
3049 case tok::kw_unsigned:
3050 case tok::kw__Complex:
3051 case tok::kw__Imaginary:
3052 case tok::kw_void:
3053 case tok::kw_char:
3054 case tok::kw_wchar_t:
3055 case tok::kw_char16_t:
3056 case tok::kw_char32_t:
3057 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003058 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003059 case tok::kw_float:
3060 case tok::kw_double:
3061 case tok::kw_bool:
3062 case tok::kw__Bool:
3063 case tok::kw__Decimal32:
3064 case tok::kw__Decimal64:
3065 case tok::kw__Decimal128:
3066 case tok::kw___vector:
3067
3068 // struct-or-union-specifier (C99) or class-specifier (C++)
3069 case tok::kw_class:
3070 case tok::kw_struct:
3071 case tok::kw_union:
3072 // enum-specifier
3073 case tok::kw_enum:
3074
3075 // typedef-name
3076 case tok::annot_typename:
3077 return true;
3078 }
3079}
3080
Steve Naroff5f8aa692008-02-11 23:15:56 +00003081/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003082/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003083bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003084 switch (Tok.getKind()) {
3085 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Chris Lattner166a8fc2009-01-04 23:41:41 +00003087 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003088 if (TryAltiVecVectorToken())
3089 return true;
3090 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003091 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003092 // Annotate typenames and C++ scope specifiers. If we get one, just
3093 // recurse to handle whatever we get.
3094 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003095 return true;
3096 if (Tok.is(tok::identifier))
3097 return false;
3098 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003099
Chris Lattner166a8fc2009-01-04 23:41:41 +00003100 case tok::coloncolon: // ::foo::bar
3101 if (NextToken().is(tok::kw_new) || // ::new
3102 NextToken().is(tok::kw_delete)) // ::delete
3103 return false;
3104
Chris Lattner166a8fc2009-01-04 23:41:41 +00003105 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003106 return true;
3107 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003108
Reid Spencer5f016e22007-07-11 17:01:13 +00003109 // GNU attributes support.
3110 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003111 // GNU typeof support.
3112 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Reid Spencer5f016e22007-07-11 17:01:13 +00003114 // type-specifiers
3115 case tok::kw_short:
3116 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003117 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003118 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 case tok::kw_signed:
3120 case tok::kw_unsigned:
3121 case tok::kw__Complex:
3122 case tok::kw__Imaginary:
3123 case tok::kw_void:
3124 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003125 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003126 case tok::kw_char16_t:
3127 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003129 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003130 case tok::kw_float:
3131 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003132 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 case tok::kw__Bool:
3134 case tok::kw__Decimal32:
3135 case tok::kw__Decimal64:
3136 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003137 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Chris Lattner99dc9142008-04-13 18:59:07 +00003139 // struct-or-union-specifier (C99) or class-specifier (C++)
3140 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 case tok::kw_struct:
3142 case tok::kw_union:
3143 // enum-specifier
3144 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 // type-qualifier
3147 case tok::kw_const:
3148 case tok::kw_volatile:
3149 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003150
3151 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003152 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003153 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Chris Lattner7c186be2008-10-20 00:25:30 +00003155 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3156 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003157 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Steve Naroff239f0732008-12-25 14:16:32 +00003159 case tok::kw___cdecl:
3160 case tok::kw___stdcall:
3161 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003162 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003163 case tok::kw___w64:
3164 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003165 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003166 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003167 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003168
3169 case tok::kw___private:
3170 case tok::kw___local:
3171 case tok::kw___global:
3172 case tok::kw___constant:
3173 case tok::kw___read_only:
3174 case tok::kw___read_write:
3175 case tok::kw___write_only:
3176
Eli Friedman290eeb02009-06-08 23:27:34 +00003177 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003178
3179 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003180 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003181
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003182 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003183 case tok::kw__Atomic:
3184 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003185 }
3186}
3187
3188/// isDeclarationSpecifier() - Return true if the current token is part of a
3189/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003190///
3191/// \param DisambiguatingWithExpression True to indicate that the purpose of
3192/// this check is to disambiguate between an expression and a declaration.
3193bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003194 switch (Tok.getKind()) {
3195 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003197 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003198 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003199
Chris Lattner166a8fc2009-01-04 23:41:41 +00003200 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003201 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003202 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003203 return false;
John Thompson82287d12010-02-05 00:12:22 +00003204 if (TryAltiVecVectorToken())
3205 return true;
3206 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003207 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003208 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003209 // Annotate typenames and C++ scope specifiers. If we get one, just
3210 // recurse to handle whatever we get.
3211 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003212 return true;
3213 if (Tok.is(tok::identifier))
3214 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003215
3216 // If we're in Objective-C and we have an Objective-C class type followed
3217 // by an identifier and then either ':' or ']', in a place where an
3218 // expression is permitted, then this is probably a class message send
3219 // missing the initial '['. In this case, we won't consider this to be
3220 // the start of a declaration.
3221 if (DisambiguatingWithExpression &&
3222 isStartOfObjCClassMessageMissingOpenBracket())
3223 return false;
3224
John McCall9ba61662010-02-26 08:45:28 +00003225 return isDeclarationSpecifier();
3226
Chris Lattner166a8fc2009-01-04 23:41:41 +00003227 case tok::coloncolon: // ::foo::bar
3228 if (NextToken().is(tok::kw_new) || // ::new
3229 NextToken().is(tok::kw_delete)) // ::delete
3230 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003231
Chris Lattner166a8fc2009-01-04 23:41:41 +00003232 // Annotate typenames and C++ scope specifiers. If we get one, just
3233 // recurse to handle whatever we get.
3234 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003235 return true;
3236 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003237
Reid Spencer5f016e22007-07-11 17:01:13 +00003238 // storage-class-specifier
3239 case tok::kw_typedef:
3240 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003241 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003242 case tok::kw_static:
3243 case tok::kw_auto:
3244 case tok::kw_register:
3245 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Douglas Gregor8d267c52011-09-09 02:06:17 +00003247 // Modules
3248 case tok::kw___module_private__:
3249
Reid Spencer5f016e22007-07-11 17:01:13 +00003250 // type-specifiers
3251 case tok::kw_short:
3252 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003253 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003254 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 case tok::kw_signed:
3256 case tok::kw_unsigned:
3257 case tok::kw__Complex:
3258 case tok::kw__Imaginary:
3259 case tok::kw_void:
3260 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003261 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003262 case tok::kw_char16_t:
3263 case tok::kw_char32_t:
3264
Reid Spencer5f016e22007-07-11 17:01:13 +00003265 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003266 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003267 case tok::kw_float:
3268 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003269 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003270 case tok::kw__Bool:
3271 case tok::kw__Decimal32:
3272 case tok::kw__Decimal64:
3273 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003274 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003275
Chris Lattner99dc9142008-04-13 18:59:07 +00003276 // struct-or-union-specifier (C99) or class-specifier (C++)
3277 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003278 case tok::kw_struct:
3279 case tok::kw_union:
3280 // enum-specifier
3281 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Reid Spencer5f016e22007-07-11 17:01:13 +00003283 // type-qualifier
3284 case tok::kw_const:
3285 case tok::kw_volatile:
3286 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003287
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 // function-specifier
3289 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003290 case tok::kw_virtual:
3291 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003292
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003293 // static_assert-declaration
3294 case tok::kw__Static_assert:
3295
Chris Lattner1ef08762007-08-09 17:01:07 +00003296 // GNU typeof support.
3297 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Chris Lattner1ef08762007-08-09 17:01:07 +00003299 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003300 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Francois Pichete3d49b42011-06-19 08:02:06 +00003303 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003304 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003305 return true;
3306
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003307 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003308 case tok::kw__Atomic:
3309 return true;
3310
Chris Lattnerf3948c42008-07-26 03:38:44 +00003311 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3312 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003313 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003314
Douglas Gregord9d75e52011-04-27 05:41:15 +00003315 // typedef-name
3316 case tok::annot_typename:
3317 return !DisambiguatingWithExpression ||
3318 !isStartOfObjCClassMessageMissingOpenBracket();
3319
Steve Naroff47f52092009-01-06 19:34:12 +00003320 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003321 case tok::kw___cdecl:
3322 case tok::kw___stdcall:
3323 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003324 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003325 case tok::kw___w64:
3326 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003327 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003328 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003329 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003330 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003331
3332 case tok::kw___private:
3333 case tok::kw___local:
3334 case tok::kw___global:
3335 case tok::kw___constant:
3336 case tok::kw___read_only:
3337 case tok::kw___read_write:
3338 case tok::kw___write_only:
3339
Eli Friedman290eeb02009-06-08 23:27:34 +00003340 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003341 }
3342}
3343
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003344bool Parser::isConstructorDeclarator() {
3345 TentativeParsingAction TPA(*this);
3346
3347 // Parse the C++ scope specifier.
3348 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003349 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3350 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003351 TPA.Revert();
3352 return false;
3353 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003354
3355 // Parse the constructor name.
3356 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3357 // We already know that we have a constructor name; just consume
3358 // the token.
3359 ConsumeToken();
3360 } else {
3361 TPA.Revert();
3362 return false;
3363 }
3364
Richard Smith22592862012-03-27 23:05:05 +00003365 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003366 if (Tok.isNot(tok::l_paren)) {
3367 TPA.Revert();
3368 return false;
3369 }
3370 ConsumeParen();
3371
Richard Smith22592862012-03-27 23:05:05 +00003372 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3373 // that we have a constructor.
3374 if (Tok.is(tok::r_paren) ||
3375 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003376 TPA.Revert();
3377 return true;
3378 }
3379
3380 // If we need to, enter the specified scope.
3381 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003382 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003383 DeclScopeObj.EnterDeclaratorScope();
3384
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003385 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003386 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003387 MaybeParseMicrosoftAttributes(Attrs);
3388
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003389 // Check whether the next token(s) are part of a declaration
3390 // specifier, in which case we have the start of a parameter and,
3391 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003392 bool IsConstructor = false;
3393 if (isDeclarationSpecifier())
3394 IsConstructor = true;
3395 else if (Tok.is(tok::identifier) ||
3396 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3397 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3398 // This might be a parenthesized member name, but is more likely to
3399 // be a constructor declaration with an invalid argument type. Keep
3400 // looking.
3401 if (Tok.is(tok::annot_cxxscope))
3402 ConsumeToken();
3403 ConsumeToken();
3404
3405 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003406 // which must have one of the following syntactic forms (see the
3407 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003408 switch (Tok.getKind()) {
3409 case tok::l_paren:
3410 // C(X ( int));
3411 case tok::l_square:
3412 // C(X [ 5]);
3413 // C(X [ [attribute]]);
3414 case tok::coloncolon:
3415 // C(X :: Y);
3416 // C(X :: *p);
3417 case tok::r_paren:
3418 // C(X )
3419 // Assume this isn't a constructor, rather than assuming it's a
3420 // constructor with an unnamed parameter of an ill-formed type.
3421 break;
3422
3423 default:
3424 IsConstructor = true;
3425 break;
3426 }
3427 }
3428
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003429 TPA.Revert();
3430 return IsConstructor;
3431}
Reid Spencer5f016e22007-07-11 17:01:13 +00003432
3433/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003434/// type-qualifier-list: [C99 6.7.5]
3435/// type-qualifier
3436/// [vendor] attributes
3437/// [ only if VendorAttributesAllowed=true ]
3438/// type-qualifier-list type-qualifier
3439/// [vendor] type-qualifier-list attributes
3440/// [ only if VendorAttributesAllowed=true ]
3441/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3442/// [ only if CXX0XAttributesAllowed=true ]
3443/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003444///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003445void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3446 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003447 bool CXX0XAttributesAllowed) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003448 if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003449 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003450 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003451 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003452 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003453 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003454 else
3455 Diag(Loc, diag::err_attributes_not_allowed);
3456 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003457
3458 SourceLocation EndLoc;
3459
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003461 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003462 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003463 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 SourceLocation Loc = Tok.getLocation();
3465
3466 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003467 case tok::code_completion:
3468 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003469 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003470
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003472 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003473 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 break;
3475 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003476 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003477 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 break;
3479 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003480 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003481 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003483
3484 // OpenCL qualifiers:
3485 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003486 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003487 goto DoneWithTypeQuals;
3488 case tok::kw___private:
3489 case tok::kw___global:
3490 case tok::kw___local:
3491 case tok::kw___constant:
3492 case tok::kw___read_only:
3493 case tok::kw___write_only:
3494 case tok::kw___read_write:
3495 ParseOpenCLQualifiers(DS);
3496 break;
3497
Eli Friedman290eeb02009-06-08 23:27:34 +00003498 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003499 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003500 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003501 case tok::kw___cdecl:
3502 case tok::kw___stdcall:
3503 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003504 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003505 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003506 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003507 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003508 continue;
3509 }
3510 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003511 case tok::kw___pascal:
3512 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003513 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003514 continue;
3515 }
3516 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003518 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003519 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003520 continue; // do *not* consume the next token!
3521 }
3522 // otherwise, FALL THROUGH!
3523 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003524 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003525 // If this is not a type-qualifier token, we're done reading type
3526 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003527 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003528 if (EndLoc.isValid())
3529 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003530 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003532
Reid Spencer5f016e22007-07-11 17:01:13 +00003533 // If the specifier combination wasn't legal, issue a diagnostic.
3534 if (isInvalid) {
3535 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003536 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003537 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003538 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003539 }
3540}
3541
3542
3543/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3544///
3545void Parser::ParseDeclarator(Declarator &D) {
3546 /// This implements the 'declarator' production in the C grammar, then checks
3547 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003548 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003549}
3550
Richard Smith9988f282012-03-29 01:16:42 +00003551static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3552 if (Kind == tok::star || Kind == tok::caret)
3553 return true;
3554
3555 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3556 if (!Lang.CPlusPlus)
3557 return false;
3558
3559 return Kind == tok::amp || Kind == tok::ampamp;
3560}
3561
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003562/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3563/// is parsed by the function passed to it. Pass null, and the direct-declarator
3564/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003565/// ptr-operator production.
3566///
Richard Smith0706df42011-10-19 21:33:05 +00003567/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003568/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3569/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003570///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003571/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3572/// [C] pointer[opt] direct-declarator
3573/// [C++] direct-declarator
3574/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003575///
3576/// pointer: [C99 6.7.5]
3577/// '*' type-qualifier-list[opt]
3578/// '*' type-qualifier-list[opt] pointer
3579///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003580/// ptr-operator:
3581/// '*' cv-qualifier-seq[opt]
3582/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003583/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003584/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003585/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003586/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003587void Parser::ParseDeclaratorInternal(Declarator &D,
3588 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003589 if (Diags.hasAllExtensionsSilenced())
3590 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003591
Sebastian Redlf30208a2009-01-24 21:16:55 +00003592 // C++ member pointers start with a '::' or a nested-name.
3593 // Member pointers get special handling, since there's no place for the
3594 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003595 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003596 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3597 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003598 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3599 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003600 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003601 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003602
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003603 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003604 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003605 // The scope spec really belongs to the direct-declarator.
3606 D.getCXXScopeSpec() = SS;
3607 if (DirectDeclParser)
3608 (this->*DirectDeclParser)(D);
3609 return;
3610 }
3611
3612 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003613 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003614 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003615 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003616 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003617
3618 // Recurse to parse whatever is left.
3619 ParseDeclaratorInternal(D, DirectDeclParser);
3620
3621 // Sema will have to catch (syntactically invalid) pointers into global
3622 // scope. It has to catch pointers into namespace scope anyway.
3623 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003624 Loc),
3625 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003626 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003627 return;
3628 }
3629 }
3630
3631 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003632 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003633 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003634 if (DirectDeclParser)
3635 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003636 return;
3637 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003638
Sebastian Redl05532f22009-03-15 22:02:01 +00003639 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3640 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003641 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003642 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003643
Chris Lattner9af55002009-03-27 04:18:06 +00003644 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003645 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003646 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003647
Reid Spencer5f016e22007-07-11 17:01:13 +00003648 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003649 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003650
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003652 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003653 if (Kind == tok::star)
3654 // Remember that we parsed a pointer type, and remember the type-quals.
3655 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003656 DS.getConstSpecLoc(),
3657 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003658 DS.getRestrictSpecLoc()),
3659 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003660 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003661 else
3662 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003663 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003664 Loc),
3665 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003666 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003667 } else {
3668 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003669 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003670
Sebastian Redl743de1f2009-03-23 00:00:23 +00003671 // Complain about rvalue references in C++03, but then go on and build
3672 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003673 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003674 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003675 diag::warn_cxx98_compat_rvalue_reference :
3676 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003677
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3679 // cv-qualifiers are introduced through the use of a typedef or of a
3680 // template type argument, in which case the cv-qualifiers are ignored.
3681 //
3682 // [GNU] Retricted references are allowed.
3683 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003684 // [C++0x] Attributes on references are not allowed.
3685 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003686 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003687
3688 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3689 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3690 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003691 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003692 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3693 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003694 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003695 }
3696
3697 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003698 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003699
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003700 if (D.getNumTypeObjects() > 0) {
3701 // C++ [dcl.ref]p4: There shall be no references to references.
3702 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3703 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003704 if (const IdentifierInfo *II = D.getIdentifier())
3705 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3706 << II;
3707 else
3708 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3709 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003710
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003711 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003712 // can go ahead and build the (technically ill-formed)
3713 // declarator: reference collapsing will take care of it.
3714 }
3715 }
3716
Reid Spencer5f016e22007-07-11 17:01:13 +00003717 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003718 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003719 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003720 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003721 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 }
3723}
3724
Richard Smith9988f282012-03-29 01:16:42 +00003725static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3726 SourceLocation EllipsisLoc) {
3727 if (EllipsisLoc.isValid()) {
3728 FixItHint Insertion;
3729 if (!D.getEllipsisLoc().isValid()) {
3730 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3731 D.setEllipsisLoc(EllipsisLoc);
3732 }
3733 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3734 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3735 }
3736}
3737
Reid Spencer5f016e22007-07-11 17:01:13 +00003738/// ParseDirectDeclarator
3739/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003740/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003741/// '(' declarator ')'
3742/// [GNU] '(' attributes declarator ')'
3743/// [C90] direct-declarator '[' constant-expression[opt] ']'
3744/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3745/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3746/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3747/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3748/// direct-declarator '(' parameter-type-list ')'
3749/// direct-declarator '(' identifier-list[opt] ')'
3750/// [GNU] direct-declarator '(' parameter-forward-declarations
3751/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003752/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3753/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003754/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003755///
3756/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003757/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003758/// '::'[opt] nested-name-specifier[opt] type-name
3759///
3760/// id-expression: [C++ 5.1]
3761/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003762/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003763///
3764/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003765/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003766/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003767/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003768/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003769/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003770///
Richard Smith5d8388c2012-03-27 01:42:32 +00003771/// Note, any additional constructs added here may need corresponding changes
3772/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003773void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003774 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003775
David Blaikie4e4d0842012-03-11 07:00:24 +00003776 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003777 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003778 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003779 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3780 D.getContext() == Declarator::MemberContext;
3781 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3782 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003783 }
3784
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003785 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003786 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003787 // Change the declaration context for name lookup, until this function
3788 // is exited (and the declarator has been parsed).
3789 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003790 }
3791
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003792 // C++0x [dcl.fct]p14:
3793 // There is a syntactic ambiguity when an ellipsis occurs at the end
3794 // of a parameter-declaration-clause without a preceding comma. In
3795 // this case, the ellipsis is parsed as part of the
3796 // abstract-declarator if the type of the parameter names a template
3797 // parameter pack that has not been expanded; otherwise, it is parsed
3798 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003799 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003800 !((D.getContext() == Declarator::PrototypeContext ||
3801 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003802 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003803 !Actions.containsUnexpandedParameterPacks(D))) {
3804 SourceLocation EllipsisLoc = ConsumeToken();
3805 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3806 // The ellipsis was put in the wrong place. Recover, and explain to
3807 // the user what they should have done.
3808 ParseDeclarator(D);
3809 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3810 return;
3811 } else
3812 D.setEllipsisLoc(EllipsisLoc);
3813
3814 // The ellipsis can't be followed by a parenthesized declarator. We
3815 // check for that in ParseParenDeclarator, after we have disambiguated
3816 // the l_paren token.
3817 }
3818
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003819 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3820 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3821 // We found something that indicates the start of an unqualified-id.
3822 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003823 bool AllowConstructorName;
3824 if (D.getDeclSpec().hasTypeSpecifier())
3825 AllowConstructorName = false;
3826 else if (D.getCXXScopeSpec().isSet())
3827 AllowConstructorName =
3828 (D.getContext() == Declarator::FileContext ||
3829 (D.getContext() == Declarator::MemberContext &&
3830 D.getDeclSpec().isFriendSpecified()));
3831 else
3832 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3833
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003834 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003835 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3836 /*EnteringContext=*/true,
3837 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003838 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003839 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003840 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003841 D.getName()) ||
3842 // Once we're past the identifier, if the scope was bad, mark the
3843 // whole declarator bad.
3844 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003845 D.SetIdentifier(0, Tok.getLocation());
3846 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003847 } else {
3848 // Parsed the unqualified-id; update range information and move along.
3849 if (D.getSourceRange().getBegin().isInvalid())
3850 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3851 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003852 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003853 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003854 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003855 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003856 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003857 "There's a C++-specific check for tok::identifier above");
3858 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3859 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3860 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003861 goto PastIdentifier;
3862 }
Richard Smith9988f282012-03-29 01:16:42 +00003863
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003864 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 // direct-declarator: '(' declarator ')'
3866 // direct-declarator: '(' attributes declarator ')'
3867 // Example: 'char (*X)' or 'int (*XX)(void)'
3868 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003869
3870 // If the declarator was parenthesized, we entered the declarator
3871 // scope when parsing the parenthesized declarator, then exited
3872 // the scope already. Re-enter the scope, if we need to.
3873 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003874 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003875 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003876 if (!D.isInvalidType() &&
3877 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003878 // Change the declaration context for name lookup, until this function
3879 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003880 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003881 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003882 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003883 // This could be something simple like "int" (in which case the declarator
3884 // portion is empty), if an abstract-declarator is allowed.
3885 D.SetIdentifier(0, Tok.getLocation());
3886 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003887 if (D.getContext() == Declarator::MemberContext)
3888 Diag(Tok, diag::err_expected_member_name_or_semi)
3889 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003890 else if (getLangOpts().CPlusPlus)
3891 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003892 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003893 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003895 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003896 }
Mike Stump1eb44332009-09-09 15:08:12 +00003897
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003898 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003899 assert(D.isPastIdentifier() &&
3900 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Sean Huntbbd37c62009-11-21 08:43:09 +00003902 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003903 if (D.getIdentifier())
3904 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003905
Reid Spencer5f016e22007-07-11 17:01:13 +00003906 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003907 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003908 // Enter function-declaration scope, limiting any declarators to the
3909 // function prototype scope, including parameter declarators.
3910 ParseScope PrototypeScope(this,
3911 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003912 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3913 // In such a case, check if we actually have a function declarator; if it
3914 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003915 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003916 // When not in file scope, warn for ambiguous function declarators, just
3917 // in case the author intended it as a variable definition.
3918 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3919 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3920 break;
3921 }
John McCall0b7e6782011-03-24 11:26:52 +00003922 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003923 BalancedDelimiterTracker T(*this, tok::l_paren);
3924 T.consumeOpen();
3925 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003926 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003927 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003928 ParseBracketDeclarator(D);
3929 } else {
3930 break;
3931 }
3932 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003933}
Reid Spencer5f016e22007-07-11 17:01:13 +00003934
Chris Lattneref4715c2008-04-06 05:45:57 +00003935/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3936/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003937/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003938/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3939///
3940/// direct-declarator:
3941/// '(' declarator ')'
3942/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003943/// direct-declarator '(' parameter-type-list ')'
3944/// direct-declarator '(' identifier-list[opt] ')'
3945/// [GNU] direct-declarator '(' parameter-forward-declarations
3946/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003947///
3948void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003949 BalancedDelimiterTracker T(*this, tok::l_paren);
3950 T.consumeOpen();
3951
Chris Lattneref4715c2008-04-06 05:45:57 +00003952 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003953
Chris Lattner7399ee02008-10-20 02:05:46 +00003954 // Eat any attributes before we look at whether this is a grouping or function
3955 // declarator paren. If this is a grouping paren, the attribute applies to
3956 // the type being built up, for example:
3957 // int (__attribute__(()) *x)(long y)
3958 // If this ends up not being a grouping paren, the attribute applies to the
3959 // first argument, for example:
3960 // int (__attribute__(()) int x)
3961 // In either case, we need to eat any attributes to be able to determine what
3962 // sort of paren this is.
3963 //
John McCall0b7e6782011-03-24 11:26:52 +00003964 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003965 bool RequiresArg = false;
3966 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003967 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003968
Chris Lattner7399ee02008-10-20 02:05:46 +00003969 // We require that the argument list (if this is a non-grouping paren) be
3970 // present even if the attribute list was empty.
3971 RequiresArg = true;
3972 }
Steve Naroff239f0732008-12-25 14:16:32 +00003973 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003974 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003975 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003976 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003977 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003978 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003979 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003980 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003981 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003982 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003983
Chris Lattneref4715c2008-04-06 05:45:57 +00003984 // If we haven't past the identifier yet (or where the identifier would be
3985 // stored, if this is an abstract declarator), then this is probably just
3986 // grouping parens. However, if this could be an abstract-declarator, then
3987 // this could also be the start of function arguments (consider 'void()').
3988 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003989
Chris Lattneref4715c2008-04-06 05:45:57 +00003990 if (!D.mayOmitIdentifier()) {
3991 // If this can't be an abstract-declarator, this *must* be a grouping
3992 // paren, because we haven't seen the identifier yet.
3993 isGrouping = true;
3994 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00003995 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
3996 NextToken().is(tok::r_paren)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003997 isDeclarationSpecifier()) { // 'int(int)' is a function.
3998 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3999 // considered to be a type, not a K&R identifier-list.
4000 isGrouping = false;
4001 } else {
4002 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4003 isGrouping = true;
4004 }
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Chris Lattneref4715c2008-04-06 05:45:57 +00004006 // If this is a grouping paren, handle:
4007 // direct-declarator: '(' declarator ')'
4008 // direct-declarator: '(' attributes declarator ')'
4009 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004010 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4011 D.setEllipsisLoc(SourceLocation());
4012
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004013 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004014 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004015 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004016 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004017 T.consumeClose();
4018 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4019 T.getCloseLocation()),
4020 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004021
4022 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004023
4024 // An ellipsis cannot be placed outside parentheses.
4025 if (EllipsisLoc.isValid())
4026 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4027
Chris Lattneref4715c2008-04-06 05:45:57 +00004028 return;
4029 }
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Chris Lattneref4715c2008-04-06 05:45:57 +00004031 // Okay, if this wasn't a grouping paren, it must be the start of a function
4032 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004033 // identifier (and remember where it would have been), then call into
4034 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004035 D.SetIdentifier(0, Tok.getLocation());
4036
David Blaikie42d6d0c2011-12-04 05:04:18 +00004037 // Enter function-declaration scope, limiting any declarators to the
4038 // function prototype scope, including parameter declarators.
4039 ParseScope PrototypeScope(this,
4040 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004041 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004042 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004043}
4044
4045/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4046/// declarator D up to a paren, which indicates that we are parsing function
4047/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004048///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004049/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004050/// after the open paren - they should be considered to be the first argument of
4051/// a parameter. If RequiresArg is true, then the first argument of the
4052/// function is required to be present and required to not be an identifier
4053/// list.
4054///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004055/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4056/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4057/// (C++0x) trailing-return-type[opt].
4058///
4059/// [C++0x] exception-specification:
4060/// dynamic-exception-specification
4061/// noexcept-specification
4062///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004063void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004064 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004065 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004066 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004067 assert(getCurScope()->isFunctionPrototypeScope() &&
4068 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004069 // lparen is already consumed!
4070 assert(D.isPastIdentifier() && "Should not call before identifier!");
4071
4072 // This should be true when the function has typed arguments.
4073 // Otherwise, it is treated as a K&R-style function.
4074 bool HasProto = false;
4075 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004076 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004077 // Remember where we see an ellipsis, if any.
4078 SourceLocation EllipsisLoc;
4079
4080 DeclSpec DS(AttrFactory);
4081 bool RefQualifierIsLValueRef = true;
4082 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004083 SourceLocation ConstQualifierLoc;
4084 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004085 ExceptionSpecificationType ESpecType = EST_None;
4086 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004087 SmallVector<ParsedType, 2> DynamicExceptions;
4088 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004089 ExprResult NoexceptExpr;
4090 ParsedType TrailingReturnType;
4091
James Molloy16f1f712012-02-29 10:24:19 +00004092 Actions.ActOnStartFunctionDeclarator();
4093
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004094 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004095 if (isFunctionDeclaratorIdentifierList()) {
4096 if (RequiresArg)
4097 Diag(Tok, diag::err_argument_required_after_attribute);
4098
4099 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4100
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004101 Tracker.consumeClose();
4102 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004103 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004104 if (Tok.isNot(tok::r_paren))
4105 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4106 else if (RequiresArg)
4107 Diag(Tok, diag::err_argument_required_after_attribute);
4108
David Blaikie4e4d0842012-03-11 07:00:24 +00004109 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004110
4111 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004112 Tracker.consumeClose();
4113 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004114
David Blaikie4e4d0842012-03-11 07:00:24 +00004115 if (getLangOpts().CPlusPlus) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004116 MaybeParseCXX0XAttributes(attrs);
4117
4118 // Parse cv-qualifier-seq[opt].
4119 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004120 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004121 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004122 ConstQualifierLoc = DS.getConstSpecLoc();
4123 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4124 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004125
4126 // Parse ref-qualifier[opt].
4127 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004128 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004129 diag::warn_cxx98_compat_ref_qualifier :
4130 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004131
4132 RefQualifierIsLValueRef = Tok.is(tok::amp);
4133 RefQualifierLoc = ConsumeToken();
4134 EndLoc = RefQualifierLoc;
4135 }
4136
4137 // Parse exception-specification[opt].
4138 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4139 DynamicExceptions,
4140 DynamicExceptionRanges,
4141 NoexceptExpr);
4142 if (ESpecType != EST_None)
4143 EndLoc = ESpecRange.getEnd();
4144
4145 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004146 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004147 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004148 SourceRange Range;
4149 TrailingReturnType = ParseTrailingReturnType(Range).get();
4150 if (Range.getEnd().isValid())
4151 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004152 }
4153 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004154 }
4155
4156 // Remember that we parsed a function type, and remember the attributes.
4157 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4158 /*isVariadic=*/EllipsisLoc.isValid(),
4159 EllipsisLoc,
4160 ParamInfo.data(), ParamInfo.size(),
4161 DS.getTypeQualifiers(),
4162 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004163 RefQualifierLoc, ConstQualifierLoc,
4164 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004165 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004166 ESpecType, ESpecRange.getBegin(),
4167 DynamicExceptions.data(),
4168 DynamicExceptionRanges.data(),
4169 DynamicExceptions.size(),
4170 NoexceptExpr.isUsable() ?
4171 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004172 Tracker.getOpenLocation(),
4173 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004174 TrailingReturnType),
4175 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004176
4177 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004178}
4179
4180/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4181/// identifier list form for a K&R-style function: void foo(a,b,c)
4182///
4183/// Note that identifier-lists are only allowed for normal declarators, not for
4184/// abstract-declarators.
4185bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004186 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004187 && Tok.is(tok::identifier)
4188 && !TryAltiVecVectorToken()
4189 // K&R identifier lists can't have typedefs as identifiers, per C99
4190 // 6.7.5.3p11.
4191 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4192 // Identifier lists follow a really simple grammar: the identifiers can
4193 // be followed *only* by a ", identifier" or ")". However, K&R
4194 // identifier lists are really rare in the brave new modern world, and
4195 // it is very common for someone to typo a type in a non-K&R style
4196 // list. If we are presented with something like: "void foo(intptr x,
4197 // float y)", we don't want to start parsing the function declarator as
4198 // though it is a K&R style declarator just because intptr is an
4199 // invalid type.
4200 //
4201 // To handle this, we check to see if the token after the first
4202 // identifier is a "," or ")". Only then do we parse it as an
4203 // identifier list.
4204 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4205}
4206
4207/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4208/// we found a K&R-style identifier list instead of a typed parameter list.
4209///
4210/// After returning, ParamInfo will hold the parsed parameters.
4211///
4212/// identifier-list: [C99 6.7.5]
4213/// identifier
4214/// identifier-list ',' identifier
4215///
4216void Parser::ParseFunctionDeclaratorIdentifierList(
4217 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004218 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004219 // If there was no identifier specified for the declarator, either we are in
4220 // an abstract-declarator, or we are in a parameter declarator which was found
4221 // to be abstract. In abstract-declarators, identifier lists are not valid:
4222 // diagnose this.
4223 if (!D.getIdentifier())
4224 Diag(Tok, diag::ext_ident_list_in_param);
4225
4226 // Maintain an efficient lookup of params we have seen so far.
4227 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4228
4229 while (1) {
4230 // If this isn't an identifier, report the error and skip until ')'.
4231 if (Tok.isNot(tok::identifier)) {
4232 Diag(Tok, diag::err_expected_ident);
4233 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4234 // Forget we parsed anything.
4235 ParamInfo.clear();
4236 return;
4237 }
4238
4239 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4240
4241 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4242 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4243 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4244
4245 // Verify that the argument identifier has not already been mentioned.
4246 if (!ParamsSoFar.insert(ParmII)) {
4247 Diag(Tok, diag::err_param_redefinition) << ParmII;
4248 } else {
4249 // Remember this identifier in ParamInfo.
4250 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4251 Tok.getLocation(),
4252 0));
4253 }
4254
4255 // Eat the identifier.
4256 ConsumeToken();
4257
4258 // The list continues if we see a comma.
4259 if (Tok.isNot(tok::comma))
4260 break;
4261 ConsumeToken();
4262 }
4263}
4264
4265/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4266/// after the opening parenthesis. This function will not parse a K&R-style
4267/// identifier list.
4268///
4269/// D is the declarator being parsed. If attrs is non-null, then the caller
4270/// parsed those arguments immediately after the open paren - they should be
4271/// considered to be the first argument of a parameter.
4272///
4273/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4274/// be the location of the ellipsis, if any was parsed.
4275///
Reid Spencer5f016e22007-07-11 17:01:13 +00004276/// parameter-type-list: [C99 6.7.5]
4277/// parameter-list
4278/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004279/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004280///
4281/// parameter-list: [C99 6.7.5]
4282/// parameter-declaration
4283/// parameter-list ',' parameter-declaration
4284///
4285/// parameter-declaration: [C99 6.7.5]
4286/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004287/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004288/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004289/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004290/// declaration-specifiers abstract-declarator[opt]
4291/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004292/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004293/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4294///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004295void Parser::ParseParameterDeclarationClause(
4296 Declarator &D,
4297 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004298 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004299 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004300
Chris Lattnerf97409f2008-04-06 06:57:35 +00004301 while (1) {
4302 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004303 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004304 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004305 }
Mike Stump1eb44332009-09-09 15:08:12 +00004306
Chris Lattnerf97409f2008-04-06 06:57:35 +00004307 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004308 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004309 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004310
John McCall7f040a92010-12-24 02:08:15 +00004311 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004312 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004313 ParseMicrosoftAttributes(DS.getAttributes());
4314
4315 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004316
4317 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004318 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004319 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4320 // attributes lost? Should they even be allowed?
4321 // FIXME: If we can leave the attributes in the token stream somehow, we can
4322 // get rid of a parameter (attrs) and this statement. It might be too much
4323 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004324 DS.takeAttributesFrom(attrs);
4325
Chris Lattnere64c5492009-02-27 18:38:20 +00004326 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004327
Chris Lattnerf97409f2008-04-06 06:57:35 +00004328 // Parse the declarator. This is "PrototypeContext", because we must
4329 // accept either 'declarator' or 'abstract-declarator' here.
4330 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4331 ParseDeclarator(ParmDecl);
4332
4333 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004334 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004335
Chris Lattnerf97409f2008-04-06 06:57:35 +00004336 // Remember this parsed parameter in ParamInfo.
4337 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004338
Douglas Gregor72b505b2008-12-16 21:30:33 +00004339 // DefArgToks is used when the parsing of default arguments needs
4340 // to be delayed.
4341 CachedTokens *DefArgToks = 0;
4342
Chris Lattnerf97409f2008-04-06 06:57:35 +00004343 // If no parameter was specified, verify that *something* was specified,
4344 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004345 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4346 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004347 // Completely missing, emit error.
4348 Diag(DSStart, diag::err_missing_param);
4349 } else {
4350 // Otherwise, we have something. Add it and let semantic analysis try
4351 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004352
Chris Lattnerf97409f2008-04-06 06:57:35 +00004353 // Inform the actions module about the parameter declarator, so it gets
4354 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004355 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004356
4357 // Parse the default argument, if any. We parse the default
4358 // arguments in all dialects; the semantic analysis in
4359 // ActOnParamDefaultArgument will reject the default argument in
4360 // C.
4361 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004362 SourceLocation EqualLoc = Tok.getLocation();
4363
Chris Lattner04421082008-04-08 04:40:51 +00004364 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004365 if (D.getContext() == Declarator::MemberContext) {
4366 // If we're inside a class definition, cache the tokens
4367 // corresponding to the default argument. We'll actually parse
4368 // them when we see the end of the class definition.
4369 // FIXME: Templates will require something similar.
4370 // FIXME: Can we use a smart pointer for Toks?
4371 DefArgToks = new CachedTokens;
4372
Mike Stump1eb44332009-09-09 15:08:12 +00004373 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004374 /*StopAtSemi=*/true,
4375 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004376 delete DefArgToks;
4377 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004378 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004379 } else {
4380 // Mark the end of the default argument so that we know when to
4381 // stop when we parse it later on.
4382 Token DefArgEnd;
4383 DefArgEnd.startToken();
4384 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4385 DefArgEnd.setLocation(Tok.getLocation());
4386 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004387 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004388 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004389 }
Chris Lattner04421082008-04-08 04:40:51 +00004390 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004391 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004392 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004393
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004394 // The argument isn't actually potentially evaluated unless it is
4395 // used.
4396 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004397 Sema::PotentiallyEvaluatedIfUsed,
4398 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004399
Sebastian Redl84407ba2012-03-14 15:54:00 +00004400 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004401 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4402 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004403 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004404 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004405 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004406 if (DefArgResult.isInvalid()) {
4407 Actions.ActOnParamDefaultArgumentError(Param);
4408 SkipUntil(tok::comma, tok::r_paren, true, true);
4409 } else {
4410 // Inform the actions module about the default argument
4411 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004412 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004413 }
Chris Lattner04421082008-04-08 04:40:51 +00004414 }
4415 }
Mike Stump1eb44332009-09-09 15:08:12 +00004416
4417 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4418 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004419 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004420 }
4421
4422 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004423 if (Tok.isNot(tok::comma)) {
4424 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004425 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4426
David Blaikie4e4d0842012-03-11 07:00:24 +00004427 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004428 // We have ellipsis without a preceding ',', which is ill-formed
4429 // in C. Complain and provide the fix.
4430 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004431 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004432 }
4433 }
4434
4435 break;
4436 }
Mike Stump1eb44332009-09-09 15:08:12 +00004437
Chris Lattnerf97409f2008-04-06 06:57:35 +00004438 // Consume the comma.
4439 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004440 }
Mike Stump1eb44332009-09-09 15:08:12 +00004441
Chris Lattner66d28652008-04-06 06:34:08 +00004442}
Chris Lattneref4715c2008-04-06 05:45:57 +00004443
Reid Spencer5f016e22007-07-11 17:01:13 +00004444/// [C90] direct-declarator '[' constant-expression[opt] ']'
4445/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4446/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4447/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4448/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4449void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004450 BalancedDelimiterTracker T(*this, tok::l_square);
4451 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004452
Chris Lattner378c7e42008-12-18 07:27:21 +00004453 // C array syntax has many features, but by-far the most common is [] and [4].
4454 // This code does a fast path to handle some of the most obvious cases.
4455 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004456 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004457 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004458 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004459
Chris Lattner378c7e42008-12-18 07:27:21 +00004460 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004461 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004462 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004463 T.getOpenLocation(),
4464 T.getCloseLocation()),
4465 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004466 return;
4467 } else if (Tok.getKind() == tok::numeric_constant &&
4468 GetLookAheadToken(1).is(tok::r_square)) {
4469 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004470 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004471 ConsumeToken();
4472
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004473 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004474 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004475 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004476
Chris Lattner378c7e42008-12-18 07:27:21 +00004477 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004478 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004479 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004480 T.getOpenLocation(),
4481 T.getCloseLocation()),
4482 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004483 return;
4484 }
Mike Stump1eb44332009-09-09 15:08:12 +00004485
Reid Spencer5f016e22007-07-11 17:01:13 +00004486 // If valid, this location is the position where we read the 'static' keyword.
4487 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004488 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004489 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004490
Reid Spencer5f016e22007-07-11 17:01:13 +00004491 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004492 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004493 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004494 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004495
Reid Spencer5f016e22007-07-11 17:01:13 +00004496 // If we haven't already read 'static', check to see if there is one after the
4497 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004498 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004499 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004500
Reid Spencer5f016e22007-07-11 17:01:13 +00004501 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4502 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004503 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004504
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004505 // Handle the case where we have '[*]' as the array size. However, a leading
4506 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4507 // the the token after the star is a ']'. Since stars in arrays are
4508 // infrequent, use of lookahead is not costly here.
4509 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004510 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004511
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004512 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004513 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004514 StaticLoc = SourceLocation(); // Drop the static.
4515 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004516 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004517 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004518 // Note, in C89, this production uses the constant-expr production instead
4519 // of assignment-expr. The only difference is that assignment-expr allows
4520 // things like '=' and '*='. Sema rejects these in C89 mode because they
4521 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004522
Douglas Gregore0762c92009-06-19 23:52:42 +00004523 // Parse the constant-expression or assignment-expression now (depending
4524 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004525 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004526 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004527 } else {
4528 EnterExpressionEvaluationContext Unevaluated(Actions,
4529 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004530 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004531 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004532 }
Mike Stump1eb44332009-09-09 15:08:12 +00004533
Reid Spencer5f016e22007-07-11 17:01:13 +00004534 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004535 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004536 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004537 // If the expression was invalid, skip it.
4538 SkipUntil(tok::r_square);
4539 return;
4540 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004541
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004542 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004543
John McCall0b7e6782011-03-24 11:26:52 +00004544 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004545 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004546
Chris Lattner378c7e42008-12-18 07:27:21 +00004547 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004548 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004549 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004550 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004551 T.getOpenLocation(),
4552 T.getCloseLocation()),
4553 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004554}
4555
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004556/// [GNU] typeof-specifier:
4557/// typeof ( expressions )
4558/// typeof ( type-name )
4559/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004560///
4561void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004562 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004563 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004564 SourceLocation StartLoc = ConsumeToken();
4565
John McCallcfb708c2010-01-13 20:03:27 +00004566 const bool hasParens = Tok.is(tok::l_paren);
4567
Eli Friedman71b8fb52012-01-21 01:01:51 +00004568 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4569
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004570 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004571 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004572 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004573 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4574 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004575 if (hasParens)
4576 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004577
4578 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004579 // FIXME: Not accurate, the range gets one token more than it should.
4580 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004581 else
4582 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004583
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004584 if (isCastExpr) {
4585 if (!CastTy) {
4586 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004587 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004588 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004589
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004590 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004591 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004592 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4593 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004594 DiagID, CastTy))
4595 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004596 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004597 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004598
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004599 // If we get here, the operand to the typeof was an expresion.
4600 if (Operand.isInvalid()) {
4601 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004602 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004603 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004604
Eli Friedman71b8fb52012-01-21 01:01:51 +00004605 // We might need to transform the operand if it is potentially evaluated.
4606 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4607 if (Operand.isInvalid()) {
4608 DS.SetTypeSpecError();
4609 return;
4610 }
4611
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004612 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004613 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004614 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4615 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004616 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004617 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004618}
Chris Lattner1b492422010-02-28 18:33:55 +00004619
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004620/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004621/// _Atomic ( type-name )
4622///
4623void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4624 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4625
4626 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004627 BalancedDelimiterTracker T(*this, tok::l_paren);
4628 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004629 SkipUntil(tok::r_paren);
4630 return;
4631 }
4632
4633 TypeResult Result = ParseTypeName();
4634 if (Result.isInvalid()) {
4635 SkipUntil(tok::r_paren);
4636 return;
4637 }
4638
4639 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004640 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004641
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004642 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004643 return;
4644
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004645 DS.setTypeofParensRange(T.getRange());
4646 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004647
4648 const char *PrevSpec = 0;
4649 unsigned DiagID;
4650 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4651 DiagID, Result.release()))
4652 Diag(StartLoc, DiagID) << PrevSpec;
4653}
4654
Chris Lattner1b492422010-02-28 18:33:55 +00004655
4656/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4657/// from TryAltiVecVectorToken.
4658bool Parser::TryAltiVecVectorTokenOutOfLine() {
4659 Token Next = NextToken();
4660 switch (Next.getKind()) {
4661 default: return false;
4662 case tok::kw_short:
4663 case tok::kw_long:
4664 case tok::kw_signed:
4665 case tok::kw_unsigned:
4666 case tok::kw_void:
4667 case tok::kw_char:
4668 case tok::kw_int:
4669 case tok::kw_float:
4670 case tok::kw_double:
4671 case tok::kw_bool:
4672 case tok::kw___pixel:
4673 Tok.setKind(tok::kw___vector);
4674 return true;
4675 case tok::identifier:
4676 if (Next.getIdentifierInfo() == Ident_pixel) {
4677 Tok.setKind(tok::kw___vector);
4678 return true;
4679 }
4680 return false;
4681 }
4682}
4683
4684bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4685 const char *&PrevSpec, unsigned &DiagID,
4686 bool &isInvalid) {
4687 if (Tok.getIdentifierInfo() == Ident_vector) {
4688 Token Next = NextToken();
4689 switch (Next.getKind()) {
4690 case tok::kw_short:
4691 case tok::kw_long:
4692 case tok::kw_signed:
4693 case tok::kw_unsigned:
4694 case tok::kw_void:
4695 case tok::kw_char:
4696 case tok::kw_int:
4697 case tok::kw_float:
4698 case tok::kw_double:
4699 case tok::kw_bool:
4700 case tok::kw___pixel:
4701 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4702 return true;
4703 case tok::identifier:
4704 if (Next.getIdentifierInfo() == Ident_pixel) {
4705 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4706 return true;
4707 }
4708 break;
4709 default:
4710 break;
4711 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004712 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004713 DS.isTypeAltiVecVector()) {
4714 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4715 return true;
4716 }
4717 return false;
4718}