blob: 08519410ddb0bde7452fd7b1d3be789abb58ea5e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000023#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.7: Declarations.
28//===----------------------------------------------------------------------===//
29
30/// ParseTypeName
31/// type-name: [C99 6.7.6]
32/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000033///
34/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000035TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000036 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000037 AccessSpecifier AS,
38 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000039 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith7796eb52012-03-12 08:56:40 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000042 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000043 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000044 if (OwnedType)
45 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000048 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000050 if (Range)
51 *Range = DeclaratorInfo.getSourceRange();
52
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000054 return true;
55
Douglas Gregor23c94db2010-07-02 17:43:08 +000056 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000059
60/// isAttributeLateParsed - Return true if the attribute has arguments that
61/// require late parsing.
62static bool isAttributeLateParsed(const IdentifierInfo &II) {
63 return llvm::StringSwitch<bool>(II.getName())
64#include "clang/Parse/AttrLateParsed.inc"
65 .Default(false);
66}
67
68
Sean Huntbbd37c62009-11-21 08:43:09 +000069/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71/// [GNU] attributes:
72/// attribute
73/// attributes attribute
74///
75/// [GNU] attribute:
76/// '__attribute__' '(' '(' attribute-list ')' ')'
77///
78/// [GNU] attribute-list:
79/// attrib
80/// attribute_list ',' attrib
81///
82/// [GNU] attrib:
83/// empty
84/// attrib-name
85/// attrib-name '(' identifier ')'
86/// attrib-name '(' identifier ',' nonempty-expr-list ')'
87/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
88///
89/// [GNU] attrib-name:
90/// identifier
91/// typespec
92/// typequal
93/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000094///
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000096/// token lookahead. Comment from gcc: "If they start with an identifier
97/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// start with that identifier; otherwise they are an expression list."
99///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000100/// GCC does not require the ',' between attribs in an attribute-list.
101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// At the moment, I am not doing 2 token lookahead. I am also unaware of
103/// any attributes that don't work (based on my limited testing). Most
104/// attributes are very simple in practice. Until we find a bug, I don't see
105/// a pressing need to implement the 2 token lookahead.
106
John McCall7f040a92010-12-24 02:08:15 +0000107void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000108 SourceLocation *endLoc,
109 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000110 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeToken();
114 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
115 "attribute")) {
116 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000117 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000124 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
125 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
128 ConsumeToken();
129 continue;
130 }
131 // we have an identifier or declaration specifier (const, int, etc.)
132 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
133 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000135 if (Tok.is(tok::l_paren)) {
136 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000137 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141
142 // Attributes in a class are parsed at the end of the class, along
143 // with other late-parsed declarations.
144 if (!ClassStack.empty())
145 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000147 // consume everything up to and including the matching right parens
148 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 Token Eof;
151 Eof.startToken();
152 Eof.setLocation(Tok.getLocation());
153 LA->Toks.push_back(Eof);
154 } else {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000158 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
159 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 }
162 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000164 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
166 SkipUntil(tok::r_paren, false);
167 }
John McCall7f040a92010-12-24 02:08:15 +0000168 if (endLoc)
169 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000173
174/// Parse the arguments to a parameterized GNU attribute
175void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
176 SourceLocation AttrNameLoc,
177 ParsedAttributes &Attrs,
178 SourceLocation *EndLoc) {
179
180 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
181
182 // Availability attributes have their own grammar.
183 if (AttrName->isStr("availability")) {
184 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
185 return;
186 }
187 // Thread safety attributes fit into the FIXME case above, so we
188 // just parse the arguments as a list of expressions
189 if (IsThreadSafetyAttribute(AttrName->getName())) {
190 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
191 return;
192 }
193
194 ConsumeParen(); // ignore the left paren loc for now
195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 IdentifierInfo *ParmName = 0;
197 SourceLocation ParmLoc;
198 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000199
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000200 switch (Tok.getKind()) {
201 case tok::kw_char:
202 case tok::kw_wchar_t:
203 case tok::kw_char16_t:
204 case tok::kw_char32_t:
205 case tok::kw_bool:
206 case tok::kw_short:
207 case tok::kw_int:
208 case tok::kw_long:
209 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000210 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000211 case tok::kw_signed:
212 case tok::kw_unsigned:
213 case tok::kw_float:
214 case tok::kw_double:
215 case tok::kw_void:
216 case tok::kw_typeof:
217 // __attribute__(( vec_type_hint(char) ))
218 // FIXME: Don't just discard the builtin type token.
219 ConsumeToken();
220 BuiltinType = true;
221 break;
222
223 case tok::identifier:
224 ParmName = Tok.getIdentifierInfo();
225 ParmLoc = ConsumeToken();
226 break;
227
228 default:
229 break;
230 }
231
232 ExprVector ArgExprs(Actions);
233
234 if (!BuiltinType &&
235 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
236 // Eat the comma.
237 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000238 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000239
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000240 // Parse the non-empty comma-separated list of expressions.
241 while (1) {
242 ExprResult ArgExpr(ParseAssignmentExpression());
243 if (ArgExpr.isInvalid()) {
244 SkipUntil(tok::r_paren);
245 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 ArgExprs.push_back(ArgExpr.release());
248 if (Tok.isNot(tok::comma))
249 break;
250 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000252 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000253 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
254 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
255 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000256 while (Tok.is(tok::identifier)) {
257 ConsumeToken();
258 if (Tok.is(tok::greater))
259 break;
260 if (Tok.is(tok::comma)) {
261 ConsumeToken();
262 continue;
263 }
264 }
265 if (Tok.isNot(tok::greater))
266 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000267 SkipUntil(tok::r_paren, false, true); // skip until ')'
268 }
269 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270
271 SourceLocation RParen = Tok.getLocation();
272 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
273 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000274 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000275 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000276 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000277 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000278 }
279}
280
281
Eli Friedmana23b4852009-06-08 07:21:15 +0000282/// ParseMicrosoftDeclSpec - Parse an __declspec construct
283///
284/// [MS] decl-specifier:
285/// __declspec ( extended-decl-modifier-seq )
286///
287/// [MS] extended-decl-modifier-seq:
288/// extended-decl-modifier[opt]
289/// extended-decl-modifier extended-decl-modifier-seq
290
John McCall7f040a92010-12-24 02:08:15 +0000291void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000292 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000293
Steve Narofff59e17e2008-12-24 20:59:21 +0000294 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000295 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
296 "declspec")) {
297 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000298 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000299 }
Francois Pichet373197b2011-05-07 19:04:49 +0000300
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000302 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
303 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000304
305 // FIXME: Remove this when we have proper __declspec(property()) support.
306 // Just skip everything inside property().
307 if (AttrName->getName() == "property") {
308 ConsumeParen();
309 SkipUntil(tok::r_paren);
310 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (Tok.is(tok::l_paren)) {
312 ConsumeParen();
313 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
314 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000315 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000316 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000317 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000318 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
319 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000320 }
321 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
322 SkipUntil(tok::r_paren, false);
323 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
325 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000326 }
327 }
328 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
329 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000330 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000331}
332
John McCall7f040a92010-12-24 02:08:15 +0000333void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000334 // Treat these like attributes
335 // FIXME: Allow Sema to distinguish between these and real attributes!
336 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000337 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000338 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000339 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000340 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000341 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
342 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000343 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
344 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000345 // FIXME: Support these properly!
346 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000347 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
348 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000349 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000350}
351
John McCall7f040a92010-12-24 02:08:15 +0000352void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000353 // Treat these like attributes
354 while (Tok.is(tok::kw___pascal)) {
355 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
356 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000357 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
358 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000359 }
John McCall7f040a92010-12-24 02:08:15 +0000360}
361
Peter Collingbournef315fa82011-02-14 01:42:53 +0000362void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
363 // Treat these like attributes
364 while (Tok.is(tok::kw___kernel)) {
365 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000366 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
367 AttrNameLoc, 0, AttrNameLoc, 0,
368 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000369 }
370}
371
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
373 SourceLocation Loc = Tok.getLocation();
374 switch(Tok.getKind()) {
375 // OpenCL qualifiers:
376 case tok::kw___private:
377 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000378 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000379 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000380 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 break;
382
383 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000384 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000385 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000386 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000387 break;
388
389 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000390 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000391 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000392 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 break;
394
395 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000396 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000397 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000398 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 break;
400
401 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000402 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000403 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000404 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 break;
406
407 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000408 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000409 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000410 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 break;
412
413 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000414 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000415 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000416 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 break;
418 default: break;
419 }
420}
421
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000422/// \brief Parse a version number.
423///
424/// version:
425/// simple-integer
426/// simple-integer ',' simple-integer
427/// simple-integer ',' simple-integer ',' simple-integer
428VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
429 Range = Tok.getLocation();
430
431 if (!Tok.is(tok::numeric_constant)) {
432 Diag(Tok, diag::err_expected_version);
433 SkipUntil(tok::comma, tok::r_paren, true, true, true);
434 return VersionTuple();
435 }
436
437 // Parse the major (and possibly minor and subminor) versions, which
438 // are stored in the numeric constant. We utilize a quirk of the
439 // lexer, which is that it handles something like 1.2.3 as a single
440 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000441 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000442 Buffer.resize(Tok.getLength()+1);
443 const char *ThisTokBegin = &Buffer[0];
444
445 // Get the spelling of the token, which eliminates trigraphs, etc.
446 bool Invalid = false;
447 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
448 if (Invalid)
449 return VersionTuple();
450
451 // Parse the major version.
452 unsigned AfterMajor = 0;
453 unsigned Major = 0;
454 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
455 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
456 ++AfterMajor;
457 }
458
459 if (AfterMajor == 0) {
460 Diag(Tok, diag::err_expected_version);
461 SkipUntil(tok::comma, tok::r_paren, true, true, true);
462 return VersionTuple();
463 }
464
465 if (AfterMajor == ActualLength) {
466 ConsumeToken();
467
468 // We only had a single version component.
469 if (Major == 0) {
470 Diag(Tok, diag::err_zero_version);
471 return VersionTuple();
472 }
473
474 return VersionTuple(Major);
475 }
476
477 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
478 Diag(Tok, diag::err_expected_version);
479 SkipUntil(tok::comma, tok::r_paren, true, true, true);
480 return VersionTuple();
481 }
482
483 // Parse the minor version.
484 unsigned AfterMinor = AfterMajor + 1;
485 unsigned Minor = 0;
486 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
487 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
488 ++AfterMinor;
489 }
490
491 if (AfterMinor == ActualLength) {
492 ConsumeToken();
493
494 // We had major.minor.
495 if (Major == 0 && Minor == 0) {
496 Diag(Tok, diag::err_zero_version);
497 return VersionTuple();
498 }
499
500 return VersionTuple(Major, Minor);
501 }
502
503 // If what follows is not a '.', we have a problem.
504 if (ThisTokBegin[AfterMinor] != '.') {
505 Diag(Tok, diag::err_expected_version);
506 SkipUntil(tok::comma, tok::r_paren, true, true, true);
507 return VersionTuple();
508 }
509
510 // Parse the subminor version.
511 unsigned AfterSubminor = AfterMinor + 1;
512 unsigned Subminor = 0;
513 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
514 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
515 ++AfterSubminor;
516 }
517
518 if (AfterSubminor != ActualLength) {
519 Diag(Tok, diag::err_expected_version);
520 SkipUntil(tok::comma, tok::r_paren, true, true, true);
521 return VersionTuple();
522 }
523 ConsumeToken();
524 return VersionTuple(Major, Minor, Subminor);
525}
526
527/// \brief Parse the contents of the "availability" attribute.
528///
529/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000530/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000531///
532/// platform:
533/// identifier
534///
535/// version-arg-list:
536/// version-arg
537/// version-arg ',' version-arg-list
538///
539/// version-arg:
540/// 'introduced' '=' version
541/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000542/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000543/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000544/// opt-message:
545/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000546void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
547 SourceLocation AvailabilityLoc,
548 ParsedAttributes &attrs,
549 SourceLocation *endLoc) {
550 SourceLocation PlatformLoc;
551 IdentifierInfo *Platform = 0;
552
553 enum { Introduced, Deprecated, Obsoleted, Unknown };
554 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000555 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000556
557 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000558 BalancedDelimiterTracker T(*this, tok::l_paren);
559 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560 Diag(Tok, diag::err_expected_lparen);
561 return;
562 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563
564 // Parse the platform name,
565 if (Tok.isNot(tok::identifier)) {
566 Diag(Tok, diag::err_availability_expected_platform);
567 SkipUntil(tok::r_paren);
568 return;
569 }
570 Platform = Tok.getIdentifierInfo();
571 PlatformLoc = ConsumeToken();
572
573 // Parse the ',' following the platform name.
574 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
575 return;
576
577 // If we haven't grabbed the pointers for the identifiers
578 // "introduced", "deprecated", and "obsoleted", do so now.
579 if (!Ident_introduced) {
580 Ident_introduced = PP.getIdentifierInfo("introduced");
581 Ident_deprecated = PP.getIdentifierInfo("deprecated");
582 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000583 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000584 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000585 }
586
587 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000588 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000589 do {
590 if (Tok.isNot(tok::identifier)) {
591 Diag(Tok, diag::err_availability_expected_change);
592 SkipUntil(tok::r_paren);
593 return;
594 }
595 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
596 SourceLocation KeywordLoc = ConsumeToken();
597
Douglas Gregorb53e4172011-03-26 03:35:55 +0000598 if (Keyword == Ident_unavailable) {
599 if (UnavailableLoc.isValid()) {
600 Diag(KeywordLoc, diag::err_availability_redundant)
601 << Keyword << SourceRange(UnavailableLoc);
602 }
603 UnavailableLoc = KeywordLoc;
604
605 if (Tok.isNot(tok::comma))
606 break;
607
608 ConsumeToken();
609 continue;
610 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000611
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000612 if (Tok.isNot(tok::equal)) {
613 Diag(Tok, diag::err_expected_equal_after)
614 << Keyword;
615 SkipUntil(tok::r_paren);
616 return;
617 }
618 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000619 if (Keyword == Ident_message) {
620 if (!isTokenStringLiteral()) {
621 Diag(Tok, diag::err_expected_string_literal);
622 SkipUntil(tok::r_paren);
623 return;
624 }
625 MessageExpr = ParseStringLiteralExpression();
626 break;
627 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628
629 SourceRange VersionRange;
630 VersionTuple Version = ParseVersionTuple(VersionRange);
631
632 if (Version.empty()) {
633 SkipUntil(tok::r_paren);
634 return;
635 }
636
637 unsigned Index;
638 if (Keyword == Ident_introduced)
639 Index = Introduced;
640 else if (Keyword == Ident_deprecated)
641 Index = Deprecated;
642 else if (Keyword == Ident_obsoleted)
643 Index = Obsoleted;
644 else
645 Index = Unknown;
646
647 if (Index < Unknown) {
648 if (!Changes[Index].KeywordLoc.isInvalid()) {
649 Diag(KeywordLoc, diag::err_availability_redundant)
650 << Keyword
651 << SourceRange(Changes[Index].KeywordLoc,
652 Changes[Index].VersionRange.getEnd());
653 }
654
655 Changes[Index].KeywordLoc = KeywordLoc;
656 Changes[Index].Version = Version;
657 Changes[Index].VersionRange = VersionRange;
658 } else {
659 Diag(KeywordLoc, diag::err_availability_unknown_change)
660 << Keyword << VersionRange;
661 }
662
663 if (Tok.isNot(tok::comma))
664 break;
665
666 ConsumeToken();
667 } while (true);
668
669 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000670 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000671 return;
672
673 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000674 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000675
Douglas Gregorb53e4172011-03-26 03:35:55 +0000676 // The 'unavailable' availability cannot be combined with any other
677 // availability changes. Make sure that hasn't happened.
678 if (UnavailableLoc.isValid()) {
679 bool Complained = false;
680 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
681 if (Changes[Index].KeywordLoc.isValid()) {
682 if (!Complained) {
683 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
684 << SourceRange(Changes[Index].KeywordLoc,
685 Changes[Index].VersionRange.getEnd());
686 Complained = true;
687 }
688
689 // Clear out the availability.
690 Changes[Index] = AvailabilityChange();
691 }
692 }
693 }
694
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000696 attrs.addNew(&Availability,
697 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000698 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000699 Platform, PlatformLoc,
700 Changes[Introduced],
701 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000702 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000703 UnavailableLoc, MessageExpr.take(),
704 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705}
706
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000707
708// Late Parsed Attributes:
709// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
710
711void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
712
713void Parser::LateParsedClass::ParseLexedAttributes() {
714 Self->ParseLexedAttributes(*Class);
715}
716
717void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000718 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000719}
720
721/// Wrapper class which calls ParseLexedAttribute, after setting up the
722/// scope appropriately.
723void Parser::ParseLexedAttributes(ParsingClass &Class) {
724 // Deal with templates
725 // FIXME: Test cases to make sure this does the right thing for templates.
726 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
727 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
728 HasTemplateScope);
729 if (HasTemplateScope)
730 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
731
732 // Set or update the scope flags to include Scope::ThisScope.
733 bool AlreadyHasClassScope = Class.TopLevelClass;
734 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
735 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
736 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
737
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000738 // Enter the scope of nested classes
739 if (!AlreadyHasClassScope)
740 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
741 Class.TagOrTemplate);
742
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000743 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
744 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
745 }
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000746
747 if (!AlreadyHasClassScope)
748 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
749 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000750}
751
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000752
753/// \brief Parse all attributes in LAs, and attach them to Decl D.
754void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
755 bool EnterScope, bool OnDefinition) {
756 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000757 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
759 }
760 LAs.clear();
761}
762
763
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000764/// \brief Finish parsing an attribute for which parsing was delayed.
765/// This will be called at the end of parsing a class declaration
766/// for each LateParsedAttribute. We consume the saved tokens and
767/// create an attribute with the arguments filled in. We add this
768/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000769void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
770 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771 // Save the current token position.
772 SourceLocation OrigLoc = Tok.getLocation();
773
774 // Append the current token at the end of the new token stream so that it
775 // doesn't get lost.
776 LA.Toks.push_back(Tok);
777 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
778 // Consume the previously pushed token.
779 ConsumeAnyToken();
780
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000781 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
782 Diag(Tok, diag::warn_attribute_on_function_definition)
783 << LA.AttrName.getName();
784 }
785
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000786 ParsedAttributes Attrs(AttrFactory);
787 SourceLocation endLoc;
788
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000789 if (LA.Decls.size() == 1) {
790 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000791
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000792 // If the Decl is templatized, add template parameters to scope.
793 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
794 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
795 if (HasTemplateScope)
796 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000797
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000798 // If the Decl is on a function, add function parameters to the scope.
799 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
800 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
801 if (HasFunctionScope)
802 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
803
804 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
805
806 if (HasFunctionScope) {
807 Actions.ActOnExitFunctionContext();
808 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
809 }
810 if (HasTemplateScope) {
811 TempScope.Exit();
812 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000813 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000814 // If there are multiple decls, then the decl cannot be within the
815 // function scope.
816 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000817 } else {
818 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000819 }
820
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
822 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
823 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000824
825 if (Tok.getLocation() != OrigLoc) {
826 // Due to a parsing error, we either went over the cached tokens or
827 // there are still cached tokens left, so we skip the leftover tokens.
828 // Since this is an uncommon situation that should be avoided, use the
829 // expensive isBeforeInTranslationUnit call.
830 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
831 OrigLoc))
832 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000833 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000834 }
835}
836
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000837/// \brief Wrapper around a case statement checking if AttrName is
838/// one of the thread safety attributes
839bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
840 return llvm::StringSwitch<bool>(AttrName)
841 .Case("guarded_by", true)
842 .Case("guarded_var", true)
843 .Case("pt_guarded_by", true)
844 .Case("pt_guarded_var", true)
845 .Case("lockable", true)
846 .Case("scoped_lockable", true)
847 .Case("no_thread_safety_analysis", true)
848 .Case("acquired_after", true)
849 .Case("acquired_before", true)
850 .Case("exclusive_lock_function", true)
851 .Case("shared_lock_function", true)
852 .Case("exclusive_trylock_function", true)
853 .Case("shared_trylock_function", true)
854 .Case("unlock_function", true)
855 .Case("lock_returned", true)
856 .Case("locks_excluded", true)
857 .Case("exclusive_locks_required", true)
858 .Case("shared_locks_required", true)
859 .Default(false);
860}
861
862/// \brief Parse the contents of thread safety attributes. These
863/// should always be parsed as an expression list.
864///
865/// We need to special case the parsing due to the fact that if the first token
866/// of the first argument is an identifier, the main parse loop will store
867/// that token as a "parameter" and the rest of
868/// the arguments will be added to a list of "arguments". However,
869/// subsequent tokens in the first argument are lost. We instead parse each
870/// argument as an expression and add all arguments to the list of "arguments".
871/// In future, we will take advantage of this special case to also
872/// deal with some argument scoping issues here (for example, referring to a
873/// function parameter in the attribute on that function).
874void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
875 SourceLocation AttrNameLoc,
876 ParsedAttributes &Attrs,
877 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000878 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000880 BalancedDelimiterTracker T(*this, tok::l_paren);
881 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882
883 ExprVector ArgExprs(Actions);
884 bool ArgExprsOk = true;
885
886 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000887 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888 ExprResult ArgExpr(ParseAssignmentExpression());
889 if (ArgExpr.isInvalid()) {
890 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000891 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000892 break;
893 } else {
894 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000896 if (Tok.isNot(tok::comma))
897 break;
898 ConsumeToken(); // Eat the comma, move to the next argument
899 }
900 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000901 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000902 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
903 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000905 if (EndLoc)
906 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000907}
908
Richard Smith6ee326a2012-04-10 01:32:12 +0000909/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
910/// of a C++11 attribute-specifier in a location where an attribute is not
911/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
912/// situation.
913///
914/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
915/// this doesn't appear to actually be an attribute-specifier, and the caller
916/// should try to parse it.
917bool Parser::DiagnoseProhibitedCXX11Attribute() {
918 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
919
920 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
921 case CAK_NotAttributeSpecifier:
922 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
923 return false;
924
925 case CAK_InvalidAttributeSpecifier:
926 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
927 return false;
928
929 case CAK_AttributeSpecifier:
930 // Parse and discard the attributes.
931 SourceLocation BeginLoc = ConsumeBracket();
932 ConsumeBracket();
933 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
934 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
935 SourceLocation EndLoc = ConsumeBracket();
936 Diag(BeginLoc, diag::err_attributes_not_allowed)
937 << SourceRange(BeginLoc, EndLoc);
938 return true;
939 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000940 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000941}
942
John McCall7f040a92010-12-24 02:08:15 +0000943void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
944 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
945 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000946}
947
Reid Spencer5f016e22007-07-11 17:01:13 +0000948/// ParseDeclaration - Parse a full 'declaration', which consists of
949/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000950/// 'Context' should be a Declarator::TheContext value. This returns the
951/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000952///
953/// declaration: [C99 6.7]
954/// block-declaration ->
955/// simple-declaration
956/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000957/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000958/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000959/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000960/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000961/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000962/// others... [FIXME]
963///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000964Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
965 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000966 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000967 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000968 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000969 // Must temporarily exit the objective-c container scope for
970 // parsing c none objective-c decls.
971 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000972
John McCalld226f652010-08-21 09:40:31 +0000973 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000974 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000975 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000976 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000977 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000978 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000979 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000980 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000981 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000982 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000983 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000984 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000985 SourceLocation InlineLoc = ConsumeToken();
986 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
987 break;
988 }
John McCall7f040a92010-12-24 02:08:15 +0000989 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000990 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000991 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000992 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000993 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000994 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000995 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000996 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000997 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000998 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000999 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001000 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001001 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001002 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001003 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001004 default:
John McCall7f040a92010-12-24 02:08:15 +00001005 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001006 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001007
Chris Lattner682bf922009-03-29 16:50:03 +00001008 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001009 // single decl, convert it now. Alias declarations can also declare a type;
1010 // include that too if it is present.
1011 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001012}
1013
1014/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1015/// declaration-specifiers init-declarator-list[opt] ';'
1016///[C90/C++]init-declarator-list ';' [TODO]
1017/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001018///
Richard Smithad762fc2011-04-14 22:09:26 +00001019/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1020/// attribute-specifier-seq[opt] type-specifier-seq declarator
1021///
Chris Lattnercd147752009-03-29 17:27:48 +00001022/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001023/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001024///
1025/// If FRI is non-null, we might be parsing a for-range-declaration instead
1026/// of a simple-declaration. If we find that we are, we also parse the
1027/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001028Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1029 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001030 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001031 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001032 bool RequireSemi,
1033 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001035 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001036 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001037
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001038 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001039 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001040
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1042 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001043 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001044 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001045 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001046 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001047 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001048 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001050
1051 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001052}
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Richard Smith0706df42011-10-19 21:33:05 +00001054/// Returns true if this might be the start of a declarator, or a common typo
1055/// for a declarator.
1056bool Parser::MightBeDeclarator(unsigned Context) {
1057 switch (Tok.getKind()) {
1058 case tok::annot_cxxscope:
1059 case tok::annot_template_id:
1060 case tok::caret:
1061 case tok::code_completion:
1062 case tok::coloncolon:
1063 case tok::ellipsis:
1064 case tok::kw___attribute:
1065 case tok::kw_operator:
1066 case tok::l_paren:
1067 case tok::star:
1068 return true;
1069
1070 case tok::amp:
1071 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001072 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001073
Richard Smith1c94c162012-01-09 22:31:44 +00001074 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001075 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001076 NextToken().is(tok::l_square);
1077
1078 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001079 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001080
Richard Smith0706df42011-10-19 21:33:05 +00001081 case tok::identifier:
1082 switch (NextToken().getKind()) {
1083 case tok::code_completion:
1084 case tok::coloncolon:
1085 case tok::comma:
1086 case tok::equal:
1087 case tok::equalequal: // Might be a typo for '='.
1088 case tok::kw_alignas:
1089 case tok::kw_asm:
1090 case tok::kw___attribute:
1091 case tok::l_brace:
1092 case tok::l_paren:
1093 case tok::l_square:
1094 case tok::less:
1095 case tok::r_brace:
1096 case tok::r_paren:
1097 case tok::r_square:
1098 case tok::semi:
1099 return true;
1100
1101 case tok::colon:
1102 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001103 // and in block scope it's probably a label. Inside a class definition,
1104 // this is a bit-field.
1105 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001106 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001107
1108 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001109 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001110
1111 default:
1112 return false;
1113 }
1114
1115 default:
1116 return false;
1117 }
1118}
1119
John McCalld8ac0572009-11-03 19:26:08 +00001120/// ParseDeclGroup - Having concluded that this is either a function
1121/// definition or a group of object declarations, actually parse the
1122/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001123Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1124 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001125 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001126 SourceLocation *DeclEnd,
1127 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001128 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001129 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001130 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001131
John McCalld8ac0572009-11-03 19:26:08 +00001132 // Bail out if the first declarator didn't seem well-formed.
1133 if (!D.hasName() && !D.mayOmitIdentifier()) {
1134 // Skip until ; or }.
1135 SkipUntil(tok::r_brace, true, true);
1136 if (Tok.is(tok::semi))
1137 ConsumeToken();
1138 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001141 // Save late-parsed attributes for now; they need to be parsed in the
1142 // appropriate function scope after the function Decl has been constructed.
1143 LateParsedAttrList LateParsedAttrs;
1144 if (D.isFunctionDeclarator())
1145 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1146
Chris Lattnerc82daef2010-07-11 22:24:20 +00001147 // Check to see if we have a function *definition* which must have a body.
1148 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1149 // Look at the next token to make sure that this isn't a function
1150 // declaration. We have to check this because __attribute__ might be the
1151 // start of a function definition in GCC-extended K&R C.
1152 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001153
Chris Lattner004659a2010-07-11 22:42:07 +00001154 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001155 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1156 Diag(Tok, diag::err_function_declared_typedef);
1157
1158 // Recover by treating the 'typedef' as spurious.
1159 DS.ClearStorageClassSpecs();
1160 }
1161
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001162 Decl *TheDecl =
1163 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001164 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001165 }
1166
1167 if (isDeclarationSpecifier()) {
1168 // If there is an invalid declaration specifier right after the function
1169 // prototype, then we must be in a missing semicolon case where this isn't
1170 // actually a body. Just fall through into the code that handles it as a
1171 // prototype, and let the top-level code handle the erroneous declspec
1172 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001173 } else {
1174 Diag(Tok, diag::err_expected_fn_body);
1175 SkipUntil(tok::semi);
1176 return DeclGroupPtrTy();
1177 }
1178 }
1179
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001180 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001181 return DeclGroupPtrTy();
1182
1183 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1184 // must parse and analyze the for-range-initializer before the declaration is
1185 // analyzed.
1186 if (FRI && Tok.is(tok::colon)) {
1187 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001188 if (Tok.is(tok::l_brace))
1189 FRI->RangeExpr = ParseBraceInitializer();
1190 else
1191 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001192 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1193 Actions.ActOnCXXForRangeDecl(ThisDecl);
1194 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001195 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001196 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1197 }
1198
Chris Lattner5f9e2722011-07-23 10:55:15 +00001199 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001200 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001201 if (LateParsedAttrs.size() > 0)
1202 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001203 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001204 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001205 DeclsInGroup.push_back(FirstDecl);
1206
Richard Smith0706df42011-10-19 21:33:05 +00001207 bool ExpectSemi = Context != Declarator::ForContext;
1208
John McCalld8ac0572009-11-03 19:26:08 +00001209 // If we don't have a comma, it is either the end of the list (a ';') or an
1210 // error, bail out.
1211 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001212 SourceLocation CommaLoc = ConsumeToken();
1213
1214 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1215 // This comma was followed by a line-break and something which can't be
1216 // the start of a declarator. The comma was probably a typo for a
1217 // semicolon.
1218 Diag(CommaLoc, diag::err_expected_semi_declaration)
1219 << FixItHint::CreateReplacement(CommaLoc, ";");
1220 ExpectSemi = false;
1221 break;
1222 }
John McCalld8ac0572009-11-03 19:26:08 +00001223
1224 // Parse the next declarator.
1225 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001226 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001227
1228 // Accept attributes in an init-declarator. In the first declarator in a
1229 // declaration, these would be part of the declspec. In subsequent
1230 // declarators, they become part of the declarator itself, so that they
1231 // don't apply to declarators after *this* one. Examples:
1232 // short __attribute__((common)) var; -> declspec
1233 // short var __attribute__((common)); -> declarator
1234 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001235 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001236
1237 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001238 if (!D.isInvalidType()) {
1239 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1240 D.complete(ThisDecl);
1241 if (ThisDecl)
1242 DeclsInGroup.push_back(ThisDecl);
1243 }
John McCalld8ac0572009-11-03 19:26:08 +00001244 }
1245
1246 if (DeclEnd)
1247 *DeclEnd = Tok.getLocation();
1248
Richard Smith0706df42011-10-19 21:33:05 +00001249 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001250 ExpectAndConsume(tok::semi,
1251 Context == Declarator::FileContext
1252 ? diag::err_invalid_token_after_toplevel_declarator
1253 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001254 // Okay, there was no semicolon and one was expected. If we see a
1255 // declaration specifier, just assume it was missing and continue parsing.
1256 // Otherwise things are very confused and we skip to recover.
1257 if (!isDeclarationSpecifier()) {
1258 SkipUntil(tok::r_brace, true, true);
1259 if (Tok.is(tok::semi))
1260 ConsumeToken();
1261 }
John McCalld8ac0572009-11-03 19:26:08 +00001262 }
1263
Douglas Gregor23c94db2010-07-02 17:43:08 +00001264 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001265 DeclsInGroup.data(),
1266 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001267}
1268
Richard Smithad762fc2011-04-14 22:09:26 +00001269/// Parse an optional simple-asm-expr and attributes, and attach them to a
1270/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001271bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001272 // If a simple-asm-expr is present, parse it.
1273 if (Tok.is(tok::kw_asm)) {
1274 SourceLocation Loc;
1275 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1276 if (AsmLabel.isInvalid()) {
1277 SkipUntil(tok::semi, true, true);
1278 return true;
1279 }
1280
1281 D.setAsmLabel(AsmLabel.release());
1282 D.SetRangeEnd(Loc);
1283 }
1284
1285 MaybeParseGNUAttributes(D);
1286 return false;
1287}
1288
Douglas Gregor1426e532009-05-12 21:31:51 +00001289/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1290/// declarator'. This method parses the remainder of the declaration
1291/// (including any attributes or initializer, among other things) and
1292/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001293///
Reid Spencer5f016e22007-07-11 17:01:13 +00001294/// init-declarator: [C99 6.7]
1295/// declarator
1296/// declarator '=' initializer
1297/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1298/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001299/// [C++] declarator initializer[opt]
1300///
1301/// [C++] initializer:
1302/// [C++] '=' initializer-clause
1303/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001304/// [C++0x] '=' 'default' [TODO]
1305/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001306/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001307///
1308/// According to the standard grammar, =default and =delete are function
1309/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001310///
John McCalld226f652010-08-21 09:40:31 +00001311Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001312 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001313 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001314 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Richard Smithad762fc2011-04-14 22:09:26 +00001316 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1317}
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Richard Smithad762fc2011-04-14 22:09:26 +00001319Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1320 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001321 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001322 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001323 switch (TemplateInfo.Kind) {
1324 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001325 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001326 break;
1327
1328 case ParsedTemplateInfo::Template:
1329 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001330 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001331 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001332 TemplateInfo.TemplateParams->data(),
1333 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001334 D);
1335 break;
1336
1337 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001338 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001339 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001340 TemplateInfo.ExternLoc,
1341 TemplateInfo.TemplateLoc,
1342 D);
1343 if (ThisRes.isInvalid()) {
1344 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001345 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001346 }
1347
1348 ThisDecl = ThisRes.get();
1349 break;
1350 }
1351 }
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Richard Smith34b41d92011-02-20 03:19:35 +00001353 bool TypeContainsAuto =
1354 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1355
Douglas Gregor1426e532009-05-12 21:31:51 +00001356 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001357 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001358 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001359 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001360 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001361 if (D.isFunctionDeclarator())
1362 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1363 << 1 /* delete */;
1364 else
1365 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001366 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001367 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001368 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1369 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001370 else
1371 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001372 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001373 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001374 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001375 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001376 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001377
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001378 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001379 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001380 cutOffParsing();
1381 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001382 }
1383
John McCall60d7b3a2010-08-24 06:29:42 +00001384 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001385
David Blaikie4e4d0842012-03-11 07:00:24 +00001386 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001387 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001388 ExitScope();
1389 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001390
Douglas Gregor1426e532009-05-12 21:31:51 +00001391 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001392 SkipUntil(tok::comma, true, true);
1393 Actions.ActOnInitializerError(ThisDecl);
1394 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001395 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1396 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001397 }
1398 } else if (Tok.is(tok::l_paren)) {
1399 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001400 BalancedDelimiterTracker T(*this, tok::l_paren);
1401 T.consumeOpen();
1402
Douglas Gregor1426e532009-05-12 21:31:51 +00001403 ExprVector Exprs(Actions);
1404 CommaLocsTy CommaLocs;
1405
David Blaikie4e4d0842012-03-11 07:00:24 +00001406 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001407 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001408 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001409 }
1410
Douglas Gregor1426e532009-05-12 21:31:51 +00001411 if (ParseExpressionList(Exprs, CommaLocs)) {
1412 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001413
David Blaikie4e4d0842012-03-11 07:00:24 +00001414 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001415 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001416 ExitScope();
1417 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001418 } else {
1419 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001420 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001421
1422 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1423 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001424
David Blaikie4e4d0842012-03-11 07:00:24 +00001425 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001426 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001427 ExitScope();
1428 }
1429
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001430 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1431 T.getCloseLocation(),
1432 move_arg(Exprs));
1433 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1434 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001435 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001436 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001437 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001438 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1439
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001440 if (D.getCXXScopeSpec().isSet()) {
1441 EnterScope(0);
1442 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1443 }
1444
1445 ExprResult Init(ParseBraceInitializer());
1446
1447 if (D.getCXXScopeSpec().isSet()) {
1448 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1449 ExitScope();
1450 }
1451
1452 if (Init.isInvalid()) {
1453 Actions.ActOnInitializerError(ThisDecl);
1454 } else
1455 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1456 /*DirectInit=*/true, TypeContainsAuto);
1457
Douglas Gregor1426e532009-05-12 21:31:51 +00001458 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001459 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001460 }
1461
Richard Smith483b9f32011-02-21 20:05:19 +00001462 Actions.FinalizeDeclaration(ThisDecl);
1463
Douglas Gregor1426e532009-05-12 21:31:51 +00001464 return ThisDecl;
1465}
1466
Reid Spencer5f016e22007-07-11 17:01:13 +00001467/// ParseSpecifierQualifierList
1468/// specifier-qualifier-list:
1469/// type-specifier specifier-qualifier-list[opt]
1470/// type-qualifier specifier-qualifier-list[opt]
1471/// [GNU] attributes specifier-qualifier-list[opt]
1472///
Richard Smith69730c12012-03-12 07:56:15 +00001473void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1474 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1476 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001477 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001478 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 // Validate declspec for type-name.
1481 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001482 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1483 Diag(Tok, diag::err_expected_type);
1484 DS.SetTypeSpecError();
1485 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1486 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001488 if (!DS.hasTypeSpecifier())
1489 DS.SetTypeSpecError();
1490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Reid Spencer5f016e22007-07-11 17:01:13 +00001492 // Issue diagnostic and remove storage class if present.
1493 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1494 if (DS.getStorageClassSpecLoc().isValid())
1495 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1496 else
1497 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1498 DS.ClearStorageClassSpecs();
1499 }
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 // Issue diagnostic and remove function specfier if present.
1502 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001503 if (DS.isInlineSpecified())
1504 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1505 if (DS.isVirtualSpecified())
1506 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1507 if (DS.isExplicitSpecified())
1508 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 DS.ClearFunctionSpecs();
1510 }
Richard Smith69730c12012-03-12 07:56:15 +00001511
1512 // Issue diagnostic and remove constexpr specfier if present.
1513 if (DS.isConstexprSpecified()) {
1514 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1515 DS.ClearConstexprSpec();
1516 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001517}
1518
Chris Lattnerc199ab32009-04-12 20:42:31 +00001519/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1520/// specified token is valid after the identifier in a declarator which
1521/// immediately follows the declspec. For example, these things are valid:
1522///
1523/// int x [ 4]; // direct-declarator
1524/// int x ( int y); // direct-declarator
1525/// int(int x ) // direct-declarator
1526/// int x ; // simple-declaration
1527/// int x = 17; // init-declarator-list
1528/// int x , y; // init-declarator-list
1529/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001530/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001531/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001532///
1533/// This is not, because 'x' does not immediately follow the declspec (though
1534/// ')' happens to be valid anyway).
1535/// int (x)
1536///
1537static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1538 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1539 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001540 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001541}
1542
Chris Lattnere40c2952009-04-14 21:34:55 +00001543
1544/// ParseImplicitInt - This method is called when we have an non-typename
1545/// identifier in a declspec (which normally terminates the decl spec) when
1546/// the declspec has no type specifier. In this case, the declspec is either
1547/// malformed or is "implicit int" (in K&R and C89).
1548///
1549/// This method handles diagnosing this prettily and returns false if the
1550/// declspec is done being processed. If it recovers and thinks there may be
1551/// other pieces of declspec after it, it returns true.
1552///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001553bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001554 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001555 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001556 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Chris Lattnere40c2952009-04-14 21:34:55 +00001558 SourceLocation Loc = Tok.getLocation();
1559 // If we see an identifier that is not a type name, we normally would
1560 // parse it as the identifer being declared. However, when a typename
1561 // is typo'd or the definition is not included, this will incorrectly
1562 // parse the typename as the identifier name and fall over misparsing
1563 // later parts of the diagnostic.
1564 //
1565 // As such, we try to do some look-ahead in cases where this would
1566 // otherwise be an "implicit-int" case to see if this is invalid. For
1567 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1568 // an identifier with implicit int, we'd get a parse error because the
1569 // next token is obviously invalid for a type. Parse these as a case
1570 // with an invalid type specifier.
1571 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Chris Lattnere40c2952009-04-14 21:34:55 +00001573 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001574 // error, do lookahead to try to do better recovery. This never applies within
1575 // a type specifier.
1576 // FIXME: Don't bail out here in languages with no implicit int (like
1577 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1578 // type or typo than a use of implicit int.
1579 if (DSC != DSC_type_specifier &&
1580 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001581 // If this token is valid for implicit int, e.g. "static x = 4", then
1582 // we just avoid eating the identifier, so it will be parsed as the
1583 // identifier in the declarator.
1584 return false;
1585 }
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Chris Lattnere40c2952009-04-14 21:34:55 +00001587 // Otherwise, if we don't consume this token, we are going to emit an
1588 // error anyway. Try to recover from various common problems. Check
1589 // to see if this was a reference to a tag name without a tag specified.
1590 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001591 //
1592 // C++ doesn't need this, and isTagName doesn't take SS.
1593 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001594 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001595 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001598 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001599 case DeclSpec::TST_enum:
1600 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1601 case DeclSpec::TST_union:
1602 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1603 case DeclSpec::TST_struct:
1604 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1605 case DeclSpec::TST_class:
1606 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Chris Lattnerf4382f52009-04-14 22:17:06 +00001609 if (TagName) {
1610 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001611 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001612 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Chris Lattnerf4382f52009-04-14 22:17:06 +00001614 // Parse this as a tag as if the missing tag were present.
1615 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001616 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001617 else
Richard Smith69730c12012-03-12 07:56:15 +00001618 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1619 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001620 return true;
1621 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001622 }
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Douglas Gregora786fdb2009-10-13 23:27:22 +00001624 // This is almost certainly an invalid type name. Let the action emit a
1625 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001626 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001627 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001628 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001629 // The action emitted a diagnostic, so we don't have to.
1630 if (T) {
1631 // The action has suggested that the type T could be used. Set that as
1632 // the type in the declaration specifiers, consume the would-be type
1633 // name token, and we're done.
1634 const char *PrevSpec;
1635 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001636 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001637 DS.SetRangeEnd(Tok.getLocation());
1638 ConsumeToken();
1639
1640 // There may be other declaration specifiers after this.
1641 return true;
1642 }
1643
1644 // Fall through; the action had no suggestion for us.
1645 } else {
1646 // The action did not emit a diagnostic, so emit one now.
1647 SourceRange R;
1648 if (SS) R = SS->getRange();
1649 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Douglas Gregora786fdb2009-10-13 23:27:22 +00001652 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001653 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001654 DS.SetRangeEnd(Tok.getLocation());
1655 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Chris Lattnere40c2952009-04-14 21:34:55 +00001657 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1658 // avoid rippling error messages on subsequent uses of the same type,
1659 // could be useful if #include was forgotten.
1660 return false;
1661}
1662
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001663/// \brief Determine the declaration specifier context from the declarator
1664/// context.
1665///
1666/// \param Context the declarator context, which is one of the
1667/// Declarator::TheContext enumerator values.
1668Parser::DeclSpecContext
1669Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1670 if (Context == Declarator::MemberContext)
1671 return DSC_class;
1672 if (Context == Declarator::FileContext)
1673 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001674 if (Context == Declarator::TrailingReturnContext)
1675 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001676 return DSC_normal;
1677}
1678
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001679/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1680///
1681/// FIXME: Simply returns an alignof() expression if the argument is a
1682/// type. Ideally, the type should be propagated directly into Sema.
1683///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001684/// [C11] type-id
1685/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001686/// [C++0x] type-id ...[opt]
1687/// [C++0x] assignment-expression ...[opt]
1688ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1689 SourceLocation &EllipsisLoc) {
1690 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001691 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001692 SourceLocation TypeLoc = Tok.getLocation();
1693 ParsedType Ty = ParseTypeName().get();
1694 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001695 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1696 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001697 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001698 ER = ParseConstantExpression();
1699
David Blaikie4e4d0842012-03-11 07:00:24 +00001700 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001701 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001702
1703 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001704}
1705
1706/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1707/// attribute to Attrs.
1708///
1709/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001710/// [C11] '_Alignas' '(' type-id ')'
1711/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001712/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1713/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001714void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1715 SourceLocation *endLoc) {
1716 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1717 "Not an alignment-specifier!");
1718
1719 SourceLocation KWLoc = Tok.getLocation();
1720 ConsumeToken();
1721
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001722 BalancedDelimiterTracker T(*this, tok::l_paren);
1723 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001724 return;
1725
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001726 SourceLocation EllipsisLoc;
1727 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001728 if (ArgExpr.isInvalid()) {
1729 SkipUntil(tok::r_paren);
1730 return;
1731 }
1732
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001733 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001734 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001735 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001736
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001737 // FIXME: Handle pack-expansions here.
1738 if (EllipsisLoc.isValid()) {
1739 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1740 return;
1741 }
1742
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001743 ExprVector ArgExprs(Actions);
1744 ArgExprs.push_back(ArgExpr.release());
1745 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001746 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001747}
1748
Reid Spencer5f016e22007-07-11 17:01:13 +00001749/// ParseDeclarationSpecifiers
1750/// declaration-specifiers: [C99 6.7]
1751/// storage-class-specifier declaration-specifiers[opt]
1752/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001753/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001754/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001755/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001756/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001757///
1758/// storage-class-specifier: [C99 6.7.1]
1759/// 'typedef'
1760/// 'extern'
1761/// 'static'
1762/// 'auto'
1763/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001764/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001765/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001766/// function-specifier: [C99 6.7.4]
1767/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001768/// [C++] 'virtual'
1769/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001770/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001771/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001772/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001773
Reid Spencer5f016e22007-07-11 17:01:13 +00001774///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001775void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001776 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001777 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001778 DeclSpecContext DSContext,
1779 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001780 if (DS.getSourceRange().isInvalid()) {
1781 DS.SetRangeStart(Tok.getLocation());
1782 DS.SetRangeEnd(Tok.getLocation());
1783 }
1784
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001785 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001787 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001789 unsigned DiagID = 0;
1790
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001792
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001794 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001795 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001796 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1797 MaybeParseCXX0XAttributes(DS.getAttributes());
1798
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 // If this is not a declaration specifier token, we're done reading decl
1800 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001801 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001804 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001805 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001806 if (DS.hasTypeSpecifier()) {
1807 bool AllowNonIdentifiers
1808 = (getCurScope()->getFlags() & (Scope::ControlScope |
1809 Scope::BlockScope |
1810 Scope::TemplateParamScope |
1811 Scope::FunctionPrototypeScope |
1812 Scope::AtCatchScope)) == 0;
1813 bool AllowNestedNameSpecifiers
1814 = DSContext == DSC_top_level ||
1815 (DSContext == DSC_class && DS.isFriendSpecified());
1816
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001817 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1818 AllowNonIdentifiers,
1819 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001820 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001821 }
1822
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001823 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1824 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1825 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001826 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1827 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001828 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001829 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001830 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001831 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001832
1833 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001834 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001835 }
1836
Chris Lattner5e02c472009-01-05 00:07:25 +00001837 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001838 // C++ scope specifier. Annotate and loop, or bail out on error.
1839 if (TryAnnotateCXXScopeToken(true)) {
1840 if (!DS.hasTypeSpecifier())
1841 DS.SetTypeSpecError();
1842 goto DoneWithDeclSpec;
1843 }
John McCall2e0a7152010-03-01 18:20:46 +00001844 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1845 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001846 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001847
1848 case tok::annot_cxxscope: {
1849 if (DS.hasTypeSpecifier())
1850 goto DoneWithDeclSpec;
1851
John McCallaa87d332009-12-12 11:40:51 +00001852 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001853 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1854 Tok.getAnnotationRange(),
1855 SS);
John McCallaa87d332009-12-12 11:40:51 +00001856
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001857 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001858 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001859 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001860 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001861 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001862 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001863
1864 // C++ [class.qual]p2:
1865 // In a lookup in which the constructor is an acceptable lookup
1866 // result and the nested-name-specifier nominates a class C:
1867 //
1868 // - if the name specified after the
1869 // nested-name-specifier, when looked up in C, is the
1870 // injected-class-name of C (Clause 9), or
1871 //
1872 // - if the name specified after the nested-name-specifier
1873 // is the same as the identifier or the
1874 // simple-template-id's template-name in the last
1875 // component of the nested-name-specifier,
1876 //
1877 // the name is instead considered to name the constructor of
1878 // class C.
1879 //
1880 // Thus, if the template-name is actually the constructor
1881 // name, then the code is ill-formed; this interpretation is
1882 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001883 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001884 if ((DSContext == DSC_top_level ||
1885 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1886 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001888 if (isConstructorDeclarator()) {
1889 // The user meant this to be an out-of-line constructor
1890 // definition, but template arguments are not allowed
1891 // there. Just allow this as a constructor; we'll
1892 // complain about it later.
1893 goto DoneWithDeclSpec;
1894 }
1895
1896 // The user meant this to name a type, but it actually names
1897 // a constructor with some extraneous template
1898 // arguments. Complain, then parse it as a type as the user
1899 // intended.
1900 Diag(TemplateId->TemplateNameLoc,
1901 diag::err_out_of_line_template_id_names_constructor)
1902 << TemplateId->Name;
1903 }
1904
John McCallaa87d332009-12-12 11:40:51 +00001905 DS.getTypeSpecScope() = SS;
1906 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001907 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001908 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001909 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001910 continue;
1911 }
1912
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001913 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001914 DS.getTypeSpecScope() = SS;
1915 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001916 if (Tok.getAnnotationValue()) {
1917 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1919 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001920 PrevSpec, DiagID, T);
1921 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001922 else
1923 DS.SetTypeSpecError();
1924 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1925 ConsumeToken(); // The typename
1926 }
1927
Douglas Gregor9135c722009-03-25 15:40:00 +00001928 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001929 goto DoneWithDeclSpec;
1930
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001931 // If we're in a context where the identifier could be a class name,
1932 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001933 if ((DSContext == DSC_top_level ||
1934 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001935 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001936 &SS)) {
1937 if (isConstructorDeclarator())
1938 goto DoneWithDeclSpec;
1939
1940 // As noted in C++ [class.qual]p2 (cited above), when the name
1941 // of the class is qualified in a context where it could name
1942 // a constructor, its a constructor name. However, we've
1943 // looked at the declarator, and the user probably meant this
1944 // to be a type. Complain that it isn't supposed to be treated
1945 // as a type, then proceed to parse it as a type.
1946 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1947 << Next.getIdentifierInfo();
1948 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001949
John McCallb3d87482010-08-24 05:47:05 +00001950 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1951 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001952 getCurScope(), &SS,
1953 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001954 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001955 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001956
Chris Lattnerf4382f52009-04-14 22:17:06 +00001957 // If the referenced identifier is not a type, then this declspec is
1958 // erroneous: We already checked about that it has no type specifier, and
1959 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001960 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001961 if (TypeRep == 0) {
1962 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001963 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001964 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
John McCallaa87d332009-12-12 11:40:51 +00001967 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001968 ConsumeToken(); // The C++ scope.
1969
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001971 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001972 if (isInvalid)
1973 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001975 DS.SetRangeEnd(Tok.getLocation());
1976 ConsumeToken(); // The typename.
1977
1978 continue;
1979 }
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Chris Lattner80d0c892009-01-21 19:48:37 +00001981 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001982 if (Tok.getAnnotationValue()) {
1983 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001985 DiagID, T);
1986 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001987 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001988
1989 if (isInvalid)
1990 break;
1991
Chris Lattner80d0c892009-01-21 19:48:37 +00001992 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1993 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Chris Lattner80d0c892009-01-21 19:48:37 +00001995 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1996 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001997 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001998 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001999 ParseObjCProtocolQualifiers(DS);
2000
Chris Lattner80d0c892009-01-21 19:48:37 +00002001 continue;
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Douglas Gregorbfad9152011-04-28 15:48:45 +00002004 case tok::kw___is_signed:
2005 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2006 // typically treats it as a trait. If we see __is_signed as it appears
2007 // in libstdc++, e.g.,
2008 //
2009 // static const bool __is_signed;
2010 //
2011 // then treat __is_signed as an identifier rather than as a keyword.
2012 if (DS.getTypeSpecType() == TST_bool &&
2013 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2014 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2015 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2016 Tok.setKind(tok::identifier);
2017 }
2018
2019 // We're done with the declaration-specifiers.
2020 goto DoneWithDeclSpec;
2021
Chris Lattner3bd934a2008-07-26 01:18:38 +00002022 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002023 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002024 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002025 // In C++, check to see if this is a scope specifier like foo::bar::, if
2026 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002027 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002028 if (TryAnnotateCXXScopeToken(true)) {
2029 if (!DS.hasTypeSpecifier())
2030 DS.SetTypeSpecError();
2031 goto DoneWithDeclSpec;
2032 }
2033 if (!Tok.is(tok::identifier))
2034 continue;
2035 }
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Chris Lattner3bd934a2008-07-26 01:18:38 +00002037 // This identifier can only be a typedef name if we haven't already seen
2038 // a type-specifier. Without this check we misparse:
2039 // typedef int X; struct Y { short X; }; as 'short int'.
2040 if (DS.hasTypeSpecifier())
2041 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002042
John Thompson82287d12010-02-05 00:12:22 +00002043 // Check for need to substitute AltiVec keyword tokens.
2044 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2045 break;
2046
John McCallb3d87482010-08-24 05:47:05 +00002047 ParsedType TypeRep =
2048 Actions.getTypeName(*Tok.getIdentifierInfo(),
2049 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002050
Chris Lattnerc199ab32009-04-12 20:42:31 +00002051 // If this is not a typedef name, don't parse it as part of the declspec,
2052 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002053 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002054 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002055 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002056 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002057
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002058 // If we're in a context where the identifier could be a class name,
2059 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002060 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002061 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002062 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002063 goto DoneWithDeclSpec;
2064
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002065 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002066 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002067 if (isInvalid)
2068 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Chris Lattner3bd934a2008-07-26 01:18:38 +00002070 DS.SetRangeEnd(Tok.getLocation());
2071 ConsumeToken(); // The identifier
2072
2073 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2074 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002075 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002076 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002077 ParseObjCProtocolQualifiers(DS);
2078
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002079 // Need to support trailing type qualifiers (e.g. "id<p> const").
2080 // If a type specifier follows, it will be diagnosed elsewhere.
2081 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002082 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002083
2084 // type-name
2085 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002086 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002087 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002088 // This template-id does not refer to a type name, so we're
2089 // done with the type-specifiers.
2090 goto DoneWithDeclSpec;
2091 }
2092
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002093 // If we're in a context where the template-id could be a
2094 // constructor name or specialization, check whether this is a
2095 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002096 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002097 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002098 isConstructorDeclarator())
2099 goto DoneWithDeclSpec;
2100
Douglas Gregor39a8de12009-02-25 19:37:18 +00002101 // Turn the template-id annotation token into a type annotation
2102 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002103 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002104 continue;
2105 }
2106
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 // GNU attributes support.
2108 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002109 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002111
2112 // Microsoft declspec support.
2113 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002114 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002115 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Steve Naroff239f0732008-12-25 14:16:32 +00002117 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002118 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002119 // FIXME: Add handling here!
2120 break;
2121
2122 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002123 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002124 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002125 case tok::kw___cdecl:
2126 case tok::kw___stdcall:
2127 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002128 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002129 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002130 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002131 continue;
2132
Dawn Perchik52fc3142010-09-03 01:29:35 +00002133 // Borland single token adornments.
2134 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002135 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002136 continue;
2137
Peter Collingbournef315fa82011-02-14 01:42:53 +00002138 // OpenCL single token adornments.
2139 case tok::kw___kernel:
2140 ParseOpenCLAttributes(DS.getAttributes());
2141 continue;
2142
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 // storage-class-specifier
2144 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002145 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2146 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 break;
2148 case tok::kw_extern:
2149 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002150 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002151 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2152 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002154 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002155 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2156 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002157 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002158 case tok::kw_static:
2159 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002160 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002161 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2162 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002163 break;
2164 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002165 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002166 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002167 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2168 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002169 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002170 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002171 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002172 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002173 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2174 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002175 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002176 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2177 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 break;
2179 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002180 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2181 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002183 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002184 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2185 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002186 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002187 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002189 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 // function-specifier
2192 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002193 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002194 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002195 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002196 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002197 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002198 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002199 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002200 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002201
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002202 // alignment-specifier
2203 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002204 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002205 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002206 ParseAlignmentSpecifier(DS.getAttributes());
2207 continue;
2208
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002209 // friend
2210 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002211 if (DSContext == DSC_class)
2212 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2213 else {
2214 PrevSpec = ""; // not actually used by the diagnostic
2215 DiagID = diag::err_friend_invalid_in_context;
2216 isInvalid = true;
2217 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002218 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Douglas Gregor8d267c52011-09-09 02:06:17 +00002220 // Modules
2221 case tok::kw___module_private__:
2222 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2223 break;
2224
Sebastian Redl2ac67232009-11-05 15:47:02 +00002225 // constexpr
2226 case tok::kw_constexpr:
2227 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2228 break;
2229
Chris Lattner80d0c892009-01-21 19:48:37 +00002230 // type-specifier
2231 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002232 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2233 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002234 break;
2235 case tok::kw_long:
2236 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2238 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002239 else
John McCallfec54012009-08-03 20:12:06 +00002240 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2241 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002242 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002243 case tok::kw___int64:
2244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2245 DiagID);
2246 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002247 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002248 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2249 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002250 break;
2251 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002252 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2253 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002254 break;
2255 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002256 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2257 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002258 break;
2259 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002260 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2261 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002262 break;
2263 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2265 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002266 break;
2267 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2269 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002270 break;
2271 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2273 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002274 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002275 case tok::kw___int128:
2276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2277 DiagID);
2278 break;
2279 case tok::kw_half:
2280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2281 DiagID);
2282 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002283 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2285 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002286 break;
2287 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2289 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002290 break;
2291 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002292 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2293 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002294 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002295 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002296 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2297 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002298 break;
2299 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002300 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2301 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002302 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002303 case tok::kw_bool:
2304 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002305 if (Tok.is(tok::kw_bool) &&
2306 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2307 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2308 PrevSpec = ""; // Not used by the diagnostic.
2309 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002310 // For better error recovery.
2311 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002312 isInvalid = true;
2313 } else {
2314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2315 DiagID);
2316 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002317 break;
2318 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2320 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002321 break;
2322 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2324 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002325 break;
2326 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002327 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2328 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002329 break;
John Thompson82287d12010-02-05 00:12:22 +00002330 case tok::kw___vector:
2331 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2332 break;
2333 case tok::kw___pixel:
2334 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2335 break;
John McCalla5fc4722011-04-09 22:50:59 +00002336 case tok::kw___unknown_anytype:
2337 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2338 PrevSpec, DiagID);
2339 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002340
2341 // class-specifier:
2342 case tok::kw_class:
2343 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002344 case tok::kw_union: {
2345 tok::TokenKind Kind = Tok.getKind();
2346 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002347 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2348 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002349 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002350 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002351
2352 // enum-specifier:
2353 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002354 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002355 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002356 continue;
2357
2358 // cv-qualifier:
2359 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002360 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002361 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002362 break;
2363 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002364 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002365 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002366 break;
2367 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002368 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002369 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002370 break;
2371
Douglas Gregord57959a2009-03-27 23:10:48 +00002372 // C++ typename-specifier:
2373 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002374 if (TryAnnotateTypeOrScopeToken()) {
2375 DS.SetTypeSpecError();
2376 goto DoneWithDeclSpec;
2377 }
2378 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002379 continue;
2380 break;
2381
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 // GNU typeof support.
2383 case tok::kw_typeof:
2384 ParseTypeofSpecifier(DS);
2385 continue;
2386
David Blaikie42d6d0c2011-12-04 05:04:18 +00002387 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002388 ParseDecltypeSpecifier(DS);
2389 continue;
2390
Sean Huntdb5d44b2011-05-19 05:37:45 +00002391 case tok::kw___underlying_type:
2392 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002393 continue;
2394
2395 case tok::kw__Atomic:
2396 ParseAtomicSpecifier(DS);
2397 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002398
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002399 // OpenCL qualifiers:
2400 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002401 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002402 goto DoneWithDeclSpec;
2403 case tok::kw___private:
2404 case tok::kw___global:
2405 case tok::kw___local:
2406 case tok::kw___constant:
2407 case tok::kw___read_only:
2408 case tok::kw___write_only:
2409 case tok::kw___read_write:
2410 ParseOpenCLQualifiers(DS);
2411 break;
2412
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002413 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002414 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002415 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2416 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002417 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002418 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Douglas Gregor46f936e2010-11-19 17:10:50 +00002420 if (!ParseObjCProtocolQualifiers(DS))
2421 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2422 << FixItHint::CreateInsertion(Loc, "id")
2423 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002424
2425 // Need to support trailing type qualifiers (e.g. "id<p> const").
2426 // If a type specifier follows, it will be diagnosed elsewhere.
2427 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 }
John McCallfec54012009-08-03 20:12:06 +00002429 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002430 if (isInvalid) {
2431 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002432 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002433
2434 if (DiagID == diag::ext_duplicate_declspec)
2435 Diag(Tok, DiagID)
2436 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2437 else
2438 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002440
Chris Lattner81c018d2008-03-13 06:29:04 +00002441 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002442 if (DiagID != diag::err_bool_redeclaration)
2443 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 }
2445}
Douglas Gregoradcac882008-12-01 23:54:00 +00002446
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002447/// ParseStructDeclaration - Parse a struct declaration without the terminating
2448/// semicolon.
2449///
Reid Spencer5f016e22007-07-11 17:01:13 +00002450/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002451/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002452/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002453/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002454/// struct-declarator-list:
2455/// struct-declarator
2456/// struct-declarator-list ',' struct-declarator
2457/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2458/// struct-declarator:
2459/// declarator
2460/// [GNU] declarator attributes[opt]
2461/// declarator[opt] ':' constant-expression
2462/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2463///
Chris Lattnere1359422008-04-10 06:46:29 +00002464void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002465ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002466
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002467 if (Tok.is(tok::kw___extension__)) {
2468 // __extension__ silences extension warnings in the subexpression.
2469 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002470 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002471 return ParseStructDeclaration(DS, Fields);
2472 }
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Steve Naroff28a7ca82007-08-20 22:28:22 +00002474 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002475 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002477 // If there are no declarators, this is a free-standing declaration
2478 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002479 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002480 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002481 return;
2482 }
2483
2484 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002485 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002486 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002487 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002488 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002489 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002490 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002491
2492 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002493 if (!FirstDeclarator)
2494 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Steve Naroff28a7ca82007-08-20 22:28:22 +00002496 /// struct-declarator: declarator
2497 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002498 if (Tok.isNot(tok::colon)) {
2499 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2500 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002501 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002502 }
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Chris Lattner04d66662007-10-09 17:33:22 +00002504 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002505 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002506 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002507 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002508 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002509 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002510 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002511 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002512
Steve Naroff28a7ca82007-08-20 22:28:22 +00002513 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002514 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002515
John McCallbdd563e2009-11-03 02:38:08 +00002516 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002517 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002518 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002519
Steve Naroff28a7ca82007-08-20 22:28:22 +00002520 // If we don't have a comma, it is either the end of the list (a ';')
2521 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002522 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002523 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002524
Steve Naroff28a7ca82007-08-20 22:28:22 +00002525 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002526 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002527
John McCallbdd563e2009-11-03 02:38:08 +00002528 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002529 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002530}
2531
2532/// ParseStructUnionBody
2533/// struct-contents:
2534/// struct-declaration-list
2535/// [EXT] empty
2536/// [GNU] "struct-declaration-list" without terminatoring ';'
2537/// struct-declaration-list:
2538/// struct-declaration
2539/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002540/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002541///
Reid Spencer5f016e22007-07-11 17:01:13 +00002542void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002543 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002544 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2545 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002547 BalancedDelimiterTracker T(*this, tok::l_brace);
2548 if (T.consumeOpen())
2549 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002551 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002552 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002553
Reid Spencer5f016e22007-07-11 17:01:13 +00002554 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2555 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002556 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002557 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2558 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2559 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002560
Chris Lattner5f9e2722011-07-23 10:55:15 +00002561 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002562
Reid Spencer5f016e22007-07-11 17:01:13 +00002563 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002564 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002565 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002568 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002569 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002570 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002571 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 ConsumeToken();
2573 continue;
2574 }
Chris Lattnere1359422008-04-10 06:46:29 +00002575
2576 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002577 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002578
John McCallbdd563e2009-11-03 02:38:08 +00002579 if (!Tok.is(tok::at)) {
2580 struct CFieldCallback : FieldCallback {
2581 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002582 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002583 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002584
John McCalld226f652010-08-21 09:40:31 +00002585 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002586 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002587 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2588
John McCalld226f652010-08-21 09:40:31 +00002589 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002590 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002591 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002592 FD.D.getDeclSpec().getSourceRange().getBegin(),
2593 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002594 FieldDecls.push_back(Field);
2595 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002596 }
John McCallbdd563e2009-11-03 02:38:08 +00002597 } Callback(*this, TagDecl, FieldDecls);
2598
2599 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002600 } else { // Handle @defs
2601 ConsumeToken();
2602 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2603 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002604 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002605 continue;
2606 }
2607 ConsumeToken();
2608 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2609 if (!Tok.is(tok::identifier)) {
2610 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002611 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002612 continue;
2613 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002614 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002615 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002616 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002617 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2618 ConsumeToken();
2619 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002620 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002621
Chris Lattner04d66662007-10-09 17:33:22 +00002622 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002624 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002625 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 break;
2627 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002628 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2629 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002631 // If we stopped at a ';', eat it.
2632 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002633 }
2634 }
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002636 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002637
John McCall0b7e6782011-03-24 11:26:52 +00002638 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002639 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002640 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002641
Douglas Gregor23c94db2010-07-02 17:43:08 +00002642 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002643 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002644 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002645 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002646 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002647 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2648 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002649}
2650
Reid Spencer5f016e22007-07-11 17:01:13 +00002651/// ParseEnumSpecifier
2652/// enum-specifier: [C99 6.7.2.2]
2653/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002654///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002655/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2656/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002657/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2658/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002659/// 'enum' identifier
2660/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002661///
Richard Smith1af83c42012-03-23 03:33:32 +00002662/// [C++11] enum-head '{' enumerator-list[opt] '}'
2663/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002664///
Richard Smith1af83c42012-03-23 03:33:32 +00002665/// enum-head: [C++11]
2666/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2667/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2668/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002669///
Richard Smith1af83c42012-03-23 03:33:32 +00002670/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002671/// 'enum'
2672/// 'enum' 'class'
2673/// 'enum' 'struct'
2674///
Richard Smith1af83c42012-03-23 03:33:32 +00002675/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002676/// ':' type-specifier-seq
2677///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002678/// [C++] elaborated-type-specifier:
2679/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2680///
Chris Lattner4c97d762009-04-12 21:49:30 +00002681void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002682 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002683 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002684 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002685 if (Tok.is(tok::code_completion)) {
2686 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002687 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002688 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002689 }
John McCall57c13002011-07-06 05:58:41 +00002690
Richard Smithbdad7a22012-01-10 01:33:14 +00002691 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002692 bool IsScopedUsingClassTag = false;
2693
David Blaikie4e4d0842012-03-11 07:00:24 +00002694 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002695 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002696 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002697 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002698 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002699 }
Richard Smith1af83c42012-03-23 03:33:32 +00002700
2701 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2702 // used to specify explicit instantiations. We extend this to also cover
2703 // explicit specializations.
2704 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2705 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2706 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2707
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002708 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002709 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002710 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002711
Aaron Ballman6454a022012-03-01 04:09:28 +00002712 // If declspecs exist after tag, parse them.
2713 while (Tok.is(tok::kw___declspec))
2714 ParseMicrosoftDeclSpec(attrs);
2715
Richard Smith7796eb52012-03-12 08:56:40 +00002716 // Enum definitions should not be parsed in a trailing-return-type.
2717 bool AllowDeclaration = DSC != DSC_trailing;
2718
2719 bool AllowFixedUnderlyingType = AllowDeclaration &&
2720 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2721 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002722
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002723 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002724 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002725 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2726 // if a fixed underlying type is allowed.
2727 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2728
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002729 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2730 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002731 return;
2732
2733 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002734 Diag(Tok, diag::err_expected_ident);
2735 if (Tok.isNot(tok::l_brace)) {
2736 // Has no name and is not a definition.
2737 // Skip the rest of this declarator, up until the comma or semicolon.
2738 SkipUntil(tok::comma, true);
2739 return;
2740 }
2741 }
2742 }
Mike Stump1eb44332009-09-09 15:08:12 +00002743
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002744 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002745 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002746 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002747 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002748
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002749 // Skip the rest of this declarator, up until the comma or semicolon.
2750 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002751 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002752 }
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002754 // If an identifier is present, consume and remember it.
2755 IdentifierInfo *Name = 0;
2756 SourceLocation NameLoc;
2757 if (Tok.is(tok::identifier)) {
2758 Name = Tok.getIdentifierInfo();
2759 NameLoc = ConsumeToken();
2760 }
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Richard Smithbdad7a22012-01-10 01:33:14 +00002762 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002763 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2764 // declaration of a scoped enumeration.
2765 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002766 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002767 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002768 }
2769
Richard Smith1af83c42012-03-23 03:33:32 +00002770 // Stop suppressing access control now we've parsed the enum name.
2771 SuppressAccess.done();
2772
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002773 TypeResult BaseType;
2774
Douglas Gregora61b3e72010-12-01 17:42:47 +00002775 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002776 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002777 bool PossibleBitfield = false;
2778 if (getCurScope()->getFlags() & Scope::ClassScope) {
2779 // If we're in class scope, this can either be an enum declaration with
2780 // an underlying type, or a declaration of a bitfield member. We try to
2781 // use a simple disambiguation scheme first to catch the common cases
2782 // (integer literal, sizeof); if it's still ambiguous, we then consider
2783 // anything that's a simple-type-specifier followed by '(' as an
2784 // expression. This suffices because function types are not valid
2785 // underlying types anyway.
2786 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2787 // If the next token starts an expression, we know we're parsing a
2788 // bit-field. This is the common case.
2789 if (TPR == TPResult::True())
2790 PossibleBitfield = true;
2791 // If the next token starts a type-specifier-seq, it may be either a
2792 // a fixed underlying type or the start of a function-style cast in C++;
2793 // lookahead one more token to see if it's obvious that we have a
2794 // fixed underlying type.
2795 else if (TPR == TPResult::False() &&
2796 GetLookAheadToken(2).getKind() == tok::semi) {
2797 // Consume the ':'.
2798 ConsumeToken();
2799 } else {
2800 // We have the start of a type-specifier-seq, so we have to perform
2801 // tentative parsing to determine whether we have an expression or a
2802 // type.
2803 TentativeParsingAction TPA(*this);
2804
2805 // Consume the ':'.
2806 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002807
2808 // If we see a type specifier followed by an open-brace, we have an
2809 // ambiguity between an underlying type and a C++11 braced
2810 // function-style cast. Resolve this by always treating it as an
2811 // underlying type.
2812 // FIXME: The standard is not entirely clear on how to disambiguate in
2813 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002814 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002815 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002816 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002817 // We'll parse this as a bitfield later.
2818 PossibleBitfield = true;
2819 TPA.Revert();
2820 } else {
2821 // We have a type-specifier-seq.
2822 TPA.Commit();
2823 }
2824 }
2825 } else {
2826 // Consume the ':'.
2827 ConsumeToken();
2828 }
2829
2830 if (!PossibleBitfield) {
2831 SourceRange Range;
2832 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002833
David Blaikie4e4d0842012-03-11 07:00:24 +00002834 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002835 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2836 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002837 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002838 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002839 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002840 }
2841
Richard Smithbdad7a22012-01-10 01:33:14 +00002842 // There are four options here. If we have 'friend enum foo;' then this is a
2843 // friend declaration, and cannot have an accompanying definition. If we have
2844 // 'enum foo;', then this is a forward declaration. If we have
2845 // 'enum foo {...' then this is a definition. Otherwise we have something
2846 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002847 //
2848 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2849 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2850 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2851 //
John McCallf312b1e2010-08-26 23:41:50 +00002852 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002853 if (DS.isFriendSpecified())
2854 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002855 else if (!AllowDeclaration)
2856 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002857 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002858 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002859 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002860 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002861 else
John McCallf312b1e2010-08-26 23:41:50 +00002862 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002863
2864 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002865 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002866 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002867 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2868 // Skip the rest of this declarator, up until the comma or semicolon.
2869 Diag(Tok, diag::err_enum_template);
2870 SkipUntil(tok::comma, true);
2871 return;
2872 }
2873
2874 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2875 // Enumerations can't be explicitly instantiated.
2876 DS.SetTypeSpecError();
2877 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2878 return;
2879 }
2880
2881 assert(TemplateInfo.TemplateParams && "no template parameters");
2882 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2883 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002884 }
Richard Smith1af83c42012-03-23 03:33:32 +00002885
Douglas Gregorb9075602011-02-22 02:55:24 +00002886 if (!Name && TUK != Sema::TUK_Definition) {
2887 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002888
Douglas Gregorb9075602011-02-22 02:55:24 +00002889 // Skip the rest of this declarator, up until the comma or semicolon.
2890 SkipUntil(tok::comma, true);
2891 return;
2892 }
Richard Smith1af83c42012-03-23 03:33:32 +00002893
Douglas Gregor402abb52009-05-28 23:31:59 +00002894 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002895 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002896 const char *PrevSpec = 0;
2897 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002898 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002899 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002900 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002901 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002902 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002903
Douglas Gregor48c89f42010-04-24 16:38:41 +00002904 if (IsDependent) {
2905 // This enum has a dependent nested-name-specifier. Handle it as a
2906 // dependent tag.
2907 if (!Name) {
2908 DS.SetTypeSpecError();
2909 Diag(Tok, diag::err_expected_type_name_after_typename);
2910 return;
2911 }
2912
Douglas Gregor23c94db2010-07-02 17:43:08 +00002913 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002914 TUK, SS, Name, StartLoc,
2915 NameLoc);
2916 if (Type.isInvalid()) {
2917 DS.SetTypeSpecError();
2918 return;
2919 }
2920
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002921 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2922 NameLoc.isValid() ? NameLoc : StartLoc,
2923 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002924 Diag(StartLoc, DiagID) << PrevSpec;
2925
2926 return;
2927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
John McCalld226f652010-08-21 09:40:31 +00002929 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002930 // The action failed to produce an enumeration tag. If this is a
2931 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002932 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002933 ConsumeBrace();
2934 SkipUntil(tok::r_brace);
2935 }
2936
2937 DS.SetTypeSpecError();
2938 return;
2939 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002940
Richard Smith7796eb52012-03-12 08:56:40 +00002941 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002942 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002943 Diag(Tok, diag::err_friend_decl_defines_type)
2944 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00002945 ConsumeBrace();
2946 SkipUntil(tok::r_brace);
2947 } else {
2948 ParseEnumBody(StartLoc, TagDecl);
2949 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002950 }
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002952 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2953 NameLoc.isValid() ? NameLoc : StartLoc,
2954 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002955 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002956}
2957
2958/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2959/// enumerator-list:
2960/// enumerator
2961/// enumerator-list ',' enumerator
2962/// enumerator:
2963/// enumeration-constant
2964/// enumeration-constant '=' constant-expression
2965/// enumeration-constant:
2966/// identifier
2967///
John McCalld226f652010-08-21 09:40:31 +00002968void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002969 // Enter the scope of the enum body and start the definition.
2970 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002971 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002972
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002973 BalancedDelimiterTracker T(*this, tok::l_brace);
2974 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002975
Chris Lattner7946dd32007-08-27 17:24:30 +00002976 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002977 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002978 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002979
Chris Lattner5f9e2722011-07-23 10:55:15 +00002980 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002981
John McCalld226f652010-08-21 09:40:31 +00002982 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002983
Reid Spencer5f016e22007-07-11 17:01:13 +00002984 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002985 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002986 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2987 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002988
John McCall5b629aa2010-10-22 23:36:17 +00002989 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002990 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002991 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002992
Reid Spencer5f016e22007-07-11 17:01:13 +00002993 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002994 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002995 ParsingDeclRAIIObject PD(*this);
2996
Chris Lattner04d66662007-10-09 17:33:22 +00002997 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002999 AssignedVal = ParseConstantExpression();
3000 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003001 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003002 }
Mike Stump1eb44332009-09-09 15:08:12 +00003003
Reid Spencer5f016e22007-07-11 17:01:13 +00003004 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003005 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3006 LastEnumConstDecl,
3007 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003008 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003009 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003010 PD.complete(EnumConstDecl);
3011
Reid Spencer5f016e22007-07-11 17:01:13 +00003012 EnumConstantDecls.push_back(EnumConstDecl);
3013 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Douglas Gregor751f6922010-09-07 14:51:08 +00003015 if (Tok.is(tok::identifier)) {
3016 // We're missing a comma between enumerators.
3017 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3018 Diag(Loc, diag::err_enumerator_list_missing_comma)
3019 << FixItHint::CreateInsertion(Loc, ", ");
3020 continue;
3021 }
3022
Chris Lattner04d66662007-10-09 17:33:22 +00003023 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003024 break;
3025 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Richard Smith7fe62082011-10-15 05:09:34 +00003027 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003028 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003029 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003030 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003031 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003032 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003033 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3034 << FixItHint::CreateRemoval(CommaLoc);
3035 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 }
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Reid Spencer5f016e22007-07-11 17:01:13 +00003038 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003039 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003040
Reid Spencer5f016e22007-07-11 17:01:13 +00003041 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003042 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003043 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003044
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003045 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3046 EnumDecl, EnumConstantDecls.data(),
3047 EnumConstantDecls.size(), getCurScope(),
3048 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Douglas Gregor72de6672009-01-08 20:45:30 +00003050 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003051 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3052 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003053}
3054
3055/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003056/// start of a type-qualifier-list.
3057bool Parser::isTypeQualifier() const {
3058 switch (Tok.getKind()) {
3059 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003060
3061 // type-qualifier only in OpenCL
3062 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003063 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003064
Steve Naroff5f8aa692008-02-11 23:15:56 +00003065 // type-qualifier
3066 case tok::kw_const:
3067 case tok::kw_volatile:
3068 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003069 case tok::kw___private:
3070 case tok::kw___local:
3071 case tok::kw___global:
3072 case tok::kw___constant:
3073 case tok::kw___read_only:
3074 case tok::kw___read_write:
3075 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003076 return true;
3077 }
3078}
3079
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003080/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3081/// is definitely a type-specifier. Return false if it isn't part of a type
3082/// specifier or if we're not sure.
3083bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3084 switch (Tok.getKind()) {
3085 default: return false;
3086 // type-specifiers
3087 case tok::kw_short:
3088 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003089 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003090 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003091 case tok::kw_signed:
3092 case tok::kw_unsigned:
3093 case tok::kw__Complex:
3094 case tok::kw__Imaginary:
3095 case tok::kw_void:
3096 case tok::kw_char:
3097 case tok::kw_wchar_t:
3098 case tok::kw_char16_t:
3099 case tok::kw_char32_t:
3100 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003101 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003102 case tok::kw_float:
3103 case tok::kw_double:
3104 case tok::kw_bool:
3105 case tok::kw__Bool:
3106 case tok::kw__Decimal32:
3107 case tok::kw__Decimal64:
3108 case tok::kw__Decimal128:
3109 case tok::kw___vector:
3110
3111 // struct-or-union-specifier (C99) or class-specifier (C++)
3112 case tok::kw_class:
3113 case tok::kw_struct:
3114 case tok::kw_union:
3115 // enum-specifier
3116 case tok::kw_enum:
3117
3118 // typedef-name
3119 case tok::annot_typename:
3120 return true;
3121 }
3122}
3123
Steve Naroff5f8aa692008-02-11 23:15:56 +00003124/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003125/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003126bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 switch (Tok.getKind()) {
3128 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Chris Lattner166a8fc2009-01-04 23:41:41 +00003130 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003131 if (TryAltiVecVectorToken())
3132 return true;
3133 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003134 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003135 // Annotate typenames and C++ scope specifiers. If we get one, just
3136 // recurse to handle whatever we get.
3137 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003138 return true;
3139 if (Tok.is(tok::identifier))
3140 return false;
3141 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003142
Chris Lattner166a8fc2009-01-04 23:41:41 +00003143 case tok::coloncolon: // ::foo::bar
3144 if (NextToken().is(tok::kw_new) || // ::new
3145 NextToken().is(tok::kw_delete)) // ::delete
3146 return false;
3147
Chris Lattner166a8fc2009-01-04 23:41:41 +00003148 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003149 return true;
3150 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 // GNU attributes support.
3153 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003154 // GNU typeof support.
3155 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 // type-specifiers
3158 case tok::kw_short:
3159 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003160 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003161 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003162 case tok::kw_signed:
3163 case tok::kw_unsigned:
3164 case tok::kw__Complex:
3165 case tok::kw__Imaginary:
3166 case tok::kw_void:
3167 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003168 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003169 case tok::kw_char16_t:
3170 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003171 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003172 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003173 case tok::kw_float:
3174 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003175 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003176 case tok::kw__Bool:
3177 case tok::kw__Decimal32:
3178 case tok::kw__Decimal64:
3179 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003180 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003181
Chris Lattner99dc9142008-04-13 18:59:07 +00003182 // struct-or-union-specifier (C99) or class-specifier (C++)
3183 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003184 case tok::kw_struct:
3185 case tok::kw_union:
3186 // enum-specifier
3187 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Reid Spencer5f016e22007-07-11 17:01:13 +00003189 // type-qualifier
3190 case tok::kw_const:
3191 case tok::kw_volatile:
3192 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003193
3194 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003195 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003196 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Chris Lattner7c186be2008-10-20 00:25:30 +00003198 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3199 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003200 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003201
Steve Naroff239f0732008-12-25 14:16:32 +00003202 case tok::kw___cdecl:
3203 case tok::kw___stdcall:
3204 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003205 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003206 case tok::kw___w64:
3207 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003208 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003209 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003210 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003211
3212 case tok::kw___private:
3213 case tok::kw___local:
3214 case tok::kw___global:
3215 case tok::kw___constant:
3216 case tok::kw___read_only:
3217 case tok::kw___read_write:
3218 case tok::kw___write_only:
3219
Eli Friedman290eeb02009-06-08 23:27:34 +00003220 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003221
3222 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003223 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003224
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003225 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003226 case tok::kw__Atomic:
3227 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003228 }
3229}
3230
3231/// isDeclarationSpecifier() - Return true if the current token is part of a
3232/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003233///
3234/// \param DisambiguatingWithExpression True to indicate that the purpose of
3235/// this check is to disambiguate between an expression and a declaration.
3236bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 switch (Tok.getKind()) {
3238 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003240 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003241 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003242
Chris Lattner166a8fc2009-01-04 23:41:41 +00003243 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003244 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003245 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003246 return false;
John Thompson82287d12010-02-05 00:12:22 +00003247 if (TryAltiVecVectorToken())
3248 return true;
3249 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003250 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003251 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003252 // Annotate typenames and C++ scope specifiers. If we get one, just
3253 // recurse to handle whatever we get.
3254 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003255 return true;
3256 if (Tok.is(tok::identifier))
3257 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003258
3259 // If we're in Objective-C and we have an Objective-C class type followed
3260 // by an identifier and then either ':' or ']', in a place where an
3261 // expression is permitted, then this is probably a class message send
3262 // missing the initial '['. In this case, we won't consider this to be
3263 // the start of a declaration.
3264 if (DisambiguatingWithExpression &&
3265 isStartOfObjCClassMessageMissingOpenBracket())
3266 return false;
3267
John McCall9ba61662010-02-26 08:45:28 +00003268 return isDeclarationSpecifier();
3269
Chris Lattner166a8fc2009-01-04 23:41:41 +00003270 case tok::coloncolon: // ::foo::bar
3271 if (NextToken().is(tok::kw_new) || // ::new
3272 NextToken().is(tok::kw_delete)) // ::delete
3273 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Chris Lattner166a8fc2009-01-04 23:41:41 +00003275 // Annotate typenames and C++ scope specifiers. If we get one, just
3276 // recurse to handle whatever we get.
3277 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003278 return true;
3279 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Reid Spencer5f016e22007-07-11 17:01:13 +00003281 // storage-class-specifier
3282 case tok::kw_typedef:
3283 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003284 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 case tok::kw_static:
3286 case tok::kw_auto:
3287 case tok::kw_register:
3288 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Douglas Gregor8d267c52011-09-09 02:06:17 +00003290 // Modules
3291 case tok::kw___module_private__:
3292
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 // type-specifiers
3294 case tok::kw_short:
3295 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003296 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003297 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 case tok::kw_signed:
3299 case tok::kw_unsigned:
3300 case tok::kw__Complex:
3301 case tok::kw__Imaginary:
3302 case tok::kw_void:
3303 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003304 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003305 case tok::kw_char16_t:
3306 case tok::kw_char32_t:
3307
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003309 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 case tok::kw_float:
3311 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003312 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 case tok::kw__Bool:
3314 case tok::kw__Decimal32:
3315 case tok::kw__Decimal64:
3316 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003317 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Chris Lattner99dc9142008-04-13 18:59:07 +00003319 // struct-or-union-specifier (C99) or class-specifier (C++)
3320 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003321 case tok::kw_struct:
3322 case tok::kw_union:
3323 // enum-specifier
3324 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 // type-qualifier
3327 case tok::kw_const:
3328 case tok::kw_volatile:
3329 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003330
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 // function-specifier
3332 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003333 case tok::kw_virtual:
3334 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003335
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003336 // static_assert-declaration
3337 case tok::kw__Static_assert:
3338
Chris Lattner1ef08762007-08-09 17:01:07 +00003339 // GNU typeof support.
3340 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003341
Chris Lattner1ef08762007-08-09 17:01:07 +00003342 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003343 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003344 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Francois Pichete3d49b42011-06-19 08:02:06 +00003346 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003347 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003348 return true;
3349
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003350 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003351 case tok::kw__Atomic:
3352 return true;
3353
Chris Lattnerf3948c42008-07-26 03:38:44 +00003354 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3355 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003356 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003357
Douglas Gregord9d75e52011-04-27 05:41:15 +00003358 // typedef-name
3359 case tok::annot_typename:
3360 return !DisambiguatingWithExpression ||
3361 !isStartOfObjCClassMessageMissingOpenBracket();
3362
Steve Naroff47f52092009-01-06 19:34:12 +00003363 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003364 case tok::kw___cdecl:
3365 case tok::kw___stdcall:
3366 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003367 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003368 case tok::kw___w64:
3369 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003370 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003371 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003372 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003373 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003374
3375 case tok::kw___private:
3376 case tok::kw___local:
3377 case tok::kw___global:
3378 case tok::kw___constant:
3379 case tok::kw___read_only:
3380 case tok::kw___read_write:
3381 case tok::kw___write_only:
3382
Eli Friedman290eeb02009-06-08 23:27:34 +00003383 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003384 }
3385}
3386
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003387bool Parser::isConstructorDeclarator() {
3388 TentativeParsingAction TPA(*this);
3389
3390 // Parse the C++ scope specifier.
3391 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003392 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3393 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003394 TPA.Revert();
3395 return false;
3396 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003397
3398 // Parse the constructor name.
3399 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3400 // We already know that we have a constructor name; just consume
3401 // the token.
3402 ConsumeToken();
3403 } else {
3404 TPA.Revert();
3405 return false;
3406 }
3407
Richard Smith22592862012-03-27 23:05:05 +00003408 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003409 if (Tok.isNot(tok::l_paren)) {
3410 TPA.Revert();
3411 return false;
3412 }
3413 ConsumeParen();
3414
Richard Smith22592862012-03-27 23:05:05 +00003415 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3416 // that we have a constructor.
3417 if (Tok.is(tok::r_paren) ||
3418 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003419 TPA.Revert();
3420 return true;
3421 }
3422
3423 // If we need to, enter the specified scope.
3424 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003425 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003426 DeclScopeObj.EnterDeclaratorScope();
3427
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003428 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003429 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003430 MaybeParseMicrosoftAttributes(Attrs);
3431
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003432 // Check whether the next token(s) are part of a declaration
3433 // specifier, in which case we have the start of a parameter and,
3434 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003435 bool IsConstructor = false;
3436 if (isDeclarationSpecifier())
3437 IsConstructor = true;
3438 else if (Tok.is(tok::identifier) ||
3439 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3440 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3441 // This might be a parenthesized member name, but is more likely to
3442 // be a constructor declaration with an invalid argument type. Keep
3443 // looking.
3444 if (Tok.is(tok::annot_cxxscope))
3445 ConsumeToken();
3446 ConsumeToken();
3447
3448 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003449 // which must have one of the following syntactic forms (see the
3450 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003451 switch (Tok.getKind()) {
3452 case tok::l_paren:
3453 // C(X ( int));
3454 case tok::l_square:
3455 // C(X [ 5]);
3456 // C(X [ [attribute]]);
3457 case tok::coloncolon:
3458 // C(X :: Y);
3459 // C(X :: *p);
3460 case tok::r_paren:
3461 // C(X )
3462 // Assume this isn't a constructor, rather than assuming it's a
3463 // constructor with an unnamed parameter of an ill-formed type.
3464 break;
3465
3466 default:
3467 IsConstructor = true;
3468 break;
3469 }
3470 }
3471
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003472 TPA.Revert();
3473 return IsConstructor;
3474}
Reid Spencer5f016e22007-07-11 17:01:13 +00003475
3476/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003477/// type-qualifier-list: [C99 6.7.5]
3478/// type-qualifier
3479/// [vendor] attributes
3480/// [ only if VendorAttributesAllowed=true ]
3481/// type-qualifier-list type-qualifier
3482/// [vendor] type-qualifier-list attributes
3483/// [ only if VendorAttributesAllowed=true ]
3484/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3485/// [ only if CXX0XAttributesAllowed=true ]
3486/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003487///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003488void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3489 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003490 bool CXX11AttributesAllowed) {
3491 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003492 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003493 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003494 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003495 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003496 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003497
3498 SourceLocation EndLoc;
3499
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003501 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003503 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003504 SourceLocation Loc = Tok.getLocation();
3505
3506 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003507 case tok::code_completion:
3508 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003509 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003510
Reid Spencer5f016e22007-07-11 17:01:13 +00003511 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003512 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003513 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003514 break;
3515 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003516 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003517 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003518 break;
3519 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003520 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003521 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003522 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003523
3524 // OpenCL qualifiers:
3525 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003526 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003527 goto DoneWithTypeQuals;
3528 case tok::kw___private:
3529 case tok::kw___global:
3530 case tok::kw___local:
3531 case tok::kw___constant:
3532 case tok::kw___read_only:
3533 case tok::kw___write_only:
3534 case tok::kw___read_write:
3535 ParseOpenCLQualifiers(DS);
3536 break;
3537
Eli Friedman290eeb02009-06-08 23:27:34 +00003538 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003539 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003540 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003541 case tok::kw___cdecl:
3542 case tok::kw___stdcall:
3543 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003544 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003545 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003546 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003547 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003548 continue;
3549 }
3550 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003551 case tok::kw___pascal:
3552 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003553 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003554 continue;
3555 }
3556 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003557 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003558 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003559 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003560 continue; // do *not* consume the next token!
3561 }
3562 // otherwise, FALL THROUGH!
3563 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003564 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003565 // If this is not a type-qualifier token, we're done reading type
3566 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003567 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003568 if (EndLoc.isValid())
3569 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003570 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003571 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003572
Reid Spencer5f016e22007-07-11 17:01:13 +00003573 // If the specifier combination wasn't legal, issue a diagnostic.
3574 if (isInvalid) {
3575 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003576 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003578 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003579 }
3580}
3581
3582
3583/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3584///
3585void Parser::ParseDeclarator(Declarator &D) {
3586 /// This implements the 'declarator' production in the C grammar, then checks
3587 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003588 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003589}
3590
Richard Smith9988f282012-03-29 01:16:42 +00003591static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3592 if (Kind == tok::star || Kind == tok::caret)
3593 return true;
3594
3595 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3596 if (!Lang.CPlusPlus)
3597 return false;
3598
3599 return Kind == tok::amp || Kind == tok::ampamp;
3600}
3601
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003602/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3603/// is parsed by the function passed to it. Pass null, and the direct-declarator
3604/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003605/// ptr-operator production.
3606///
Richard Smith0706df42011-10-19 21:33:05 +00003607/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003608/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3609/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003610///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003611/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3612/// [C] pointer[opt] direct-declarator
3613/// [C++] direct-declarator
3614/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003615///
3616/// pointer: [C99 6.7.5]
3617/// '*' type-qualifier-list[opt]
3618/// '*' type-qualifier-list[opt] pointer
3619///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003620/// ptr-operator:
3621/// '*' cv-qualifier-seq[opt]
3622/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003623/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003624/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003625/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003626/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003627void Parser::ParseDeclaratorInternal(Declarator &D,
3628 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003629 if (Diags.hasAllExtensionsSilenced())
3630 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003631
Sebastian Redlf30208a2009-01-24 21:16:55 +00003632 // C++ member pointers start with a '::' or a nested-name.
3633 // Member pointers get special handling, since there's no place for the
3634 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003635 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003636 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3637 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003638 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3639 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003640 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003641 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003642
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003643 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003644 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003645 // The scope spec really belongs to the direct-declarator.
3646 D.getCXXScopeSpec() = SS;
3647 if (DirectDeclParser)
3648 (this->*DirectDeclParser)(D);
3649 return;
3650 }
3651
3652 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003653 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003654 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003655 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003656 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003657
3658 // Recurse to parse whatever is left.
3659 ParseDeclaratorInternal(D, DirectDeclParser);
3660
3661 // Sema will have to catch (syntactically invalid) pointers into global
3662 // scope. It has to catch pointers into namespace scope anyway.
3663 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003664 Loc),
3665 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003666 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003667 return;
3668 }
3669 }
3670
3671 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003672 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003673 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003674 if (DirectDeclParser)
3675 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003676 return;
3677 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003678
Sebastian Redl05532f22009-03-15 22:02:01 +00003679 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3680 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003681 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003682 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003683
Chris Lattner9af55002009-03-27 04:18:06 +00003684 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003685 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003686 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003687
Richard Smith6ee326a2012-04-10 01:32:12 +00003688 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003690 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003691
Reid Spencer5f016e22007-07-11 17:01:13 +00003692 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003693 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003694 if (Kind == tok::star)
3695 // Remember that we parsed a pointer type, and remember the type-quals.
3696 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003697 DS.getConstSpecLoc(),
3698 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003699 DS.getRestrictSpecLoc()),
3700 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003701 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003702 else
3703 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003704 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003705 Loc),
3706 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003707 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 } else {
3709 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003710 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003711
Sebastian Redl743de1f2009-03-23 00:00:23 +00003712 // Complain about rvalue references in C++03, but then go on and build
3713 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003714 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003715 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003716 diag::warn_cxx98_compat_rvalue_reference :
3717 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003718
Richard Smith6ee326a2012-04-10 01:32:12 +00003719 // GNU-style and C++11 attributes are allowed here, as is restrict.
3720 ParseTypeQualifierListOpt(DS);
3721 D.ExtendWithDeclSpec(DS);
3722
Reid Spencer5f016e22007-07-11 17:01:13 +00003723 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3724 // cv-qualifiers are introduced through the use of a typedef or of a
3725 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003726 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3727 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3728 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003729 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3731 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003732 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 }
3734
3735 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003736 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003737
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003738 if (D.getNumTypeObjects() > 0) {
3739 // C++ [dcl.ref]p4: There shall be no references to references.
3740 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3741 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003742 if (const IdentifierInfo *II = D.getIdentifier())
3743 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3744 << II;
3745 else
3746 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3747 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003748
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003749 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003750 // can go ahead and build the (technically ill-formed)
3751 // declarator: reference collapsing will take care of it.
3752 }
3753 }
3754
Reid Spencer5f016e22007-07-11 17:01:13 +00003755 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003756 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003757 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003758 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003759 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 }
3761}
3762
Richard Smith9988f282012-03-29 01:16:42 +00003763static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3764 SourceLocation EllipsisLoc) {
3765 if (EllipsisLoc.isValid()) {
3766 FixItHint Insertion;
3767 if (!D.getEllipsisLoc().isValid()) {
3768 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3769 D.setEllipsisLoc(EllipsisLoc);
3770 }
3771 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3772 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3773 }
3774}
3775
Reid Spencer5f016e22007-07-11 17:01:13 +00003776/// ParseDirectDeclarator
3777/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003778/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003779/// '(' declarator ')'
3780/// [GNU] '(' attributes declarator ')'
3781/// [C90] direct-declarator '[' constant-expression[opt] ']'
3782/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3783/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3784/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3785/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003786/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3787/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003788/// direct-declarator '(' parameter-type-list ')'
3789/// direct-declarator '(' identifier-list[opt] ')'
3790/// [GNU] direct-declarator '(' parameter-forward-declarations
3791/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003792/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3793/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003794/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3795/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3796/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003797/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003798/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003799///
3800/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003801/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003802/// '::'[opt] nested-name-specifier[opt] type-name
3803///
3804/// id-expression: [C++ 5.1]
3805/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003806/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003807///
3808/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003809/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003810/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003811/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003812/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003813/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003814///
Richard Smith5d8388c2012-03-27 01:42:32 +00003815/// Note, any additional constructs added here may need corresponding changes
3816/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003817void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003818 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003819
David Blaikie4e4d0842012-03-11 07:00:24 +00003820 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003821 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003822 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003823 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3824 D.getContext() == Declarator::MemberContext;
3825 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3826 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003827 }
3828
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003829 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003830 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003831 // Change the declaration context for name lookup, until this function
3832 // is exited (and the declarator has been parsed).
3833 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003834 }
3835
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003836 // C++0x [dcl.fct]p14:
3837 // There is a syntactic ambiguity when an ellipsis occurs at the end
3838 // of a parameter-declaration-clause without a preceding comma. In
3839 // this case, the ellipsis is parsed as part of the
3840 // abstract-declarator if the type of the parameter names a template
3841 // parameter pack that has not been expanded; otherwise, it is parsed
3842 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003843 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003844 !((D.getContext() == Declarator::PrototypeContext ||
3845 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003846 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003847 !Actions.containsUnexpandedParameterPacks(D))) {
3848 SourceLocation EllipsisLoc = ConsumeToken();
3849 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3850 // The ellipsis was put in the wrong place. Recover, and explain to
3851 // the user what they should have done.
3852 ParseDeclarator(D);
3853 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3854 return;
3855 } else
3856 D.setEllipsisLoc(EllipsisLoc);
3857
3858 // The ellipsis can't be followed by a parenthesized declarator. We
3859 // check for that in ParseParenDeclarator, after we have disambiguated
3860 // the l_paren token.
3861 }
3862
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003863 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3864 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3865 // We found something that indicates the start of an unqualified-id.
3866 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003867 bool AllowConstructorName;
3868 if (D.getDeclSpec().hasTypeSpecifier())
3869 AllowConstructorName = false;
3870 else if (D.getCXXScopeSpec().isSet())
3871 AllowConstructorName =
3872 (D.getContext() == Declarator::FileContext ||
3873 (D.getContext() == Declarator::MemberContext &&
3874 D.getDeclSpec().isFriendSpecified()));
3875 else
3876 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3877
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003878 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003879 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3880 /*EnteringContext=*/true,
3881 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003882 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003883 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003884 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003885 D.getName()) ||
3886 // Once we're past the identifier, if the scope was bad, mark the
3887 // whole declarator bad.
3888 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003889 D.SetIdentifier(0, Tok.getLocation());
3890 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003891 } else {
3892 // Parsed the unqualified-id; update range information and move along.
3893 if (D.getSourceRange().getBegin().isInvalid())
3894 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3895 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003896 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003897 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003898 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003899 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003900 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003901 "There's a C++-specific check for tok::identifier above");
3902 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3903 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3904 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003905 goto PastIdentifier;
3906 }
Richard Smith9988f282012-03-29 01:16:42 +00003907
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003908 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003909 // direct-declarator: '(' declarator ')'
3910 // direct-declarator: '(' attributes declarator ')'
3911 // Example: 'char (*X)' or 'int (*XX)(void)'
3912 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003913
3914 // If the declarator was parenthesized, we entered the declarator
3915 // scope when parsing the parenthesized declarator, then exited
3916 // the scope already. Re-enter the scope, if we need to.
3917 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003918 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003919 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003920 if (!D.isInvalidType() &&
3921 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003922 // Change the declaration context for name lookup, until this function
3923 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003924 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003925 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003926 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003927 // This could be something simple like "int" (in which case the declarator
3928 // portion is empty), if an abstract-declarator is allowed.
3929 D.SetIdentifier(0, Tok.getLocation());
3930 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003931 if (D.getContext() == Declarator::MemberContext)
3932 Diag(Tok, diag::err_expected_member_name_or_semi)
3933 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003934 else if (getLangOpts().CPlusPlus)
3935 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003936 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003937 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003938 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003939 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003940 }
Mike Stump1eb44332009-09-09 15:08:12 +00003941
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003942 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003943 assert(D.isPastIdentifier() &&
3944 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003945
Richard Smith6ee326a2012-04-10 01:32:12 +00003946 // Don't parse attributes unless we have parsed an unparenthesized name.
3947 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00003948 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003949
Reid Spencer5f016e22007-07-11 17:01:13 +00003950 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003951 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003952 // Enter function-declaration scope, limiting any declarators to the
3953 // function prototype scope, including parameter declarators.
3954 ParseScope PrototypeScope(this,
3955 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003956 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3957 // In such a case, check if we actually have a function declarator; if it
3958 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003959 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003960 // When not in file scope, warn for ambiguous function declarators, just
3961 // in case the author intended it as a variable definition.
3962 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3963 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3964 break;
3965 }
John McCall0b7e6782011-03-24 11:26:52 +00003966 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003967 BalancedDelimiterTracker T(*this, tok::l_paren);
3968 T.consumeOpen();
3969 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003970 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003971 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003972 ParseBracketDeclarator(D);
3973 } else {
3974 break;
3975 }
3976 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003977}
Reid Spencer5f016e22007-07-11 17:01:13 +00003978
Chris Lattneref4715c2008-04-06 05:45:57 +00003979/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3980/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003981/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003982/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3983///
3984/// direct-declarator:
3985/// '(' declarator ')'
3986/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003987/// direct-declarator '(' parameter-type-list ')'
3988/// direct-declarator '(' identifier-list[opt] ')'
3989/// [GNU] direct-declarator '(' parameter-forward-declarations
3990/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003991///
3992void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003993 BalancedDelimiterTracker T(*this, tok::l_paren);
3994 T.consumeOpen();
3995
Chris Lattneref4715c2008-04-06 05:45:57 +00003996 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003997
Chris Lattner7399ee02008-10-20 02:05:46 +00003998 // Eat any attributes before we look at whether this is a grouping or function
3999 // declarator paren. If this is a grouping paren, the attribute applies to
4000 // the type being built up, for example:
4001 // int (__attribute__(()) *x)(long y)
4002 // If this ends up not being a grouping paren, the attribute applies to the
4003 // first argument, for example:
4004 // int (__attribute__(()) int x)
4005 // In either case, we need to eat any attributes to be able to determine what
4006 // sort of paren this is.
4007 //
John McCall0b7e6782011-03-24 11:26:52 +00004008 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004009 bool RequiresArg = false;
4010 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004011 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004012
Chris Lattner7399ee02008-10-20 02:05:46 +00004013 // We require that the argument list (if this is a non-grouping paren) be
4014 // present even if the attribute list was empty.
4015 RequiresArg = true;
4016 }
Steve Naroff239f0732008-12-25 14:16:32 +00004017 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004018 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004019 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004020 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004021 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004022 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004023 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004024 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004025 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004026 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004027
Chris Lattneref4715c2008-04-06 05:45:57 +00004028 // If we haven't past the identifier yet (or where the identifier would be
4029 // stored, if this is an abstract declarator), then this is probably just
4030 // grouping parens. However, if this could be an abstract-declarator, then
4031 // this could also be the start of function arguments (consider 'void()').
4032 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004033
Chris Lattneref4715c2008-04-06 05:45:57 +00004034 if (!D.mayOmitIdentifier()) {
4035 // If this can't be an abstract-declarator, this *must* be a grouping
4036 // paren, because we haven't seen the identifier yet.
4037 isGrouping = true;
4038 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004039 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4040 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004041 isDeclarationSpecifier() || // 'int(int)' is a function.
4042 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004043 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4044 // considered to be a type, not a K&R identifier-list.
4045 isGrouping = false;
4046 } else {
4047 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4048 isGrouping = true;
4049 }
Mike Stump1eb44332009-09-09 15:08:12 +00004050
Chris Lattneref4715c2008-04-06 05:45:57 +00004051 // If this is a grouping paren, handle:
4052 // direct-declarator: '(' declarator ')'
4053 // direct-declarator: '(' attributes declarator ')'
4054 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004055 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4056 D.setEllipsisLoc(SourceLocation());
4057
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004058 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004059 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004060 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004061 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004062 T.consumeClose();
4063 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4064 T.getCloseLocation()),
4065 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004066
4067 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004068
4069 // An ellipsis cannot be placed outside parentheses.
4070 if (EllipsisLoc.isValid())
4071 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4072
Chris Lattneref4715c2008-04-06 05:45:57 +00004073 return;
4074 }
Mike Stump1eb44332009-09-09 15:08:12 +00004075
Chris Lattneref4715c2008-04-06 05:45:57 +00004076 // Okay, if this wasn't a grouping paren, it must be the start of a function
4077 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004078 // identifier (and remember where it would have been), then call into
4079 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004080 D.SetIdentifier(0, Tok.getLocation());
4081
David Blaikie42d6d0c2011-12-04 05:04:18 +00004082 // Enter function-declaration scope, limiting any declarators to the
4083 // function prototype scope, including parameter declarators.
4084 ParseScope PrototypeScope(this,
4085 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004086 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004087 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004088}
4089
4090/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4091/// declarator D up to a paren, which indicates that we are parsing function
4092/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004093///
Richard Smith6ee326a2012-04-10 01:32:12 +00004094/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4095/// immediately after the open paren - they should be considered to be the
4096/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004097///
Richard Smith6ee326a2012-04-10 01:32:12 +00004098/// If RequiresArg is true, then the first argument of the function is required
4099/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004100///
Richard Smith6ee326a2012-04-10 01:32:12 +00004101/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4102/// (C++11) ref-qualifier[opt], exception-specification[opt],
4103/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4104///
4105/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004106/// dynamic-exception-specification
4107/// noexcept-specification
4108///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004109void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004110 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004111 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004112 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004113 assert(getCurScope()->isFunctionPrototypeScope() &&
4114 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004115 // lparen is already consumed!
4116 assert(D.isPastIdentifier() && "Should not call before identifier!");
4117
4118 // This should be true when the function has typed arguments.
4119 // Otherwise, it is treated as a K&R-style function.
4120 bool HasProto = false;
4121 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004122 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004123 // Remember where we see an ellipsis, if any.
4124 SourceLocation EllipsisLoc;
4125
4126 DeclSpec DS(AttrFactory);
4127 bool RefQualifierIsLValueRef = true;
4128 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004129 SourceLocation ConstQualifierLoc;
4130 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004131 ExceptionSpecificationType ESpecType = EST_None;
4132 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004133 SmallVector<ParsedType, 2> DynamicExceptions;
4134 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004135 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004136 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004137 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004138
James Molloy16f1f712012-02-29 10:24:19 +00004139 Actions.ActOnStartFunctionDeclarator();
4140
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004141 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004142 if (isFunctionDeclaratorIdentifierList()) {
4143 if (RequiresArg)
4144 Diag(Tok, diag::err_argument_required_after_attribute);
4145
4146 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4147
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004148 Tracker.consumeClose();
4149 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004150 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004152 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004153 else if (RequiresArg)
4154 Diag(Tok, diag::err_argument_required_after_attribute);
4155
David Blaikie4e4d0842012-03-11 07:00:24 +00004156 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004157
4158 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004159 Tracker.consumeClose();
4160 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004161
David Blaikie4e4d0842012-03-11 07:00:24 +00004162 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004163 // FIXME: Accept these components in any order, and produce fixits to
4164 // correct the order if the user gets it wrong. Ideally we should deal
4165 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004166
4167 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004168 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4169 if (!DS.getSourceRange().getEnd().isInvalid()) {
4170 EndLoc = DS.getSourceRange().getEnd();
4171 ConstQualifierLoc = DS.getConstSpecLoc();
4172 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4173 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004174
4175 // Parse ref-qualifier[opt].
4176 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004177 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004178 diag::warn_cxx98_compat_ref_qualifier :
4179 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004180
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004181 RefQualifierIsLValueRef = Tok.is(tok::amp);
4182 RefQualifierLoc = ConsumeToken();
4183 EndLoc = RefQualifierLoc;
4184 }
4185
4186 // Parse exception-specification[opt].
4187 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4188 DynamicExceptions,
4189 DynamicExceptionRanges,
4190 NoexceptExpr);
4191 if (ESpecType != EST_None)
4192 EndLoc = ESpecRange.getEnd();
4193
Richard Smith6ee326a2012-04-10 01:32:12 +00004194 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4195 // after the exception-specification.
4196 MaybeParseCXX0XAttributes(FnAttrs);
4197
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004198 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004199 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004200 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004201 SourceRange Range;
4202 TrailingReturnType = ParseTrailingReturnType(Range).get();
4203 if (Range.getEnd().isValid())
4204 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004205 }
4206 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004207 }
4208
4209 // Remember that we parsed a function type, and remember the attributes.
4210 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4211 /*isVariadic=*/EllipsisLoc.isValid(),
4212 EllipsisLoc,
4213 ParamInfo.data(), ParamInfo.size(),
4214 DS.getTypeQualifiers(),
4215 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004216 RefQualifierLoc, ConstQualifierLoc,
4217 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004218 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004219 ESpecType, ESpecRange.getBegin(),
4220 DynamicExceptions.data(),
4221 DynamicExceptionRanges.data(),
4222 DynamicExceptions.size(),
4223 NoexceptExpr.isUsable() ?
4224 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004225 Tracker.getOpenLocation(),
4226 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004227 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004228 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004229
4230 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004231}
4232
4233/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4234/// identifier list form for a K&R-style function: void foo(a,b,c)
4235///
4236/// Note that identifier-lists are only allowed for normal declarators, not for
4237/// abstract-declarators.
4238bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004239 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004240 && Tok.is(tok::identifier)
4241 && !TryAltiVecVectorToken()
4242 // K&R identifier lists can't have typedefs as identifiers, per C99
4243 // 6.7.5.3p11.
4244 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4245 // Identifier lists follow a really simple grammar: the identifiers can
4246 // be followed *only* by a ", identifier" or ")". However, K&R
4247 // identifier lists are really rare in the brave new modern world, and
4248 // it is very common for someone to typo a type in a non-K&R style
4249 // list. If we are presented with something like: "void foo(intptr x,
4250 // float y)", we don't want to start parsing the function declarator as
4251 // though it is a K&R style declarator just because intptr is an
4252 // invalid type.
4253 //
4254 // To handle this, we check to see if the token after the first
4255 // identifier is a "," or ")". Only then do we parse it as an
4256 // identifier list.
4257 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4258}
4259
4260/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4261/// we found a K&R-style identifier list instead of a typed parameter list.
4262///
4263/// After returning, ParamInfo will hold the parsed parameters.
4264///
4265/// identifier-list: [C99 6.7.5]
4266/// identifier
4267/// identifier-list ',' identifier
4268///
4269void Parser::ParseFunctionDeclaratorIdentifierList(
4270 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004271 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004272 // If there was no identifier specified for the declarator, either we are in
4273 // an abstract-declarator, or we are in a parameter declarator which was found
4274 // to be abstract. In abstract-declarators, identifier lists are not valid:
4275 // diagnose this.
4276 if (!D.getIdentifier())
4277 Diag(Tok, diag::ext_ident_list_in_param);
4278
4279 // Maintain an efficient lookup of params we have seen so far.
4280 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4281
4282 while (1) {
4283 // If this isn't an identifier, report the error and skip until ')'.
4284 if (Tok.isNot(tok::identifier)) {
4285 Diag(Tok, diag::err_expected_ident);
4286 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4287 // Forget we parsed anything.
4288 ParamInfo.clear();
4289 return;
4290 }
4291
4292 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4293
4294 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4295 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4296 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4297
4298 // Verify that the argument identifier has not already been mentioned.
4299 if (!ParamsSoFar.insert(ParmII)) {
4300 Diag(Tok, diag::err_param_redefinition) << ParmII;
4301 } else {
4302 // Remember this identifier in ParamInfo.
4303 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4304 Tok.getLocation(),
4305 0));
4306 }
4307
4308 // Eat the identifier.
4309 ConsumeToken();
4310
4311 // The list continues if we see a comma.
4312 if (Tok.isNot(tok::comma))
4313 break;
4314 ConsumeToken();
4315 }
4316}
4317
4318/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4319/// after the opening parenthesis. This function will not parse a K&R-style
4320/// identifier list.
4321///
Richard Smith6ce48a72012-04-11 04:01:28 +00004322/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4323/// caller parsed those arguments immediately after the open paren - they should
4324/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004325///
4326/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4327/// be the location of the ellipsis, if any was parsed.
4328///
Reid Spencer5f016e22007-07-11 17:01:13 +00004329/// parameter-type-list: [C99 6.7.5]
4330/// parameter-list
4331/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004332/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004333///
4334/// parameter-list: [C99 6.7.5]
4335/// parameter-declaration
4336/// parameter-list ',' parameter-declaration
4337///
4338/// parameter-declaration: [C99 6.7.5]
4339/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004340/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004341/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004342/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004343/// declaration-specifiers abstract-declarator[opt]
4344/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004345/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004346/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004347/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004348///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004349void Parser::ParseParameterDeclarationClause(
4350 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004351 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004352 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004353 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004354
Chris Lattnerf97409f2008-04-06 06:57:35 +00004355 while (1) {
4356 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004357 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4358 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004359 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004360 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004361 }
Mike Stump1eb44332009-09-09 15:08:12 +00004362
Chris Lattnerf97409f2008-04-06 06:57:35 +00004363 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004364 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004365 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004366
Richard Smith6ce48a72012-04-11 04:01:28 +00004367 // Parse any C++11 attributes.
4368 MaybeParseCXX0XAttributes(DS.getAttributes());
4369
John McCall7f040a92010-12-24 02:08:15 +00004370 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004371 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004372 ParseMicrosoftAttributes(DS.getAttributes());
4373
4374 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004375
4376 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004377 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004378 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004379 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4380 // too much hassle.
4381 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004382
Chris Lattnere64c5492009-02-27 18:38:20 +00004383 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004384
Chris Lattnerf97409f2008-04-06 06:57:35 +00004385 // Parse the declarator. This is "PrototypeContext", because we must
4386 // accept either 'declarator' or 'abstract-declarator' here.
4387 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4388 ParseDeclarator(ParmDecl);
4389
4390 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004391 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004392
Chris Lattnerf97409f2008-04-06 06:57:35 +00004393 // Remember this parsed parameter in ParamInfo.
4394 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004395
Douglas Gregor72b505b2008-12-16 21:30:33 +00004396 // DefArgToks is used when the parsing of default arguments needs
4397 // to be delayed.
4398 CachedTokens *DefArgToks = 0;
4399
Chris Lattnerf97409f2008-04-06 06:57:35 +00004400 // If no parameter was specified, verify that *something* was specified,
4401 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004402 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4403 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004404 // Completely missing, emit error.
4405 Diag(DSStart, diag::err_missing_param);
4406 } else {
4407 // Otherwise, we have something. Add it and let semantic analysis try
4408 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004409
Chris Lattnerf97409f2008-04-06 06:57:35 +00004410 // Inform the actions module about the parameter declarator, so it gets
4411 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004412 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004413
4414 // Parse the default argument, if any. We parse the default
4415 // arguments in all dialects; the semantic analysis in
4416 // ActOnParamDefaultArgument will reject the default argument in
4417 // C.
4418 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004419 SourceLocation EqualLoc = Tok.getLocation();
4420
Chris Lattner04421082008-04-08 04:40:51 +00004421 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004422 if (D.getContext() == Declarator::MemberContext) {
4423 // If we're inside a class definition, cache the tokens
4424 // corresponding to the default argument. We'll actually parse
4425 // them when we see the end of the class definition.
4426 // FIXME: Templates will require something similar.
4427 // FIXME: Can we use a smart pointer for Toks?
4428 DefArgToks = new CachedTokens;
4429
Mike Stump1eb44332009-09-09 15:08:12 +00004430 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004431 /*StopAtSemi=*/true,
4432 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004433 delete DefArgToks;
4434 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004435 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004436 } else {
4437 // Mark the end of the default argument so that we know when to
4438 // stop when we parse it later on.
4439 Token DefArgEnd;
4440 DefArgEnd.startToken();
4441 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4442 DefArgEnd.setLocation(Tok.getLocation());
4443 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004444 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004445 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004446 }
Chris Lattner04421082008-04-08 04:40:51 +00004447 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004448 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004449 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004450
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004451 // The argument isn't actually potentially evaluated unless it is
4452 // used.
4453 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004454 Sema::PotentiallyEvaluatedIfUsed,
4455 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004456
Sebastian Redl84407ba2012-03-14 15:54:00 +00004457 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004458 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4459 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004460 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004461 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004462 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004463 if (DefArgResult.isInvalid()) {
4464 Actions.ActOnParamDefaultArgumentError(Param);
4465 SkipUntil(tok::comma, tok::r_paren, true, true);
4466 } else {
4467 // Inform the actions module about the default argument
4468 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004469 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004470 }
Chris Lattner04421082008-04-08 04:40:51 +00004471 }
4472 }
Mike Stump1eb44332009-09-09 15:08:12 +00004473
4474 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4475 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004476 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004477 }
4478
4479 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004480 if (Tok.isNot(tok::comma)) {
4481 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004482 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4483
David Blaikie4e4d0842012-03-11 07:00:24 +00004484 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004485 // We have ellipsis without a preceding ',', which is ill-formed
4486 // in C. Complain and provide the fix.
4487 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004488 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004489 }
4490 }
4491
4492 break;
4493 }
Mike Stump1eb44332009-09-09 15:08:12 +00004494
Chris Lattnerf97409f2008-04-06 06:57:35 +00004495 // Consume the comma.
4496 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004497 }
Mike Stump1eb44332009-09-09 15:08:12 +00004498
Chris Lattner66d28652008-04-06 06:34:08 +00004499}
Chris Lattneref4715c2008-04-06 05:45:57 +00004500
Reid Spencer5f016e22007-07-11 17:01:13 +00004501/// [C90] direct-declarator '[' constant-expression[opt] ']'
4502/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4503/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4504/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4505/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004506/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4507/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004508void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004509 if (CheckProhibitedCXX11Attribute())
4510 return;
4511
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004512 BalancedDelimiterTracker T(*this, tok::l_square);
4513 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004514
Chris Lattner378c7e42008-12-18 07:27:21 +00004515 // C array syntax has many features, but by-far the most common is [] and [4].
4516 // This code does a fast path to handle some of the most obvious cases.
4517 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004518 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004519 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004520 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004521
Chris Lattner378c7e42008-12-18 07:27:21 +00004522 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004523 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004524 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004525 T.getOpenLocation(),
4526 T.getCloseLocation()),
4527 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004528 return;
4529 } else if (Tok.getKind() == tok::numeric_constant &&
4530 GetLookAheadToken(1).is(tok::r_square)) {
4531 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004532 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004533 ConsumeToken();
4534
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004535 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004536 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004537 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004538
Chris Lattner378c7e42008-12-18 07:27:21 +00004539 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004540 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004541 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004542 T.getOpenLocation(),
4543 T.getCloseLocation()),
4544 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004545 return;
4546 }
Mike Stump1eb44332009-09-09 15:08:12 +00004547
Reid Spencer5f016e22007-07-11 17:01:13 +00004548 // If valid, this location is the position where we read the 'static' keyword.
4549 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004550 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004551 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004552
Reid Spencer5f016e22007-07-11 17:01:13 +00004553 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004554 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004555 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004556 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004557
Reid Spencer5f016e22007-07-11 17:01:13 +00004558 // If we haven't already read 'static', check to see if there is one after the
4559 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004560 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004561 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004562
Reid Spencer5f016e22007-07-11 17:01:13 +00004563 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4564 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004565 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004566
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004567 // Handle the case where we have '[*]' as the array size. However, a leading
4568 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4569 // the the token after the star is a ']'. Since stars in arrays are
4570 // infrequent, use of lookahead is not costly here.
4571 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004572 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004573
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004574 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004575 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004576 StaticLoc = SourceLocation(); // Drop the static.
4577 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004578 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004579 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004580 // Note, in C89, this production uses the constant-expr production instead
4581 // of assignment-expr. The only difference is that assignment-expr allows
4582 // things like '=' and '*='. Sema rejects these in C89 mode because they
4583 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004584
Douglas Gregore0762c92009-06-19 23:52:42 +00004585 // Parse the constant-expression or assignment-expression now (depending
4586 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004587 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004588 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004589 } else {
4590 EnterExpressionEvaluationContext Unevaluated(Actions,
4591 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004592 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004593 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004594 }
Mike Stump1eb44332009-09-09 15:08:12 +00004595
Reid Spencer5f016e22007-07-11 17:01:13 +00004596 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004597 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004598 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004599 // If the expression was invalid, skip it.
4600 SkipUntil(tok::r_square);
4601 return;
4602 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004604 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004605
John McCall0b7e6782011-03-24 11:26:52 +00004606 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004607 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004608
Chris Lattner378c7e42008-12-18 07:27:21 +00004609 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004610 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004611 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004612 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004613 T.getOpenLocation(),
4614 T.getCloseLocation()),
4615 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004616}
4617
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004618/// [GNU] typeof-specifier:
4619/// typeof ( expressions )
4620/// typeof ( type-name )
4621/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004622///
4623void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004624 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004625 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004626 SourceLocation StartLoc = ConsumeToken();
4627
John McCallcfb708c2010-01-13 20:03:27 +00004628 const bool hasParens = Tok.is(tok::l_paren);
4629
Eli Friedman71b8fb52012-01-21 01:01:51 +00004630 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4631
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004632 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004633 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004634 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004635 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4636 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004637 if (hasParens)
4638 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004639
4640 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004641 // FIXME: Not accurate, the range gets one token more than it should.
4642 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004643 else
4644 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004645
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004646 if (isCastExpr) {
4647 if (!CastTy) {
4648 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004649 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004650 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004651
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004652 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004653 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004654 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4655 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004656 DiagID, CastTy))
4657 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004658 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004659 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004660
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004661 // If we get here, the operand to the typeof was an expresion.
4662 if (Operand.isInvalid()) {
4663 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004664 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004665 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004666
Eli Friedman71b8fb52012-01-21 01:01:51 +00004667 // We might need to transform the operand if it is potentially evaluated.
4668 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4669 if (Operand.isInvalid()) {
4670 DS.SetTypeSpecError();
4671 return;
4672 }
4673
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004674 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004675 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004676 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4677 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004678 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004679 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004680}
Chris Lattner1b492422010-02-28 18:33:55 +00004681
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004682/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004683/// _Atomic ( type-name )
4684///
4685void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4686 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4687
4688 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004689 BalancedDelimiterTracker T(*this, tok::l_paren);
4690 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004691 SkipUntil(tok::r_paren);
4692 return;
4693 }
4694
4695 TypeResult Result = ParseTypeName();
4696 if (Result.isInvalid()) {
4697 SkipUntil(tok::r_paren);
4698 return;
4699 }
4700
4701 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004702 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004703
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004704 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004705 return;
4706
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004707 DS.setTypeofParensRange(T.getRange());
4708 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004709
4710 const char *PrevSpec = 0;
4711 unsigned DiagID;
4712 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4713 DiagID, Result.release()))
4714 Diag(StartLoc, DiagID) << PrevSpec;
4715}
4716
Chris Lattner1b492422010-02-28 18:33:55 +00004717
4718/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4719/// from TryAltiVecVectorToken.
4720bool Parser::TryAltiVecVectorTokenOutOfLine() {
4721 Token Next = NextToken();
4722 switch (Next.getKind()) {
4723 default: return false;
4724 case tok::kw_short:
4725 case tok::kw_long:
4726 case tok::kw_signed:
4727 case tok::kw_unsigned:
4728 case tok::kw_void:
4729 case tok::kw_char:
4730 case tok::kw_int:
4731 case tok::kw_float:
4732 case tok::kw_double:
4733 case tok::kw_bool:
4734 case tok::kw___pixel:
4735 Tok.setKind(tok::kw___vector);
4736 return true;
4737 case tok::identifier:
4738 if (Next.getIdentifierInfo() == Ident_pixel) {
4739 Tok.setKind(tok::kw___vector);
4740 return true;
4741 }
4742 return false;
4743 }
4744}
4745
4746bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4747 const char *&PrevSpec, unsigned &DiagID,
4748 bool &isInvalid) {
4749 if (Tok.getIdentifierInfo() == Ident_vector) {
4750 Token Next = NextToken();
4751 switch (Next.getKind()) {
4752 case tok::kw_short:
4753 case tok::kw_long:
4754 case tok::kw_signed:
4755 case tok::kw_unsigned:
4756 case tok::kw_void:
4757 case tok::kw_char:
4758 case tok::kw_int:
4759 case tok::kw_float:
4760 case tok::kw_double:
4761 case tok::kw_bool:
4762 case tok::kw___pixel:
4763 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4764 return true;
4765 case tok::identifier:
4766 if (Next.getIdentifierInfo() == Ident_pixel) {
4767 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4768 return true;
4769 }
4770 break;
4771 default:
4772 break;
4773 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004774 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004775 DS.isTypeAltiVecVector()) {
4776 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4777 return true;
4778 }
4779 return false;
4780}