blob: 75c423340c36620090099898f7ca8a8817785a3d [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"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnereaaebc72009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000056
57/// isAttributeLateParsed - Return true if the attribute has arguments that
58/// require late parsing.
59static bool isAttributeLateParsed(const IdentifierInfo &II) {
60 return llvm::StringSwitch<bool>(II.getName())
61#include "clang/Parse/AttrLateParsed.inc"
62 .Default(false);
63}
64
65
Sean Huntbbd37c62009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000067///
68/// [GNU] attributes:
69/// attribute
70/// attributes attribute
71///
72/// [GNU] attribute:
73/// '__attribute__' '(' '(' attribute-list ')' ')'
74///
75/// [GNU] attribute-list:
76/// attrib
77/// attribute_list ',' attrib
78///
79/// [GNU] attrib:
80/// empty
81/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
85///
86/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000091///
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000093/// token lookahead. Comment from gcc: "If they start with an identifier
94/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// start with that identifier; otherwise they are an expression list."
96///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000097/// GCC does not require the ',' between attribs in an attribute-list.
98///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
103
John McCall7f040a92010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000114 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall7f040a92010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000192 IdentifierInfo *ParmName = 0;
193 SourceLocation ParmLoc;
194 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 switch (Tok.getKind()) {
197 case tok::kw_char:
198 case tok::kw_wchar_t:
199 case tok::kw_char16_t:
200 case tok::kw_char32_t:
201 case tok::kw_bool:
202 case tok::kw_short:
203 case tok::kw_int:
204 case tok::kw_long:
205 case tok::kw___int64:
206 case tok::kw_signed:
207 case tok::kw_unsigned:
208 case tok::kw_float:
209 case tok::kw_double:
210 case tok::kw_void:
211 case tok::kw_typeof:
212 // __attribute__(( vec_type_hint(char) ))
213 // FIXME: Don't just discard the builtin type token.
214 ConsumeToken();
215 BuiltinType = true;
216 break;
217
218 case tok::identifier:
219 ParmName = Tok.getIdentifierInfo();
220 ParmLoc = ConsumeToken();
221 break;
222
223 default:
224 break;
225 }
226
227 ExprVector ArgExprs(Actions);
228
229 if (!BuiltinType &&
230 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
231 // Eat the comma.
232 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000233 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000235 // Parse the non-empty comma-separated list of expressions.
236 while (1) {
237 ExprResult ArgExpr(ParseAssignmentExpression());
238 if (ArgExpr.isInvalid()) {
239 SkipUntil(tok::r_paren);
240 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000242 ArgExprs.push_back(ArgExpr.release());
243 if (Tok.isNot(tok::comma))
244 break;
245 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000248 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
249 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
250 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000251 while (Tok.is(tok::identifier)) {
252 ConsumeToken();
253 if (Tok.is(tok::greater))
254 break;
255 if (Tok.is(tok::comma)) {
256 ConsumeToken();
257 continue;
258 }
259 }
260 if (Tok.isNot(tok::greater))
261 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000262 SkipUntil(tok::r_paren, false, true); // skip until ')'
263 }
264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265
266 SourceLocation RParen = Tok.getLocation();
267 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
268 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000269 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
271 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
272 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000273 }
274}
275
276
Eli Friedmana23b4852009-06-08 07:21:15 +0000277/// ParseMicrosoftDeclSpec - Parse an __declspec construct
278///
279/// [MS] decl-specifier:
280/// __declspec ( extended-decl-modifier-seq )
281///
282/// [MS] extended-decl-modifier-seq:
283/// extended-decl-modifier[opt]
284/// extended-decl-modifier extended-decl-modifier-seq
285
John McCall7f040a92010-12-24 02:08:15 +0000286void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000287 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000288
Steve Narofff59e17e2008-12-24 20:59:21 +0000289 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000290 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
291 "declspec")) {
292 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000293 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 }
Francois Pichet373197b2011-05-07 19:04:49 +0000295
Eli Friedman290eeb02009-06-08 23:27:34 +0000296 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000299
300 // FIXME: Remove this when we have proper __declspec(property()) support.
301 // Just skip everything inside property().
302 if (AttrName->getName() == "property") {
303 ConsumeParen();
304 SkipUntil(tok::r_paren);
305 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000306 if (Tok.is(tok::l_paren)) {
307 ConsumeParen();
308 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
309 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000310 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000312 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000313 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
314 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 }
316 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
317 SkipUntil(tok::r_paren, false);
318 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
320 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000321 }
322 }
323 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
324 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000325 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000326}
327
John McCall7f040a92010-12-24 02:08:15 +0000328void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000329 // Treat these like attributes
330 // FIXME: Allow Sema to distinguish between these and real attributes!
331 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000332 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000333 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000334 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000335 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000336 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
337 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000338 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
339 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000340 // FIXME: Support these properly!
341 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000345}
346
John McCall7f040a92010-12-24 02:08:15 +0000347void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000348 // Treat these like attributes
349 while (Tok.is(tok::kw___pascal)) {
350 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
351 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000352 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 }
John McCall7f040a92010-12-24 02:08:15 +0000355}
356
Peter Collingbournef315fa82011-02-14 01:42:53 +0000357void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
358 // Treat these like attributes
359 while (Tok.is(tok::kw___kernel)) {
360 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000361 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
362 AttrNameLoc, 0, AttrNameLoc, 0,
363 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000364 }
365}
366
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000367void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
368 SourceLocation Loc = Tok.getLocation();
369 switch(Tok.getKind()) {
370 // OpenCL qualifiers:
371 case tok::kw___private:
372 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413 default: break;
414 }
415}
416
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000417/// \brief Parse a version number.
418///
419/// version:
420/// simple-integer
421/// simple-integer ',' simple-integer
422/// simple-integer ',' simple-integer ',' simple-integer
423VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
424 Range = Tok.getLocation();
425
426 if (!Tok.is(tok::numeric_constant)) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 // Parse the major (and possibly minor and subminor) versions, which
433 // are stored in the numeric constant. We utilize a quirk of the
434 // lexer, which is that it handles something like 1.2.3 as a single
435 // numeric constant, rather than two separate tokens.
436 llvm::SmallString<512> Buffer;
437 Buffer.resize(Tok.getLength()+1);
438 const char *ThisTokBegin = &Buffer[0];
439
440 // Get the spelling of the token, which eliminates trigraphs, etc.
441 bool Invalid = false;
442 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
443 if (Invalid)
444 return VersionTuple();
445
446 // Parse the major version.
447 unsigned AfterMajor = 0;
448 unsigned Major = 0;
449 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
450 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
451 ++AfterMajor;
452 }
453
454 if (AfterMajor == 0) {
455 Diag(Tok, diag::err_expected_version);
456 SkipUntil(tok::comma, tok::r_paren, true, true, true);
457 return VersionTuple();
458 }
459
460 if (AfterMajor == ActualLength) {
461 ConsumeToken();
462
463 // We only had a single version component.
464 if (Major == 0) {
465 Diag(Tok, diag::err_zero_version);
466 return VersionTuple();
467 }
468
469 return VersionTuple(Major);
470 }
471
472 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 // Parse the minor version.
479 unsigned AfterMinor = AfterMajor + 1;
480 unsigned Minor = 0;
481 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
482 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
483 ++AfterMinor;
484 }
485
486 if (AfterMinor == ActualLength) {
487 ConsumeToken();
488
489 // We had major.minor.
490 if (Major == 0 && Minor == 0) {
491 Diag(Tok, diag::err_zero_version);
492 return VersionTuple();
493 }
494
495 return VersionTuple(Major, Minor);
496 }
497
498 // If what follows is not a '.', we have a problem.
499 if (ThisTokBegin[AfterMinor] != '.') {
500 Diag(Tok, diag::err_expected_version);
501 SkipUntil(tok::comma, tok::r_paren, true, true, true);
502 return VersionTuple();
503 }
504
505 // Parse the subminor version.
506 unsigned AfterSubminor = AfterMinor + 1;
507 unsigned Subminor = 0;
508 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
509 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
510 ++AfterSubminor;
511 }
512
513 if (AfterSubminor != ActualLength) {
514 Diag(Tok, diag::err_expected_version);
515 SkipUntil(tok::comma, tok::r_paren, true, true, true);
516 return VersionTuple();
517 }
518 ConsumeToken();
519 return VersionTuple(Major, Minor, Subminor);
520}
521
522/// \brief Parse the contents of the "availability" attribute.
523///
524/// availability-attribute:
525/// 'availability' '(' platform ',' version-arg-list ')'
526///
527/// platform:
528/// identifier
529///
530/// version-arg-list:
531/// version-arg
532/// version-arg ',' version-arg-list
533///
534/// version-arg:
535/// 'introduced' '=' version
536/// 'deprecated' '=' version
537/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000538/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
540 SourceLocation AvailabilityLoc,
541 ParsedAttributes &attrs,
542 SourceLocation *endLoc) {
543 SourceLocation PlatformLoc;
544 IdentifierInfo *Platform = 0;
545
546 enum { Introduced, Deprecated, Obsoleted, Unknown };
547 AvailabilityChange Changes[Unknown];
548
549 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
551 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000552 Diag(Tok, diag::err_expected_lparen);
553 return;
554 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555
556 // Parse the platform name,
557 if (Tok.isNot(tok::identifier)) {
558 Diag(Tok, diag::err_availability_expected_platform);
559 SkipUntil(tok::r_paren);
560 return;
561 }
562 Platform = Tok.getIdentifierInfo();
563 PlatformLoc = ConsumeToken();
564
565 // Parse the ',' following the platform name.
566 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
567 return;
568
569 // If we haven't grabbed the pointers for the identifiers
570 // "introduced", "deprecated", and "obsoleted", do so now.
571 if (!Ident_introduced) {
572 Ident_introduced = PP.getIdentifierInfo("introduced");
573 Ident_deprecated = PP.getIdentifierInfo("deprecated");
574 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000575 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000576 }
577
578 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000579 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000580 do {
581 if (Tok.isNot(tok::identifier)) {
582 Diag(Tok, diag::err_availability_expected_change);
583 SkipUntil(tok::r_paren);
584 return;
585 }
586 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
587 SourceLocation KeywordLoc = ConsumeToken();
588
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 if (Keyword == Ident_unavailable) {
590 if (UnavailableLoc.isValid()) {
591 Diag(KeywordLoc, diag::err_availability_redundant)
592 << Keyword << SourceRange(UnavailableLoc);
593 }
594 UnavailableLoc = KeywordLoc;
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 continue;
601 }
602
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000603 if (Tok.isNot(tok::equal)) {
604 Diag(Tok, diag::err_expected_equal_after)
605 << Keyword;
606 SkipUntil(tok::r_paren);
607 return;
608 }
609 ConsumeToken();
610
611 SourceRange VersionRange;
612 VersionTuple Version = ParseVersionTuple(VersionRange);
613
614 if (Version.empty()) {
615 SkipUntil(tok::r_paren);
616 return;
617 }
618
619 unsigned Index;
620 if (Keyword == Ident_introduced)
621 Index = Introduced;
622 else if (Keyword == Ident_deprecated)
623 Index = Deprecated;
624 else if (Keyword == Ident_obsoleted)
625 Index = Obsoleted;
626 else
627 Index = Unknown;
628
629 if (Index < Unknown) {
630 if (!Changes[Index].KeywordLoc.isInvalid()) {
631 Diag(KeywordLoc, diag::err_availability_redundant)
632 << Keyword
633 << SourceRange(Changes[Index].KeywordLoc,
634 Changes[Index].VersionRange.getEnd());
635 }
636
637 Changes[Index].KeywordLoc = KeywordLoc;
638 Changes[Index].Version = Version;
639 Changes[Index].VersionRange = VersionRange;
640 } else {
641 Diag(KeywordLoc, diag::err_availability_unknown_change)
642 << Keyword << VersionRange;
643 }
644
645 if (Tok.isNot(tok::comma))
646 break;
647
648 ConsumeToken();
649 } while (true);
650
651 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000653 return;
654
655 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000656 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000657
Douglas Gregorb53e4172011-03-26 03:35:55 +0000658 // The 'unavailable' availability cannot be combined with any other
659 // availability changes. Make sure that hasn't happened.
660 if (UnavailableLoc.isValid()) {
661 bool Complained = false;
662 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
663 if (Changes[Index].KeywordLoc.isValid()) {
664 if (!Complained) {
665 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
666 << SourceRange(Changes[Index].KeywordLoc,
667 Changes[Index].VersionRange.getEnd());
668 Complained = true;
669 }
670
671 // Clear out the availability.
672 Changes[Index] = AvailabilityChange();
673 }
674 }
675 }
676
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000678 attrs.addNew(&Availability,
679 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000680 0, SourceLocation(),
681 Platform, PlatformLoc,
682 Changes[Introduced],
683 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000684 Changes[Obsoleted],
685 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686}
687
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000688
689// Late Parsed Attributes:
690// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
691
692void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
693
694void Parser::LateParsedClass::ParseLexedAttributes() {
695 Self->ParseLexedAttributes(*Class);
696}
697
698void Parser::LateParsedAttribute::ParseLexedAttributes() {
699 Self->ParseLexedAttribute(*this);
700}
701
702/// Wrapper class which calls ParseLexedAttribute, after setting up the
703/// scope appropriately.
704void Parser::ParseLexedAttributes(ParsingClass &Class) {
705 // Deal with templates
706 // FIXME: Test cases to make sure this does the right thing for templates.
707 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
708 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
709 HasTemplateScope);
710 if (HasTemplateScope)
711 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
712
713 // Set or update the scope flags to include Scope::ThisScope.
714 bool AlreadyHasClassScope = Class.TopLevelClass;
715 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
716 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
717 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
718
719 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
720 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
721 }
722}
723
724/// \brief Finish parsing an attribute for which parsing was delayed.
725/// This will be called at the end of parsing a class declaration
726/// for each LateParsedAttribute. We consume the saved tokens and
727/// create an attribute with the arguments filled in. We add this
728/// to the Attribute list for the decl.
729void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
730 // Save the current token position.
731 SourceLocation OrigLoc = Tok.getLocation();
732
733 // Append the current token at the end of the new token stream so that it
734 // doesn't get lost.
735 LA.Toks.push_back(Tok);
736 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
737 // Consume the previously pushed token.
738 ConsumeAnyToken();
739
740 ParsedAttributes Attrs(AttrFactory);
741 SourceLocation endLoc;
742
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000743 // If the Decl is templatized, add template parameters to scope.
744 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
745 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
746 if (HasTemplateScope)
747 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
748
749 // If the Decl is on a function, add function parameters to the scope.
750 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
751 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
752 if (HasFunctionScope)
753 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
754
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
756
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000757 if (HasFunctionScope) {
758 Actions.ActOnExitFunctionContext();
759 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
760 }
761 if (HasTemplateScope) {
762 TempScope.Exit();
763 }
764
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000765 // Late parsed attributes must be attached to Decls by hand. If the
766 // LA.D is not set, then this was not done properly.
767 assert(LA.D && "No decl attached to late parsed attribute");
768 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
769
770 if (Tok.getLocation() != OrigLoc) {
771 // Due to a parsing error, we either went over the cached tokens or
772 // there are still cached tokens left, so we skip the leftover tokens.
773 // Since this is an uncommon situation that should be avoided, use the
774 // expensive isBeforeInTranslationUnit call.
775 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
776 OrigLoc))
777 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
778 ConsumeAnyToken();
779 }
780}
781
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000782/// \brief Wrapper around a case statement checking if AttrName is
783/// one of the thread safety attributes
784bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
785 return llvm::StringSwitch<bool>(AttrName)
786 .Case("guarded_by", true)
787 .Case("guarded_var", true)
788 .Case("pt_guarded_by", true)
789 .Case("pt_guarded_var", true)
790 .Case("lockable", true)
791 .Case("scoped_lockable", true)
792 .Case("no_thread_safety_analysis", true)
793 .Case("acquired_after", true)
794 .Case("acquired_before", true)
795 .Case("exclusive_lock_function", true)
796 .Case("shared_lock_function", true)
797 .Case("exclusive_trylock_function", true)
798 .Case("shared_trylock_function", true)
799 .Case("unlock_function", true)
800 .Case("lock_returned", true)
801 .Case("locks_excluded", true)
802 .Case("exclusive_locks_required", true)
803 .Case("shared_locks_required", true)
804 .Default(false);
805}
806
807/// \brief Parse the contents of thread safety attributes. These
808/// should always be parsed as an expression list.
809///
810/// We need to special case the parsing due to the fact that if the first token
811/// of the first argument is an identifier, the main parse loop will store
812/// that token as a "parameter" and the rest of
813/// the arguments will be added to a list of "arguments". However,
814/// subsequent tokens in the first argument are lost. We instead parse each
815/// argument as an expression and add all arguments to the list of "arguments".
816/// In future, we will take advantage of this special case to also
817/// deal with some argument scoping issues here (for example, referring to a
818/// function parameter in the attribute on that function).
819void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
820 SourceLocation AttrNameLoc,
821 ParsedAttributes &Attrs,
822 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000823 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000824
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000825 BalancedDelimiterTracker T(*this, tok::l_paren);
826 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827
828 ExprVector ArgExprs(Actions);
829 bool ArgExprsOk = true;
830
831 // now parse the list of expressions
832 while (1) {
833 ExprResult ArgExpr(ParseAssignmentExpression());
834 if (ArgExpr.isInvalid()) {
835 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000836 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 break;
838 } else {
839 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 if (Tok.isNot(tok::comma))
842 break;
843 ConsumeToken(); // Eat the comma, move to the next argument
844 }
845 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000846 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000847 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
848 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000850 if (EndLoc)
851 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000852}
853
John McCall7f040a92010-12-24 02:08:15 +0000854void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
855 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
856 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000857}
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859/// ParseDeclaration - Parse a full 'declaration', which consists of
860/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000861/// 'Context' should be a Declarator::TheContext value. This returns the
862/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000863///
864/// declaration: [C99 6.7]
865/// block-declaration ->
866/// simple-declaration
867/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000868/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000869/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000870/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000871/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000872/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000873/// others... [FIXME]
874///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000875Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
876 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000877 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000878 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000879 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000880 // Must temporarily exit the objective-c container scope for
881 // parsing c none objective-c decls.
882 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000883
John McCalld226f652010-08-21 09:40:31 +0000884 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000885 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000886 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000887 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000888 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000889 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000890 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000891 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000892 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000893 // Could be the start of an inline namespace. Allowed as an ext in C++03.
894 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000895 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000896 SourceLocation InlineLoc = ConsumeToken();
897 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
898 break;
899 }
John McCall7f040a92010-12-24 02:08:15 +0000900 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000901 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000902 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000904 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000905 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000906 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000907 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000908 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000909 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000910 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000911 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000912 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000913 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000914 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000915 default:
John McCall7f040a92010-12-24 02:08:15 +0000916 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000917 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000918
Chris Lattner682bf922009-03-29 16:50:03 +0000919 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000920 // single decl, convert it now. Alias declarations can also declare a type;
921 // include that too if it is present.
922 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000923}
924
925/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
926/// declaration-specifiers init-declarator-list[opt] ';'
927///[C90/C++]init-declarator-list ';' [TODO]
928/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000929///
Richard Smithad762fc2011-04-14 22:09:26 +0000930/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
931/// attribute-specifier-seq[opt] type-specifier-seq declarator
932///
Chris Lattnercd147752009-03-29 17:27:48 +0000933/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000934/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000935///
936/// If FRI is non-null, we might be parsing a for-range-declaration instead
937/// of a simple-declaration. If we find that we are, we also parse the
938/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000939Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
940 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000941 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000942 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000943 bool RequireSemi,
944 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000946 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000947 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000948
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000949 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000950 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000951 StmtResult R = Actions.ActOnVlaStmt(DS);
952 if (R.isUsable())
953 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
956 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000957 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000958 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000959 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000960 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000961 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000962 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000964
965 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000966}
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Richard Smith0706df42011-10-19 21:33:05 +0000968/// Returns true if this might be the start of a declarator, or a common typo
969/// for a declarator.
970bool Parser::MightBeDeclarator(unsigned Context) {
971 switch (Tok.getKind()) {
972 case tok::annot_cxxscope:
973 case tok::annot_template_id:
974 case tok::caret:
975 case tok::code_completion:
976 case tok::coloncolon:
977 case tok::ellipsis:
978 case tok::kw___attribute:
979 case tok::kw_operator:
980 case tok::l_paren:
981 case tok::star:
982 return true;
983
984 case tok::amp:
985 case tok::ampamp:
986 case tok::colon: // Might be a typo for '::'.
987 return getLang().CPlusPlus;
988
989 case tok::identifier:
990 switch (NextToken().getKind()) {
991 case tok::code_completion:
992 case tok::coloncolon:
993 case tok::comma:
994 case tok::equal:
995 case tok::equalequal: // Might be a typo for '='.
996 case tok::kw_alignas:
997 case tok::kw_asm:
998 case tok::kw___attribute:
999 case tok::l_brace:
1000 case tok::l_paren:
1001 case tok::l_square:
1002 case tok::less:
1003 case tok::r_brace:
1004 case tok::r_paren:
1005 case tok::r_square:
1006 case tok::semi:
1007 return true;
1008
1009 case tok::colon:
1010 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
1011 // and in block scope it's probably a label.
1012 return getLang().CPlusPlus && Context == Declarator::FileContext;
1013
1014 default:
1015 return false;
1016 }
1017
1018 default:
1019 return false;
1020 }
1021}
1022
John McCalld8ac0572009-11-03 19:26:08 +00001023/// ParseDeclGroup - Having concluded that this is either a function
1024/// definition or a group of object declarations, actually parse the
1025/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001026Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1027 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001028 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001029 SourceLocation *DeclEnd,
1030 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001031 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001032 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001033 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001034
John McCalld8ac0572009-11-03 19:26:08 +00001035 // Bail out if the first declarator didn't seem well-formed.
1036 if (!D.hasName() && !D.mayOmitIdentifier()) {
1037 // Skip until ; or }.
1038 SkipUntil(tok::r_brace, true, true);
1039 if (Tok.is(tok::semi))
1040 ConsumeToken();
1041 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Richard Smith874d2532011-11-29 05:27:40 +00001044 // Do we have a stray semicolon in the middle of a function definition?
1045 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1046 Tok.is(tok::semi) && Context == Declarator::FileContext) {
1047 const Token &Next = NextToken();
1048 if (Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1049 (getLang().CPlusPlus &&
1050 (Next.is(tok::colon) || Next.is(tok::equal)))) {
1051 // Pretend we didn't see the semicolon.
1052 SourceLocation SemiLoc = ConsumeToken();
1053 Diag(SemiLoc, diag::err_stray_semi_function_definition)
1054 << FixItHint::CreateRemoval(SemiLoc);
1055 assert(isStartOfFunctionDefinition(D) && "expected a function defn");
1056 }
1057 }
1058
Chris Lattnerc82daef2010-07-11 22:24:20 +00001059 // Check to see if we have a function *definition* which must have a body.
1060 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1061 // Look at the next token to make sure that this isn't a function
1062 // declaration. We have to check this because __attribute__ might be the
1063 // start of a function definition in GCC-extended K&R C.
1064 !isDeclarationAfterDeclarator()) {
Richard Smith874d2532011-11-29 05:27:40 +00001065
Chris Lattner004659a2010-07-11 22:42:07 +00001066 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001067 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1068 Diag(Tok, diag::err_function_declared_typedef);
1069
1070 // Recover by treating the 'typedef' as spurious.
1071 DS.ClearStorageClassSpecs();
1072 }
1073
John McCalld226f652010-08-21 09:40:31 +00001074 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001075 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001076 }
1077
1078 if (isDeclarationSpecifier()) {
1079 // If there is an invalid declaration specifier right after the function
1080 // prototype, then we must be in a missing semicolon case where this isn't
1081 // actually a body. Just fall through into the code that handles it as a
1082 // prototype, and let the top-level code handle the erroneous declspec
1083 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001084 } else {
1085 Diag(Tok, diag::err_expected_fn_body);
1086 SkipUntil(tok::semi);
1087 return DeclGroupPtrTy();
1088 }
1089 }
1090
Richard Smithad762fc2011-04-14 22:09:26 +00001091 if (ParseAttributesAfterDeclarator(D))
1092 return DeclGroupPtrTy();
1093
1094 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1095 // must parse and analyze the for-range-initializer before the declaration is
1096 // analyzed.
1097 if (FRI && Tok.is(tok::colon)) {
1098 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001099 if (Tok.is(tok::l_brace))
1100 FRI->RangeExpr = ParseBraceInitializer();
1101 else
1102 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001103 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1104 Actions.ActOnCXXForRangeDecl(ThisDecl);
1105 Actions.FinalizeDeclaration(ThisDecl);
1106 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1107 }
1108
Chris Lattner5f9e2722011-07-23 10:55:15 +00001109 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001110 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001111 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001112 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001113 DeclsInGroup.push_back(FirstDecl);
1114
Richard Smith0706df42011-10-19 21:33:05 +00001115 bool ExpectSemi = Context != Declarator::ForContext;
1116
John McCalld8ac0572009-11-03 19:26:08 +00001117 // If we don't have a comma, it is either the end of the list (a ';') or an
1118 // error, bail out.
1119 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001120 SourceLocation CommaLoc = ConsumeToken();
1121
1122 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1123 // This comma was followed by a line-break and something which can't be
1124 // the start of a declarator. The comma was probably a typo for a
1125 // semicolon.
1126 Diag(CommaLoc, diag::err_expected_semi_declaration)
1127 << FixItHint::CreateReplacement(CommaLoc, ";");
1128 ExpectSemi = false;
1129 break;
1130 }
John McCalld8ac0572009-11-03 19:26:08 +00001131
1132 // Parse the next declarator.
1133 D.clear();
1134
1135 // Accept attributes in an init-declarator. In the first declarator in a
1136 // declaration, these would be part of the declspec. In subsequent
1137 // declarators, they become part of the declarator itself, so that they
1138 // don't apply to declarators after *this* one. Examples:
1139 // short __attribute__((common)) var; -> declspec
1140 // short var __attribute__((common)); -> declarator
1141 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001142 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001143
1144 ParseDeclarator(D);
1145
John McCalld226f652010-08-21 09:40:31 +00001146 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001147 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001148 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001149 DeclsInGroup.push_back(ThisDecl);
1150 }
1151
1152 if (DeclEnd)
1153 *DeclEnd = Tok.getLocation();
1154
Richard Smith0706df42011-10-19 21:33:05 +00001155 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001156 ExpectAndConsume(tok::semi,
1157 Context == Declarator::FileContext
1158 ? diag::err_invalid_token_after_toplevel_declarator
1159 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001160 // Okay, there was no semicolon and one was expected. If we see a
1161 // declaration specifier, just assume it was missing and continue parsing.
1162 // Otherwise things are very confused and we skip to recover.
1163 if (!isDeclarationSpecifier()) {
1164 SkipUntil(tok::r_brace, true, true);
1165 if (Tok.is(tok::semi))
1166 ConsumeToken();
1167 }
John McCalld8ac0572009-11-03 19:26:08 +00001168 }
1169
Douglas Gregor23c94db2010-07-02 17:43:08 +00001170 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001171 DeclsInGroup.data(),
1172 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001173}
1174
Richard Smithad762fc2011-04-14 22:09:26 +00001175/// Parse an optional simple-asm-expr and attributes, and attach them to a
1176/// declarator. Returns true on an error.
1177bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1178 // If a simple-asm-expr is present, parse it.
1179 if (Tok.is(tok::kw_asm)) {
1180 SourceLocation Loc;
1181 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1182 if (AsmLabel.isInvalid()) {
1183 SkipUntil(tok::semi, true, true);
1184 return true;
1185 }
1186
1187 D.setAsmLabel(AsmLabel.release());
1188 D.SetRangeEnd(Loc);
1189 }
1190
1191 MaybeParseGNUAttributes(D);
1192 return false;
1193}
1194
Douglas Gregor1426e532009-05-12 21:31:51 +00001195/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1196/// declarator'. This method parses the remainder of the declaration
1197/// (including any attributes or initializer, among other things) and
1198/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001199///
Reid Spencer5f016e22007-07-11 17:01:13 +00001200/// init-declarator: [C99 6.7]
1201/// declarator
1202/// declarator '=' initializer
1203/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1204/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001205/// [C++] declarator initializer[opt]
1206///
1207/// [C++] initializer:
1208/// [C++] '=' initializer-clause
1209/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001210/// [C++0x] '=' 'default' [TODO]
1211/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001212/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001213///
1214/// According to the standard grammar, =default and =delete are function
1215/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001216///
John McCalld226f652010-08-21 09:40:31 +00001217Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001218 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001219 if (ParseAttributesAfterDeclarator(D))
1220 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Richard Smithad762fc2011-04-14 22:09:26 +00001222 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1223}
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Richard Smithad762fc2011-04-14 22:09:26 +00001225Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1226 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001227 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001228 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001229 switch (TemplateInfo.Kind) {
1230 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001231 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001232 break;
1233
1234 case ParsedTemplateInfo::Template:
1235 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001236 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001237 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001238 TemplateInfo.TemplateParams->data(),
1239 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001240 D);
1241 break;
1242
1243 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001244 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001245 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001246 TemplateInfo.ExternLoc,
1247 TemplateInfo.TemplateLoc,
1248 D);
1249 if (ThisRes.isInvalid()) {
1250 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001251 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001252 }
1253
1254 ThisDecl = ThisRes.get();
1255 break;
1256 }
1257 }
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Richard Smith34b41d92011-02-20 03:19:35 +00001259 bool TypeContainsAuto =
1260 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1261
Douglas Gregor1426e532009-05-12 21:31:51 +00001262 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001263 if (isTokenEqualOrMistypedEqualEqual(
1264 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001265 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001266 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001267 if (D.isFunctionDeclarator())
1268 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1269 << 1 /* delete */;
1270 else
1271 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001272 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001273 if (D.isFunctionDeclarator())
1274 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1275 << 1 /* delete */;
1276 else
1277 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001278 } else {
John McCall731ad842009-12-19 09:28:58 +00001279 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1280 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001281 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001282 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001283
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001284 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001285 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001286 cutOffParsing();
1287 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001288 }
1289
John McCall60d7b3a2010-08-24 06:29:42 +00001290 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001291
John McCall731ad842009-12-19 09:28:58 +00001292 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001293 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001294 ExitScope();
1295 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001296
Douglas Gregor1426e532009-05-12 21:31:51 +00001297 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001298 SkipUntil(tok::comma, true, true);
1299 Actions.ActOnInitializerError(ThisDecl);
1300 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001301 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1302 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001303 }
1304 } else if (Tok.is(tok::l_paren)) {
1305 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001306 BalancedDelimiterTracker T(*this, tok::l_paren);
1307 T.consumeOpen();
1308
Douglas Gregor1426e532009-05-12 21:31:51 +00001309 ExprVector Exprs(Actions);
1310 CommaLocsTy CommaLocs;
1311
Douglas Gregorb4debae2009-12-22 17:47:17 +00001312 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1313 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001314 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001315 }
1316
Douglas Gregor1426e532009-05-12 21:31:51 +00001317 if (ParseExpressionList(Exprs, CommaLocs)) {
1318 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001319
1320 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001321 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001322 ExitScope();
1323 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001324 } else {
1325 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001326 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001327
1328 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1329 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001330
1331 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001332 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001333 ExitScope();
1334 }
1335
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001336 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001337 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001338 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001339 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001340 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001341 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1342 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001343 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1344
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001345 if (D.getCXXScopeSpec().isSet()) {
1346 EnterScope(0);
1347 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1348 }
1349
1350 ExprResult Init(ParseBraceInitializer());
1351
1352 if (D.getCXXScopeSpec().isSet()) {
1353 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1354 ExitScope();
1355 }
1356
1357 if (Init.isInvalid()) {
1358 Actions.ActOnInitializerError(ThisDecl);
1359 } else
1360 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1361 /*DirectInit=*/true, TypeContainsAuto);
1362
Douglas Gregor1426e532009-05-12 21:31:51 +00001363 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001364 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001365 }
1366
Richard Smith483b9f32011-02-21 20:05:19 +00001367 Actions.FinalizeDeclaration(ThisDecl);
1368
Douglas Gregor1426e532009-05-12 21:31:51 +00001369 return ThisDecl;
1370}
1371
Reid Spencer5f016e22007-07-11 17:01:13 +00001372/// ParseSpecifierQualifierList
1373/// specifier-qualifier-list:
1374/// type-specifier specifier-qualifier-list[opt]
1375/// type-qualifier specifier-qualifier-list[opt]
1376/// [GNU] attributes specifier-qualifier-list[opt]
1377///
Richard Smithc89edf52011-07-01 19:46:12 +00001378void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1380 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001381 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001382 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // Validate declspec for type-name.
1385 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001386 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001387 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 // Issue diagnostic and remove storage class if present.
1391 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1392 if (DS.getStorageClassSpecLoc().isValid())
1393 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1394 else
1395 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1396 DS.ClearStorageClassSpecs();
1397 }
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 // Issue diagnostic and remove function specfier if present.
1400 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001401 if (DS.isInlineSpecified())
1402 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1403 if (DS.isVirtualSpecified())
1404 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1405 if (DS.isExplicitSpecified())
1406 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 DS.ClearFunctionSpecs();
1408 }
1409}
1410
Chris Lattnerc199ab32009-04-12 20:42:31 +00001411/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1412/// specified token is valid after the identifier in a declarator which
1413/// immediately follows the declspec. For example, these things are valid:
1414///
1415/// int x [ 4]; // direct-declarator
1416/// int x ( int y); // direct-declarator
1417/// int(int x ) // direct-declarator
1418/// int x ; // simple-declaration
1419/// int x = 17; // init-declarator-list
1420/// int x , y; // init-declarator-list
1421/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001422/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001423/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001424///
1425/// This is not, because 'x' does not immediately follow the declspec (though
1426/// ')' happens to be valid anyway).
1427/// int (x)
1428///
1429static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1430 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1431 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001432 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001433}
1434
Chris Lattnere40c2952009-04-14 21:34:55 +00001435
1436/// ParseImplicitInt - This method is called when we have an non-typename
1437/// identifier in a declspec (which normally terminates the decl spec) when
1438/// the declspec has no type specifier. In this case, the declspec is either
1439/// malformed or is "implicit int" (in K&R and C89).
1440///
1441/// This method handles diagnosing this prettily and returns false if the
1442/// declspec is done being processed. If it recovers and thinks there may be
1443/// other pieces of declspec after it, it returns true.
1444///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001445bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001446 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001447 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001448 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Chris Lattnere40c2952009-04-14 21:34:55 +00001450 SourceLocation Loc = Tok.getLocation();
1451 // If we see an identifier that is not a type name, we normally would
1452 // parse it as the identifer being declared. However, when a typename
1453 // is typo'd or the definition is not included, this will incorrectly
1454 // parse the typename as the identifier name and fall over misparsing
1455 // later parts of the diagnostic.
1456 //
1457 // As such, we try to do some look-ahead in cases where this would
1458 // otherwise be an "implicit-int" case to see if this is invalid. For
1459 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1460 // an identifier with implicit int, we'd get a parse error because the
1461 // next token is obviously invalid for a type. Parse these as a case
1462 // with an invalid type specifier.
1463 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Chris Lattnere40c2952009-04-14 21:34:55 +00001465 // Since we know that this either implicit int (which is rare) or an
1466 // error, we'd do lookahead to try to do better recovery.
1467 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1468 // If this token is valid for implicit int, e.g. "static x = 4", then
1469 // we just avoid eating the identifier, so it will be parsed as the
1470 // identifier in the declarator.
1471 return false;
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattnere40c2952009-04-14 21:34:55 +00001474 // Otherwise, if we don't consume this token, we are going to emit an
1475 // error anyway. Try to recover from various common problems. Check
1476 // to see if this was a reference to a tag name without a tag specified.
1477 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001478 //
1479 // C++ doesn't need this, and isTagName doesn't take SS.
1480 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001481 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001482 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor23c94db2010-07-02 17:43:08 +00001484 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001485 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001486 case DeclSpec::TST_enum:
1487 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1488 case DeclSpec::TST_union:
1489 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1490 case DeclSpec::TST_struct:
1491 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1492 case DeclSpec::TST_class:
1493 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Chris Lattnerf4382f52009-04-14 22:17:06 +00001496 if (TagName) {
1497 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001498 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001499 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Chris Lattnerf4382f52009-04-14 22:17:06 +00001501 // Parse this as a tag as if the missing tag were present.
1502 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001503 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001504 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001505 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001506 return true;
1507 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregora786fdb2009-10-13 23:27:22 +00001510 // This is almost certainly an invalid type name. Let the action emit a
1511 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001512 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001513 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001514 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001515 // The action emitted a diagnostic, so we don't have to.
1516 if (T) {
1517 // The action has suggested that the type T could be used. Set that as
1518 // the type in the declaration specifiers, consume the would-be type
1519 // name token, and we're done.
1520 const char *PrevSpec;
1521 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001522 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001523 DS.SetRangeEnd(Tok.getLocation());
1524 ConsumeToken();
1525
1526 // There may be other declaration specifiers after this.
1527 return true;
1528 }
1529
1530 // Fall through; the action had no suggestion for us.
1531 } else {
1532 // The action did not emit a diagnostic, so emit one now.
1533 SourceRange R;
1534 if (SS) R = SS->getRange();
1535 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Douglas Gregora786fdb2009-10-13 23:27:22 +00001538 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001539 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001540 unsigned DiagID;
1541 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001542 DS.SetRangeEnd(Tok.getLocation());
1543 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Chris Lattnere40c2952009-04-14 21:34:55 +00001545 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1546 // avoid rippling error messages on subsequent uses of the same type,
1547 // could be useful if #include was forgotten.
1548 return false;
1549}
1550
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001551/// \brief Determine the declaration specifier context from the declarator
1552/// context.
1553///
1554/// \param Context the declarator context, which is one of the
1555/// Declarator::TheContext enumerator values.
1556Parser::DeclSpecContext
1557Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1558 if (Context == Declarator::MemberContext)
1559 return DSC_class;
1560 if (Context == Declarator::FileContext)
1561 return DSC_top_level;
1562 return DSC_normal;
1563}
1564
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001565/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1566///
1567/// FIXME: Simply returns an alignof() expression if the argument is a
1568/// type. Ideally, the type should be propagated directly into Sema.
1569///
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001570/// [C1X] type-id
1571/// [C1X] constant-expression
1572/// [C++0x] type-id ...[opt]
1573/// [C++0x] assignment-expression ...[opt]
1574ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1575 SourceLocation &EllipsisLoc) {
1576 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001577 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001578 SourceLocation TypeLoc = Tok.getLocation();
1579 ParsedType Ty = ParseTypeName().get();
1580 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001581 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1582 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001583 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001584 ER = ParseConstantExpression();
1585
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001586 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1587 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001588
1589 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001590}
1591
1592/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1593/// attribute to Attrs.
1594///
1595/// alignment-specifier:
1596/// [C1X] '_Alignas' '(' type-id ')'
1597/// [C1X] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001598/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1599/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001600void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1601 SourceLocation *endLoc) {
1602 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1603 "Not an alignment-specifier!");
1604
1605 SourceLocation KWLoc = Tok.getLocation();
1606 ConsumeToken();
1607
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001608 BalancedDelimiterTracker T(*this, tok::l_paren);
1609 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001610 return;
1611
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001612 SourceLocation EllipsisLoc;
1613 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001614 if (ArgExpr.isInvalid()) {
1615 SkipUntil(tok::r_paren);
1616 return;
1617 }
1618
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001619 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001620 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001621 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001622
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001623 // FIXME: Handle pack-expansions here.
1624 if (EllipsisLoc.isValid()) {
1625 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1626 return;
1627 }
1628
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001629 ExprVector ArgExprs(Actions);
1630 ArgExprs.push_back(ArgExpr.release());
1631 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001632 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001633}
1634
Reid Spencer5f016e22007-07-11 17:01:13 +00001635/// ParseDeclarationSpecifiers
1636/// declaration-specifiers: [C99 6.7]
1637/// storage-class-specifier declaration-specifiers[opt]
1638/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001639/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001640/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001641/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001642/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001643///
1644/// storage-class-specifier: [C99 6.7.1]
1645/// 'typedef'
1646/// 'extern'
1647/// 'static'
1648/// 'auto'
1649/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001650/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001651/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001652/// function-specifier: [C99 6.7.4]
1653/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001654/// [C++] 'virtual'
1655/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001656/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001657/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001658/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001659
Reid Spencer5f016e22007-07-11 17:01:13 +00001660///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001661void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001662 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001663 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001664 DeclSpecContext DSContext) {
1665 if (DS.getSourceRange().isInvalid()) {
1666 DS.SetRangeStart(Tok.getLocation());
1667 DS.SetRangeEnd(Tok.getLocation());
1668 }
1669
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001670 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001672 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001674 unsigned DiagID = 0;
1675
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001677
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001679 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001680 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001681 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1682 MaybeParseCXX0XAttributes(DS.getAttributes());
1683
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 // If this is not a declaration specifier token, we're done reading decl
1685 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001686 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001689 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001690 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001691 if (DS.hasTypeSpecifier()) {
1692 bool AllowNonIdentifiers
1693 = (getCurScope()->getFlags() & (Scope::ControlScope |
1694 Scope::BlockScope |
1695 Scope::TemplateParamScope |
1696 Scope::FunctionPrototypeScope |
1697 Scope::AtCatchScope)) == 0;
1698 bool AllowNestedNameSpecifiers
1699 = DSContext == DSC_top_level ||
1700 (DSContext == DSC_class && DS.isFriendSpecified());
1701
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001702 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1703 AllowNonIdentifiers,
1704 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001705 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001706 }
1707
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001708 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1709 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1710 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001711 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1712 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001713 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001714 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001715 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001716 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001717
1718 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001719 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001720 }
1721
Chris Lattner5e02c472009-01-05 00:07:25 +00001722 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001723 // C++ scope specifier. Annotate and loop, or bail out on error.
1724 if (TryAnnotateCXXScopeToken(true)) {
1725 if (!DS.hasTypeSpecifier())
1726 DS.SetTypeSpecError();
1727 goto DoneWithDeclSpec;
1728 }
John McCall2e0a7152010-03-01 18:20:46 +00001729 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1730 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001731 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001732
1733 case tok::annot_cxxscope: {
1734 if (DS.hasTypeSpecifier())
1735 goto DoneWithDeclSpec;
1736
John McCallaa87d332009-12-12 11:40:51 +00001737 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001738 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1739 Tok.getAnnotationRange(),
1740 SS);
John McCallaa87d332009-12-12 11:40:51 +00001741
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001742 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001743 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001744 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001745 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001746 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001747 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001748
1749 // C++ [class.qual]p2:
1750 // In a lookup in which the constructor is an acceptable lookup
1751 // result and the nested-name-specifier nominates a class C:
1752 //
1753 // - if the name specified after the
1754 // nested-name-specifier, when looked up in C, is the
1755 // injected-class-name of C (Clause 9), or
1756 //
1757 // - if the name specified after the nested-name-specifier
1758 // is the same as the identifier or the
1759 // simple-template-id's template-name in the last
1760 // component of the nested-name-specifier,
1761 //
1762 // the name is instead considered to name the constructor of
1763 // class C.
1764 //
1765 // Thus, if the template-name is actually the constructor
1766 // name, then the code is ill-formed; this interpretation is
1767 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001768 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001769 if ((DSContext == DSC_top_level ||
1770 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1771 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001772 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001773 if (isConstructorDeclarator()) {
1774 // The user meant this to be an out-of-line constructor
1775 // definition, but template arguments are not allowed
1776 // there. Just allow this as a constructor; we'll
1777 // complain about it later.
1778 goto DoneWithDeclSpec;
1779 }
1780
1781 // The user meant this to name a type, but it actually names
1782 // a constructor with some extraneous template
1783 // arguments. Complain, then parse it as a type as the user
1784 // intended.
1785 Diag(TemplateId->TemplateNameLoc,
1786 diag::err_out_of_line_template_id_names_constructor)
1787 << TemplateId->Name;
1788 }
1789
John McCallaa87d332009-12-12 11:40:51 +00001790 DS.getTypeSpecScope() = SS;
1791 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001792 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001793 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001794 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001795 continue;
1796 }
1797
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001798 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001799 DS.getTypeSpecScope() = SS;
1800 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001801 if (Tok.getAnnotationValue()) {
1802 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1804 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001805 PrevSpec, DiagID, T);
1806 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001807 else
1808 DS.SetTypeSpecError();
1809 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1810 ConsumeToken(); // The typename
1811 }
1812
Douglas Gregor9135c722009-03-25 15:40:00 +00001813 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001814 goto DoneWithDeclSpec;
1815
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001816 // If we're in a context where the identifier could be a class name,
1817 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001818 if ((DSContext == DSC_top_level ||
1819 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001820 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001821 &SS)) {
1822 if (isConstructorDeclarator())
1823 goto DoneWithDeclSpec;
1824
1825 // As noted in C++ [class.qual]p2 (cited above), when the name
1826 // of the class is qualified in a context where it could name
1827 // a constructor, its a constructor name. However, we've
1828 // looked at the declarator, and the user probably meant this
1829 // to be a type. Complain that it isn't supposed to be treated
1830 // as a type, then proceed to parse it as a type.
1831 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1832 << Next.getIdentifierInfo();
1833 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001834
John McCallb3d87482010-08-24 05:47:05 +00001835 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1836 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001837 getCurScope(), &SS,
1838 false, false, ParsedType(),
1839 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001840
Chris Lattnerf4382f52009-04-14 22:17:06 +00001841 // If the referenced identifier is not a type, then this declspec is
1842 // erroneous: We already checked about that it has no type specifier, and
1843 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001844 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001845 if (TypeRep == 0) {
1846 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001847 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001848 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850
John McCallaa87d332009-12-12 11:40:51 +00001851 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001852 ConsumeToken(); // The C++ scope.
1853
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001854 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001855 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001856 if (isInvalid)
1857 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001859 DS.SetRangeEnd(Tok.getLocation());
1860 ConsumeToken(); // The typename.
1861
1862 continue;
1863 }
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Chris Lattner80d0c892009-01-21 19:48:37 +00001865 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001866 if (Tok.getAnnotationValue()) {
1867 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001868 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001869 DiagID, T);
1870 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001871 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001872
1873 if (isInvalid)
1874 break;
1875
Chris Lattner80d0c892009-01-21 19:48:37 +00001876 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1877 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Chris Lattner80d0c892009-01-21 19:48:37 +00001879 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1880 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001881 // Objective-C interface.
1882 if (Tok.is(tok::less) && getLang().ObjC1)
1883 ParseObjCProtocolQualifiers(DS);
1884
Chris Lattner80d0c892009-01-21 19:48:37 +00001885 continue;
1886 }
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Douglas Gregorbfad9152011-04-28 15:48:45 +00001888 case tok::kw___is_signed:
1889 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1890 // typically treats it as a trait. If we see __is_signed as it appears
1891 // in libstdc++, e.g.,
1892 //
1893 // static const bool __is_signed;
1894 //
1895 // then treat __is_signed as an identifier rather than as a keyword.
1896 if (DS.getTypeSpecType() == TST_bool &&
1897 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1898 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1899 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1900 Tok.setKind(tok::identifier);
1901 }
1902
1903 // We're done with the declaration-specifiers.
1904 goto DoneWithDeclSpec;
1905
Chris Lattner3bd934a2008-07-26 01:18:38 +00001906 // typedef-name
1907 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001908 // In C++, check to see if this is a scope specifier like foo::bar::, if
1909 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001910 if (getLang().CPlusPlus) {
1911 if (TryAnnotateCXXScopeToken(true)) {
1912 if (!DS.hasTypeSpecifier())
1913 DS.SetTypeSpecError();
1914 goto DoneWithDeclSpec;
1915 }
1916 if (!Tok.is(tok::identifier))
1917 continue;
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Chris Lattner3bd934a2008-07-26 01:18:38 +00001920 // This identifier can only be a typedef name if we haven't already seen
1921 // a type-specifier. Without this check we misparse:
1922 // typedef int X; struct Y { short X; }; as 'short int'.
1923 if (DS.hasTypeSpecifier())
1924 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
John Thompson82287d12010-02-05 00:12:22 +00001926 // Check for need to substitute AltiVec keyword tokens.
1927 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1928 break;
1929
Chris Lattner3bd934a2008-07-26 01:18:38 +00001930 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001931 ParsedType TypeRep =
1932 Actions.getTypeName(*Tok.getIdentifierInfo(),
1933 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001934
Chris Lattnerc199ab32009-04-12 20:42:31 +00001935 // If this is not a typedef name, don't parse it as part of the declspec,
1936 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001937 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001938 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001939 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001940 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001941
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001942 // If we're in a context where the identifier could be a class name,
1943 // check whether this is a constructor declaration.
1944 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001945 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001946 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001947 goto DoneWithDeclSpec;
1948
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001950 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001951 if (isInvalid)
1952 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Chris Lattner3bd934a2008-07-26 01:18:38 +00001954 DS.SetRangeEnd(Tok.getLocation());
1955 ConsumeToken(); // The identifier
1956
1957 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1958 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001959 // Objective-C interface.
1960 if (Tok.is(tok::less) && getLang().ObjC1)
1961 ParseObjCProtocolQualifiers(DS);
1962
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001963 // Need to support trailing type qualifiers (e.g. "id<p> const").
1964 // If a type specifier follows, it will be diagnosed elsewhere.
1965 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001966 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001967
1968 // type-name
1969 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001970 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001971 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001972 // This template-id does not refer to a type name, so we're
1973 // done with the type-specifiers.
1974 goto DoneWithDeclSpec;
1975 }
1976
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001977 // If we're in a context where the template-id could be a
1978 // constructor name or specialization, check whether this is a
1979 // constructor declaration.
1980 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001981 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001982 isConstructorDeclarator())
1983 goto DoneWithDeclSpec;
1984
Douglas Gregor39a8de12009-02-25 19:37:18 +00001985 // Turn the template-id annotation token into a type annotation
1986 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001987 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001988 continue;
1989 }
1990
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 // GNU attributes support.
1992 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001993 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001995
1996 // Microsoft declspec support.
1997 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001998 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001999 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Steve Naroff239f0732008-12-25 14:16:32 +00002001 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002002 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002003 // FIXME: Add handling here!
2004 break;
2005
2006 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002007 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002008 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002009 case tok::kw___cdecl:
2010 case tok::kw___stdcall:
2011 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002012 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002013 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002014 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002015 continue;
2016
Dawn Perchik52fc3142010-09-03 01:29:35 +00002017 // Borland single token adornments.
2018 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002019 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002020 continue;
2021
Peter Collingbournef315fa82011-02-14 01:42:53 +00002022 // OpenCL single token adornments.
2023 case tok::kw___kernel:
2024 ParseOpenCLAttributes(DS.getAttributes());
2025 continue;
2026
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 // storage-class-specifier
2028 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002029 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2030 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 break;
2032 case tok::kw_extern:
2033 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002034 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002035 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2036 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002038 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002039 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2040 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002041 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 case tok::kw_static:
2043 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002044 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002045 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2046 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 break;
2048 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002049 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002050 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002051 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2052 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002053 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002054 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002055 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002056 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2058 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002059 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002060 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2061 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 break;
2063 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002064 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2065 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002067 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002068 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2069 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002070 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002072 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 // function-specifier
2076 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002079 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002081 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002082 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002084 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002085
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086 // alignment-specifier
2087 case tok::kw__Alignas:
2088 if (!getLang().C1X)
2089 Diag(Tok, diag::ext_c1x_alignas);
2090 ParseAlignmentSpecifier(DS.getAttributes());
2091 continue;
2092
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002093 // friend
2094 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002095 if (DSContext == DSC_class)
2096 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2097 else {
2098 PrevSpec = ""; // not actually used by the diagnostic
2099 DiagID = diag::err_friend_invalid_in_context;
2100 isInvalid = true;
2101 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002102 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Douglas Gregor8d267c52011-09-09 02:06:17 +00002104 // Modules
2105 case tok::kw___module_private__:
2106 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2107 break;
2108
Sebastian Redl2ac67232009-11-05 15:47:02 +00002109 // constexpr
2110 case tok::kw_constexpr:
2111 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2112 break;
2113
Chris Lattner80d0c892009-01-21 19:48:37 +00002114 // type-specifier
2115 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002116 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2117 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002118 break;
2119 case tok::kw_long:
2120 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002121 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2122 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002123 else
John McCallfec54012009-08-03 20:12:06 +00002124 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002127 case tok::kw___int64:
2128 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2129 DiagID);
2130 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002131 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002132 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2133 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002134 break;
2135 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002136 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2137 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002138 break;
2139 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002140 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2141 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002142 break;
2143 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002144 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2145 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002146 break;
2147 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2149 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002150 break;
2151 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2153 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002154 break;
2155 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2157 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002158 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002159 case tok::kw_half:
2160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2161 DiagID);
2162 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002163 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
2167 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2169 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002170 break;
2171 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2173 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002174 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002175 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002176 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2177 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002178 break;
2179 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2181 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002182 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002183 case tok::kw_bool:
2184 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002185 if (Tok.is(tok::kw_bool) &&
2186 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2187 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2188 PrevSpec = ""; // Not used by the diagnostic.
2189 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002190 // For better error recovery.
2191 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002192 isInvalid = true;
2193 } else {
2194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2195 DiagID);
2196 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002197 break;
2198 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002199 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2200 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002201 break;
2202 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002203 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2204 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002205 break;
2206 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2208 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002209 break;
John Thompson82287d12010-02-05 00:12:22 +00002210 case tok::kw___vector:
2211 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2212 break;
2213 case tok::kw___pixel:
2214 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2215 break;
John McCalla5fc4722011-04-09 22:50:59 +00002216 case tok::kw___unknown_anytype:
2217 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2218 PrevSpec, DiagID);
2219 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002220
2221 // class-specifier:
2222 case tok::kw_class:
2223 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002224 case tok::kw_union: {
2225 tok::TokenKind Kind = Tok.getKind();
2226 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002227 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002228 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002229 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002230
2231 // enum-specifier:
2232 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002233 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002234 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 continue;
2236
2237 // cv-qualifier:
2238 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002239 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2240 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002241 break;
2242 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002243 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2244 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002245 break;
2246 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002247 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2248 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 break;
2250
Douglas Gregord57959a2009-03-27 23:10:48 +00002251 // C++ typename-specifier:
2252 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002253 if (TryAnnotateTypeOrScopeToken()) {
2254 DS.SetTypeSpecError();
2255 goto DoneWithDeclSpec;
2256 }
2257 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002258 continue;
2259 break;
2260
Chris Lattner80d0c892009-01-21 19:48:37 +00002261 // GNU typeof support.
2262 case tok::kw_typeof:
2263 ParseTypeofSpecifier(DS);
2264 continue;
2265
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002266 case tok::kw_decltype:
2267 ParseDecltypeSpecifier(DS);
2268 continue;
2269
Sean Huntdb5d44b2011-05-19 05:37:45 +00002270 case tok::kw___underlying_type:
2271 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002272 continue;
2273
2274 case tok::kw__Atomic:
2275 ParseAtomicSpecifier(DS);
2276 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002277
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002278 // OpenCL qualifiers:
2279 case tok::kw_private:
2280 if (!getLang().OpenCL)
2281 goto DoneWithDeclSpec;
2282 case tok::kw___private:
2283 case tok::kw___global:
2284 case tok::kw___local:
2285 case tok::kw___constant:
2286 case tok::kw___read_only:
2287 case tok::kw___write_only:
2288 case tok::kw___read_write:
2289 ParseOpenCLQualifiers(DS);
2290 break;
2291
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002292 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002293 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002294 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2295 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002296 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002297 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Douglas Gregor46f936e2010-11-19 17:10:50 +00002299 if (!ParseObjCProtocolQualifiers(DS))
2300 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2301 << FixItHint::CreateInsertion(Loc, "id")
2302 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002303
2304 // Need to support trailing type qualifiers (e.g. "id<p> const").
2305 // If a type specifier follows, it will be diagnosed elsewhere.
2306 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 }
John McCallfec54012009-08-03 20:12:06 +00002308 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 if (isInvalid) {
2310 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002311 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002312
2313 if (DiagID == diag::ext_duplicate_declspec)
2314 Diag(Tok, DiagID)
2315 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2316 else
2317 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002319
Chris Lattner81c018d2008-03-13 06:29:04 +00002320 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002321 if (DiagID != diag::err_bool_redeclaration)
2322 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 }
2324}
Douglas Gregoradcac882008-12-01 23:54:00 +00002325
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002326/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002327/// primarily follow the C++ grammar with additions for C99 and GNU,
2328/// which together subsume the C grammar. Note that the C++
2329/// type-specifier also includes the C type-qualifier (for const,
2330/// volatile, and C99 restrict). Returns true if a type-specifier was
2331/// found (and parsed), false otherwise.
2332///
2333/// type-specifier: [C++ 7.1.5]
2334/// simple-type-specifier
2335/// class-specifier
2336/// enum-specifier
2337/// elaborated-type-specifier [TODO]
2338/// cv-qualifier
2339///
2340/// cv-qualifier: [C++ 7.1.5.1]
2341/// 'const'
2342/// 'volatile'
2343/// [C99] 'restrict'
2344///
2345/// simple-type-specifier: [ C++ 7.1.5.2]
2346/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2347/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2348/// 'char'
2349/// 'wchar_t'
2350/// 'bool'
2351/// 'short'
2352/// 'int'
2353/// 'long'
2354/// 'signed'
2355/// 'unsigned'
2356/// 'float'
2357/// 'double'
2358/// 'void'
2359/// [C99] '_Bool'
2360/// [C99] '_Complex'
2361/// [C99] '_Imaginary' // Removed in TC2?
2362/// [GNU] '_Decimal32'
2363/// [GNU] '_Decimal64'
2364/// [GNU] '_Decimal128'
2365/// [GNU] typeof-specifier
2366/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2367/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002368/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002369/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002370bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002371 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002372 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002373 const ParsedTemplateInfo &TemplateInfo,
2374 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002375 SourceLocation Loc = Tok.getLocation();
2376
2377 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002378 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002379 // If we already have a type specifier, this identifier is not a type.
2380 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2381 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2382 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2383 return false;
John Thompson82287d12010-02-05 00:12:22 +00002384 // Check for need to substitute AltiVec keyword tokens.
2385 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2386 break;
2387 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002388 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002389 // Annotate typenames and C++ scope specifiers. If we get one, just
2390 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002391 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2392 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002393 return true;
2394 if (Tok.is(tok::identifier))
2395 return false;
2396 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2397 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002398 case tok::coloncolon: // ::foo::bar
2399 if (NextToken().is(tok::kw_new) || // ::new
2400 NextToken().is(tok::kw_delete)) // ::delete
2401 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattner166a8fc2009-01-04 23:41:41 +00002403 // Annotate typenames and C++ scope specifiers. If we get one, just
2404 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002405 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2406 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002407 return true;
2408 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2409 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Douglas Gregor12e083c2008-11-07 15:42:26 +00002411 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002412 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002413 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2415 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002416 DiagID, T);
2417 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002418 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002419 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2420 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Douglas Gregor12e083c2008-11-07 15:42:26 +00002422 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2423 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2424 // Objective-C interface. If we don't have Objective-C or a '<', this is
2425 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002426 if (Tok.is(tok::less) && getLang().ObjC1)
2427 ParseObjCProtocolQualifiers(DS);
2428
Douglas Gregor12e083c2008-11-07 15:42:26 +00002429 return true;
2430 }
2431
2432 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002433 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002434 break;
2435 case tok::kw_long:
2436 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002437 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2438 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002439 else
John McCallfec54012009-08-03 20:12:06 +00002440 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2441 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002442 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002443 case tok::kw___int64:
2444 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2445 DiagID);
2446 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002447 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002448 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002449 break;
2450 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002451 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2452 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002453 break;
2454 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002455 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2456 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002457 break;
2458 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002459 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2460 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002461 break;
2462 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002463 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002464 break;
2465 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002466 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002467 break;
2468 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002469 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002470 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002471 case tok::kw_half:
2472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2473 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002474 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002476 break;
2477 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002479 break;
2480 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002482 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002483 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002485 break;
2486 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002487 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002488 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002489 case tok::kw_bool:
2490 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002492 break;
2493 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2495 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002496 break;
2497 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2499 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002500 break;
2501 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2503 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002504 break;
John Thompson82287d12010-02-05 00:12:22 +00002505 case tok::kw___vector:
2506 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2507 break;
2508 case tok::kw___pixel:
2509 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2510 break;
2511
Douglas Gregor12e083c2008-11-07 15:42:26 +00002512 // class-specifier:
2513 case tok::kw_class:
2514 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002515 case tok::kw_union: {
2516 tok::TokenKind Kind = Tok.getKind();
2517 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002518 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002519 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002520 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002521 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002522 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002523
2524 // enum-specifier:
2525 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002526 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002527 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002528 return true;
2529
2530 // cv-qualifier:
2531 case tok::kw_const:
2532 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002533 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002534 break;
2535 case tok::kw_volatile:
2536 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002537 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002538 break;
2539 case tok::kw_restrict:
2540 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002541 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002542 break;
2543
2544 // GNU typeof support.
2545 case tok::kw_typeof:
2546 ParseTypeofSpecifier(DS);
2547 return true;
2548
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002549 // C++0x decltype support.
2550 case tok::kw_decltype:
2551 ParseDecltypeSpecifier(DS);
2552 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Sean Huntdb5d44b2011-05-19 05:37:45 +00002554 // C++0x type traits support.
2555 case tok::kw___underlying_type:
2556 ParseUnderlyingTypeSpecifier(DS);
2557 return true;
2558
Eli Friedmanb001de72011-10-06 23:00:33 +00002559 case tok::kw__Atomic:
2560 ParseAtomicSpecifier(DS);
2561 return true;
2562
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002563 // OpenCL qualifiers:
2564 case tok::kw_private:
2565 if (!getLang().OpenCL)
2566 return false;
2567 case tok::kw___private:
2568 case tok::kw___global:
2569 case tok::kw___local:
2570 case tok::kw___constant:
2571 case tok::kw___read_only:
2572 case tok::kw___write_only:
2573 case tok::kw___read_write:
2574 ParseOpenCLQualifiers(DS);
2575 break;
2576
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002577 // C++0x auto support.
2578 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002579 // This is only called in situations where a storage-class specifier is
2580 // illegal, so we can assume an auto type specifier was intended even in
2581 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2582 // extension diagnostic.
2583 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002584 return false;
2585
John McCallfec54012009-08-03 20:12:06 +00002586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002587 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002588
Eli Friedman290eeb02009-06-08 23:27:34 +00002589 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002590 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002591 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002592 case tok::kw___cdecl:
2593 case tok::kw___stdcall:
2594 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002595 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002596 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002597 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002598 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002599
Dawn Perchik52fc3142010-09-03 01:29:35 +00002600 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002601 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002602 return true;
2603
Douglas Gregor12e083c2008-11-07 15:42:26 +00002604 default:
2605 // Not a type-specifier; do nothing.
2606 return false;
2607 }
2608
2609 // If the specifier combination wasn't legal, issue a diagnostic.
2610 if (isInvalid) {
2611 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002612 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002613 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002614 }
2615 DS.SetRangeEnd(Tok.getLocation());
2616 ConsumeToken(); // whatever we parsed above.
2617 return true;
2618}
Reid Spencer5f016e22007-07-11 17:01:13 +00002619
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002620/// ParseStructDeclaration - Parse a struct declaration without the terminating
2621/// semicolon.
2622///
Reid Spencer5f016e22007-07-11 17:01:13 +00002623/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002624/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002625/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002626/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002627/// struct-declarator-list:
2628/// struct-declarator
2629/// struct-declarator-list ',' struct-declarator
2630/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2631/// struct-declarator:
2632/// declarator
2633/// [GNU] declarator attributes[opt]
2634/// declarator[opt] ':' constant-expression
2635/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2636///
Chris Lattnere1359422008-04-10 06:46:29 +00002637void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002638ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002639
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002640 if (Tok.is(tok::kw___extension__)) {
2641 // __extension__ silences extension warnings in the subexpression.
2642 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002643 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002644 return ParseStructDeclaration(DS, Fields);
2645 }
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Steve Naroff28a7ca82007-08-20 22:28:22 +00002647 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002648 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002650 // If there are no declarators, this is a free-standing declaration
2651 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002652 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002653 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002654 return;
2655 }
2656
2657 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002658 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002659 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002660 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002661 FieldDeclarator DeclaratorInfo(DS);
2662
2663 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002664 if (!FirstDeclarator)
2665 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002666
Steve Naroff28a7ca82007-08-20 22:28:22 +00002667 /// struct-declarator: declarator
2668 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002669 if (Tok.isNot(tok::colon)) {
2670 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2671 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002672 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002673 }
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Chris Lattner04d66662007-10-09 17:33:22 +00002675 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002676 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002677 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002678 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002679 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002680 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002681 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002682 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002683
Steve Naroff28a7ca82007-08-20 22:28:22 +00002684 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002685 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002686
John McCallbdd563e2009-11-03 02:38:08 +00002687 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002688 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002689 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002690
Steve Naroff28a7ca82007-08-20 22:28:22 +00002691 // If we don't have a comma, it is either the end of the list (a ';')
2692 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002693 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002694 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002695
Steve Naroff28a7ca82007-08-20 22:28:22 +00002696 // Consume the comma.
2697 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002698
John McCallbdd563e2009-11-03 02:38:08 +00002699 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002700 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002701}
2702
2703/// ParseStructUnionBody
2704/// struct-contents:
2705/// struct-declaration-list
2706/// [EXT] empty
2707/// [GNU] "struct-declaration-list" without terminatoring ';'
2708/// struct-declaration-list:
2709/// struct-declaration
2710/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002711/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002712///
Reid Spencer5f016e22007-07-11 17:01:13 +00002713void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002714 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002715 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2716 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002718 BalancedDelimiterTracker T(*this, tok::l_brace);
2719 if (T.consumeOpen())
2720 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002722 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002723 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002724
Reid Spencer5f016e22007-07-11 17:01:13 +00002725 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2726 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002727 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002728 Diag(Tok, diag::ext_empty_struct_union)
2729 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002730
Chris Lattner5f9e2722011-07-23 10:55:15 +00002731 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002732
Reid Spencer5f016e22007-07-11 17:01:13 +00002733 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002734 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002735 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Reid Spencer5f016e22007-07-11 17:01:13 +00002737 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002738 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002739 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002740 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002741 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 ConsumeToken();
2743 continue;
2744 }
Chris Lattnere1359422008-04-10 06:46:29 +00002745
2746 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002747 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002748
John McCallbdd563e2009-11-03 02:38:08 +00002749 if (!Tok.is(tok::at)) {
2750 struct CFieldCallback : FieldCallback {
2751 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002752 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002753 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002754
John McCalld226f652010-08-21 09:40:31 +00002755 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002756 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002757 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2758
John McCalld226f652010-08-21 09:40:31 +00002759 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002760 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002761 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002762 FD.D.getDeclSpec().getSourceRange().getBegin(),
2763 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002764 FieldDecls.push_back(Field);
2765 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002766 }
John McCallbdd563e2009-11-03 02:38:08 +00002767 } Callback(*this, TagDecl, FieldDecls);
2768
2769 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002770 } else { // Handle @defs
2771 ConsumeToken();
2772 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2773 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002774 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002775 continue;
2776 }
2777 ConsumeToken();
2778 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2779 if (!Tok.is(tok::identifier)) {
2780 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002781 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002782 continue;
2783 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002784 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002785 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002786 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002787 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2788 ConsumeToken();
2789 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002790 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002791
Chris Lattner04d66662007-10-09 17:33:22 +00002792 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002793 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002794 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002795 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002796 break;
2797 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002798 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2799 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002801 // If we stopped at a ';', eat it.
2802 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002803 }
2804 }
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002806 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002807
John McCall0b7e6782011-03-24 11:26:52 +00002808 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002810 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002811
Douglas Gregor23c94db2010-07-02 17:43:08 +00002812 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002813 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002814 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002815 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002816 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002817 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2818 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002819}
2820
Reid Spencer5f016e22007-07-11 17:01:13 +00002821/// ParseEnumSpecifier
2822/// enum-specifier: [C99 6.7.2.2]
2823/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002824///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002825/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2826/// '}' attributes[opt]
2827/// 'enum' identifier
2828/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002829///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002830/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2831/// [C++0x] enum-head '{' enumerator-list ',' '}'
2832///
2833/// enum-head: [C++0x]
2834/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2835/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2836///
2837/// enum-key: [C++0x]
2838/// 'enum'
2839/// 'enum' 'class'
2840/// 'enum' 'struct'
2841///
2842/// enum-base: [C++0x]
2843/// ':' type-specifier-seq
2844///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002845/// [C++] elaborated-type-specifier:
2846/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2847///
Chris Lattner4c97d762009-04-12 21:49:30 +00002848void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002849 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002850 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002852 if (Tok.is(tok::code_completion)) {
2853 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002854 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002855 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002856 }
John McCall57c13002011-07-06 05:58:41 +00002857
2858 bool IsScopedEnum = false;
2859 bool IsScopedUsingClassTag = false;
2860
2861 if (getLang().CPlusPlus0x &&
2862 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002863 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002864 IsScopedEnum = true;
2865 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2866 ConsumeToken();
2867 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002868
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002869 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002870 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002871 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002872
Douglas Gregor5471bc82011-09-08 17:18:35 +00002873 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002874 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002875
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002876 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002877 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002878 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2879 // if a fixed underlying type is allowed.
2880 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2881
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002882 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2883 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002884 return;
2885
2886 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002887 Diag(Tok, diag::err_expected_ident);
2888 if (Tok.isNot(tok::l_brace)) {
2889 // Has no name and is not a definition.
2890 // Skip the rest of this declarator, up until the comma or semicolon.
2891 SkipUntil(tok::comma, true);
2892 return;
2893 }
2894 }
2895 }
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002897 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002898 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2899 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002900 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002902 // Skip the rest of this declarator, up until the comma or semicolon.
2903 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002904 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002905 }
Mike Stump1eb44332009-09-09 15:08:12 +00002906
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002907 // If an identifier is present, consume and remember it.
2908 IdentifierInfo *Name = 0;
2909 SourceLocation NameLoc;
2910 if (Tok.is(tok::identifier)) {
2911 Name = Tok.getIdentifierInfo();
2912 NameLoc = ConsumeToken();
2913 }
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002915 if (!Name && IsScopedEnum) {
2916 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2917 // declaration of a scoped enumeration.
2918 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2919 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002920 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002921 }
2922
2923 TypeResult BaseType;
2924
Douglas Gregora61b3e72010-12-01 17:42:47 +00002925 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002926 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002927 bool PossibleBitfield = false;
2928 if (getCurScope()->getFlags() & Scope::ClassScope) {
2929 // If we're in class scope, this can either be an enum declaration with
2930 // an underlying type, or a declaration of a bitfield member. We try to
2931 // use a simple disambiguation scheme first to catch the common cases
2932 // (integer literal, sizeof); if it's still ambiguous, we then consider
2933 // anything that's a simple-type-specifier followed by '(' as an
2934 // expression. This suffices because function types are not valid
2935 // underlying types anyway.
2936 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2937 // If the next token starts an expression, we know we're parsing a
2938 // bit-field. This is the common case.
2939 if (TPR == TPResult::True())
2940 PossibleBitfield = true;
2941 // If the next token starts a type-specifier-seq, it may be either a
2942 // a fixed underlying type or the start of a function-style cast in C++;
2943 // lookahead one more token to see if it's obvious that we have a
2944 // fixed underlying type.
2945 else if (TPR == TPResult::False() &&
2946 GetLookAheadToken(2).getKind() == tok::semi) {
2947 // Consume the ':'.
2948 ConsumeToken();
2949 } else {
2950 // We have the start of a type-specifier-seq, so we have to perform
2951 // tentative parsing to determine whether we have an expression or a
2952 // type.
2953 TentativeParsingAction TPA(*this);
2954
2955 // Consume the ':'.
2956 ConsumeToken();
2957
Douglas Gregor86f208c2011-02-22 20:32:04 +00002958 if ((getLang().CPlusPlus &&
2959 isCXXDeclarationSpecifier() != TPResult::True()) ||
2960 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002961 // We'll parse this as a bitfield later.
2962 PossibleBitfield = true;
2963 TPA.Revert();
2964 } else {
2965 // We have a type-specifier-seq.
2966 TPA.Commit();
2967 }
2968 }
2969 } else {
2970 // Consume the ':'.
2971 ConsumeToken();
2972 }
2973
2974 if (!PossibleBitfield) {
2975 SourceRange Range;
2976 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002977
Douglas Gregor5471bc82011-09-08 17:18:35 +00002978 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002979 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2980 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002981 if (getLang().CPlusPlus0x)
2982 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002983 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002984 }
2985
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002986 // There are three options here. If we have 'enum foo;', then this is a
2987 // forward declaration. If we have 'enum foo {...' then this is a
2988 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2989 //
2990 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2991 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2992 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2993 //
John McCallf312b1e2010-08-26 23:41:50 +00002994 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002995 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002996 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002997 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002998 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002999 else
John McCallf312b1e2010-08-26 23:41:50 +00003000 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003001
3002 // enums cannot be templates, although they can be referenced from a
3003 // template.
3004 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003005 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003006 Diag(Tok, diag::err_enum_template);
3007
3008 // Skip the rest of this declarator, up until the comma or semicolon.
3009 SkipUntil(tok::comma, true);
3010 return;
3011 }
3012
Douglas Gregorb9075602011-02-22 02:55:24 +00003013 if (!Name && TUK != Sema::TUK_Definition) {
3014 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3015
3016 // Skip the rest of this declarator, up until the comma or semicolon.
3017 SkipUntil(tok::comma, true);
3018 return;
3019 }
3020
Douglas Gregor402abb52009-05-28 23:31:59 +00003021 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003022 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003023 const char *PrevSpec = 0;
3024 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003025 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003026 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003027 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003028 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003029 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003030 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003031
Douglas Gregor48c89f42010-04-24 16:38:41 +00003032 if (IsDependent) {
3033 // This enum has a dependent nested-name-specifier. Handle it as a
3034 // dependent tag.
3035 if (!Name) {
3036 DS.SetTypeSpecError();
3037 Diag(Tok, diag::err_expected_type_name_after_typename);
3038 return;
3039 }
3040
Douglas Gregor23c94db2010-07-02 17:43:08 +00003041 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003042 TUK, SS, Name, StartLoc,
3043 NameLoc);
3044 if (Type.isInvalid()) {
3045 DS.SetTypeSpecError();
3046 return;
3047 }
3048
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003049 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3050 NameLoc.isValid() ? NameLoc : StartLoc,
3051 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003052 Diag(StartLoc, DiagID) << PrevSpec;
3053
3054 return;
3055 }
Mike Stump1eb44332009-09-09 15:08:12 +00003056
John McCalld226f652010-08-21 09:40:31 +00003057 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003058 // The action failed to produce an enumeration tag. If this is a
3059 // definition, consume the entire definition.
3060 if (Tok.is(tok::l_brace)) {
3061 ConsumeBrace();
3062 SkipUntil(tok::r_brace);
3063 }
3064
3065 DS.SetTypeSpecError();
3066 return;
3067 }
3068
Chris Lattner04d66662007-10-09 17:33:22 +00003069 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00003070 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003071
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003072 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3073 NameLoc.isValid() ? NameLoc : StartLoc,
3074 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003075 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003076}
3077
3078/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3079/// enumerator-list:
3080/// enumerator
3081/// enumerator-list ',' enumerator
3082/// enumerator:
3083/// enumeration-constant
3084/// enumeration-constant '=' constant-expression
3085/// enumeration-constant:
3086/// identifier
3087///
John McCalld226f652010-08-21 09:40:31 +00003088void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003089 // Enter the scope of the enum body and start the definition.
3090 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003091 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003092
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003093 BalancedDelimiterTracker T(*this, tok::l_brace);
3094 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Chris Lattner7946dd32007-08-27 17:24:30 +00003096 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003097 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003098 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003099
Chris Lattner5f9e2722011-07-23 10:55:15 +00003100 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003101
John McCalld226f652010-08-21 09:40:31 +00003102 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003105 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3107 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003108
John McCall5b629aa2010-10-22 23:36:17 +00003109 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003110 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003111 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003112
Reid Spencer5f016e22007-07-11 17:01:13 +00003113 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003114 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003115 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003117 AssignedVal = ParseConstantExpression();
3118 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Reid Spencer5f016e22007-07-11 17:01:13 +00003122 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003123 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3124 LastEnumConstDecl,
3125 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003126 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003127 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 EnumConstantDecls.push_back(EnumConstDecl);
3129 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Douglas Gregor751f6922010-09-07 14:51:08 +00003131 if (Tok.is(tok::identifier)) {
3132 // We're missing a comma between enumerators.
3133 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3134 Diag(Loc, diag::err_enumerator_list_missing_comma)
3135 << FixItHint::CreateInsertion(Loc, ", ");
3136 continue;
3137 }
3138
Chris Lattner04d66662007-10-09 17:33:22 +00003139 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003140 break;
3141 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Richard Smith7fe62082011-10-15 05:09:34 +00003143 if (Tok.isNot(tok::identifier)) {
3144 if (!getLang().C99 && !getLang().CPlusPlus0x)
3145 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3146 << getLang().CPlusPlus
3147 << FixItHint::CreateRemoval(CommaLoc);
3148 else if (getLang().CPlusPlus0x)
3149 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3150 << FixItHint::CreateRemoval(CommaLoc);
3151 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 }
Mike Stump1eb44332009-09-09 15:08:12 +00003153
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003155 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003158 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003159 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003160
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003161 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3162 EnumDecl, EnumConstantDecls.data(),
3163 EnumConstantDecls.size(), getCurScope(),
3164 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003165
Douglas Gregor72de6672009-01-08 20:45:30 +00003166 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003167 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3168 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003169}
3170
3171/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003172/// start of a type-qualifier-list.
3173bool Parser::isTypeQualifier() const {
3174 switch (Tok.getKind()) {
3175 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003176
3177 // type-qualifier only in OpenCL
3178 case tok::kw_private:
3179 return getLang().OpenCL;
3180
Steve Naroff5f8aa692008-02-11 23:15:56 +00003181 // type-qualifier
3182 case tok::kw_const:
3183 case tok::kw_volatile:
3184 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003185 case tok::kw___private:
3186 case tok::kw___local:
3187 case tok::kw___global:
3188 case tok::kw___constant:
3189 case tok::kw___read_only:
3190 case tok::kw___read_write:
3191 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003192 return true;
3193 }
3194}
3195
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003196/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3197/// is definitely a type-specifier. Return false if it isn't part of a type
3198/// specifier or if we're not sure.
3199bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3200 switch (Tok.getKind()) {
3201 default: return false;
3202 // type-specifiers
3203 case tok::kw_short:
3204 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003205 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003206 case tok::kw_signed:
3207 case tok::kw_unsigned:
3208 case tok::kw__Complex:
3209 case tok::kw__Imaginary:
3210 case tok::kw_void:
3211 case tok::kw_char:
3212 case tok::kw_wchar_t:
3213 case tok::kw_char16_t:
3214 case tok::kw_char32_t:
3215 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003216 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003217 case tok::kw_float:
3218 case tok::kw_double:
3219 case tok::kw_bool:
3220 case tok::kw__Bool:
3221 case tok::kw__Decimal32:
3222 case tok::kw__Decimal64:
3223 case tok::kw__Decimal128:
3224 case tok::kw___vector:
3225
3226 // struct-or-union-specifier (C99) or class-specifier (C++)
3227 case tok::kw_class:
3228 case tok::kw_struct:
3229 case tok::kw_union:
3230 // enum-specifier
3231 case tok::kw_enum:
3232
3233 // typedef-name
3234 case tok::annot_typename:
3235 return true;
3236 }
3237}
3238
Steve Naroff5f8aa692008-02-11 23:15:56 +00003239/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003240/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003241bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003242 switch (Tok.getKind()) {
3243 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Chris Lattner166a8fc2009-01-04 23:41:41 +00003245 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003246 if (TryAltiVecVectorToken())
3247 return true;
3248 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003249 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003250 // Annotate typenames and C++ scope specifiers. If we get one, just
3251 // recurse to handle whatever we get.
3252 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003253 return true;
3254 if (Tok.is(tok::identifier))
3255 return false;
3256 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003257
Chris Lattner166a8fc2009-01-04 23:41:41 +00003258 case tok::coloncolon: // ::foo::bar
3259 if (NextToken().is(tok::kw_new) || // ::new
3260 NextToken().is(tok::kw_delete)) // ::delete
3261 return false;
3262
Chris Lattner166a8fc2009-01-04 23:41:41 +00003263 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003264 return true;
3265 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Reid Spencer5f016e22007-07-11 17:01:13 +00003267 // GNU attributes support.
3268 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003269 // GNU typeof support.
3270 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Reid Spencer5f016e22007-07-11 17:01:13 +00003272 // type-specifiers
3273 case tok::kw_short:
3274 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003275 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003276 case tok::kw_signed:
3277 case tok::kw_unsigned:
3278 case tok::kw__Complex:
3279 case tok::kw__Imaginary:
3280 case tok::kw_void:
3281 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003282 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003283 case tok::kw_char16_t:
3284 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003286 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003287 case tok::kw_float:
3288 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003289 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 case tok::kw__Bool:
3291 case tok::kw__Decimal32:
3292 case tok::kw__Decimal64:
3293 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003294 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Chris Lattner99dc9142008-04-13 18:59:07 +00003296 // struct-or-union-specifier (C99) or class-specifier (C++)
3297 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 case tok::kw_struct:
3299 case tok::kw_union:
3300 // enum-specifier
3301 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // type-qualifier
3304 case tok::kw_const:
3305 case tok::kw_volatile:
3306 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003307
3308 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003309 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Chris Lattner7c186be2008-10-20 00:25:30 +00003312 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3313 case tok::less:
3314 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Steve Naroff239f0732008-12-25 14:16:32 +00003316 case tok::kw___cdecl:
3317 case tok::kw___stdcall:
3318 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003319 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003320 case tok::kw___w64:
3321 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003322 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003323 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003324 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003325
3326 case tok::kw___private:
3327 case tok::kw___local:
3328 case tok::kw___global:
3329 case tok::kw___constant:
3330 case tok::kw___read_only:
3331 case tok::kw___read_write:
3332 case tok::kw___write_only:
3333
Eli Friedman290eeb02009-06-08 23:27:34 +00003334 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003335
3336 case tok::kw_private:
3337 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003338
3339 // C1x _Atomic()
3340 case tok::kw__Atomic:
3341 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003342 }
3343}
3344
3345/// isDeclarationSpecifier() - Return true if the current token is part of a
3346/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003347///
3348/// \param DisambiguatingWithExpression True to indicate that the purpose of
3349/// this check is to disambiguate between an expression and a declaration.
3350bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003351 switch (Tok.getKind()) {
3352 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003354 case tok::kw_private:
3355 return getLang().OpenCL;
3356
Chris Lattner166a8fc2009-01-04 23:41:41 +00003357 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003358 // Unfortunate hack to support "Class.factoryMethod" notation.
3359 if (getLang().ObjC1 && NextToken().is(tok::period))
3360 return false;
John Thompson82287d12010-02-05 00:12:22 +00003361 if (TryAltiVecVectorToken())
3362 return true;
3363 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003364 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003365 // Annotate typenames and C++ scope specifiers. If we get one, just
3366 // recurse to handle whatever we get.
3367 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003368 return true;
3369 if (Tok.is(tok::identifier))
3370 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003371
3372 // If we're in Objective-C and we have an Objective-C class type followed
3373 // by an identifier and then either ':' or ']', in a place where an
3374 // expression is permitted, then this is probably a class message send
3375 // missing the initial '['. In this case, we won't consider this to be
3376 // the start of a declaration.
3377 if (DisambiguatingWithExpression &&
3378 isStartOfObjCClassMessageMissingOpenBracket())
3379 return false;
3380
John McCall9ba61662010-02-26 08:45:28 +00003381 return isDeclarationSpecifier();
3382
Chris Lattner166a8fc2009-01-04 23:41:41 +00003383 case tok::coloncolon: // ::foo::bar
3384 if (NextToken().is(tok::kw_new) || // ::new
3385 NextToken().is(tok::kw_delete)) // ::delete
3386 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003387
Chris Lattner166a8fc2009-01-04 23:41:41 +00003388 // Annotate typenames and C++ scope specifiers. If we get one, just
3389 // recurse to handle whatever we get.
3390 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003391 return true;
3392 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003393
Reid Spencer5f016e22007-07-11 17:01:13 +00003394 // storage-class-specifier
3395 case tok::kw_typedef:
3396 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003397 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003398 case tok::kw_static:
3399 case tok::kw_auto:
3400 case tok::kw_register:
3401 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003402
Douglas Gregor8d267c52011-09-09 02:06:17 +00003403 // Modules
3404 case tok::kw___module_private__:
3405
Reid Spencer5f016e22007-07-11 17:01:13 +00003406 // type-specifiers
3407 case tok::kw_short:
3408 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003409 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003410 case tok::kw_signed:
3411 case tok::kw_unsigned:
3412 case tok::kw__Complex:
3413 case tok::kw__Imaginary:
3414 case tok::kw_void:
3415 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003416 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003417 case tok::kw_char16_t:
3418 case tok::kw_char32_t:
3419
Reid Spencer5f016e22007-07-11 17:01:13 +00003420 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003421 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003422 case tok::kw_float:
3423 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003424 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003425 case tok::kw__Bool:
3426 case tok::kw__Decimal32:
3427 case tok::kw__Decimal64:
3428 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003429 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003430
Chris Lattner99dc9142008-04-13 18:59:07 +00003431 // struct-or-union-specifier (C99) or class-specifier (C++)
3432 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003433 case tok::kw_struct:
3434 case tok::kw_union:
3435 // enum-specifier
3436 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003437
Reid Spencer5f016e22007-07-11 17:01:13 +00003438 // type-qualifier
3439 case tok::kw_const:
3440 case tok::kw_volatile:
3441 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003442
Reid Spencer5f016e22007-07-11 17:01:13 +00003443 // function-specifier
3444 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003445 case tok::kw_virtual:
3446 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003447
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003448 // static_assert-declaration
3449 case tok::kw__Static_assert:
3450
Chris Lattner1ef08762007-08-09 17:01:07 +00003451 // GNU typeof support.
3452 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Chris Lattner1ef08762007-08-09 17:01:07 +00003454 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003455 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Francois Pichete3d49b42011-06-19 08:02:06 +00003458 // C++0x decltype.
3459 case tok::kw_decltype:
3460 return true;
3461
Eli Friedmanb001de72011-10-06 23:00:33 +00003462 // C1x _Atomic()
3463 case tok::kw__Atomic:
3464 return true;
3465
Chris Lattnerf3948c42008-07-26 03:38:44 +00003466 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3467 case tok::less:
3468 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Douglas Gregord9d75e52011-04-27 05:41:15 +00003470 // typedef-name
3471 case tok::annot_typename:
3472 return !DisambiguatingWithExpression ||
3473 !isStartOfObjCClassMessageMissingOpenBracket();
3474
Steve Naroff47f52092009-01-06 19:34:12 +00003475 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003476 case tok::kw___cdecl:
3477 case tok::kw___stdcall:
3478 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003479 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003480 case tok::kw___w64:
3481 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003482 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003483 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003484 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003485 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003486
3487 case tok::kw___private:
3488 case tok::kw___local:
3489 case tok::kw___global:
3490 case tok::kw___constant:
3491 case tok::kw___read_only:
3492 case tok::kw___read_write:
3493 case tok::kw___write_only:
3494
Eli Friedman290eeb02009-06-08 23:27:34 +00003495 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003496 }
3497}
3498
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003499bool Parser::isConstructorDeclarator() {
3500 TentativeParsingAction TPA(*this);
3501
3502 // Parse the C++ scope specifier.
3503 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003504 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3505 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003506 TPA.Revert();
3507 return false;
3508 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003509
3510 // Parse the constructor name.
3511 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3512 // We already know that we have a constructor name; just consume
3513 // the token.
3514 ConsumeToken();
3515 } else {
3516 TPA.Revert();
3517 return false;
3518 }
3519
3520 // Current class name must be followed by a left parentheses.
3521 if (Tok.isNot(tok::l_paren)) {
3522 TPA.Revert();
3523 return false;
3524 }
3525 ConsumeParen();
3526
3527 // A right parentheses or ellipsis signals that we have a constructor.
3528 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3529 TPA.Revert();
3530 return true;
3531 }
3532
3533 // If we need to, enter the specified scope.
3534 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003535 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003536 DeclScopeObj.EnterDeclaratorScope();
3537
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003538 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003539 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003540 MaybeParseMicrosoftAttributes(Attrs);
3541
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003542 // Check whether the next token(s) are part of a declaration
3543 // specifier, in which case we have the start of a parameter and,
3544 // therefore, we know that this is a constructor.
3545 bool IsConstructor = isDeclarationSpecifier();
3546 TPA.Revert();
3547 return IsConstructor;
3548}
Reid Spencer5f016e22007-07-11 17:01:13 +00003549
3550/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003551/// type-qualifier-list: [C99 6.7.5]
3552/// type-qualifier
3553/// [vendor] attributes
3554/// [ only if VendorAttributesAllowed=true ]
3555/// type-qualifier-list type-qualifier
3556/// [vendor] type-qualifier-list attributes
3557/// [ only if VendorAttributesAllowed=true ]
3558/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3559/// [ only if CXX0XAttributesAllowed=true ]
3560/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003561///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003562void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3563 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003564 bool CXX0XAttributesAllowed) {
3565 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3566 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003567 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003568 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003569 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003570 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003571 else
3572 Diag(Loc, diag::err_attributes_not_allowed);
3573 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003574
3575 SourceLocation EndLoc;
3576
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003578 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003579 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003580 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003581 SourceLocation Loc = Tok.getLocation();
3582
3583 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003584 case tok::code_completion:
3585 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003586 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003587
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003589 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3590 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 break;
3592 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003593 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3594 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003595 break;
3596 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003597 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3598 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003599 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003600
3601 // OpenCL qualifiers:
3602 case tok::kw_private:
3603 if (!getLang().OpenCL)
3604 goto DoneWithTypeQuals;
3605 case tok::kw___private:
3606 case tok::kw___global:
3607 case tok::kw___local:
3608 case tok::kw___constant:
3609 case tok::kw___read_only:
3610 case tok::kw___write_only:
3611 case tok::kw___read_write:
3612 ParseOpenCLQualifiers(DS);
3613 break;
3614
Eli Friedman290eeb02009-06-08 23:27:34 +00003615 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003616 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003617 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003618 case tok::kw___cdecl:
3619 case tok::kw___stdcall:
3620 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003621 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003622 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003623 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003624 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003625 continue;
3626 }
3627 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003628 case tok::kw___pascal:
3629 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003630 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003631 continue;
3632 }
3633 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003634 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003635 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003636 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003637 continue; // do *not* consume the next token!
3638 }
3639 // otherwise, FALL THROUGH!
3640 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003641 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003642 // If this is not a type-qualifier token, we're done reading type
3643 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003644 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003645 if (EndLoc.isValid())
3646 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003647 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003648 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003649
Reid Spencer5f016e22007-07-11 17:01:13 +00003650 // If the specifier combination wasn't legal, issue a diagnostic.
3651 if (isInvalid) {
3652 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003653 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003654 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003655 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 }
3657}
3658
3659
3660/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3661///
3662void Parser::ParseDeclarator(Declarator &D) {
3663 /// This implements the 'declarator' production in the C grammar, then checks
3664 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003665 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003666}
3667
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003668/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3669/// is parsed by the function passed to it. Pass null, and the direct-declarator
3670/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003671/// ptr-operator production.
3672///
Richard Smith0706df42011-10-19 21:33:05 +00003673/// If the grammar of this construct is extended, matching changes must also be
3674/// made to TryParseDeclarator and MightBeDeclarator.
3675///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003676/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3677/// [C] pointer[opt] direct-declarator
3678/// [C++] direct-declarator
3679/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003680///
3681/// pointer: [C99 6.7.5]
3682/// '*' type-qualifier-list[opt]
3683/// '*' type-qualifier-list[opt] pointer
3684///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003685/// ptr-operator:
3686/// '*' cv-qualifier-seq[opt]
3687/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003688/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003689/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003690/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003691/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003692void Parser::ParseDeclaratorInternal(Declarator &D,
3693 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003694 if (Diags.hasAllExtensionsSilenced())
3695 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003696
Sebastian Redlf30208a2009-01-24 21:16:55 +00003697 // C++ member pointers start with a '::' or a nested-name.
3698 // Member pointers get special handling, since there's no place for the
3699 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003700 if (getLang().CPlusPlus &&
3701 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3702 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003703 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3704 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003705 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003706 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003707
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003708 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003709 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003710 // The scope spec really belongs to the direct-declarator.
3711 D.getCXXScopeSpec() = SS;
3712 if (DirectDeclParser)
3713 (this->*DirectDeclParser)(D);
3714 return;
3715 }
3716
3717 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003718 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003719 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003720 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003721 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003722
3723 // Recurse to parse whatever is left.
3724 ParseDeclaratorInternal(D, DirectDeclParser);
3725
3726 // Sema will have to catch (syntactically invalid) pointers into global
3727 // scope. It has to catch pointers into namespace scope anyway.
3728 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003729 Loc),
3730 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003731 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003732 return;
3733 }
3734 }
3735
3736 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003737 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003738 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003739 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003740 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003741 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003742 if (DirectDeclParser)
3743 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003744 return;
3745 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003746
Sebastian Redl05532f22009-03-15 22:02:01 +00003747 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3748 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003749 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003750 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003751
Chris Lattner9af55002009-03-27 04:18:06 +00003752 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003753 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003754 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003755
Reid Spencer5f016e22007-07-11 17:01:13 +00003756 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003757 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003758
Reid Spencer5f016e22007-07-11 17:01:13 +00003759 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003760 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003761 if (Kind == tok::star)
3762 // Remember that we parsed a pointer type, and remember the type-quals.
3763 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003764 DS.getConstSpecLoc(),
3765 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003766 DS.getRestrictSpecLoc()),
3767 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003768 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003769 else
3770 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003771 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003772 Loc),
3773 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003774 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003775 } else {
3776 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003777 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003778
Sebastian Redl743de1f2009-03-23 00:00:23 +00003779 // Complain about rvalue references in C++03, but then go on and build
3780 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003781 if (Kind == tok::ampamp)
3782 Diag(Loc, getLang().CPlusPlus0x ?
3783 diag::warn_cxx98_compat_rvalue_reference :
3784 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003785
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3787 // cv-qualifiers are introduced through the use of a typedef or of a
3788 // template type argument, in which case the cv-qualifiers are ignored.
3789 //
3790 // [GNU] Retricted references are allowed.
3791 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003792 // [C++0x] Attributes on references are not allowed.
3793 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003794 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003795
3796 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3797 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3798 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003799 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003800 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3801 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003802 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003803 }
3804
3805 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003806 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003807
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003808 if (D.getNumTypeObjects() > 0) {
3809 // C++ [dcl.ref]p4: There shall be no references to references.
3810 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3811 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003812 if (const IdentifierInfo *II = D.getIdentifier())
3813 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3814 << II;
3815 else
3816 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3817 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003818
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003819 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003820 // can go ahead and build the (technically ill-formed)
3821 // declarator: reference collapsing will take care of it.
3822 }
3823 }
3824
Reid Spencer5f016e22007-07-11 17:01:13 +00003825 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003826 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003827 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003828 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003829 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003830 }
3831}
3832
3833/// ParseDirectDeclarator
3834/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003835/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003836/// '(' declarator ')'
3837/// [GNU] '(' attributes declarator ')'
3838/// [C90] direct-declarator '[' constant-expression[opt] ']'
3839/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3840/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3841/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3842/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3843/// direct-declarator '(' parameter-type-list ')'
3844/// direct-declarator '(' identifier-list[opt] ')'
3845/// [GNU] direct-declarator '(' parameter-forward-declarations
3846/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003847/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3848/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003849/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003850///
3851/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003852/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003853/// '::'[opt] nested-name-specifier[opt] type-name
3854///
3855/// id-expression: [C++ 5.1]
3856/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003857/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003858///
3859/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003860/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003861/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003862/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003863/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003864/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003865///
Reid Spencer5f016e22007-07-11 17:01:13 +00003866void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003867 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003868
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003869 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3870 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003871 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003872 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3873 D.getContext() == Declarator::MemberContext;
3874 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3875 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003876 }
3877
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003878 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003879 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003880 // Change the declaration context for name lookup, until this function
3881 // is exited (and the declarator has been parsed).
3882 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003883 }
3884
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003885 // C++0x [dcl.fct]p14:
3886 // There is a syntactic ambiguity when an ellipsis occurs at the end
3887 // of a parameter-declaration-clause without a preceding comma. In
3888 // this case, the ellipsis is parsed as part of the
3889 // abstract-declarator if the type of the parameter names a template
3890 // parameter pack that has not been expanded; otherwise, it is parsed
3891 // as part of the parameter-declaration-clause.
3892 if (Tok.is(tok::ellipsis) &&
3893 !((D.getContext() == Declarator::PrototypeContext ||
3894 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003895 NextToken().is(tok::r_paren) &&
3896 !Actions.containsUnexpandedParameterPacks(D)))
3897 D.setEllipsisLoc(ConsumeToken());
3898
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003899 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3900 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3901 // We found something that indicates the start of an unqualified-id.
3902 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003903 bool AllowConstructorName;
3904 if (D.getDeclSpec().hasTypeSpecifier())
3905 AllowConstructorName = false;
3906 else if (D.getCXXScopeSpec().isSet())
3907 AllowConstructorName =
3908 (D.getContext() == Declarator::FileContext ||
3909 (D.getContext() == Declarator::MemberContext &&
3910 D.getDeclSpec().isFriendSpecified()));
3911 else
3912 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3913
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003914 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3915 /*EnteringContext=*/true,
3916 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003917 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003918 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003919 D.getName()) ||
3920 // Once we're past the identifier, if the scope was bad, mark the
3921 // whole declarator bad.
3922 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003923 D.SetIdentifier(0, Tok.getLocation());
3924 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003925 } else {
3926 // Parsed the unqualified-id; update range information and move along.
3927 if (D.getSourceRange().getBegin().isInvalid())
3928 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3929 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003930 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003931 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003932 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003933 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003934 assert(!getLang().CPlusPlus &&
3935 "There's a C++-specific check for tok::identifier above");
3936 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3937 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3938 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003939 goto PastIdentifier;
3940 }
3941
3942 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003943 // direct-declarator: '(' declarator ')'
3944 // direct-declarator: '(' attributes declarator ')'
3945 // Example: 'char (*X)' or 'int (*XX)(void)'
3946 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003947
3948 // If the declarator was parenthesized, we entered the declarator
3949 // scope when parsing the parenthesized declarator, then exited
3950 // the scope already. Re-enter the scope, if we need to.
3951 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003952 // If there was an error parsing parenthesized declarator, declarator
3953 // scope may have been enterred before. Don't do it again.
3954 if (!D.isInvalidType() &&
3955 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003956 // Change the declaration context for name lookup, until this function
3957 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003958 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003959 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003960 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003961 // This could be something simple like "int" (in which case the declarator
3962 // portion is empty), if an abstract-declarator is allowed.
3963 D.SetIdentifier(0, Tok.getLocation());
3964 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003965 if (D.getContext() == Declarator::MemberContext)
3966 Diag(Tok, diag::err_expected_member_name_or_semi)
3967 << D.getDeclSpec().getSourceRange();
3968 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003969 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003970 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003971 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003972 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003973 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003974 }
Mike Stump1eb44332009-09-09 15:08:12 +00003975
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003976 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003977 assert(D.isPastIdentifier() &&
3978 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003979
Sean Huntbbd37c62009-11-21 08:43:09 +00003980 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003981 if (D.getIdentifier())
3982 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003983
Reid Spencer5f016e22007-07-11 17:01:13 +00003984 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003985 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003986 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3987 // In such a case, check if we actually have a function declarator; if it
3988 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003989 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3990 // When not in file scope, warn for ambiguous function declarators, just
3991 // in case the author intended it as a variable definition.
3992 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3993 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3994 break;
3995 }
John McCall0b7e6782011-03-24 11:26:52 +00003996 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003997 BalancedDelimiterTracker T(*this, tok::l_paren);
3998 T.consumeOpen();
3999 ParseFunctionDeclarator(D, attrs, T);
Chris Lattner04d66662007-10-09 17:33:22 +00004000 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004001 ParseBracketDeclarator(D);
4002 } else {
4003 break;
4004 }
4005 }
4006}
4007
Chris Lattneref4715c2008-04-06 05:45:57 +00004008/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4009/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004010/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004011/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4012///
4013/// direct-declarator:
4014/// '(' declarator ')'
4015/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004016/// direct-declarator '(' parameter-type-list ')'
4017/// direct-declarator '(' identifier-list[opt] ')'
4018/// [GNU] direct-declarator '(' parameter-forward-declarations
4019/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004020///
4021void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004022 BalancedDelimiterTracker T(*this, tok::l_paren);
4023 T.consumeOpen();
4024
Chris Lattneref4715c2008-04-06 05:45:57 +00004025 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004026
Chris Lattner7399ee02008-10-20 02:05:46 +00004027 // Eat any attributes before we look at whether this is a grouping or function
4028 // declarator paren. If this is a grouping paren, the attribute applies to
4029 // the type being built up, for example:
4030 // int (__attribute__(()) *x)(long y)
4031 // If this ends up not being a grouping paren, the attribute applies to the
4032 // first argument, for example:
4033 // int (__attribute__(()) int x)
4034 // In either case, we need to eat any attributes to be able to determine what
4035 // sort of paren this is.
4036 //
John McCall0b7e6782011-03-24 11:26:52 +00004037 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004038 bool RequiresArg = false;
4039 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004040 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004041
Chris Lattner7399ee02008-10-20 02:05:46 +00004042 // We require that the argument list (if this is a non-grouping paren) be
4043 // present even if the attribute list was empty.
4044 RequiresArg = true;
4045 }
Steve Naroff239f0732008-12-25 14:16:32 +00004046 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004047 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004048 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004049 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004050 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004051 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004052 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004053 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004054 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004055 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004056
Chris Lattneref4715c2008-04-06 05:45:57 +00004057 // If we haven't past the identifier yet (or where the identifier would be
4058 // stored, if this is an abstract declarator), then this is probably just
4059 // grouping parens. However, if this could be an abstract-declarator, then
4060 // this could also be the start of function arguments (consider 'void()').
4061 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004062
Chris Lattneref4715c2008-04-06 05:45:57 +00004063 if (!D.mayOmitIdentifier()) {
4064 // If this can't be an abstract-declarator, this *must* be a grouping
4065 // paren, because we haven't seen the identifier yet.
4066 isGrouping = true;
4067 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004068 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004069 isDeclarationSpecifier()) { // 'int(int)' is a function.
4070 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4071 // considered to be a type, not a K&R identifier-list.
4072 isGrouping = false;
4073 } else {
4074 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4075 isGrouping = true;
4076 }
Mike Stump1eb44332009-09-09 15:08:12 +00004077
Chris Lattneref4715c2008-04-06 05:45:57 +00004078 // If this is a grouping paren, handle:
4079 // direct-declarator: '(' declarator ')'
4080 // direct-declarator: '(' attributes declarator ')'
4081 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004082 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004083 D.setGroupingParens(true);
4084
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004085 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004086 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004087 T.consumeClose();
4088 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4089 T.getCloseLocation()),
4090 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004091
4092 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004093 return;
4094 }
Mike Stump1eb44332009-09-09 15:08:12 +00004095
Chris Lattneref4715c2008-04-06 05:45:57 +00004096 // Okay, if this wasn't a grouping paren, it must be the start of a function
4097 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004098 // identifier (and remember where it would have been), then call into
4099 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004100 D.SetIdentifier(0, Tok.getLocation());
4101
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004102 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00004103}
4104
4105/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4106/// declarator D up to a paren, which indicates that we are parsing function
4107/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004108///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004109/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004110/// after the open paren - they should be considered to be the first argument of
4111/// a parameter. If RequiresArg is true, then the first argument of the
4112/// function is required to be present and required to not be an identifier
4113/// list.
4114///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004115/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4116/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4117/// (C++0x) trailing-return-type[opt].
4118///
4119/// [C++0x] exception-specification:
4120/// dynamic-exception-specification
4121/// noexcept-specification
4122///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004123void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004124 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004125 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004126 bool RequiresArg) {
4127 // lparen is already consumed!
4128 assert(D.isPastIdentifier() && "Should not call before identifier!");
4129
4130 // This should be true when the function has typed arguments.
4131 // Otherwise, it is treated as a K&R-style function.
4132 bool HasProto = false;
4133 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004134 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004135 // Remember where we see an ellipsis, if any.
4136 SourceLocation EllipsisLoc;
4137
4138 DeclSpec DS(AttrFactory);
4139 bool RefQualifierIsLValueRef = true;
4140 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004141 SourceLocation ConstQualifierLoc;
4142 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004143 ExceptionSpecificationType ESpecType = EST_None;
4144 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004145 SmallVector<ParsedType, 2> DynamicExceptions;
4146 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004147 ExprResult NoexceptExpr;
4148 ParsedType TrailingReturnType;
4149
4150 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 if (isFunctionDeclaratorIdentifierList()) {
4152 if (RequiresArg)
4153 Diag(Tok, diag::err_argument_required_after_attribute);
4154
4155 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4156
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004157 Tracker.consumeClose();
4158 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004159 } else {
4160 // Enter function-declaration scope, limiting any declarators to the
4161 // function prototype scope, including parameter declarators.
4162 ParseScope PrototypeScope(this,
4163 Scope::FunctionPrototypeScope|Scope::DeclScope);
4164
4165 if (Tok.isNot(tok::r_paren))
4166 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4167 else if (RequiresArg)
4168 Diag(Tok, diag::err_argument_required_after_attribute);
4169
4170 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4171
4172 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004173 Tracker.consumeClose();
4174 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004175
4176 if (getLang().CPlusPlus) {
4177 MaybeParseCXX0XAttributes(attrs);
4178
4179 // Parse cv-qualifier-seq[opt].
4180 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004181 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004182 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004183 ConstQualifierLoc = DS.getConstSpecLoc();
4184 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4185 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004186
4187 // Parse ref-qualifier[opt].
4188 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004189 Diag(Tok, getLang().CPlusPlus0x ?
4190 diag::warn_cxx98_compat_ref_qualifier :
4191 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004192
4193 RefQualifierIsLValueRef = Tok.is(tok::amp);
4194 RefQualifierLoc = ConsumeToken();
4195 EndLoc = RefQualifierLoc;
4196 }
4197
4198 // Parse exception-specification[opt].
4199 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4200 DynamicExceptions,
4201 DynamicExceptionRanges,
4202 NoexceptExpr);
4203 if (ESpecType != EST_None)
4204 EndLoc = ESpecRange.getEnd();
4205
4206 // Parse trailing-return-type[opt].
4207 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004208 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004209 SourceRange Range;
4210 TrailingReturnType = ParseTrailingReturnType(Range).get();
4211 if (Range.getEnd().isValid())
4212 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004213 }
4214 }
4215
4216 // Leave prototype scope.
4217 PrototypeScope.Exit();
4218 }
4219
4220 // Remember that we parsed a function type, and remember the attributes.
4221 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4222 /*isVariadic=*/EllipsisLoc.isValid(),
4223 EllipsisLoc,
4224 ParamInfo.data(), ParamInfo.size(),
4225 DS.getTypeQualifiers(),
4226 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004227 RefQualifierLoc, ConstQualifierLoc,
4228 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004229 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004230 ESpecType, ESpecRange.getBegin(),
4231 DynamicExceptions.data(),
4232 DynamicExceptionRanges.data(),
4233 DynamicExceptions.size(),
4234 NoexceptExpr.isUsable() ?
4235 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004236 Tracker.getOpenLocation(),
4237 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004238 TrailingReturnType),
4239 attrs, EndLoc);
4240}
4241
4242/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4243/// identifier list form for a K&R-style function: void foo(a,b,c)
4244///
4245/// Note that identifier-lists are only allowed for normal declarators, not for
4246/// abstract-declarators.
4247bool Parser::isFunctionDeclaratorIdentifierList() {
4248 return !getLang().CPlusPlus
4249 && Tok.is(tok::identifier)
4250 && !TryAltiVecVectorToken()
4251 // K&R identifier lists can't have typedefs as identifiers, per C99
4252 // 6.7.5.3p11.
4253 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4254 // Identifier lists follow a really simple grammar: the identifiers can
4255 // be followed *only* by a ", identifier" or ")". However, K&R
4256 // identifier lists are really rare in the brave new modern world, and
4257 // it is very common for someone to typo a type in a non-K&R style
4258 // list. If we are presented with something like: "void foo(intptr x,
4259 // float y)", we don't want to start parsing the function declarator as
4260 // though it is a K&R style declarator just because intptr is an
4261 // invalid type.
4262 //
4263 // To handle this, we check to see if the token after the first
4264 // identifier is a "," or ")". Only then do we parse it as an
4265 // identifier list.
4266 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4267}
4268
4269/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4270/// we found a K&R-style identifier list instead of a typed parameter list.
4271///
4272/// After returning, ParamInfo will hold the parsed parameters.
4273///
4274/// identifier-list: [C99 6.7.5]
4275/// identifier
4276/// identifier-list ',' identifier
4277///
4278void Parser::ParseFunctionDeclaratorIdentifierList(
4279 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004280 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004281 // If there was no identifier specified for the declarator, either we are in
4282 // an abstract-declarator, or we are in a parameter declarator which was found
4283 // to be abstract. In abstract-declarators, identifier lists are not valid:
4284 // diagnose this.
4285 if (!D.getIdentifier())
4286 Diag(Tok, diag::ext_ident_list_in_param);
4287
4288 // Maintain an efficient lookup of params we have seen so far.
4289 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4290
4291 while (1) {
4292 // If this isn't an identifier, report the error and skip until ')'.
4293 if (Tok.isNot(tok::identifier)) {
4294 Diag(Tok, diag::err_expected_ident);
4295 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4296 // Forget we parsed anything.
4297 ParamInfo.clear();
4298 return;
4299 }
4300
4301 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4302
4303 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4304 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4305 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4306
4307 // Verify that the argument identifier has not already been mentioned.
4308 if (!ParamsSoFar.insert(ParmII)) {
4309 Diag(Tok, diag::err_param_redefinition) << ParmII;
4310 } else {
4311 // Remember this identifier in ParamInfo.
4312 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4313 Tok.getLocation(),
4314 0));
4315 }
4316
4317 // Eat the identifier.
4318 ConsumeToken();
4319
4320 // The list continues if we see a comma.
4321 if (Tok.isNot(tok::comma))
4322 break;
4323 ConsumeToken();
4324 }
4325}
4326
4327/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4328/// after the opening parenthesis. This function will not parse a K&R-style
4329/// identifier list.
4330///
4331/// D is the declarator being parsed. If attrs is non-null, then the caller
4332/// parsed those arguments immediately after the open paren - they should be
4333/// considered to be the first argument of a parameter.
4334///
4335/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4336/// be the location of the ellipsis, if any was parsed.
4337///
Reid Spencer5f016e22007-07-11 17:01:13 +00004338/// parameter-type-list: [C99 6.7.5]
4339/// parameter-list
4340/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004341/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004342///
4343/// parameter-list: [C99 6.7.5]
4344/// parameter-declaration
4345/// parameter-list ',' parameter-declaration
4346///
4347/// parameter-declaration: [C99 6.7.5]
4348/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004349/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004350/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004351/// declaration-specifiers abstract-declarator[opt]
4352/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004353/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004354/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4355///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004356void Parser::ParseParameterDeclarationClause(
4357 Declarator &D,
4358 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004359 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004360 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004361
Chris Lattnerf97409f2008-04-06 06:57:35 +00004362 while (1) {
4363 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004364 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004365 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004366 }
Mike Stump1eb44332009-09-09 15:08:12 +00004367
Chris Lattnerf97409f2008-04-06 06:57:35 +00004368 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004369 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004370 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004371
John McCall7f040a92010-12-24 02:08:15 +00004372 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004373 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004374 ParseMicrosoftAttributes(DS.getAttributes());
4375
4376 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004377
4378 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004379 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004380 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4381 // attributes lost? Should they even be allowed?
4382 // FIXME: If we can leave the attributes in the token stream somehow, we can
4383 // get rid of a parameter (attrs) and this statement. It might be too much
4384 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004385 DS.takeAttributesFrom(attrs);
4386
Chris Lattnere64c5492009-02-27 18:38:20 +00004387 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004388
Chris Lattnerf97409f2008-04-06 06:57:35 +00004389 // Parse the declarator. This is "PrototypeContext", because we must
4390 // accept either 'declarator' or 'abstract-declarator' here.
4391 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4392 ParseDeclarator(ParmDecl);
4393
4394 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004395 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004396
Chris Lattnerf97409f2008-04-06 06:57:35 +00004397 // Remember this parsed parameter in ParamInfo.
4398 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004399
Douglas Gregor72b505b2008-12-16 21:30:33 +00004400 // DefArgToks is used when the parsing of default arguments needs
4401 // to be delayed.
4402 CachedTokens *DefArgToks = 0;
4403
Chris Lattnerf97409f2008-04-06 06:57:35 +00004404 // If no parameter was specified, verify that *something* was specified,
4405 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004406 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4407 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004408 // Completely missing, emit error.
4409 Diag(DSStart, diag::err_missing_param);
4410 } else {
4411 // Otherwise, we have something. Add it and let semantic analysis try
4412 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004413
Chris Lattnerf97409f2008-04-06 06:57:35 +00004414 // Inform the actions module about the parameter declarator, so it gets
4415 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004416 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004417
4418 // Parse the default argument, if any. We parse the default
4419 // arguments in all dialects; the semantic analysis in
4420 // ActOnParamDefaultArgument will reject the default argument in
4421 // C.
4422 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004423 SourceLocation EqualLoc = Tok.getLocation();
4424
Chris Lattner04421082008-04-08 04:40:51 +00004425 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004426 if (D.getContext() == Declarator::MemberContext) {
4427 // If we're inside a class definition, cache the tokens
4428 // corresponding to the default argument. We'll actually parse
4429 // them when we see the end of the class definition.
4430 // FIXME: Templates will require something similar.
4431 // FIXME: Can we use a smart pointer for Toks?
4432 DefArgToks = new CachedTokens;
4433
Mike Stump1eb44332009-09-09 15:08:12 +00004434 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004435 /*StopAtSemi=*/true,
4436 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004437 delete DefArgToks;
4438 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004439 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004440 } else {
4441 // Mark the end of the default argument so that we know when to
4442 // stop when we parse it later on.
4443 Token DefArgEnd;
4444 DefArgEnd.startToken();
4445 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4446 DefArgEnd.setLocation(Tok.getLocation());
4447 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004448 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004449 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004450 }
Chris Lattner04421082008-04-08 04:40:51 +00004451 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004452 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004453 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004454
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004455 // The argument isn't actually potentially evaluated unless it is
4456 // used.
4457 EnterExpressionEvaluationContext Eval(Actions,
4458 Sema::PotentiallyEvaluatedIfUsed);
4459
John McCall60d7b3a2010-08-24 06:29:42 +00004460 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004461 if (DefArgResult.isInvalid()) {
4462 Actions.ActOnParamDefaultArgumentError(Param);
4463 SkipUntil(tok::comma, tok::r_paren, true, true);
4464 } else {
4465 // Inform the actions module about the default argument
4466 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004467 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004468 }
Chris Lattner04421082008-04-08 04:40:51 +00004469 }
4470 }
Mike Stump1eb44332009-09-09 15:08:12 +00004471
4472 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4473 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004474 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004475 }
4476
4477 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004478 if (Tok.isNot(tok::comma)) {
4479 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004480 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4481
4482 if (!getLang().CPlusPlus) {
4483 // We have ellipsis without a preceding ',', which is ill-formed
4484 // in C. Complain and provide the fix.
4485 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004486 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004487 }
4488 }
4489
4490 break;
4491 }
Mike Stump1eb44332009-09-09 15:08:12 +00004492
Chris Lattnerf97409f2008-04-06 06:57:35 +00004493 // Consume the comma.
4494 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004495 }
Mike Stump1eb44332009-09-09 15:08:12 +00004496
Chris Lattner66d28652008-04-06 06:34:08 +00004497}
Chris Lattneref4715c2008-04-06 05:45:57 +00004498
Reid Spencer5f016e22007-07-11 17:01:13 +00004499/// [C90] direct-declarator '[' constant-expression[opt] ']'
4500/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4501/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4502/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4503/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4504void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004505 BalancedDelimiterTracker T(*this, tok::l_square);
4506 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004507
Chris Lattner378c7e42008-12-18 07:27:21 +00004508 // C array syntax has many features, but by-far the most common is [] and [4].
4509 // This code does a fast path to handle some of the most obvious cases.
4510 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004511 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004512 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004513 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004514
Chris Lattner378c7e42008-12-18 07:27:21 +00004515 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004516 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004517 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004518 T.getOpenLocation(),
4519 T.getCloseLocation()),
4520 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004521 return;
4522 } else if (Tok.getKind() == tok::numeric_constant &&
4523 GetLookAheadToken(1).is(tok::r_square)) {
4524 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004525 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004526 ConsumeToken();
4527
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004528 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004529 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004530 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004531
Chris Lattner378c7e42008-12-18 07:27:21 +00004532 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004533 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004534 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004535 T.getOpenLocation(),
4536 T.getCloseLocation()),
4537 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004538 return;
4539 }
Mike Stump1eb44332009-09-09 15:08:12 +00004540
Reid Spencer5f016e22007-07-11 17:01:13 +00004541 // If valid, this location is the position where we read the 'static' keyword.
4542 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004543 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004544 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004545
Reid Spencer5f016e22007-07-11 17:01:13 +00004546 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004547 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004548 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004549 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004550
Reid Spencer5f016e22007-07-11 17:01:13 +00004551 // If we haven't already read 'static', check to see if there is one after the
4552 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004553 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004554 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004555
Reid Spencer5f016e22007-07-11 17:01:13 +00004556 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4557 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004558 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004559
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004560 // Handle the case where we have '[*]' as the array size. However, a leading
4561 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4562 // the the token after the star is a ']'. Since stars in arrays are
4563 // infrequent, use of lookahead is not costly here.
4564 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004565 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004566
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004567 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004568 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004569 StaticLoc = SourceLocation(); // Drop the static.
4570 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004571 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004572 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004573 // Note, in C89, this production uses the constant-expr production instead
4574 // of assignment-expr. The only difference is that assignment-expr allows
4575 // things like '=' and '*='. Sema rejects these in C89 mode because they
4576 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004577
Douglas Gregore0762c92009-06-19 23:52:42 +00004578 // Parse the constant-expression or assignment-expression now (depending
4579 // on dialect).
4580 if (getLang().CPlusPlus)
4581 NumElements = ParseConstantExpression();
4582 else
4583 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004584 }
Mike Stump1eb44332009-09-09 15:08:12 +00004585
Reid Spencer5f016e22007-07-11 17:01:13 +00004586 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004587 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004588 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004589 // If the expression was invalid, skip it.
4590 SkipUntil(tok::r_square);
4591 return;
4592 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004593
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004594 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004595
John McCall0b7e6782011-03-24 11:26:52 +00004596 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004597 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004598
Chris Lattner378c7e42008-12-18 07:27:21 +00004599 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004600 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004601 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004602 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004603 T.getOpenLocation(),
4604 T.getCloseLocation()),
4605 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004606}
4607
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004608/// [GNU] typeof-specifier:
4609/// typeof ( expressions )
4610/// typeof ( type-name )
4611/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004612///
4613void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004614 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004615 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004616 SourceLocation StartLoc = ConsumeToken();
4617
John McCallcfb708c2010-01-13 20:03:27 +00004618 const bool hasParens = Tok.is(tok::l_paren);
4619
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004620 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004621 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004622 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004623 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4624 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004625 if (hasParens)
4626 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004627
4628 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004629 // FIXME: Not accurate, the range gets one token more than it should.
4630 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004631 else
4632 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004633
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004634 if (isCastExpr) {
4635 if (!CastTy) {
4636 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004637 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004638 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004639
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004640 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004641 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004642 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4643 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004644 DiagID, CastTy))
4645 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004646 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004647 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004648
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004649 // If we get here, the operand to the typeof was an expresion.
4650 if (Operand.isInvalid()) {
4651 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004652 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004653 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004654
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004655 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004656 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004657 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4658 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004659 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004660 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004661}
Chris Lattner1b492422010-02-28 18:33:55 +00004662
Eli Friedmanb001de72011-10-06 23:00:33 +00004663/// [C1X] atomic-specifier:
4664/// _Atomic ( type-name )
4665///
4666void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4667 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4668
4669 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004670 BalancedDelimiterTracker T(*this, tok::l_paren);
4671 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004672 SkipUntil(tok::r_paren);
4673 return;
4674 }
4675
4676 TypeResult Result = ParseTypeName();
4677 if (Result.isInvalid()) {
4678 SkipUntil(tok::r_paren);
4679 return;
4680 }
4681
4682 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004683 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004684
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004685 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004686 return;
4687
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004688 DS.setTypeofParensRange(T.getRange());
4689 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004690
4691 const char *PrevSpec = 0;
4692 unsigned DiagID;
4693 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4694 DiagID, Result.release()))
4695 Diag(StartLoc, DiagID) << PrevSpec;
4696}
4697
Chris Lattner1b492422010-02-28 18:33:55 +00004698
4699/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4700/// from TryAltiVecVectorToken.
4701bool Parser::TryAltiVecVectorTokenOutOfLine() {
4702 Token Next = NextToken();
4703 switch (Next.getKind()) {
4704 default: return false;
4705 case tok::kw_short:
4706 case tok::kw_long:
4707 case tok::kw_signed:
4708 case tok::kw_unsigned:
4709 case tok::kw_void:
4710 case tok::kw_char:
4711 case tok::kw_int:
4712 case tok::kw_float:
4713 case tok::kw_double:
4714 case tok::kw_bool:
4715 case tok::kw___pixel:
4716 Tok.setKind(tok::kw___vector);
4717 return true;
4718 case tok::identifier:
4719 if (Next.getIdentifierInfo() == Ident_pixel) {
4720 Tok.setKind(tok::kw___vector);
4721 return true;
4722 }
4723 return false;
4724 }
4725}
4726
4727bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4728 const char *&PrevSpec, unsigned &DiagID,
4729 bool &isInvalid) {
4730 if (Tok.getIdentifierInfo() == Ident_vector) {
4731 Token Next = NextToken();
4732 switch (Next.getKind()) {
4733 case tok::kw_short:
4734 case tok::kw_long:
4735 case tok::kw_signed:
4736 case tok::kw_unsigned:
4737 case tok::kw_void:
4738 case tok::kw_char:
4739 case tok::kw_int:
4740 case tok::kw_float:
4741 case tok::kw_double:
4742 case tok::kw_bool:
4743 case tok::kw___pixel:
4744 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4745 return true;
4746 case tok::identifier:
4747 if (Next.getIdentifierInfo() == Ident_pixel) {
4748 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4749 return true;
4750 }
4751 break;
4752 default:
4753 break;
4754 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004755 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004756 DS.isTypeAltiVecVector()) {
4757 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4758 return true;
4759 }
4760 return false;
4761}