blob: 9bafa92d7f5862b18f28ece3bc418f66aaf0911b [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 }
940}
941
John McCall7f040a92010-12-24 02:08:15 +0000942void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
943 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
944 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000945}
946
Reid Spencer5f016e22007-07-11 17:01:13 +0000947/// ParseDeclaration - Parse a full 'declaration', which consists of
948/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000949/// 'Context' should be a Declarator::TheContext value. This returns the
950/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000951///
952/// declaration: [C99 6.7]
953/// block-declaration ->
954/// simple-declaration
955/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000956/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000957/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000958/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000959/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000960/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000961/// others... [FIXME]
962///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000963Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
964 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000965 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000966 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000967 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000968 // Must temporarily exit the objective-c container scope for
969 // parsing c none objective-c decls.
970 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000971
John McCalld226f652010-08-21 09:40:31 +0000972 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000973 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000974 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000975 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000976 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000977 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000978 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000979 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000980 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000981 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000982 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000983 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000984 SourceLocation InlineLoc = ConsumeToken();
985 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
986 break;
987 }
John McCall7f040a92010-12-24 02:08:15 +0000988 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000989 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000990 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000991 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000992 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000993 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000994 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000995 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000996 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000997 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000998 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000999 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001000 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001001 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001002 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001003 default:
John McCall7f040a92010-12-24 02:08:15 +00001004 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001005 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001006
Chris Lattner682bf922009-03-29 16:50:03 +00001007 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001008 // single decl, convert it now. Alias declarations can also declare a type;
1009 // include that too if it is present.
1010 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001011}
1012
1013/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1014/// declaration-specifiers init-declarator-list[opt] ';'
1015///[C90/C++]init-declarator-list ';' [TODO]
1016/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001017///
Richard Smithad762fc2011-04-14 22:09:26 +00001018/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1019/// attribute-specifier-seq[opt] type-specifier-seq declarator
1020///
Chris Lattnercd147752009-03-29 17:27:48 +00001021/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001022/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001023///
1024/// If FRI is non-null, we might be parsing a for-range-declaration instead
1025/// of a simple-declaration. If we find that we are, we also parse the
1026/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001027Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1028 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001029 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001030 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001031 bool RequireSemi,
1032 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001034 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001035 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001036
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001037 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001038 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001039
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1041 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001042 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001043 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001044 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001045 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001046 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001047 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001049
1050 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001051}
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Richard Smith0706df42011-10-19 21:33:05 +00001053/// Returns true if this might be the start of a declarator, or a common typo
1054/// for a declarator.
1055bool Parser::MightBeDeclarator(unsigned Context) {
1056 switch (Tok.getKind()) {
1057 case tok::annot_cxxscope:
1058 case tok::annot_template_id:
1059 case tok::caret:
1060 case tok::code_completion:
1061 case tok::coloncolon:
1062 case tok::ellipsis:
1063 case tok::kw___attribute:
1064 case tok::kw_operator:
1065 case tok::l_paren:
1066 case tok::star:
1067 return true;
1068
1069 case tok::amp:
1070 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001071 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001072
Richard Smith1c94c162012-01-09 22:31:44 +00001073 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001074 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001075 NextToken().is(tok::l_square);
1076
1077 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001078 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001079
Richard Smith0706df42011-10-19 21:33:05 +00001080 case tok::identifier:
1081 switch (NextToken().getKind()) {
1082 case tok::code_completion:
1083 case tok::coloncolon:
1084 case tok::comma:
1085 case tok::equal:
1086 case tok::equalequal: // Might be a typo for '='.
1087 case tok::kw_alignas:
1088 case tok::kw_asm:
1089 case tok::kw___attribute:
1090 case tok::l_brace:
1091 case tok::l_paren:
1092 case tok::l_square:
1093 case tok::less:
1094 case tok::r_brace:
1095 case tok::r_paren:
1096 case tok::r_square:
1097 case tok::semi:
1098 return true;
1099
1100 case tok::colon:
1101 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001102 // and in block scope it's probably a label. Inside a class definition,
1103 // this is a bit-field.
1104 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001105 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001106
1107 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001108 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001109
1110 default:
1111 return false;
1112 }
1113
1114 default:
1115 return false;
1116 }
1117}
1118
John McCalld8ac0572009-11-03 19:26:08 +00001119/// ParseDeclGroup - Having concluded that this is either a function
1120/// definition or a group of object declarations, actually parse the
1121/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001122Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1123 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001124 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001125 SourceLocation *DeclEnd,
1126 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001127 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001128 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001129 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001130
John McCalld8ac0572009-11-03 19:26:08 +00001131 // Bail out if the first declarator didn't seem well-formed.
1132 if (!D.hasName() && !D.mayOmitIdentifier()) {
1133 // Skip until ; or }.
1134 SkipUntil(tok::r_brace, true, true);
1135 if (Tok.is(tok::semi))
1136 ConsumeToken();
1137 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001140 // Save late-parsed attributes for now; they need to be parsed in the
1141 // appropriate function scope after the function Decl has been constructed.
1142 LateParsedAttrList LateParsedAttrs;
1143 if (D.isFunctionDeclarator())
1144 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1145
Chris Lattnerc82daef2010-07-11 22:24:20 +00001146 // Check to see if we have a function *definition* which must have a body.
1147 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1148 // Look at the next token to make sure that this isn't a function
1149 // declaration. We have to check this because __attribute__ might be the
1150 // start of a function definition in GCC-extended K&R C.
1151 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001152
Chris Lattner004659a2010-07-11 22:42:07 +00001153 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001154 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1155 Diag(Tok, diag::err_function_declared_typedef);
1156
1157 // Recover by treating the 'typedef' as spurious.
1158 DS.ClearStorageClassSpecs();
1159 }
1160
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001161 Decl *TheDecl =
1162 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001163 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001164 }
1165
1166 if (isDeclarationSpecifier()) {
1167 // If there is an invalid declaration specifier right after the function
1168 // prototype, then we must be in a missing semicolon case where this isn't
1169 // actually a body. Just fall through into the code that handles it as a
1170 // prototype, and let the top-level code handle the erroneous declspec
1171 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001172 } else {
1173 Diag(Tok, diag::err_expected_fn_body);
1174 SkipUntil(tok::semi);
1175 return DeclGroupPtrTy();
1176 }
1177 }
1178
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001179 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001180 return DeclGroupPtrTy();
1181
1182 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1183 // must parse and analyze the for-range-initializer before the declaration is
1184 // analyzed.
1185 if (FRI && Tok.is(tok::colon)) {
1186 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001187 if (Tok.is(tok::l_brace))
1188 FRI->RangeExpr = ParseBraceInitializer();
1189 else
1190 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001191 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1192 Actions.ActOnCXXForRangeDecl(ThisDecl);
1193 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001194 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001195 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1196 }
1197
Chris Lattner5f9e2722011-07-23 10:55:15 +00001198 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001199 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001200 if (LateParsedAttrs.size() > 0)
1201 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001202 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001203 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001204 DeclsInGroup.push_back(FirstDecl);
1205
Richard Smith0706df42011-10-19 21:33:05 +00001206 bool ExpectSemi = Context != Declarator::ForContext;
1207
John McCalld8ac0572009-11-03 19:26:08 +00001208 // If we don't have a comma, it is either the end of the list (a ';') or an
1209 // error, bail out.
1210 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001211 SourceLocation CommaLoc = ConsumeToken();
1212
1213 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1214 // This comma was followed by a line-break and something which can't be
1215 // the start of a declarator. The comma was probably a typo for a
1216 // semicolon.
1217 Diag(CommaLoc, diag::err_expected_semi_declaration)
1218 << FixItHint::CreateReplacement(CommaLoc, ";");
1219 ExpectSemi = false;
1220 break;
1221 }
John McCalld8ac0572009-11-03 19:26:08 +00001222
1223 // Parse the next declarator.
1224 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001225 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001226
1227 // Accept attributes in an init-declarator. In the first declarator in a
1228 // declaration, these would be part of the declspec. In subsequent
1229 // declarators, they become part of the declarator itself, so that they
1230 // don't apply to declarators after *this* one. Examples:
1231 // short __attribute__((common)) var; -> declspec
1232 // short var __attribute__((common)); -> declarator
1233 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001234 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001235
1236 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001237 if (!D.isInvalidType()) {
1238 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1239 D.complete(ThisDecl);
1240 if (ThisDecl)
1241 DeclsInGroup.push_back(ThisDecl);
1242 }
John McCalld8ac0572009-11-03 19:26:08 +00001243 }
1244
1245 if (DeclEnd)
1246 *DeclEnd = Tok.getLocation();
1247
Richard Smith0706df42011-10-19 21:33:05 +00001248 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001249 ExpectAndConsume(tok::semi,
1250 Context == Declarator::FileContext
1251 ? diag::err_invalid_token_after_toplevel_declarator
1252 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001253 // Okay, there was no semicolon and one was expected. If we see a
1254 // declaration specifier, just assume it was missing and continue parsing.
1255 // Otherwise things are very confused and we skip to recover.
1256 if (!isDeclarationSpecifier()) {
1257 SkipUntil(tok::r_brace, true, true);
1258 if (Tok.is(tok::semi))
1259 ConsumeToken();
1260 }
John McCalld8ac0572009-11-03 19:26:08 +00001261 }
1262
Douglas Gregor23c94db2010-07-02 17:43:08 +00001263 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001264 DeclsInGroup.data(),
1265 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001266}
1267
Richard Smithad762fc2011-04-14 22:09:26 +00001268/// Parse an optional simple-asm-expr and attributes, and attach them to a
1269/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001270bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001271 // If a simple-asm-expr is present, parse it.
1272 if (Tok.is(tok::kw_asm)) {
1273 SourceLocation Loc;
1274 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1275 if (AsmLabel.isInvalid()) {
1276 SkipUntil(tok::semi, true, true);
1277 return true;
1278 }
1279
1280 D.setAsmLabel(AsmLabel.release());
1281 D.SetRangeEnd(Loc);
1282 }
1283
1284 MaybeParseGNUAttributes(D);
1285 return false;
1286}
1287
Douglas Gregor1426e532009-05-12 21:31:51 +00001288/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1289/// declarator'. This method parses the remainder of the declaration
1290/// (including any attributes or initializer, among other things) and
1291/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001292///
Reid Spencer5f016e22007-07-11 17:01:13 +00001293/// init-declarator: [C99 6.7]
1294/// declarator
1295/// declarator '=' initializer
1296/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1297/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001298/// [C++] declarator initializer[opt]
1299///
1300/// [C++] initializer:
1301/// [C++] '=' initializer-clause
1302/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001303/// [C++0x] '=' 'default' [TODO]
1304/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001305/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001306///
1307/// According to the standard grammar, =default and =delete are function
1308/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001309///
John McCalld226f652010-08-21 09:40:31 +00001310Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001311 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001312 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001313 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Richard Smithad762fc2011-04-14 22:09:26 +00001315 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1316}
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Richard Smithad762fc2011-04-14 22:09:26 +00001318Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1319 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001320 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001321 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001322 switch (TemplateInfo.Kind) {
1323 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001324 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001325 break;
1326
1327 case ParsedTemplateInfo::Template:
1328 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001330 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001331 TemplateInfo.TemplateParams->data(),
1332 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001333 D);
1334 break;
1335
1336 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001337 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001338 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001339 TemplateInfo.ExternLoc,
1340 TemplateInfo.TemplateLoc,
1341 D);
1342 if (ThisRes.isInvalid()) {
1343 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001344 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001345 }
1346
1347 ThisDecl = ThisRes.get();
1348 break;
1349 }
1350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Richard Smith34b41d92011-02-20 03:19:35 +00001352 bool TypeContainsAuto =
1353 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1354
Douglas Gregor1426e532009-05-12 21:31:51 +00001355 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001356 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001357 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001358 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001359 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001360 if (D.isFunctionDeclarator())
1361 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1362 << 1 /* delete */;
1363 else
1364 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001365 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001366 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001367 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1368 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001369 else
1370 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001371 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001372 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001373 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001374 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001375 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001376
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001377 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001378 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001379 cutOffParsing();
1380 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001381 }
1382
John McCall60d7b3a2010-08-24 06:29:42 +00001383 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001384
David Blaikie4e4d0842012-03-11 07:00:24 +00001385 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001386 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001387 ExitScope();
1388 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001389
Douglas Gregor1426e532009-05-12 21:31:51 +00001390 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001391 SkipUntil(tok::comma, true, true);
1392 Actions.ActOnInitializerError(ThisDecl);
1393 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001394 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1395 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001396 }
1397 } else if (Tok.is(tok::l_paren)) {
1398 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001399 BalancedDelimiterTracker T(*this, tok::l_paren);
1400 T.consumeOpen();
1401
Douglas Gregor1426e532009-05-12 21:31:51 +00001402 ExprVector Exprs(Actions);
1403 CommaLocsTy CommaLocs;
1404
David Blaikie4e4d0842012-03-11 07:00:24 +00001405 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001406 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001407 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001408 }
1409
Douglas Gregor1426e532009-05-12 21:31:51 +00001410 if (ParseExpressionList(Exprs, CommaLocs)) {
1411 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001412
David Blaikie4e4d0842012-03-11 07:00:24 +00001413 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001414 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001415 ExitScope();
1416 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001417 } else {
1418 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001419 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001420
1421 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1422 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001423
David Blaikie4e4d0842012-03-11 07:00:24 +00001424 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001425 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001426 ExitScope();
1427 }
1428
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001429 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1430 T.getCloseLocation(),
1431 move_arg(Exprs));
1432 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1433 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001434 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001435 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001436 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001437 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1438
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001439 if (D.getCXXScopeSpec().isSet()) {
1440 EnterScope(0);
1441 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1442 }
1443
1444 ExprResult Init(ParseBraceInitializer());
1445
1446 if (D.getCXXScopeSpec().isSet()) {
1447 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1448 ExitScope();
1449 }
1450
1451 if (Init.isInvalid()) {
1452 Actions.ActOnInitializerError(ThisDecl);
1453 } else
1454 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1455 /*DirectInit=*/true, TypeContainsAuto);
1456
Douglas Gregor1426e532009-05-12 21:31:51 +00001457 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001458 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001459 }
1460
Richard Smith483b9f32011-02-21 20:05:19 +00001461 Actions.FinalizeDeclaration(ThisDecl);
1462
Douglas Gregor1426e532009-05-12 21:31:51 +00001463 return ThisDecl;
1464}
1465
Reid Spencer5f016e22007-07-11 17:01:13 +00001466/// ParseSpecifierQualifierList
1467/// specifier-qualifier-list:
1468/// type-specifier specifier-qualifier-list[opt]
1469/// type-qualifier specifier-qualifier-list[opt]
1470/// [GNU] attributes specifier-qualifier-list[opt]
1471///
Richard Smith69730c12012-03-12 07:56:15 +00001472void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1473 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1475 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001476 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001477 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // Validate declspec for type-name.
1480 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001481 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1482 Diag(Tok, diag::err_expected_type);
1483 DS.SetTypeSpecError();
1484 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1485 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001486 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001487 if (!DS.hasTypeSpecifier())
1488 DS.SetTypeSpecError();
1489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 // Issue diagnostic and remove storage class if present.
1492 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1493 if (DS.getStorageClassSpecLoc().isValid())
1494 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1495 else
1496 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1497 DS.ClearStorageClassSpecs();
1498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 // Issue diagnostic and remove function specfier if present.
1501 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001502 if (DS.isInlineSpecified())
1503 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1504 if (DS.isVirtualSpecified())
1505 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1506 if (DS.isExplicitSpecified())
1507 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 DS.ClearFunctionSpecs();
1509 }
Richard Smith69730c12012-03-12 07:56:15 +00001510
1511 // Issue diagnostic and remove constexpr specfier if present.
1512 if (DS.isConstexprSpecified()) {
1513 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1514 DS.ClearConstexprSpec();
1515 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001516}
1517
Chris Lattnerc199ab32009-04-12 20:42:31 +00001518/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1519/// specified token is valid after the identifier in a declarator which
1520/// immediately follows the declspec. For example, these things are valid:
1521///
1522/// int x [ 4]; // direct-declarator
1523/// int x ( int y); // direct-declarator
1524/// int(int x ) // direct-declarator
1525/// int x ; // simple-declaration
1526/// int x = 17; // init-declarator-list
1527/// int x , y; // init-declarator-list
1528/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001529/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001530/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001531///
1532/// This is not, because 'x' does not immediately follow the declspec (though
1533/// ')' happens to be valid anyway).
1534/// int (x)
1535///
1536static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1537 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1538 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001539 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001540}
1541
Chris Lattnere40c2952009-04-14 21:34:55 +00001542
1543/// ParseImplicitInt - This method is called when we have an non-typename
1544/// identifier in a declspec (which normally terminates the decl spec) when
1545/// the declspec has no type specifier. In this case, the declspec is either
1546/// malformed or is "implicit int" (in K&R and C89).
1547///
1548/// This method handles diagnosing this prettily and returns false if the
1549/// declspec is done being processed. If it recovers and thinks there may be
1550/// other pieces of declspec after it, it returns true.
1551///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001552bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001553 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001554 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001555 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattnere40c2952009-04-14 21:34:55 +00001557 SourceLocation Loc = Tok.getLocation();
1558 // If we see an identifier that is not a type name, we normally would
1559 // parse it as the identifer being declared. However, when a typename
1560 // is typo'd or the definition is not included, this will incorrectly
1561 // parse the typename as the identifier name and fall over misparsing
1562 // later parts of the diagnostic.
1563 //
1564 // As such, we try to do some look-ahead in cases where this would
1565 // otherwise be an "implicit-int" case to see if this is invalid. For
1566 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1567 // an identifier with implicit int, we'd get a parse error because the
1568 // next token is obviously invalid for a type. Parse these as a case
1569 // with an invalid type specifier.
1570 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Chris Lattnere40c2952009-04-14 21:34:55 +00001572 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001573 // error, do lookahead to try to do better recovery. This never applies within
1574 // a type specifier.
1575 // FIXME: Don't bail out here in languages with no implicit int (like
1576 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1577 // type or typo than a use of implicit int.
1578 if (DSC != DSC_type_specifier &&
1579 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001580 // If this token is valid for implicit int, e.g. "static x = 4", then
1581 // we just avoid eating the identifier, so it will be parsed as the
1582 // identifier in the declarator.
1583 return false;
1584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Chris Lattnere40c2952009-04-14 21:34:55 +00001586 // Otherwise, if we don't consume this token, we are going to emit an
1587 // error anyway. Try to recover from various common problems. Check
1588 // to see if this was a reference to a tag name without a tag specified.
1589 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001590 //
1591 // C++ doesn't need this, and isTagName doesn't take SS.
1592 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001593 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001594 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregor23c94db2010-07-02 17:43:08 +00001596 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001597 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001598 case DeclSpec::TST_enum:
1599 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1600 case DeclSpec::TST_union:
1601 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1602 case DeclSpec::TST_struct:
1603 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1604 case DeclSpec::TST_class:
1605 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Chris Lattnerf4382f52009-04-14 22:17:06 +00001608 if (TagName) {
1609 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001610 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001611 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattnerf4382f52009-04-14 22:17:06 +00001613 // Parse this as a tag as if the missing tag were present.
1614 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001615 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001616 else
Richard Smith69730c12012-03-12 07:56:15 +00001617 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1618 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001619 return true;
1620 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Douglas Gregora786fdb2009-10-13 23:27:22 +00001623 // This is almost certainly an invalid type name. Let the action emit a
1624 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001625 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001626 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001627 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001628 // The action emitted a diagnostic, so we don't have to.
1629 if (T) {
1630 // The action has suggested that the type T could be used. Set that as
1631 // the type in the declaration specifiers, consume the would-be type
1632 // name token, and we're done.
1633 const char *PrevSpec;
1634 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001635 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001636 DS.SetRangeEnd(Tok.getLocation());
1637 ConsumeToken();
1638
1639 // There may be other declaration specifiers after this.
1640 return true;
1641 }
1642
1643 // Fall through; the action had no suggestion for us.
1644 } else {
1645 // The action did not emit a diagnostic, so emit one now.
1646 SourceRange R;
1647 if (SS) R = SS->getRange();
1648 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregora786fdb2009-10-13 23:27:22 +00001651 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001652 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001653 DS.SetRangeEnd(Tok.getLocation());
1654 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Chris Lattnere40c2952009-04-14 21:34:55 +00001656 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1657 // avoid rippling error messages on subsequent uses of the same type,
1658 // could be useful if #include was forgotten.
1659 return false;
1660}
1661
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001662/// \brief Determine the declaration specifier context from the declarator
1663/// context.
1664///
1665/// \param Context the declarator context, which is one of the
1666/// Declarator::TheContext enumerator values.
1667Parser::DeclSpecContext
1668Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1669 if (Context == Declarator::MemberContext)
1670 return DSC_class;
1671 if (Context == Declarator::FileContext)
1672 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001673 if (Context == Declarator::TrailingReturnContext)
1674 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001675 return DSC_normal;
1676}
1677
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001678/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1679///
1680/// FIXME: Simply returns an alignof() expression if the argument is a
1681/// type. Ideally, the type should be propagated directly into Sema.
1682///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001683/// [C11] type-id
1684/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001685/// [C++0x] type-id ...[opt]
1686/// [C++0x] assignment-expression ...[opt]
1687ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1688 SourceLocation &EllipsisLoc) {
1689 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001690 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001691 SourceLocation TypeLoc = Tok.getLocation();
1692 ParsedType Ty = ParseTypeName().get();
1693 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001694 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1695 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001696 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001697 ER = ParseConstantExpression();
1698
David Blaikie4e4d0842012-03-11 07:00:24 +00001699 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001700 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001701
1702 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001703}
1704
1705/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1706/// attribute to Attrs.
1707///
1708/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001709/// [C11] '_Alignas' '(' type-id ')'
1710/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001711/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1712/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001713void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1714 SourceLocation *endLoc) {
1715 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1716 "Not an alignment-specifier!");
1717
1718 SourceLocation KWLoc = Tok.getLocation();
1719 ConsumeToken();
1720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001721 BalancedDelimiterTracker T(*this, tok::l_paren);
1722 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001723 return;
1724
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001725 SourceLocation EllipsisLoc;
1726 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001727 if (ArgExpr.isInvalid()) {
1728 SkipUntil(tok::r_paren);
1729 return;
1730 }
1731
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001732 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001733 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001734 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001735
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001736 // FIXME: Handle pack-expansions here.
1737 if (EllipsisLoc.isValid()) {
1738 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1739 return;
1740 }
1741
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001742 ExprVector ArgExprs(Actions);
1743 ArgExprs.push_back(ArgExpr.release());
1744 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001745 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001746}
1747
Reid Spencer5f016e22007-07-11 17:01:13 +00001748/// ParseDeclarationSpecifiers
1749/// declaration-specifiers: [C99 6.7]
1750/// storage-class-specifier declaration-specifiers[opt]
1751/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001752/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001753/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001754/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001755/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001756///
1757/// storage-class-specifier: [C99 6.7.1]
1758/// 'typedef'
1759/// 'extern'
1760/// 'static'
1761/// 'auto'
1762/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001763/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001764/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001765/// function-specifier: [C99 6.7.4]
1766/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001767/// [C++] 'virtual'
1768/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001769/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001770/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001771/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001772
Reid Spencer5f016e22007-07-11 17:01:13 +00001773///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001774void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001775 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001776 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001777 DeclSpecContext DSContext,
1778 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001779 if (DS.getSourceRange().isInvalid()) {
1780 DS.SetRangeStart(Tok.getLocation());
1781 DS.SetRangeEnd(Tok.getLocation());
1782 }
1783
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001784 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001786 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001788 unsigned DiagID = 0;
1789
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001791
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001793 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001794 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001795 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1796 MaybeParseCXX0XAttributes(DS.getAttributes());
1797
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 // If this is not a declaration specifier token, we're done reading decl
1799 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001800 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001803 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001804 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001805 if (DS.hasTypeSpecifier()) {
1806 bool AllowNonIdentifiers
1807 = (getCurScope()->getFlags() & (Scope::ControlScope |
1808 Scope::BlockScope |
1809 Scope::TemplateParamScope |
1810 Scope::FunctionPrototypeScope |
1811 Scope::AtCatchScope)) == 0;
1812 bool AllowNestedNameSpecifiers
1813 = DSContext == DSC_top_level ||
1814 (DSContext == DSC_class && DS.isFriendSpecified());
1815
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001816 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1817 AllowNonIdentifiers,
1818 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001819 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001820 }
1821
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001822 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1823 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1824 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001825 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1826 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001827 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001828 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001829 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001830 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001831
1832 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001833 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001834 }
1835
Chris Lattner5e02c472009-01-05 00:07:25 +00001836 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001837 // C++ scope specifier. Annotate and loop, or bail out on error.
1838 if (TryAnnotateCXXScopeToken(true)) {
1839 if (!DS.hasTypeSpecifier())
1840 DS.SetTypeSpecError();
1841 goto DoneWithDeclSpec;
1842 }
John McCall2e0a7152010-03-01 18:20:46 +00001843 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1844 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001845 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001846
1847 case tok::annot_cxxscope: {
1848 if (DS.hasTypeSpecifier())
1849 goto DoneWithDeclSpec;
1850
John McCallaa87d332009-12-12 11:40:51 +00001851 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001852 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1853 Tok.getAnnotationRange(),
1854 SS);
John McCallaa87d332009-12-12 11:40:51 +00001855
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001856 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001857 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001858 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001859 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001860 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001861 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001862
1863 // C++ [class.qual]p2:
1864 // In a lookup in which the constructor is an acceptable lookup
1865 // result and the nested-name-specifier nominates a class C:
1866 //
1867 // - if the name specified after the
1868 // nested-name-specifier, when looked up in C, is the
1869 // injected-class-name of C (Clause 9), or
1870 //
1871 // - if the name specified after the nested-name-specifier
1872 // is the same as the identifier or the
1873 // simple-template-id's template-name in the last
1874 // component of the nested-name-specifier,
1875 //
1876 // the name is instead considered to name the constructor of
1877 // class C.
1878 //
1879 // Thus, if the template-name is actually the constructor
1880 // name, then the code is ill-formed; this interpretation is
1881 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001882 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001883 if ((DSContext == DSC_top_level ||
1884 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1885 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001886 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001887 if (isConstructorDeclarator()) {
1888 // The user meant this to be an out-of-line constructor
1889 // definition, but template arguments are not allowed
1890 // there. Just allow this as a constructor; we'll
1891 // complain about it later.
1892 goto DoneWithDeclSpec;
1893 }
1894
1895 // The user meant this to name a type, but it actually names
1896 // a constructor with some extraneous template
1897 // arguments. Complain, then parse it as a type as the user
1898 // intended.
1899 Diag(TemplateId->TemplateNameLoc,
1900 diag::err_out_of_line_template_id_names_constructor)
1901 << TemplateId->Name;
1902 }
1903
John McCallaa87d332009-12-12 11:40:51 +00001904 DS.getTypeSpecScope() = SS;
1905 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001906 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001907 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001908 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001909 continue;
1910 }
1911
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001912 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001913 DS.getTypeSpecScope() = SS;
1914 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001915 if (Tok.getAnnotationValue()) {
1916 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001917 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1918 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001919 PrevSpec, DiagID, T);
1920 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001921 else
1922 DS.SetTypeSpecError();
1923 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1924 ConsumeToken(); // The typename
1925 }
1926
Douglas Gregor9135c722009-03-25 15:40:00 +00001927 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001928 goto DoneWithDeclSpec;
1929
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001930 // If we're in a context where the identifier could be a class name,
1931 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001932 if ((DSContext == DSC_top_level ||
1933 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001934 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001935 &SS)) {
1936 if (isConstructorDeclarator())
1937 goto DoneWithDeclSpec;
1938
1939 // As noted in C++ [class.qual]p2 (cited above), when the name
1940 // of the class is qualified in a context where it could name
1941 // a constructor, its a constructor name. However, we've
1942 // looked at the declarator, and the user probably meant this
1943 // to be a type. Complain that it isn't supposed to be treated
1944 // as a type, then proceed to parse it as a type.
1945 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1946 << Next.getIdentifierInfo();
1947 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001948
John McCallb3d87482010-08-24 05:47:05 +00001949 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1950 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001951 getCurScope(), &SS,
1952 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001953 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001954 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001955
Chris Lattnerf4382f52009-04-14 22:17:06 +00001956 // If the referenced identifier is not a type, then this declspec is
1957 // erroneous: We already checked about that it has no type specifier, and
1958 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001959 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001960 if (TypeRep == 0) {
1961 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001962 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001963 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001964 }
Mike Stump1eb44332009-09-09 15:08:12 +00001965
John McCallaa87d332009-12-12 11:40:51 +00001966 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001967 ConsumeToken(); // The C++ scope.
1968
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001970 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001971 if (isInvalid)
1972 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001974 DS.SetRangeEnd(Tok.getLocation());
1975 ConsumeToken(); // The typename.
1976
1977 continue;
1978 }
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Chris Lattner80d0c892009-01-21 19:48:37 +00001980 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001981 if (Tok.getAnnotationValue()) {
1982 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001984 DiagID, T);
1985 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001986 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001987
1988 if (isInvalid)
1989 break;
1990
Chris Lattner80d0c892009-01-21 19:48:37 +00001991 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1992 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Chris Lattner80d0c892009-01-21 19:48:37 +00001994 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1995 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001996 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001997 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001998 ParseObjCProtocolQualifiers(DS);
1999
Chris Lattner80d0c892009-01-21 19:48:37 +00002000 continue;
2001 }
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Douglas Gregorbfad9152011-04-28 15:48:45 +00002003 case tok::kw___is_signed:
2004 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2005 // typically treats it as a trait. If we see __is_signed as it appears
2006 // in libstdc++, e.g.,
2007 //
2008 // static const bool __is_signed;
2009 //
2010 // then treat __is_signed as an identifier rather than as a keyword.
2011 if (DS.getTypeSpecType() == TST_bool &&
2012 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2013 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2014 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2015 Tok.setKind(tok::identifier);
2016 }
2017
2018 // We're done with the declaration-specifiers.
2019 goto DoneWithDeclSpec;
2020
Chris Lattner3bd934a2008-07-26 01:18:38 +00002021 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002022 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002023 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002024 // In C++, check to see if this is a scope specifier like foo::bar::, if
2025 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002026 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002027 if (TryAnnotateCXXScopeToken(true)) {
2028 if (!DS.hasTypeSpecifier())
2029 DS.SetTypeSpecError();
2030 goto DoneWithDeclSpec;
2031 }
2032 if (!Tok.is(tok::identifier))
2033 continue;
2034 }
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Chris Lattner3bd934a2008-07-26 01:18:38 +00002036 // This identifier can only be a typedef name if we haven't already seen
2037 // a type-specifier. Without this check we misparse:
2038 // typedef int X; struct Y { short X; }; as 'short int'.
2039 if (DS.hasTypeSpecifier())
2040 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002041
John Thompson82287d12010-02-05 00:12:22 +00002042 // Check for need to substitute AltiVec keyword tokens.
2043 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2044 break;
2045
John McCallb3d87482010-08-24 05:47:05 +00002046 ParsedType TypeRep =
2047 Actions.getTypeName(*Tok.getIdentifierInfo(),
2048 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002049
Chris Lattnerc199ab32009-04-12 20:42:31 +00002050 // If this is not a typedef name, don't parse it as part of the declspec,
2051 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002052 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002053 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002054 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002055 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002056
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002057 // If we're in a context where the identifier could be a class name,
2058 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002059 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002060 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002061 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002062 goto DoneWithDeclSpec;
2063
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002065 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002066 if (isInvalid)
2067 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Chris Lattner3bd934a2008-07-26 01:18:38 +00002069 DS.SetRangeEnd(Tok.getLocation());
2070 ConsumeToken(); // The identifier
2071
2072 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2073 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002074 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002075 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002076 ParseObjCProtocolQualifiers(DS);
2077
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002078 // Need to support trailing type qualifiers (e.g. "id<p> const").
2079 // If a type specifier follows, it will be diagnosed elsewhere.
2080 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002081 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002082
2083 // type-name
2084 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002085 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002086 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002087 // This template-id does not refer to a type name, so we're
2088 // done with the type-specifiers.
2089 goto DoneWithDeclSpec;
2090 }
2091
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002092 // If we're in a context where the template-id could be a
2093 // constructor name or specialization, check whether this is a
2094 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002095 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002096 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002097 isConstructorDeclarator())
2098 goto DoneWithDeclSpec;
2099
Douglas Gregor39a8de12009-02-25 19:37:18 +00002100 // Turn the template-id annotation token into a type annotation
2101 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002102 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002103 continue;
2104 }
2105
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 // GNU attributes support.
2107 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002108 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002110
2111 // Microsoft declspec support.
2112 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002113 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002114 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Steve Naroff239f0732008-12-25 14:16:32 +00002116 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002117 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002118 // FIXME: Add handling here!
2119 break;
2120
2121 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002122 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002123 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002124 case tok::kw___cdecl:
2125 case tok::kw___stdcall:
2126 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002127 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002128 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002129 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002130 continue;
2131
Dawn Perchik52fc3142010-09-03 01:29:35 +00002132 // Borland single token adornments.
2133 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002134 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002135 continue;
2136
Peter Collingbournef315fa82011-02-14 01:42:53 +00002137 // OpenCL single token adornments.
2138 case tok::kw___kernel:
2139 ParseOpenCLAttributes(DS.getAttributes());
2140 continue;
2141
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 // storage-class-specifier
2143 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002144 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2145 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 break;
2147 case tok::kw_extern:
2148 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002149 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002150 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2151 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002152 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002153 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002154 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2155 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002156 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 case tok::kw_static:
2158 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002159 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002160 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2161 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002162 break;
2163 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002164 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002165 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002166 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2167 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002168 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002169 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002170 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002171 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2173 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002174 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002175 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2176 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 break;
2178 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002179 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2180 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002182 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002183 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2184 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002185 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002186 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002187 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002188 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 // function-specifier
2191 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002192 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002194 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002195 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002196 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002197 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002198 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002199 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002200
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002201 // alignment-specifier
2202 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002203 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002204 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002205 ParseAlignmentSpecifier(DS.getAttributes());
2206 continue;
2207
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002208 // friend
2209 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002210 if (DSContext == DSC_class)
2211 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2212 else {
2213 PrevSpec = ""; // not actually used by the diagnostic
2214 DiagID = diag::err_friend_invalid_in_context;
2215 isInvalid = true;
2216 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002217 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Douglas Gregor8d267c52011-09-09 02:06:17 +00002219 // Modules
2220 case tok::kw___module_private__:
2221 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2222 break;
2223
Sebastian Redl2ac67232009-11-05 15:47:02 +00002224 // constexpr
2225 case tok::kw_constexpr:
2226 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2227 break;
2228
Chris Lattner80d0c892009-01-21 19:48:37 +00002229 // type-specifier
2230 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002231 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2232 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002233 break;
2234 case tok::kw_long:
2235 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002236 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2237 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002238 else
John McCallfec54012009-08-03 20:12:06 +00002239 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2240 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002241 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002242 case tok::kw___int64:
2243 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2244 DiagID);
2245 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002246 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002247 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2248 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 break;
2250 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2252 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002253 break;
2254 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002255 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2256 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002257 break;
2258 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002259 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2260 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002261 break;
2262 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2264 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002265 break;
2266 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2268 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 break;
2270 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2272 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002273 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002274 case tok::kw___int128:
2275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2276 DiagID);
2277 break;
2278 case tok::kw_half:
2279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2280 DiagID);
2281 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002282 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002283 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2284 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002285 break;
2286 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2288 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002289 break;
2290 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2292 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002293 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002294 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2296 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002297 break;
2298 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2300 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002301 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002302 case tok::kw_bool:
2303 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002304 if (Tok.is(tok::kw_bool) &&
2305 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2306 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2307 PrevSpec = ""; // Not used by the diagnostic.
2308 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002309 // For better error recovery.
2310 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002311 isInvalid = true;
2312 } else {
2313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2314 DiagID);
2315 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002316 break;
2317 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2319 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002320 break;
2321 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2323 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002324 break;
2325 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2327 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002328 break;
John Thompson82287d12010-02-05 00:12:22 +00002329 case tok::kw___vector:
2330 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2331 break;
2332 case tok::kw___pixel:
2333 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2334 break;
John McCalla5fc4722011-04-09 22:50:59 +00002335 case tok::kw___unknown_anytype:
2336 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2337 PrevSpec, DiagID);
2338 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002339
2340 // class-specifier:
2341 case tok::kw_class:
2342 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002343 case tok::kw_union: {
2344 tok::TokenKind Kind = Tok.getKind();
2345 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002346 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2347 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002348 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002349 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002350
2351 // enum-specifier:
2352 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002353 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002354 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002355 continue;
2356
2357 // cv-qualifier:
2358 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002359 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002360 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002361 break;
2362 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002363 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002364 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002365 break;
2366 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002367 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002368 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002369 break;
2370
Douglas Gregord57959a2009-03-27 23:10:48 +00002371 // C++ typename-specifier:
2372 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002373 if (TryAnnotateTypeOrScopeToken()) {
2374 DS.SetTypeSpecError();
2375 goto DoneWithDeclSpec;
2376 }
2377 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002378 continue;
2379 break;
2380
Chris Lattner80d0c892009-01-21 19:48:37 +00002381 // GNU typeof support.
2382 case tok::kw_typeof:
2383 ParseTypeofSpecifier(DS);
2384 continue;
2385
David Blaikie42d6d0c2011-12-04 05:04:18 +00002386 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002387 ParseDecltypeSpecifier(DS);
2388 continue;
2389
Sean Huntdb5d44b2011-05-19 05:37:45 +00002390 case tok::kw___underlying_type:
2391 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002392 continue;
2393
2394 case tok::kw__Atomic:
2395 ParseAtomicSpecifier(DS);
2396 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002397
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002398 // OpenCL qualifiers:
2399 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002400 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002401 goto DoneWithDeclSpec;
2402 case tok::kw___private:
2403 case tok::kw___global:
2404 case tok::kw___local:
2405 case tok::kw___constant:
2406 case tok::kw___read_only:
2407 case tok::kw___write_only:
2408 case tok::kw___read_write:
2409 ParseOpenCLQualifiers(DS);
2410 break;
2411
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002412 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002413 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002414 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2415 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002416 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002417 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Douglas Gregor46f936e2010-11-19 17:10:50 +00002419 if (!ParseObjCProtocolQualifiers(DS))
2420 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2421 << FixItHint::CreateInsertion(Loc, "id")
2422 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002423
2424 // Need to support trailing type qualifiers (e.g. "id<p> const").
2425 // If a type specifier follows, it will be diagnosed elsewhere.
2426 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002427 }
John McCallfec54012009-08-03 20:12:06 +00002428 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 if (isInvalid) {
2430 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002431 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002432
2433 if (DiagID == diag::ext_duplicate_declspec)
2434 Diag(Tok, DiagID)
2435 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2436 else
2437 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002438 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002439
Chris Lattner81c018d2008-03-13 06:29:04 +00002440 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002441 if (DiagID != diag::err_bool_redeclaration)
2442 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002443 }
2444}
Douglas Gregoradcac882008-12-01 23:54:00 +00002445
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002446/// ParseStructDeclaration - Parse a struct declaration without the terminating
2447/// semicolon.
2448///
Reid Spencer5f016e22007-07-11 17:01:13 +00002449/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002450/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002451/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002452/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002453/// struct-declarator-list:
2454/// struct-declarator
2455/// struct-declarator-list ',' struct-declarator
2456/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2457/// struct-declarator:
2458/// declarator
2459/// [GNU] declarator attributes[opt]
2460/// declarator[opt] ':' constant-expression
2461/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2462///
Chris Lattnere1359422008-04-10 06:46:29 +00002463void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002464ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002465
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002466 if (Tok.is(tok::kw___extension__)) {
2467 // __extension__ silences extension warnings in the subexpression.
2468 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002469 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002470 return ParseStructDeclaration(DS, Fields);
2471 }
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Steve Naroff28a7ca82007-08-20 22:28:22 +00002473 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002474 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002475
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002476 // If there are no declarators, this is a free-standing declaration
2477 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002478 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002479 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002480 return;
2481 }
2482
2483 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002484 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002485 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002486 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002487 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002488 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002489 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002490
2491 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002492 if (!FirstDeclarator)
2493 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Steve Naroff28a7ca82007-08-20 22:28:22 +00002495 /// struct-declarator: declarator
2496 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002497 if (Tok.isNot(tok::colon)) {
2498 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2499 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002500 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002501 }
Mike Stump1eb44332009-09-09 15:08:12 +00002502
Chris Lattner04d66662007-10-09 17:33:22 +00002503 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002504 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002505 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002506 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002507 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002508 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002509 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002510 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002511
Steve Naroff28a7ca82007-08-20 22:28:22 +00002512 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002513 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002514
John McCallbdd563e2009-11-03 02:38:08 +00002515 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002516 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002517 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002518
Steve Naroff28a7ca82007-08-20 22:28:22 +00002519 // If we don't have a comma, it is either the end of the list (a ';')
2520 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002521 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002522 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002523
Steve Naroff28a7ca82007-08-20 22:28:22 +00002524 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002525 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002526
John McCallbdd563e2009-11-03 02:38:08 +00002527 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002528 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002529}
2530
2531/// ParseStructUnionBody
2532/// struct-contents:
2533/// struct-declaration-list
2534/// [EXT] empty
2535/// [GNU] "struct-declaration-list" without terminatoring ';'
2536/// struct-declaration-list:
2537/// struct-declaration
2538/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002539/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002540///
Reid Spencer5f016e22007-07-11 17:01:13 +00002541void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002542 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002543 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2544 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002546 BalancedDelimiterTracker T(*this, tok::l_brace);
2547 if (T.consumeOpen())
2548 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002550 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002551 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002552
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2554 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002555 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002556 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2557 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2558 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002559
Chris Lattner5f9e2722011-07-23 10:55:15 +00002560 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002561
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002563 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002564 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Reid Spencer5f016e22007-07-11 17:01:13 +00002566 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002567 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002568 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002569 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002570 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 ConsumeToken();
2572 continue;
2573 }
Chris Lattnere1359422008-04-10 06:46:29 +00002574
2575 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002576 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002577
John McCallbdd563e2009-11-03 02:38:08 +00002578 if (!Tok.is(tok::at)) {
2579 struct CFieldCallback : FieldCallback {
2580 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002581 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002582 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002583
John McCalld226f652010-08-21 09:40:31 +00002584 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002585 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002586 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2587
John McCalld226f652010-08-21 09:40:31 +00002588 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002589 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002590 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002591 FD.D.getDeclSpec().getSourceRange().getBegin(),
2592 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002593 FieldDecls.push_back(Field);
2594 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002595 }
John McCallbdd563e2009-11-03 02:38:08 +00002596 } Callback(*this, TagDecl, FieldDecls);
2597
2598 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002599 } else { // Handle @defs
2600 ConsumeToken();
2601 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2602 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002603 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002604 continue;
2605 }
2606 ConsumeToken();
2607 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2608 if (!Tok.is(tok::identifier)) {
2609 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002610 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002611 continue;
2612 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002613 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002614 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002615 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002616 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2617 ConsumeToken();
2618 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002619 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002620
Chris Lattner04d66662007-10-09 17:33:22 +00002621 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002622 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002623 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002624 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 break;
2626 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002627 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2628 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002630 // If we stopped at a ';', eat it.
2631 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002632 }
2633 }
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002635 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002636
John McCall0b7e6782011-03-24 11:26:52 +00002637 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002638 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002639 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002640
Douglas Gregor23c94db2010-07-02 17:43:08 +00002641 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002642 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002643 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002644 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002645 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002646 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2647 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002648}
2649
Reid Spencer5f016e22007-07-11 17:01:13 +00002650/// ParseEnumSpecifier
2651/// enum-specifier: [C99 6.7.2.2]
2652/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002653///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002654/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2655/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002656/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2657/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002658/// 'enum' identifier
2659/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002660///
Richard Smith1af83c42012-03-23 03:33:32 +00002661/// [C++11] enum-head '{' enumerator-list[opt] '}'
2662/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002663///
Richard Smith1af83c42012-03-23 03:33:32 +00002664/// enum-head: [C++11]
2665/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2666/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2667/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002668///
Richard Smith1af83c42012-03-23 03:33:32 +00002669/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002670/// 'enum'
2671/// 'enum' 'class'
2672/// 'enum' 'struct'
2673///
Richard Smith1af83c42012-03-23 03:33:32 +00002674/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002675/// ':' type-specifier-seq
2676///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002677/// [C++] elaborated-type-specifier:
2678/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2679///
Chris Lattner4c97d762009-04-12 21:49:30 +00002680void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002681 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002682 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002684 if (Tok.is(tok::code_completion)) {
2685 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002686 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002687 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002688 }
John McCall57c13002011-07-06 05:58:41 +00002689
Richard Smithbdad7a22012-01-10 01:33:14 +00002690 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002691 bool IsScopedUsingClassTag = false;
2692
David Blaikie4e4d0842012-03-11 07:00:24 +00002693 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002694 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002695 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002696 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002697 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002698 }
Richard Smith1af83c42012-03-23 03:33:32 +00002699
2700 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2701 // used to specify explicit instantiations. We extend this to also cover
2702 // explicit specializations.
2703 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2704 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2705 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2706
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002707 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002708 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002709 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002710
Aaron Ballman6454a022012-03-01 04:09:28 +00002711 // If declspecs exist after tag, parse them.
2712 while (Tok.is(tok::kw___declspec))
2713 ParseMicrosoftDeclSpec(attrs);
2714
Richard Smith7796eb52012-03-12 08:56:40 +00002715 // Enum definitions should not be parsed in a trailing-return-type.
2716 bool AllowDeclaration = DSC != DSC_trailing;
2717
2718 bool AllowFixedUnderlyingType = AllowDeclaration &&
2719 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2720 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002721
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002722 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002723 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002724 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2725 // if a fixed underlying type is allowed.
2726 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2727
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002728 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2729 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002730 return;
2731
2732 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002733 Diag(Tok, diag::err_expected_ident);
2734 if (Tok.isNot(tok::l_brace)) {
2735 // Has no name and is not a definition.
2736 // Skip the rest of this declarator, up until the comma or semicolon.
2737 SkipUntil(tok::comma, true);
2738 return;
2739 }
2740 }
2741 }
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002743 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002744 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002745 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002746 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002748 // Skip the rest of this declarator, up until the comma or semicolon.
2749 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002750 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002753 // If an identifier is present, consume and remember it.
2754 IdentifierInfo *Name = 0;
2755 SourceLocation NameLoc;
2756 if (Tok.is(tok::identifier)) {
2757 Name = Tok.getIdentifierInfo();
2758 NameLoc = ConsumeToken();
2759 }
Mike Stump1eb44332009-09-09 15:08:12 +00002760
Richard Smithbdad7a22012-01-10 01:33:14 +00002761 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002762 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2763 // declaration of a scoped enumeration.
2764 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002765 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002766 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002767 }
2768
Richard Smith1af83c42012-03-23 03:33:32 +00002769 // Stop suppressing access control now we've parsed the enum name.
2770 SuppressAccess.done();
2771
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002772 TypeResult BaseType;
2773
Douglas Gregora61b3e72010-12-01 17:42:47 +00002774 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002775 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002776 bool PossibleBitfield = false;
2777 if (getCurScope()->getFlags() & Scope::ClassScope) {
2778 // If we're in class scope, this can either be an enum declaration with
2779 // an underlying type, or a declaration of a bitfield member. We try to
2780 // use a simple disambiguation scheme first to catch the common cases
2781 // (integer literal, sizeof); if it's still ambiguous, we then consider
2782 // anything that's a simple-type-specifier followed by '(' as an
2783 // expression. This suffices because function types are not valid
2784 // underlying types anyway.
2785 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2786 // If the next token starts an expression, we know we're parsing a
2787 // bit-field. This is the common case.
2788 if (TPR == TPResult::True())
2789 PossibleBitfield = true;
2790 // If the next token starts a type-specifier-seq, it may be either a
2791 // a fixed underlying type or the start of a function-style cast in C++;
2792 // lookahead one more token to see if it's obvious that we have a
2793 // fixed underlying type.
2794 else if (TPR == TPResult::False() &&
2795 GetLookAheadToken(2).getKind() == tok::semi) {
2796 // Consume the ':'.
2797 ConsumeToken();
2798 } else {
2799 // We have the start of a type-specifier-seq, so we have to perform
2800 // tentative parsing to determine whether we have an expression or a
2801 // type.
2802 TentativeParsingAction TPA(*this);
2803
2804 // Consume the ':'.
2805 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002806
2807 // If we see a type specifier followed by an open-brace, we have an
2808 // ambiguity between an underlying type and a C++11 braced
2809 // function-style cast. Resolve this by always treating it as an
2810 // underlying type.
2811 // FIXME: The standard is not entirely clear on how to disambiguate in
2812 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002813 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002814 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002815 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002816 // We'll parse this as a bitfield later.
2817 PossibleBitfield = true;
2818 TPA.Revert();
2819 } else {
2820 // We have a type-specifier-seq.
2821 TPA.Commit();
2822 }
2823 }
2824 } else {
2825 // Consume the ':'.
2826 ConsumeToken();
2827 }
2828
2829 if (!PossibleBitfield) {
2830 SourceRange Range;
2831 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002832
David Blaikie4e4d0842012-03-11 07:00:24 +00002833 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002834 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2835 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002836 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002837 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002838 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002839 }
2840
Richard Smithbdad7a22012-01-10 01:33:14 +00002841 // There are four options here. If we have 'friend enum foo;' then this is a
2842 // friend declaration, and cannot have an accompanying definition. If we have
2843 // 'enum foo;', then this is a forward declaration. If we have
2844 // 'enum foo {...' then this is a definition. Otherwise we have something
2845 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002846 //
2847 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2848 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2849 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2850 //
John McCallf312b1e2010-08-26 23:41:50 +00002851 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002852 if (DS.isFriendSpecified())
2853 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002854 else if (!AllowDeclaration)
2855 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002856 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002857 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002858 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002859 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002860 else
John McCallf312b1e2010-08-26 23:41:50 +00002861 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002862
2863 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002864 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002865 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002866 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2867 // Skip the rest of this declarator, up until the comma or semicolon.
2868 Diag(Tok, diag::err_enum_template);
2869 SkipUntil(tok::comma, true);
2870 return;
2871 }
2872
2873 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2874 // Enumerations can't be explicitly instantiated.
2875 DS.SetTypeSpecError();
2876 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2877 return;
2878 }
2879
2880 assert(TemplateInfo.TemplateParams && "no template parameters");
2881 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2882 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002883 }
Richard Smith1af83c42012-03-23 03:33:32 +00002884
Douglas Gregorb9075602011-02-22 02:55:24 +00002885 if (!Name && TUK != Sema::TUK_Definition) {
2886 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002887
Douglas Gregorb9075602011-02-22 02:55:24 +00002888 // Skip the rest of this declarator, up until the comma or semicolon.
2889 SkipUntil(tok::comma, true);
2890 return;
2891 }
Richard Smith1af83c42012-03-23 03:33:32 +00002892
Douglas Gregor402abb52009-05-28 23:31:59 +00002893 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002894 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002895 const char *PrevSpec = 0;
2896 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002897 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002898 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002899 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002900 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002901 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002902
Douglas Gregor48c89f42010-04-24 16:38:41 +00002903 if (IsDependent) {
2904 // This enum has a dependent nested-name-specifier. Handle it as a
2905 // dependent tag.
2906 if (!Name) {
2907 DS.SetTypeSpecError();
2908 Diag(Tok, diag::err_expected_type_name_after_typename);
2909 return;
2910 }
2911
Douglas Gregor23c94db2010-07-02 17:43:08 +00002912 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002913 TUK, SS, Name, StartLoc,
2914 NameLoc);
2915 if (Type.isInvalid()) {
2916 DS.SetTypeSpecError();
2917 return;
2918 }
2919
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002920 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2921 NameLoc.isValid() ? NameLoc : StartLoc,
2922 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002923 Diag(StartLoc, DiagID) << PrevSpec;
2924
2925 return;
2926 }
Mike Stump1eb44332009-09-09 15:08:12 +00002927
John McCalld226f652010-08-21 09:40:31 +00002928 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002929 // The action failed to produce an enumeration tag. If this is a
2930 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002931 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002932 ConsumeBrace();
2933 SkipUntil(tok::r_brace);
2934 }
2935
2936 DS.SetTypeSpecError();
2937 return;
2938 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002939
Richard Smith7796eb52012-03-12 08:56:40 +00002940 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002941 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002942 Diag(Tok, diag::err_friend_decl_defines_type)
2943 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00002944 ConsumeBrace();
2945 SkipUntil(tok::r_brace);
2946 } else {
2947 ParseEnumBody(StartLoc, TagDecl);
2948 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002949 }
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002951 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2952 NameLoc.isValid() ? NameLoc : StartLoc,
2953 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002954 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002955}
2956
2957/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2958/// enumerator-list:
2959/// enumerator
2960/// enumerator-list ',' enumerator
2961/// enumerator:
2962/// enumeration-constant
2963/// enumeration-constant '=' constant-expression
2964/// enumeration-constant:
2965/// identifier
2966///
John McCalld226f652010-08-21 09:40:31 +00002967void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002968 // Enter the scope of the enum body and start the definition.
2969 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002970 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002971
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002972 BalancedDelimiterTracker T(*this, tok::l_brace);
2973 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002974
Chris Lattner7946dd32007-08-27 17:24:30 +00002975 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002976 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002977 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Chris Lattner5f9e2722011-07-23 10:55:15 +00002979 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002980
John McCalld226f652010-08-21 09:40:31 +00002981 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Reid Spencer5f016e22007-07-11 17:01:13 +00002983 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002984 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002985 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2986 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002987
John McCall5b629aa2010-10-22 23:36:17 +00002988 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002989 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002990 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002991
Reid Spencer5f016e22007-07-11 17:01:13 +00002992 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002993 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002994 ParsingDeclRAIIObject PD(*this);
2995
Chris Lattner04d66662007-10-09 17:33:22 +00002996 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002997 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002998 AssignedVal = ParseConstantExpression();
2999 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003001 }
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Reid Spencer5f016e22007-07-11 17:01:13 +00003003 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003004 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3005 LastEnumConstDecl,
3006 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003007 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003008 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003009 PD.complete(EnumConstDecl);
3010
Reid Spencer5f016e22007-07-11 17:01:13 +00003011 EnumConstantDecls.push_back(EnumConstDecl);
3012 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Douglas Gregor751f6922010-09-07 14:51:08 +00003014 if (Tok.is(tok::identifier)) {
3015 // We're missing a comma between enumerators.
3016 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3017 Diag(Loc, diag::err_enumerator_list_missing_comma)
3018 << FixItHint::CreateInsertion(Loc, ", ");
3019 continue;
3020 }
3021
Chris Lattner04d66662007-10-09 17:33:22 +00003022 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 break;
3024 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003025
Richard Smith7fe62082011-10-15 05:09:34 +00003026 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003027 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003028 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003029 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003030 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003031 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003032 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3033 << FixItHint::CreateRemoval(CommaLoc);
3034 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003035 }
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Reid Spencer5f016e22007-07-11 17:01:13 +00003037 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003038 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003039
Reid Spencer5f016e22007-07-11 17:01:13 +00003040 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003041 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003042 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003043
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003044 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3045 EnumDecl, EnumConstantDecls.data(),
3046 EnumConstantDecls.size(), getCurScope(),
3047 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003048
Douglas Gregor72de6672009-01-08 20:45:30 +00003049 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003050 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3051 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003052}
3053
3054/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003055/// start of a type-qualifier-list.
3056bool Parser::isTypeQualifier() const {
3057 switch (Tok.getKind()) {
3058 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003059
3060 // type-qualifier only in OpenCL
3061 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003062 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003063
Steve Naroff5f8aa692008-02-11 23:15:56 +00003064 // type-qualifier
3065 case tok::kw_const:
3066 case tok::kw_volatile:
3067 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003068 case tok::kw___private:
3069 case tok::kw___local:
3070 case tok::kw___global:
3071 case tok::kw___constant:
3072 case tok::kw___read_only:
3073 case tok::kw___read_write:
3074 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003075 return true;
3076 }
3077}
3078
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003079/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3080/// is definitely a type-specifier. Return false if it isn't part of a type
3081/// specifier or if we're not sure.
3082bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3083 switch (Tok.getKind()) {
3084 default: return false;
3085 // type-specifiers
3086 case tok::kw_short:
3087 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003088 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003089 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003090 case tok::kw_signed:
3091 case tok::kw_unsigned:
3092 case tok::kw__Complex:
3093 case tok::kw__Imaginary:
3094 case tok::kw_void:
3095 case tok::kw_char:
3096 case tok::kw_wchar_t:
3097 case tok::kw_char16_t:
3098 case tok::kw_char32_t:
3099 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003100 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003101 case tok::kw_float:
3102 case tok::kw_double:
3103 case tok::kw_bool:
3104 case tok::kw__Bool:
3105 case tok::kw__Decimal32:
3106 case tok::kw__Decimal64:
3107 case tok::kw__Decimal128:
3108 case tok::kw___vector:
3109
3110 // struct-or-union-specifier (C99) or class-specifier (C++)
3111 case tok::kw_class:
3112 case tok::kw_struct:
3113 case tok::kw_union:
3114 // enum-specifier
3115 case tok::kw_enum:
3116
3117 // typedef-name
3118 case tok::annot_typename:
3119 return true;
3120 }
3121}
3122
Steve Naroff5f8aa692008-02-11 23:15:56 +00003123/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003124/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003125bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003126 switch (Tok.getKind()) {
3127 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Chris Lattner166a8fc2009-01-04 23:41:41 +00003129 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003130 if (TryAltiVecVectorToken())
3131 return true;
3132 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003133 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003134 // Annotate typenames and C++ scope specifiers. If we get one, just
3135 // recurse to handle whatever we get.
3136 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003137 return true;
3138 if (Tok.is(tok::identifier))
3139 return false;
3140 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003141
Chris Lattner166a8fc2009-01-04 23:41:41 +00003142 case tok::coloncolon: // ::foo::bar
3143 if (NextToken().is(tok::kw_new) || // ::new
3144 NextToken().is(tok::kw_delete)) // ::delete
3145 return false;
3146
Chris Lattner166a8fc2009-01-04 23:41:41 +00003147 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003148 return true;
3149 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003150
Reid Spencer5f016e22007-07-11 17:01:13 +00003151 // GNU attributes support.
3152 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003153 // GNU typeof support.
3154 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003155
Reid Spencer5f016e22007-07-11 17:01:13 +00003156 // type-specifiers
3157 case tok::kw_short:
3158 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003159 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003160 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 case tok::kw_signed:
3162 case tok::kw_unsigned:
3163 case tok::kw__Complex:
3164 case tok::kw__Imaginary:
3165 case tok::kw_void:
3166 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003167 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003168 case tok::kw_char16_t:
3169 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003171 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003172 case tok::kw_float:
3173 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003174 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003175 case tok::kw__Bool:
3176 case tok::kw__Decimal32:
3177 case tok::kw__Decimal64:
3178 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003179 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Chris Lattner99dc9142008-04-13 18:59:07 +00003181 // struct-or-union-specifier (C99) or class-specifier (C++)
3182 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 case tok::kw_struct:
3184 case tok::kw_union:
3185 // enum-specifier
3186 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Reid Spencer5f016e22007-07-11 17:01:13 +00003188 // type-qualifier
3189 case tok::kw_const:
3190 case tok::kw_volatile:
3191 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003192
3193 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003194 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Chris Lattner7c186be2008-10-20 00:25:30 +00003197 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3198 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003199 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Steve Naroff239f0732008-12-25 14:16:32 +00003201 case tok::kw___cdecl:
3202 case tok::kw___stdcall:
3203 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003204 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003205 case tok::kw___w64:
3206 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003207 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003208 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003209 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003210
3211 case tok::kw___private:
3212 case tok::kw___local:
3213 case tok::kw___global:
3214 case tok::kw___constant:
3215 case tok::kw___read_only:
3216 case tok::kw___read_write:
3217 case tok::kw___write_only:
3218
Eli Friedman290eeb02009-06-08 23:27:34 +00003219 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003220
3221 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003222 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003223
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003224 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003225 case tok::kw__Atomic:
3226 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003227 }
3228}
3229
3230/// isDeclarationSpecifier() - Return true if the current token is part of a
3231/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003232///
3233/// \param DisambiguatingWithExpression True to indicate that the purpose of
3234/// this check is to disambiguate between an expression and a declaration.
3235bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003236 switch (Tok.getKind()) {
3237 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003238
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003239 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003240 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003241
Chris Lattner166a8fc2009-01-04 23:41:41 +00003242 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003243 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003244 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003245 return false;
John Thompson82287d12010-02-05 00:12:22 +00003246 if (TryAltiVecVectorToken())
3247 return true;
3248 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003249 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003250 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003251 // Annotate typenames and C++ scope specifiers. If we get one, just
3252 // recurse to handle whatever we get.
3253 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003254 return true;
3255 if (Tok.is(tok::identifier))
3256 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003257
3258 // If we're in Objective-C and we have an Objective-C class type followed
3259 // by an identifier and then either ':' or ']', in a place where an
3260 // expression is permitted, then this is probably a class message send
3261 // missing the initial '['. In this case, we won't consider this to be
3262 // the start of a declaration.
3263 if (DisambiguatingWithExpression &&
3264 isStartOfObjCClassMessageMissingOpenBracket())
3265 return false;
3266
John McCall9ba61662010-02-26 08:45:28 +00003267 return isDeclarationSpecifier();
3268
Chris Lattner166a8fc2009-01-04 23:41:41 +00003269 case tok::coloncolon: // ::foo::bar
3270 if (NextToken().is(tok::kw_new) || // ::new
3271 NextToken().is(tok::kw_delete)) // ::delete
3272 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Chris Lattner166a8fc2009-01-04 23:41:41 +00003274 // Annotate typenames and C++ scope specifiers. If we get one, just
3275 // recurse to handle whatever we get.
3276 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003277 return true;
3278 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // storage-class-specifier
3281 case tok::kw_typedef:
3282 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003283 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003284 case tok::kw_static:
3285 case tok::kw_auto:
3286 case tok::kw_register:
3287 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Douglas Gregor8d267c52011-09-09 02:06:17 +00003289 // Modules
3290 case tok::kw___module_private__:
3291
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 // type-specifiers
3293 case tok::kw_short:
3294 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003295 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003296 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003297 case tok::kw_signed:
3298 case tok::kw_unsigned:
3299 case tok::kw__Complex:
3300 case tok::kw__Imaginary:
3301 case tok::kw_void:
3302 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003303 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003304 case tok::kw_char16_t:
3305 case tok::kw_char32_t:
3306
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003308 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003309 case tok::kw_float:
3310 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003311 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003312 case tok::kw__Bool:
3313 case tok::kw__Decimal32:
3314 case tok::kw__Decimal64:
3315 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003316 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Chris Lattner99dc9142008-04-13 18:59:07 +00003318 // struct-or-union-specifier (C99) or class-specifier (C++)
3319 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003320 case tok::kw_struct:
3321 case tok::kw_union:
3322 // enum-specifier
3323 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 // type-qualifier
3326 case tok::kw_const:
3327 case tok::kw_volatile:
3328 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003329
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 // function-specifier
3331 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003332 case tok::kw_virtual:
3333 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003334
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003335 // static_assert-declaration
3336 case tok::kw__Static_assert:
3337
Chris Lattner1ef08762007-08-09 17:01:07 +00003338 // GNU typeof support.
3339 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Chris Lattner1ef08762007-08-09 17:01:07 +00003341 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003342 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003343 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Francois Pichete3d49b42011-06-19 08:02:06 +00003345 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003346 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003347 return true;
3348
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003349 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003350 case tok::kw__Atomic:
3351 return true;
3352
Chris Lattnerf3948c42008-07-26 03:38:44 +00003353 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3354 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003355 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Douglas Gregord9d75e52011-04-27 05:41:15 +00003357 // typedef-name
3358 case tok::annot_typename:
3359 return !DisambiguatingWithExpression ||
3360 !isStartOfObjCClassMessageMissingOpenBracket();
3361
Steve Naroff47f52092009-01-06 19:34:12 +00003362 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003363 case tok::kw___cdecl:
3364 case tok::kw___stdcall:
3365 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003366 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003367 case tok::kw___w64:
3368 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003369 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003370 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003371 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003372 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003373
3374 case tok::kw___private:
3375 case tok::kw___local:
3376 case tok::kw___global:
3377 case tok::kw___constant:
3378 case tok::kw___read_only:
3379 case tok::kw___read_write:
3380 case tok::kw___write_only:
3381
Eli Friedman290eeb02009-06-08 23:27:34 +00003382 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003383 }
3384}
3385
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003386bool Parser::isConstructorDeclarator() {
3387 TentativeParsingAction TPA(*this);
3388
3389 // Parse the C++ scope specifier.
3390 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003391 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3392 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003393 TPA.Revert();
3394 return false;
3395 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003396
3397 // Parse the constructor name.
3398 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3399 // We already know that we have a constructor name; just consume
3400 // the token.
3401 ConsumeToken();
3402 } else {
3403 TPA.Revert();
3404 return false;
3405 }
3406
Richard Smith22592862012-03-27 23:05:05 +00003407 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003408 if (Tok.isNot(tok::l_paren)) {
3409 TPA.Revert();
3410 return false;
3411 }
3412 ConsumeParen();
3413
Richard Smith22592862012-03-27 23:05:05 +00003414 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3415 // that we have a constructor.
3416 if (Tok.is(tok::r_paren) ||
3417 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003418 TPA.Revert();
3419 return true;
3420 }
3421
3422 // If we need to, enter the specified scope.
3423 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003424 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003425 DeclScopeObj.EnterDeclaratorScope();
3426
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003427 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003428 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003429 MaybeParseMicrosoftAttributes(Attrs);
3430
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003431 // Check whether the next token(s) are part of a declaration
3432 // specifier, in which case we have the start of a parameter and,
3433 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003434 bool IsConstructor = false;
3435 if (isDeclarationSpecifier())
3436 IsConstructor = true;
3437 else if (Tok.is(tok::identifier) ||
3438 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3439 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3440 // This might be a parenthesized member name, but is more likely to
3441 // be a constructor declaration with an invalid argument type. Keep
3442 // looking.
3443 if (Tok.is(tok::annot_cxxscope))
3444 ConsumeToken();
3445 ConsumeToken();
3446
3447 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003448 // which must have one of the following syntactic forms (see the
3449 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003450 switch (Tok.getKind()) {
3451 case tok::l_paren:
3452 // C(X ( int));
3453 case tok::l_square:
3454 // C(X [ 5]);
3455 // C(X [ [attribute]]);
3456 case tok::coloncolon:
3457 // C(X :: Y);
3458 // C(X :: *p);
3459 case tok::r_paren:
3460 // C(X )
3461 // Assume this isn't a constructor, rather than assuming it's a
3462 // constructor with an unnamed parameter of an ill-formed type.
3463 break;
3464
3465 default:
3466 IsConstructor = true;
3467 break;
3468 }
3469 }
3470
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003471 TPA.Revert();
3472 return IsConstructor;
3473}
Reid Spencer5f016e22007-07-11 17:01:13 +00003474
3475/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003476/// type-qualifier-list: [C99 6.7.5]
3477/// type-qualifier
3478/// [vendor] attributes
3479/// [ only if VendorAttributesAllowed=true ]
3480/// type-qualifier-list type-qualifier
3481/// [vendor] type-qualifier-list attributes
3482/// [ only if VendorAttributesAllowed=true ]
3483/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3484/// [ only if CXX0XAttributesAllowed=true ]
3485/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003486///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003487void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3488 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003489 bool CXX0XAttributesAllowed) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003490 if (getLangOpts().CPlusPlus0x && CXX0XAttributesAllowed &&
3491 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003492 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003493 ParseCXX0XAttributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003494 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003495 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003496
3497 SourceLocation EndLoc;
3498
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003500 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003501 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003502 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003503 SourceLocation Loc = Tok.getLocation();
3504
3505 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003506 case tok::code_completion:
3507 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003508 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003509
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003511 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003512 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 break;
3514 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003515 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003516 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 break;
3518 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003519 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003520 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003522
3523 // OpenCL qualifiers:
3524 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003525 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003526 goto DoneWithTypeQuals;
3527 case tok::kw___private:
3528 case tok::kw___global:
3529 case tok::kw___local:
3530 case tok::kw___constant:
3531 case tok::kw___read_only:
3532 case tok::kw___write_only:
3533 case tok::kw___read_write:
3534 ParseOpenCLQualifiers(DS);
3535 break;
3536
Eli Friedman290eeb02009-06-08 23:27:34 +00003537 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003538 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003539 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003540 case tok::kw___cdecl:
3541 case tok::kw___stdcall:
3542 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003543 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003544 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003545 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003546 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003547 continue;
3548 }
3549 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003550 case tok::kw___pascal:
3551 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003552 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003553 continue;
3554 }
3555 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003556 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003557 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003558 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003559 continue; // do *not* consume the next token!
3560 }
3561 // otherwise, FALL THROUGH!
3562 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003563 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003564 // If this is not a type-qualifier token, we're done reading type
3565 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003566 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003567 if (EndLoc.isValid())
3568 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003569 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003571
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 // If the specifier combination wasn't legal, issue a diagnostic.
3573 if (isInvalid) {
3574 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003575 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003576 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003577 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 }
3579}
3580
3581
3582/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3583///
3584void Parser::ParseDeclarator(Declarator &D) {
3585 /// This implements the 'declarator' production in the C grammar, then checks
3586 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003587 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003588}
3589
Richard Smith9988f282012-03-29 01:16:42 +00003590static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3591 if (Kind == tok::star || Kind == tok::caret)
3592 return true;
3593
3594 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3595 if (!Lang.CPlusPlus)
3596 return false;
3597
3598 return Kind == tok::amp || Kind == tok::ampamp;
3599}
3600
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003601/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3602/// is parsed by the function passed to it. Pass null, and the direct-declarator
3603/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003604/// ptr-operator production.
3605///
Richard Smith0706df42011-10-19 21:33:05 +00003606/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003607/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3608/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003609///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003610/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3611/// [C] pointer[opt] direct-declarator
3612/// [C++] direct-declarator
3613/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003614///
3615/// pointer: [C99 6.7.5]
3616/// '*' type-qualifier-list[opt]
3617/// '*' type-qualifier-list[opt] pointer
3618///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003619/// ptr-operator:
3620/// '*' cv-qualifier-seq[opt]
3621/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003622/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003623/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003624/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003625/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003626void Parser::ParseDeclaratorInternal(Declarator &D,
3627 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003628 if (Diags.hasAllExtensionsSilenced())
3629 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003630
Sebastian Redlf30208a2009-01-24 21:16:55 +00003631 // C++ member pointers start with a '::' or a nested-name.
3632 // Member pointers get special handling, since there's no place for the
3633 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003634 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003635 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3636 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003637 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3638 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003639 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003640 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003641
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003642 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003643 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003644 // The scope spec really belongs to the direct-declarator.
3645 D.getCXXScopeSpec() = SS;
3646 if (DirectDeclParser)
3647 (this->*DirectDeclParser)(D);
3648 return;
3649 }
3650
3651 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003652 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003653 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003654 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003655 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003656
3657 // Recurse to parse whatever is left.
3658 ParseDeclaratorInternal(D, DirectDeclParser);
3659
3660 // Sema will have to catch (syntactically invalid) pointers into global
3661 // scope. It has to catch pointers into namespace scope anyway.
3662 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003663 Loc),
3664 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003665 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003666 return;
3667 }
3668 }
3669
3670 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003671 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003672 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003673 if (DirectDeclParser)
3674 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003675 return;
3676 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003677
Sebastian Redl05532f22009-03-15 22:02:01 +00003678 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3679 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003680 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003681 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003682
Chris Lattner9af55002009-03-27 04:18:06 +00003683 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003684 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003685 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003686
Richard Smith6ee326a2012-04-10 01:32:12 +00003687 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003688 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003689 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003690
Reid Spencer5f016e22007-07-11 17:01:13 +00003691 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003692 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003693 if (Kind == tok::star)
3694 // Remember that we parsed a pointer type, and remember the type-quals.
3695 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003696 DS.getConstSpecLoc(),
3697 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003698 DS.getRestrictSpecLoc()),
3699 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003700 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003701 else
3702 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003703 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003704 Loc),
3705 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003706 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003707 } else {
3708 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003709 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003710
Sebastian Redl743de1f2009-03-23 00:00:23 +00003711 // Complain about rvalue references in C++03, but then go on and build
3712 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003713 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003714 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003715 diag::warn_cxx98_compat_rvalue_reference :
3716 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003717
Richard Smith6ee326a2012-04-10 01:32:12 +00003718 // GNU-style and C++11 attributes are allowed here, as is restrict.
3719 ParseTypeQualifierListOpt(DS);
3720 D.ExtendWithDeclSpec(DS);
3721
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3723 // cv-qualifiers are introduced through the use of a typedef or of a
3724 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3726 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3727 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003728 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003729 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3730 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003731 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003732 }
3733
3734 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003735 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003736
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003737 if (D.getNumTypeObjects() > 0) {
3738 // C++ [dcl.ref]p4: There shall be no references to references.
3739 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3740 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003741 if (const IdentifierInfo *II = D.getIdentifier())
3742 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3743 << II;
3744 else
3745 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3746 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003747
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003748 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003749 // can go ahead and build the (technically ill-formed)
3750 // declarator: reference collapsing will take care of it.
3751 }
3752 }
3753
Reid Spencer5f016e22007-07-11 17:01:13 +00003754 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003755 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003756 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003757 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003758 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003759 }
3760}
3761
Richard Smith9988f282012-03-29 01:16:42 +00003762static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3763 SourceLocation EllipsisLoc) {
3764 if (EllipsisLoc.isValid()) {
3765 FixItHint Insertion;
3766 if (!D.getEllipsisLoc().isValid()) {
3767 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3768 D.setEllipsisLoc(EllipsisLoc);
3769 }
3770 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3771 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3772 }
3773}
3774
Reid Spencer5f016e22007-07-11 17:01:13 +00003775/// ParseDirectDeclarator
3776/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003777/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003778/// '(' declarator ')'
3779/// [GNU] '(' attributes declarator ')'
3780/// [C90] direct-declarator '[' constant-expression[opt] ']'
3781/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3782/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3783/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3784/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003785/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3786/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003787/// direct-declarator '(' parameter-type-list ')'
3788/// direct-declarator '(' identifier-list[opt] ')'
3789/// [GNU] direct-declarator '(' parameter-forward-declarations
3790/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003791/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3792/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003793/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3794/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3795/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003796/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003797/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003798///
3799/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003800/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003801/// '::'[opt] nested-name-specifier[opt] type-name
3802///
3803/// id-expression: [C++ 5.1]
3804/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003805/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003806///
3807/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003808/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003809/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003810/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003811/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003812/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003813///
Richard Smith5d8388c2012-03-27 01:42:32 +00003814/// Note, any additional constructs added here may need corresponding changes
3815/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003816void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003817 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003818
David Blaikie4e4d0842012-03-11 07:00:24 +00003819 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003820 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003821 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003822 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3823 D.getContext() == Declarator::MemberContext;
3824 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3825 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003826 }
3827
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003828 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003829 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003830 // Change the declaration context for name lookup, until this function
3831 // is exited (and the declarator has been parsed).
3832 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003833 }
3834
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003835 // C++0x [dcl.fct]p14:
3836 // There is a syntactic ambiguity when an ellipsis occurs at the end
3837 // of a parameter-declaration-clause without a preceding comma. In
3838 // this case, the ellipsis is parsed as part of the
3839 // abstract-declarator if the type of the parameter names a template
3840 // parameter pack that has not been expanded; otherwise, it is parsed
3841 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003842 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003843 !((D.getContext() == Declarator::PrototypeContext ||
3844 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003845 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003846 !Actions.containsUnexpandedParameterPacks(D))) {
3847 SourceLocation EllipsisLoc = ConsumeToken();
3848 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3849 // The ellipsis was put in the wrong place. Recover, and explain to
3850 // the user what they should have done.
3851 ParseDeclarator(D);
3852 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3853 return;
3854 } else
3855 D.setEllipsisLoc(EllipsisLoc);
3856
3857 // The ellipsis can't be followed by a parenthesized declarator. We
3858 // check for that in ParseParenDeclarator, after we have disambiguated
3859 // the l_paren token.
3860 }
3861
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003862 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3863 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3864 // We found something that indicates the start of an unqualified-id.
3865 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003866 bool AllowConstructorName;
3867 if (D.getDeclSpec().hasTypeSpecifier())
3868 AllowConstructorName = false;
3869 else if (D.getCXXScopeSpec().isSet())
3870 AllowConstructorName =
3871 (D.getContext() == Declarator::FileContext ||
3872 (D.getContext() == Declarator::MemberContext &&
3873 D.getDeclSpec().isFriendSpecified()));
3874 else
3875 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3876
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003877 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003878 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3879 /*EnteringContext=*/true,
3880 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003881 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003882 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003883 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003884 D.getName()) ||
3885 // Once we're past the identifier, if the scope was bad, mark the
3886 // whole declarator bad.
3887 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003888 D.SetIdentifier(0, Tok.getLocation());
3889 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003890 } else {
3891 // Parsed the unqualified-id; update range information and move along.
3892 if (D.getSourceRange().getBegin().isInvalid())
3893 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3894 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003895 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003896 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003897 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003898 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003899 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003900 "There's a C++-specific check for tok::identifier above");
3901 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3902 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3903 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003904 goto PastIdentifier;
3905 }
Richard Smith9988f282012-03-29 01:16:42 +00003906
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003907 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 // direct-declarator: '(' declarator ')'
3909 // direct-declarator: '(' attributes declarator ')'
3910 // Example: 'char (*X)' or 'int (*XX)(void)'
3911 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003912
3913 // If the declarator was parenthesized, we entered the declarator
3914 // scope when parsing the parenthesized declarator, then exited
3915 // the scope already. Re-enter the scope, if we need to.
3916 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003917 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003918 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003919 if (!D.isInvalidType() &&
3920 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003921 // Change the declaration context for name lookup, until this function
3922 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003923 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003924 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003925 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003926 // This could be something simple like "int" (in which case the declarator
3927 // portion is empty), if an abstract-declarator is allowed.
3928 D.SetIdentifier(0, Tok.getLocation());
3929 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003930 if (D.getContext() == Declarator::MemberContext)
3931 Diag(Tok, diag::err_expected_member_name_or_semi)
3932 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003933 else if (getLangOpts().CPlusPlus)
3934 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003935 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003936 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003937 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003938 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003939 }
Mike Stump1eb44332009-09-09 15:08:12 +00003940
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003941 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003942 assert(D.isPastIdentifier() &&
3943 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003944
Richard Smith6ee326a2012-04-10 01:32:12 +00003945 // Don't parse attributes unless we have parsed an unparenthesized name.
3946 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00003947 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003948
Reid Spencer5f016e22007-07-11 17:01:13 +00003949 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003950 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003951 // Enter function-declaration scope, limiting any declarators to the
3952 // function prototype scope, including parameter declarators.
3953 ParseScope PrototypeScope(this,
3954 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003955 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3956 // In such a case, check if we actually have a function declarator; if it
3957 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003958 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003959 // When not in file scope, warn for ambiguous function declarators, just
3960 // in case the author intended it as a variable definition.
3961 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3962 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3963 break;
3964 }
John McCall0b7e6782011-03-24 11:26:52 +00003965 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003966 BalancedDelimiterTracker T(*this, tok::l_paren);
3967 T.consumeOpen();
3968 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003969 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003970 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003971 ParseBracketDeclarator(D);
3972 } else {
3973 break;
3974 }
3975 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003976}
Reid Spencer5f016e22007-07-11 17:01:13 +00003977
Chris Lattneref4715c2008-04-06 05:45:57 +00003978/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3979/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003980/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003981/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3982///
3983/// direct-declarator:
3984/// '(' declarator ')'
3985/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003986/// direct-declarator '(' parameter-type-list ')'
3987/// direct-declarator '(' identifier-list[opt] ')'
3988/// [GNU] direct-declarator '(' parameter-forward-declarations
3989/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003990///
3991void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003992 BalancedDelimiterTracker T(*this, tok::l_paren);
3993 T.consumeOpen();
3994
Chris Lattneref4715c2008-04-06 05:45:57 +00003995 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003996
Chris Lattner7399ee02008-10-20 02:05:46 +00003997 // Eat any attributes before we look at whether this is a grouping or function
3998 // declarator paren. If this is a grouping paren, the attribute applies to
3999 // the type being built up, for example:
4000 // int (__attribute__(()) *x)(long y)
4001 // If this ends up not being a grouping paren, the attribute applies to the
4002 // first argument, for example:
4003 // int (__attribute__(()) int x)
4004 // In either case, we need to eat any attributes to be able to determine what
4005 // sort of paren this is.
4006 //
John McCall0b7e6782011-03-24 11:26:52 +00004007 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004008 bool RequiresArg = false;
4009 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004010 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004011
Chris Lattner7399ee02008-10-20 02:05:46 +00004012 // We require that the argument list (if this is a non-grouping paren) be
4013 // present even if the attribute list was empty.
4014 RequiresArg = true;
4015 }
Steve Naroff239f0732008-12-25 14:16:32 +00004016 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004017 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004018 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004019 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004020 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004021 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004022 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004023 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004024 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004025 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004026
Chris Lattneref4715c2008-04-06 05:45:57 +00004027 // If we haven't past the identifier yet (or where the identifier would be
4028 // stored, if this is an abstract declarator), then this is probably just
4029 // grouping parens. However, if this could be an abstract-declarator, then
4030 // this could also be the start of function arguments (consider 'void()').
4031 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004032
Chris Lattneref4715c2008-04-06 05:45:57 +00004033 if (!D.mayOmitIdentifier()) {
4034 // If this can't be an abstract-declarator, this *must* be a grouping
4035 // paren, because we haven't seen the identifier yet.
4036 isGrouping = true;
4037 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004038 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4039 NextToken().is(tok::r_paren)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004040 isDeclarationSpecifier()) { // 'int(int)' is a function.
4041 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4042 // considered to be a type, not a K&R identifier-list.
4043 isGrouping = false;
4044 } else {
4045 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4046 isGrouping = true;
4047 }
Mike Stump1eb44332009-09-09 15:08:12 +00004048
Chris Lattneref4715c2008-04-06 05:45:57 +00004049 // If this is a grouping paren, handle:
4050 // direct-declarator: '(' declarator ')'
4051 // direct-declarator: '(' attributes declarator ')'
4052 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004053 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4054 D.setEllipsisLoc(SourceLocation());
4055
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004056 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004057 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004058 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004059 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004060 T.consumeClose();
4061 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4062 T.getCloseLocation()),
4063 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004064
4065 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004066
4067 // An ellipsis cannot be placed outside parentheses.
4068 if (EllipsisLoc.isValid())
4069 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4070
Chris Lattneref4715c2008-04-06 05:45:57 +00004071 return;
4072 }
Mike Stump1eb44332009-09-09 15:08:12 +00004073
Chris Lattneref4715c2008-04-06 05:45:57 +00004074 // Okay, if this wasn't a grouping paren, it must be the start of a function
4075 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004076 // identifier (and remember where it would have been), then call into
4077 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004078 D.SetIdentifier(0, Tok.getLocation());
4079
David Blaikie42d6d0c2011-12-04 05:04:18 +00004080 // Enter function-declaration scope, limiting any declarators to the
4081 // function prototype scope, including parameter declarators.
4082 ParseScope PrototypeScope(this,
4083 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004084 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004085 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004086}
4087
4088/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4089/// declarator D up to a paren, which indicates that we are parsing function
4090/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004091///
Richard Smith6ee326a2012-04-10 01:32:12 +00004092/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4093/// immediately after the open paren - they should be considered to be the
4094/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004095///
Richard Smith6ee326a2012-04-10 01:32:12 +00004096/// If RequiresArg is true, then the first argument of the function is required
4097/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004098///
Richard Smith6ee326a2012-04-10 01:32:12 +00004099/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4100/// (C++11) ref-qualifier[opt], exception-specification[opt],
4101/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4102///
4103/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004104/// dynamic-exception-specification
4105/// noexcept-specification
4106///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004107void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004108 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004109 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004110 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004111 assert(getCurScope()->isFunctionPrototypeScope() &&
4112 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004113 // lparen is already consumed!
4114 assert(D.isPastIdentifier() && "Should not call before identifier!");
4115
4116 // This should be true when the function has typed arguments.
4117 // Otherwise, it is treated as a K&R-style function.
4118 bool HasProto = false;
4119 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004120 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004121 // Remember where we see an ellipsis, if any.
4122 SourceLocation EllipsisLoc;
4123
4124 DeclSpec DS(AttrFactory);
4125 bool RefQualifierIsLValueRef = true;
4126 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004127 SourceLocation ConstQualifierLoc;
4128 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004129 ExceptionSpecificationType ESpecType = EST_None;
4130 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004131 SmallVector<ParsedType, 2> DynamicExceptions;
4132 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004133 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004134 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004135 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004136
James Molloy16f1f712012-02-29 10:24:19 +00004137 Actions.ActOnStartFunctionDeclarator();
4138
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004139 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004140 if (isFunctionDeclaratorIdentifierList()) {
4141 if (RequiresArg)
4142 Diag(Tok, diag::err_argument_required_after_attribute);
4143
4144 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4145
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004146 Tracker.consumeClose();
4147 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004148 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004149 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004150 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 else if (RequiresArg)
4152 Diag(Tok, diag::err_argument_required_after_attribute);
4153
David Blaikie4e4d0842012-03-11 07:00:24 +00004154 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004155
4156 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004157 Tracker.consumeClose();
4158 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004159
David Blaikie4e4d0842012-03-11 07:00:24 +00004160 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004161 // FIXME: Accept these components in any order, and produce fixits to
4162 // correct the order if the user gets it wrong. Ideally we should deal
4163 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004164
4165 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004166 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4167 if (!DS.getSourceRange().getEnd().isInvalid()) {
4168 EndLoc = DS.getSourceRange().getEnd();
4169 ConstQualifierLoc = DS.getConstSpecLoc();
4170 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4171 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004172
4173 // Parse ref-qualifier[opt].
4174 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004175 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004176 diag::warn_cxx98_compat_ref_qualifier :
4177 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004178
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004179 RefQualifierIsLValueRef = Tok.is(tok::amp);
4180 RefQualifierLoc = ConsumeToken();
4181 EndLoc = RefQualifierLoc;
4182 }
4183
4184 // Parse exception-specification[opt].
4185 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4186 DynamicExceptions,
4187 DynamicExceptionRanges,
4188 NoexceptExpr);
4189 if (ESpecType != EST_None)
4190 EndLoc = ESpecRange.getEnd();
4191
Richard Smith6ee326a2012-04-10 01:32:12 +00004192 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4193 // after the exception-specification.
4194 MaybeParseCXX0XAttributes(FnAttrs);
4195
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004196 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004197 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004198 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004199 SourceRange Range;
4200 TrailingReturnType = ParseTrailingReturnType(Range).get();
4201 if (Range.getEnd().isValid())
4202 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203 }
4204 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004205 }
4206
4207 // Remember that we parsed a function type, and remember the attributes.
4208 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4209 /*isVariadic=*/EllipsisLoc.isValid(),
4210 EllipsisLoc,
4211 ParamInfo.data(), ParamInfo.size(),
4212 DS.getTypeQualifiers(),
4213 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004214 RefQualifierLoc, ConstQualifierLoc,
4215 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004216 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004217 ESpecType, ESpecRange.getBegin(),
4218 DynamicExceptions.data(),
4219 DynamicExceptionRanges.data(),
4220 DynamicExceptions.size(),
4221 NoexceptExpr.isUsable() ?
4222 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004223 Tracker.getOpenLocation(),
4224 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004225 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004226 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004227
4228 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004229}
4230
4231/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4232/// identifier list form for a K&R-style function: void foo(a,b,c)
4233///
4234/// Note that identifier-lists are only allowed for normal declarators, not for
4235/// abstract-declarators.
4236bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004237 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004238 && Tok.is(tok::identifier)
4239 && !TryAltiVecVectorToken()
4240 // K&R identifier lists can't have typedefs as identifiers, per C99
4241 // 6.7.5.3p11.
4242 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4243 // Identifier lists follow a really simple grammar: the identifiers can
4244 // be followed *only* by a ", identifier" or ")". However, K&R
4245 // identifier lists are really rare in the brave new modern world, and
4246 // it is very common for someone to typo a type in a non-K&R style
4247 // list. If we are presented with something like: "void foo(intptr x,
4248 // float y)", we don't want to start parsing the function declarator as
4249 // though it is a K&R style declarator just because intptr is an
4250 // invalid type.
4251 //
4252 // To handle this, we check to see if the token after the first
4253 // identifier is a "," or ")". Only then do we parse it as an
4254 // identifier list.
4255 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4256}
4257
4258/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4259/// we found a K&R-style identifier list instead of a typed parameter list.
4260///
4261/// After returning, ParamInfo will hold the parsed parameters.
4262///
4263/// identifier-list: [C99 6.7.5]
4264/// identifier
4265/// identifier-list ',' identifier
4266///
4267void Parser::ParseFunctionDeclaratorIdentifierList(
4268 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004269 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004270 // If there was no identifier specified for the declarator, either we are in
4271 // an abstract-declarator, or we are in a parameter declarator which was found
4272 // to be abstract. In abstract-declarators, identifier lists are not valid:
4273 // diagnose this.
4274 if (!D.getIdentifier())
4275 Diag(Tok, diag::ext_ident_list_in_param);
4276
4277 // Maintain an efficient lookup of params we have seen so far.
4278 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4279
4280 while (1) {
4281 // If this isn't an identifier, report the error and skip until ')'.
4282 if (Tok.isNot(tok::identifier)) {
4283 Diag(Tok, diag::err_expected_ident);
4284 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4285 // Forget we parsed anything.
4286 ParamInfo.clear();
4287 return;
4288 }
4289
4290 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4291
4292 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4293 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4294 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4295
4296 // Verify that the argument identifier has not already been mentioned.
4297 if (!ParamsSoFar.insert(ParmII)) {
4298 Diag(Tok, diag::err_param_redefinition) << ParmII;
4299 } else {
4300 // Remember this identifier in ParamInfo.
4301 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4302 Tok.getLocation(),
4303 0));
4304 }
4305
4306 // Eat the identifier.
4307 ConsumeToken();
4308
4309 // The list continues if we see a comma.
4310 if (Tok.isNot(tok::comma))
4311 break;
4312 ConsumeToken();
4313 }
4314}
4315
4316/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4317/// after the opening parenthesis. This function will not parse a K&R-style
4318/// identifier list.
4319///
4320/// D is the declarator being parsed. If attrs is non-null, then the caller
4321/// parsed those arguments immediately after the open paren - they should be
4322/// considered to be the first argument of a parameter.
4323///
4324/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4325/// be the location of the ellipsis, if any was parsed.
4326///
Reid Spencer5f016e22007-07-11 17:01:13 +00004327/// parameter-type-list: [C99 6.7.5]
4328/// parameter-list
4329/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004330/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004331///
4332/// parameter-list: [C99 6.7.5]
4333/// parameter-declaration
4334/// parameter-list ',' parameter-declaration
4335///
4336/// parameter-declaration: [C99 6.7.5]
4337/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004338/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004339/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004340/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004341/// declaration-specifiers abstract-declarator[opt]
4342/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004343/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004344/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4345///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004346void Parser::ParseParameterDeclarationClause(
4347 Declarator &D,
4348 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004349 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004350 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004351
Chris Lattnerf97409f2008-04-06 06:57:35 +00004352 while (1) {
4353 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004354 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004355 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004356 }
Mike Stump1eb44332009-09-09 15:08:12 +00004357
Chris Lattnerf97409f2008-04-06 06:57:35 +00004358 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004359 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004360 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004361
John McCall7f040a92010-12-24 02:08:15 +00004362 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004363 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004364 ParseMicrosoftAttributes(DS.getAttributes());
4365
4366 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004367
4368 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004369 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004370 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4371 // attributes lost? Should they even be allowed?
4372 // FIXME: If we can leave the attributes in the token stream somehow, we can
4373 // get rid of a parameter (attrs) and this statement. It might be too much
4374 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004375 DS.takeAttributesFrom(attrs);
4376
Chris Lattnere64c5492009-02-27 18:38:20 +00004377 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004378
Chris Lattnerf97409f2008-04-06 06:57:35 +00004379 // Parse the declarator. This is "PrototypeContext", because we must
4380 // accept either 'declarator' or 'abstract-declarator' here.
4381 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4382 ParseDeclarator(ParmDecl);
4383
4384 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004385 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004386
Chris Lattnerf97409f2008-04-06 06:57:35 +00004387 // Remember this parsed parameter in ParamInfo.
4388 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004389
Douglas Gregor72b505b2008-12-16 21:30:33 +00004390 // DefArgToks is used when the parsing of default arguments needs
4391 // to be delayed.
4392 CachedTokens *DefArgToks = 0;
4393
Chris Lattnerf97409f2008-04-06 06:57:35 +00004394 // If no parameter was specified, verify that *something* was specified,
4395 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004396 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4397 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004398 // Completely missing, emit error.
4399 Diag(DSStart, diag::err_missing_param);
4400 } else {
4401 // Otherwise, we have something. Add it and let semantic analysis try
4402 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004403
Chris Lattnerf97409f2008-04-06 06:57:35 +00004404 // Inform the actions module about the parameter declarator, so it gets
4405 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004406 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004407
4408 // Parse the default argument, if any. We parse the default
4409 // arguments in all dialects; the semantic analysis in
4410 // ActOnParamDefaultArgument will reject the default argument in
4411 // C.
4412 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004413 SourceLocation EqualLoc = Tok.getLocation();
4414
Chris Lattner04421082008-04-08 04:40:51 +00004415 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004416 if (D.getContext() == Declarator::MemberContext) {
4417 // If we're inside a class definition, cache the tokens
4418 // corresponding to the default argument. We'll actually parse
4419 // them when we see the end of the class definition.
4420 // FIXME: Templates will require something similar.
4421 // FIXME: Can we use a smart pointer for Toks?
4422 DefArgToks = new CachedTokens;
4423
Mike Stump1eb44332009-09-09 15:08:12 +00004424 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004425 /*StopAtSemi=*/true,
4426 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004427 delete DefArgToks;
4428 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004429 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004430 } else {
4431 // Mark the end of the default argument so that we know when to
4432 // stop when we parse it later on.
4433 Token DefArgEnd;
4434 DefArgEnd.startToken();
4435 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4436 DefArgEnd.setLocation(Tok.getLocation());
4437 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004438 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004439 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004440 }
Chris Lattner04421082008-04-08 04:40:51 +00004441 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004442 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004443 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004444
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004445 // The argument isn't actually potentially evaluated unless it is
4446 // used.
4447 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004448 Sema::PotentiallyEvaluatedIfUsed,
4449 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004450
Sebastian Redl84407ba2012-03-14 15:54:00 +00004451 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004452 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4453 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004454 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004455 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004456 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004457 if (DefArgResult.isInvalid()) {
4458 Actions.ActOnParamDefaultArgumentError(Param);
4459 SkipUntil(tok::comma, tok::r_paren, true, true);
4460 } else {
4461 // Inform the actions module about the default argument
4462 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004463 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004464 }
Chris Lattner04421082008-04-08 04:40:51 +00004465 }
4466 }
Mike Stump1eb44332009-09-09 15:08:12 +00004467
4468 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4469 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004470 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004471 }
4472
4473 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004474 if (Tok.isNot(tok::comma)) {
4475 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004476 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4477
David Blaikie4e4d0842012-03-11 07:00:24 +00004478 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004479 // We have ellipsis without a preceding ',', which is ill-formed
4480 // in C. Complain and provide the fix.
4481 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004482 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004483 }
4484 }
4485
4486 break;
4487 }
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Chris Lattnerf97409f2008-04-06 06:57:35 +00004489 // Consume the comma.
4490 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004491 }
Mike Stump1eb44332009-09-09 15:08:12 +00004492
Chris Lattner66d28652008-04-06 06:34:08 +00004493}
Chris Lattneref4715c2008-04-06 05:45:57 +00004494
Reid Spencer5f016e22007-07-11 17:01:13 +00004495/// [C90] direct-declarator '[' constant-expression[opt] ']'
4496/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4497/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4498/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4499/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004500/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4501/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004502void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004503 if (CheckProhibitedCXX11Attribute())
4504 return;
4505
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004506 BalancedDelimiterTracker T(*this, tok::l_square);
4507 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004508
Chris Lattner378c7e42008-12-18 07:27:21 +00004509 // C array syntax has many features, but by-far the most common is [] and [4].
4510 // This code does a fast path to handle some of the most obvious cases.
4511 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004512 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004513 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004514 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004515
Chris Lattner378c7e42008-12-18 07:27:21 +00004516 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004517 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004518 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004519 T.getOpenLocation(),
4520 T.getCloseLocation()),
4521 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004522 return;
4523 } else if (Tok.getKind() == tok::numeric_constant &&
4524 GetLookAheadToken(1).is(tok::r_square)) {
4525 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004526 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004527 ConsumeToken();
4528
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004529 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004530 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004531 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004532
Chris Lattner378c7e42008-12-18 07:27:21 +00004533 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004534 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004535 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004536 T.getOpenLocation(),
4537 T.getCloseLocation()),
4538 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004539 return;
4540 }
Mike Stump1eb44332009-09-09 15:08:12 +00004541
Reid Spencer5f016e22007-07-11 17:01:13 +00004542 // If valid, this location is the position where we read the 'static' keyword.
4543 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004544 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004545 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004546
Reid Spencer5f016e22007-07-11 17:01:13 +00004547 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004548 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004549 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004550 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Reid Spencer5f016e22007-07-11 17:01:13 +00004552 // If we haven't already read 'static', check to see if there is one after the
4553 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004554 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004555 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004556
Reid Spencer5f016e22007-07-11 17:01:13 +00004557 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4558 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004559 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004560
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004561 // Handle the case where we have '[*]' as the array size. However, a leading
4562 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4563 // the the token after the star is a ']'. Since stars in arrays are
4564 // infrequent, use of lookahead is not costly here.
4565 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004566 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004567
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004568 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004569 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004570 StaticLoc = SourceLocation(); // Drop the static.
4571 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004572 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004573 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004574 // Note, in C89, this production uses the constant-expr production instead
4575 // of assignment-expr. The only difference is that assignment-expr allows
4576 // things like '=' and '*='. Sema rejects these in C89 mode because they
4577 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004578
Douglas Gregore0762c92009-06-19 23:52:42 +00004579 // Parse the constant-expression or assignment-expression now (depending
4580 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004581 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004582 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004583 } else {
4584 EnterExpressionEvaluationContext Unevaluated(Actions,
4585 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004586 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004587 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004588 }
Mike Stump1eb44332009-09-09 15:08:12 +00004589
Reid Spencer5f016e22007-07-11 17:01:13 +00004590 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004591 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004592 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004593 // If the expression was invalid, skip it.
4594 SkipUntil(tok::r_square);
4595 return;
4596 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004597
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004598 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004599
John McCall0b7e6782011-03-24 11:26:52 +00004600 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004601 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004602
Chris Lattner378c7e42008-12-18 07:27:21 +00004603 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004604 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004605 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004606 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004607 T.getOpenLocation(),
4608 T.getCloseLocation()),
4609 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004610}
4611
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004612/// [GNU] typeof-specifier:
4613/// typeof ( expressions )
4614/// typeof ( type-name )
4615/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004616///
4617void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004618 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004619 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004620 SourceLocation StartLoc = ConsumeToken();
4621
John McCallcfb708c2010-01-13 20:03:27 +00004622 const bool hasParens = Tok.is(tok::l_paren);
4623
Eli Friedman71b8fb52012-01-21 01:01:51 +00004624 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4625
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004626 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004627 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004628 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004629 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4630 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004631 if (hasParens)
4632 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004633
4634 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004635 // FIXME: Not accurate, the range gets one token more than it should.
4636 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004637 else
4638 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004639
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004640 if (isCastExpr) {
4641 if (!CastTy) {
4642 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004643 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004644 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004645
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004646 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004647 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004648 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4649 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004650 DiagID, CastTy))
4651 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004652 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004653 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004654
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004655 // If we get here, the operand to the typeof was an expresion.
4656 if (Operand.isInvalid()) {
4657 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004658 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004659 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004660
Eli Friedman71b8fb52012-01-21 01:01:51 +00004661 // We might need to transform the operand if it is potentially evaluated.
4662 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4663 if (Operand.isInvalid()) {
4664 DS.SetTypeSpecError();
4665 return;
4666 }
4667
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004668 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004669 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004670 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4671 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004672 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004673 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004674}
Chris Lattner1b492422010-02-28 18:33:55 +00004675
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004676/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004677/// _Atomic ( type-name )
4678///
4679void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4680 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4681
4682 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004683 BalancedDelimiterTracker T(*this, tok::l_paren);
4684 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004685 SkipUntil(tok::r_paren);
4686 return;
4687 }
4688
4689 TypeResult Result = ParseTypeName();
4690 if (Result.isInvalid()) {
4691 SkipUntil(tok::r_paren);
4692 return;
4693 }
4694
4695 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004696 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004697
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004698 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004699 return;
4700
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004701 DS.setTypeofParensRange(T.getRange());
4702 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004703
4704 const char *PrevSpec = 0;
4705 unsigned DiagID;
4706 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4707 DiagID, Result.release()))
4708 Diag(StartLoc, DiagID) << PrevSpec;
4709}
4710
Chris Lattner1b492422010-02-28 18:33:55 +00004711
4712/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4713/// from TryAltiVecVectorToken.
4714bool Parser::TryAltiVecVectorTokenOutOfLine() {
4715 Token Next = NextToken();
4716 switch (Next.getKind()) {
4717 default: return false;
4718 case tok::kw_short:
4719 case tok::kw_long:
4720 case tok::kw_signed:
4721 case tok::kw_unsigned:
4722 case tok::kw_void:
4723 case tok::kw_char:
4724 case tok::kw_int:
4725 case tok::kw_float:
4726 case tok::kw_double:
4727 case tok::kw_bool:
4728 case tok::kw___pixel:
4729 Tok.setKind(tok::kw___vector);
4730 return true;
4731 case tok::identifier:
4732 if (Next.getIdentifierInfo() == Ident_pixel) {
4733 Tok.setKind(tok::kw___vector);
4734 return true;
4735 }
4736 return false;
4737 }
4738}
4739
4740bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4741 const char *&PrevSpec, unsigned &DiagID,
4742 bool &isInvalid) {
4743 if (Tok.getIdentifierInfo() == Ident_vector) {
4744 Token Next = NextToken();
4745 switch (Next.getKind()) {
4746 case tok::kw_short:
4747 case tok::kw_long:
4748 case tok::kw_signed:
4749 case tok::kw_unsigned:
4750 case tok::kw_void:
4751 case tok::kw_char:
4752 case tok::kw_int:
4753 case tok::kw_float:
4754 case tok::kw_double:
4755 case tok::kw_bool:
4756 case tok::kw___pixel:
4757 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4758 return true;
4759 case tok::identifier:
4760 if (Next.getIdentifierInfo() == Ident_pixel) {
4761 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4762 return true;
4763 }
4764 break;
4765 default:
4766 break;
4767 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004768 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004769 DS.isTypeAltiVecVector()) {
4770 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4771 return true;
4772 }
4773 return false;
4774}