blob: be5d5ae54cdbc2b224debbc9114b487cfee4a3d6 [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:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000525/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000526///
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'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000539/// opt-message:
540/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000541void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
542 SourceLocation AvailabilityLoc,
543 ParsedAttributes &attrs,
544 SourceLocation *endLoc) {
545 SourceLocation PlatformLoc;
546 IdentifierInfo *Platform = 0;
547
548 enum { Introduced, Deprecated, Obsoleted, Unknown };
549 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000550 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000551
552 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000553 BalancedDelimiterTracker T(*this, tok::l_paren);
554 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555 Diag(Tok, diag::err_expected_lparen);
556 return;
557 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000558
559 // Parse the platform name,
560 if (Tok.isNot(tok::identifier)) {
561 Diag(Tok, diag::err_availability_expected_platform);
562 SkipUntil(tok::r_paren);
563 return;
564 }
565 Platform = Tok.getIdentifierInfo();
566 PlatformLoc = ConsumeToken();
567
568 // Parse the ',' following the platform name.
569 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
570 return;
571
572 // If we haven't grabbed the pointers for the identifiers
573 // "introduced", "deprecated", and "obsoleted", do so now.
574 if (!Ident_introduced) {
575 Ident_introduced = PP.getIdentifierInfo("introduced");
576 Ident_deprecated = PP.getIdentifierInfo("deprecated");
577 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000578 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000579 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000580 }
581
582 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000583 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000584 do {
585 if (Tok.isNot(tok::identifier)) {
586 Diag(Tok, diag::err_availability_expected_change);
587 SkipUntil(tok::r_paren);
588 return;
589 }
590 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
591 SourceLocation KeywordLoc = ConsumeToken();
592
Douglas Gregorb53e4172011-03-26 03:35:55 +0000593 if (Keyword == Ident_unavailable) {
594 if (UnavailableLoc.isValid()) {
595 Diag(KeywordLoc, diag::err_availability_redundant)
596 << Keyword << SourceRange(UnavailableLoc);
597 }
598 UnavailableLoc = KeywordLoc;
599
600 if (Tok.isNot(tok::comma))
601 break;
602
603 ConsumeToken();
604 continue;
605 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000606
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000607 if (Tok.isNot(tok::equal)) {
608 Diag(Tok, diag::err_expected_equal_after)
609 << Keyword;
610 SkipUntil(tok::r_paren);
611 return;
612 }
613 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000614 if (Keyword == Ident_message) {
615 if (!isTokenStringLiteral()) {
616 Diag(Tok, diag::err_expected_string_literal);
617 SkipUntil(tok::r_paren);
618 return;
619 }
620 MessageExpr = ParseStringLiteralExpression();
621 break;
622 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000623
624 SourceRange VersionRange;
625 VersionTuple Version = ParseVersionTuple(VersionRange);
626
627 if (Version.empty()) {
628 SkipUntil(tok::r_paren);
629 return;
630 }
631
632 unsigned Index;
633 if (Keyword == Ident_introduced)
634 Index = Introduced;
635 else if (Keyword == Ident_deprecated)
636 Index = Deprecated;
637 else if (Keyword == Ident_obsoleted)
638 Index = Obsoleted;
639 else
640 Index = Unknown;
641
642 if (Index < Unknown) {
643 if (!Changes[Index].KeywordLoc.isInvalid()) {
644 Diag(KeywordLoc, diag::err_availability_redundant)
645 << Keyword
646 << SourceRange(Changes[Index].KeywordLoc,
647 Changes[Index].VersionRange.getEnd());
648 }
649
650 Changes[Index].KeywordLoc = KeywordLoc;
651 Changes[Index].Version = Version;
652 Changes[Index].VersionRange = VersionRange;
653 } else {
654 Diag(KeywordLoc, diag::err_availability_unknown_change)
655 << Keyword << VersionRange;
656 }
657
658 if (Tok.isNot(tok::comma))
659 break;
660
661 ConsumeToken();
662 } while (true);
663
664 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000665 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000666 return;
667
668 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670
Douglas Gregorb53e4172011-03-26 03:35:55 +0000671 // The 'unavailable' availability cannot be combined with any other
672 // availability changes. Make sure that hasn't happened.
673 if (UnavailableLoc.isValid()) {
674 bool Complained = false;
675 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
676 if (Changes[Index].KeywordLoc.isValid()) {
677 if (!Complained) {
678 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
679 << SourceRange(Changes[Index].KeywordLoc,
680 Changes[Index].VersionRange.getEnd());
681 Complained = true;
682 }
683
684 // Clear out the availability.
685 Changes[Index] = AvailabilityChange();
686 }
687 }
688 }
689
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000690 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000691 attrs.addNew(&Availability,
692 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000693 0, SourceLocation(),
694 Platform, PlatformLoc,
695 Changes[Introduced],
696 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000697 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000698 UnavailableLoc, MessageExpr.take(),
699 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000700}
701
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000702
703// Late Parsed Attributes:
704// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
705
706void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
707
708void Parser::LateParsedClass::ParseLexedAttributes() {
709 Self->ParseLexedAttributes(*Class);
710}
711
712void Parser::LateParsedAttribute::ParseLexedAttributes() {
713 Self->ParseLexedAttribute(*this);
714}
715
716/// Wrapper class which calls ParseLexedAttribute, after setting up the
717/// scope appropriately.
718void Parser::ParseLexedAttributes(ParsingClass &Class) {
719 // Deal with templates
720 // FIXME: Test cases to make sure this does the right thing for templates.
721 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
722 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
723 HasTemplateScope);
724 if (HasTemplateScope)
725 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
726
727 // Set or update the scope flags to include Scope::ThisScope.
728 bool AlreadyHasClassScope = Class.TopLevelClass;
729 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
730 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
731 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
732
733 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
734 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
735 }
736}
737
738/// \brief Finish parsing an attribute for which parsing was delayed.
739/// This will be called at the end of parsing a class declaration
740/// for each LateParsedAttribute. We consume the saved tokens and
741/// create an attribute with the arguments filled in. We add this
742/// to the Attribute list for the decl.
743void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
744 // Save the current token position.
745 SourceLocation OrigLoc = Tok.getLocation();
746
747 // Append the current token at the end of the new token stream so that it
748 // doesn't get lost.
749 LA.Toks.push_back(Tok);
750 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
751 // Consume the previously pushed token.
752 ConsumeAnyToken();
753
754 ParsedAttributes Attrs(AttrFactory);
755 SourceLocation endLoc;
756
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000757 // If the Decl is templatized, add template parameters to scope.
758 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
759 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
760 if (HasTemplateScope)
761 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
762
763 // If the Decl is on a function, add function parameters to the scope.
764 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
765 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
766 if (HasFunctionScope)
767 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
768
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000769 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
770
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000771 if (HasFunctionScope) {
772 Actions.ActOnExitFunctionContext();
773 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
774 }
775 if (HasTemplateScope) {
776 TempScope.Exit();
777 }
778
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000779 // Late parsed attributes must be attached to Decls by hand. If the
780 // LA.D is not set, then this was not done properly.
781 assert(LA.D && "No decl attached to late parsed attribute");
782 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
783
784 if (Tok.getLocation() != OrigLoc) {
785 // Due to a parsing error, we either went over the cached tokens or
786 // there are still cached tokens left, so we skip the leftover tokens.
787 // Since this is an uncommon situation that should be avoided, use the
788 // expensive isBeforeInTranslationUnit call.
789 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
790 OrigLoc))
791 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
792 ConsumeAnyToken();
793 }
794}
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796/// \brief Wrapper around a case statement checking if AttrName is
797/// one of the thread safety attributes
798bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
799 return llvm::StringSwitch<bool>(AttrName)
800 .Case("guarded_by", true)
801 .Case("guarded_var", true)
802 .Case("pt_guarded_by", true)
803 .Case("pt_guarded_var", true)
804 .Case("lockable", true)
805 .Case("scoped_lockable", true)
806 .Case("no_thread_safety_analysis", true)
807 .Case("acquired_after", true)
808 .Case("acquired_before", true)
809 .Case("exclusive_lock_function", true)
810 .Case("shared_lock_function", true)
811 .Case("exclusive_trylock_function", true)
812 .Case("shared_trylock_function", true)
813 .Case("unlock_function", true)
814 .Case("lock_returned", true)
815 .Case("locks_excluded", true)
816 .Case("exclusive_locks_required", true)
817 .Case("shared_locks_required", true)
818 .Default(false);
819}
820
821/// \brief Parse the contents of thread safety attributes. These
822/// should always be parsed as an expression list.
823///
824/// We need to special case the parsing due to the fact that if the first token
825/// of the first argument is an identifier, the main parse loop will store
826/// that token as a "parameter" and the rest of
827/// the arguments will be added to a list of "arguments". However,
828/// subsequent tokens in the first argument are lost. We instead parse each
829/// argument as an expression and add all arguments to the list of "arguments".
830/// In future, we will take advantage of this special case to also
831/// deal with some argument scoping issues here (for example, referring to a
832/// function parameter in the attribute on that function).
833void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
834 SourceLocation AttrNameLoc,
835 ParsedAttributes &Attrs,
836 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000839 BalancedDelimiterTracker T(*this, tok::l_paren);
840 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841
842 ExprVector ArgExprs(Actions);
843 bool ArgExprsOk = true;
844
845 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000846 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000847 ExprResult ArgExpr(ParseAssignmentExpression());
848 if (ArgExpr.isInvalid()) {
849 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000850 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000851 break;
852 } else {
853 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000854 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000855 if (Tok.isNot(tok::comma))
856 break;
857 ConsumeToken(); // Eat the comma, move to the next argument
858 }
859 // Match the ')'.
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000860 if (ArgExprsOk && !T.consumeClose() && ArgExprs.size() > 0) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000861 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
862 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000863 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000864 if (EndLoc)
865 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000866}
867
John McCall7f040a92010-12-24 02:08:15 +0000868void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
869 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
870 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000871}
872
Reid Spencer5f016e22007-07-11 17:01:13 +0000873/// ParseDeclaration - Parse a full 'declaration', which consists of
874/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000875/// 'Context' should be a Declarator::TheContext value. This returns the
876/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000877///
878/// declaration: [C99 6.7]
879/// block-declaration ->
880/// simple-declaration
881/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000882/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000883/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000884/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000885/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000886/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000887/// others... [FIXME]
888///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000889Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
890 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000891 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000892 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000893 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000894 // Must temporarily exit the objective-c container scope for
895 // parsing c none objective-c decls.
896 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000897
John McCalld226f652010-08-21 09:40:31 +0000898 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000899 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000900 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000901 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000902 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000905 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000906 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000907 // Could be the start of an inline namespace. Allowed as an ext in C++03.
908 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000909 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000910 SourceLocation InlineLoc = ConsumeToken();
911 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
912 break;
913 }
John McCall7f040a92010-12-24 02:08:15 +0000914 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000915 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000916 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000917 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000918 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000919 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000920 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000921 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000922 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000923 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000924 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000925 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000926 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000927 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000928 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000929 default:
John McCall7f040a92010-12-24 02:08:15 +0000930 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000931 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000932
Chris Lattner682bf922009-03-29 16:50:03 +0000933 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000934 // single decl, convert it now. Alias declarations can also declare a type;
935 // include that too if it is present.
936 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000937}
938
939/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
940/// declaration-specifiers init-declarator-list[opt] ';'
941///[C90/C++]init-declarator-list ';' [TODO]
942/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000943///
Richard Smithad762fc2011-04-14 22:09:26 +0000944/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
945/// attribute-specifier-seq[opt] type-specifier-seq declarator
946///
Chris Lattnercd147752009-03-29 17:27:48 +0000947/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000948/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000949///
950/// If FRI is non-null, we might be parsing a for-range-declaration instead
951/// of a simple-declaration. If we find that we are, we also parse the
952/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000953Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
954 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000955 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000956 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000957 bool RequireSemi,
958 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000960 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000961 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000962
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000963 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000964 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
967 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000968 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000969 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000970 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000971 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000972 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000973 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000975
976 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000977}
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Richard Smith0706df42011-10-19 21:33:05 +0000979/// Returns true if this might be the start of a declarator, or a common typo
980/// for a declarator.
981bool Parser::MightBeDeclarator(unsigned Context) {
982 switch (Tok.getKind()) {
983 case tok::annot_cxxscope:
984 case tok::annot_template_id:
985 case tok::caret:
986 case tok::code_completion:
987 case tok::coloncolon:
988 case tok::ellipsis:
989 case tok::kw___attribute:
990 case tok::kw_operator:
991 case tok::l_paren:
992 case tok::star:
993 return true;
994
995 case tok::amp:
996 case tok::ampamp:
Richard Smith0706df42011-10-19 21:33:05 +0000997 return getLang().CPlusPlus;
998
Richard Smith1c94c162012-01-09 22:31:44 +0000999 case tok::l_square: // Might be an attribute on an unnamed bit-field.
1000 return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
1001 NextToken().is(tok::l_square);
1002
1003 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1004 return Context == Declarator::MemberContext || getLang().CPlusPlus;
1005
Richard Smith0706df42011-10-19 21:33:05 +00001006 case tok::identifier:
1007 switch (NextToken().getKind()) {
1008 case tok::code_completion:
1009 case tok::coloncolon:
1010 case tok::comma:
1011 case tok::equal:
1012 case tok::equalequal: // Might be a typo for '='.
1013 case tok::kw_alignas:
1014 case tok::kw_asm:
1015 case tok::kw___attribute:
1016 case tok::l_brace:
1017 case tok::l_paren:
1018 case tok::l_square:
1019 case tok::less:
1020 case tok::r_brace:
1021 case tok::r_paren:
1022 case tok::r_square:
1023 case tok::semi:
1024 return true;
1025
1026 case tok::colon:
1027 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001028 // and in block scope it's probably a label. Inside a class definition,
1029 // this is a bit-field.
1030 return Context == Declarator::MemberContext ||
1031 (getLang().CPlusPlus && Context == Declarator::FileContext);
1032
1033 case tok::identifier: // Possible virt-specifier.
1034 return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001035
1036 default:
1037 return false;
1038 }
1039
1040 default:
1041 return false;
1042 }
1043}
1044
John McCalld8ac0572009-11-03 19:26:08 +00001045/// ParseDeclGroup - Having concluded that this is either a function
1046/// definition or a group of object declarations, actually parse the
1047/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001048Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1049 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001050 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001051 SourceLocation *DeclEnd,
1052 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001053 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001054 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001055 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001056
John McCalld8ac0572009-11-03 19:26:08 +00001057 // Bail out if the first declarator didn't seem well-formed.
1058 if (!D.hasName() && !D.mayOmitIdentifier()) {
1059 // Skip until ; or }.
1060 SkipUntil(tok::r_brace, true, true);
1061 if (Tok.is(tok::semi))
1062 ConsumeToken();
1063 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattnerc82daef2010-07-11 22:24:20 +00001066 // Check to see if we have a function *definition* which must have a body.
1067 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1068 // Look at the next token to make sure that this isn't a function
1069 // declaration. We have to check this because __attribute__ might be the
1070 // start of a function definition in GCC-extended K&R C.
1071 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001072
Chris Lattner004659a2010-07-11 22:42:07 +00001073 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001074 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1075 Diag(Tok, diag::err_function_declared_typedef);
1076
1077 // Recover by treating the 'typedef' as spurious.
1078 DS.ClearStorageClassSpecs();
1079 }
1080
John McCalld226f652010-08-21 09:40:31 +00001081 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001082 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001083 }
1084
1085 if (isDeclarationSpecifier()) {
1086 // If there is an invalid declaration specifier right after the function
1087 // prototype, then we must be in a missing semicolon case where this isn't
1088 // actually a body. Just fall through into the code that handles it as a
1089 // prototype, and let the top-level code handle the erroneous declspec
1090 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001091 } else {
1092 Diag(Tok, diag::err_expected_fn_body);
1093 SkipUntil(tok::semi);
1094 return DeclGroupPtrTy();
1095 }
1096 }
1097
Richard Smithad762fc2011-04-14 22:09:26 +00001098 if (ParseAttributesAfterDeclarator(D))
1099 return DeclGroupPtrTy();
1100
1101 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1102 // must parse and analyze the for-range-initializer before the declaration is
1103 // analyzed.
1104 if (FRI && Tok.is(tok::colon)) {
1105 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001106 if (Tok.is(tok::l_brace))
1107 FRI->RangeExpr = ParseBraceInitializer();
1108 else
1109 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001110 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1111 Actions.ActOnCXXForRangeDecl(ThisDecl);
1112 Actions.FinalizeDeclaration(ThisDecl);
1113 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1114 }
1115
Chris Lattner5f9e2722011-07-23 10:55:15 +00001116 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001117 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001118 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001119 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001120 DeclsInGroup.push_back(FirstDecl);
1121
Richard Smith0706df42011-10-19 21:33:05 +00001122 bool ExpectSemi = Context != Declarator::ForContext;
1123
John McCalld8ac0572009-11-03 19:26:08 +00001124 // If we don't have a comma, it is either the end of the list (a ';') or an
1125 // error, bail out.
1126 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001127 SourceLocation CommaLoc = ConsumeToken();
1128
1129 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1130 // This comma was followed by a line-break and something which can't be
1131 // the start of a declarator. The comma was probably a typo for a
1132 // semicolon.
1133 Diag(CommaLoc, diag::err_expected_semi_declaration)
1134 << FixItHint::CreateReplacement(CommaLoc, ";");
1135 ExpectSemi = false;
1136 break;
1137 }
John McCalld8ac0572009-11-03 19:26:08 +00001138
1139 // Parse the next declarator.
1140 D.clear();
1141
1142 // Accept attributes in an init-declarator. In the first declarator in a
1143 // declaration, these would be part of the declspec. In subsequent
1144 // declarators, they become part of the declarator itself, so that they
1145 // don't apply to declarators after *this* one. Examples:
1146 // short __attribute__((common)) var; -> declspec
1147 // short var __attribute__((common)); -> declarator
1148 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001149 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001150
1151 ParseDeclarator(D);
1152
John McCalld226f652010-08-21 09:40:31 +00001153 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001154 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001155 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001156 DeclsInGroup.push_back(ThisDecl);
1157 }
1158
1159 if (DeclEnd)
1160 *DeclEnd = Tok.getLocation();
1161
Richard Smith0706df42011-10-19 21:33:05 +00001162 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001163 ExpectAndConsume(tok::semi,
1164 Context == Declarator::FileContext
1165 ? diag::err_invalid_token_after_toplevel_declarator
1166 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001167 // Okay, there was no semicolon and one was expected. If we see a
1168 // declaration specifier, just assume it was missing and continue parsing.
1169 // Otherwise things are very confused and we skip to recover.
1170 if (!isDeclarationSpecifier()) {
1171 SkipUntil(tok::r_brace, true, true);
1172 if (Tok.is(tok::semi))
1173 ConsumeToken();
1174 }
John McCalld8ac0572009-11-03 19:26:08 +00001175 }
1176
Douglas Gregor23c94db2010-07-02 17:43:08 +00001177 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001178 DeclsInGroup.data(),
1179 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001180}
1181
Richard Smithad762fc2011-04-14 22:09:26 +00001182/// Parse an optional simple-asm-expr and attributes, and attach them to a
1183/// declarator. Returns true on an error.
1184bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1185 // If a simple-asm-expr is present, parse it.
1186 if (Tok.is(tok::kw_asm)) {
1187 SourceLocation Loc;
1188 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1189 if (AsmLabel.isInvalid()) {
1190 SkipUntil(tok::semi, true, true);
1191 return true;
1192 }
1193
1194 D.setAsmLabel(AsmLabel.release());
1195 D.SetRangeEnd(Loc);
1196 }
1197
1198 MaybeParseGNUAttributes(D);
1199 return false;
1200}
1201
Douglas Gregor1426e532009-05-12 21:31:51 +00001202/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1203/// declarator'. This method parses the remainder of the declaration
1204/// (including any attributes or initializer, among other things) and
1205/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001206///
Reid Spencer5f016e22007-07-11 17:01:13 +00001207/// init-declarator: [C99 6.7]
1208/// declarator
1209/// declarator '=' initializer
1210/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1211/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001212/// [C++] declarator initializer[opt]
1213///
1214/// [C++] initializer:
1215/// [C++] '=' initializer-clause
1216/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001217/// [C++0x] '=' 'default' [TODO]
1218/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001219/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001220///
1221/// According to the standard grammar, =default and =delete are function
1222/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001223///
John McCalld226f652010-08-21 09:40:31 +00001224Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001225 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001226 if (ParseAttributesAfterDeclarator(D))
1227 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Richard Smithad762fc2011-04-14 22:09:26 +00001229 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1230}
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Richard Smithad762fc2011-04-14 22:09:26 +00001232Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1233 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001234 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001235 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001236 switch (TemplateInfo.Kind) {
1237 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001238 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001239 break;
1240
1241 case ParsedTemplateInfo::Template:
1242 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001243 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001244 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001245 TemplateInfo.TemplateParams->data(),
1246 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001247 D);
1248 break;
1249
1250 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001251 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001252 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001253 TemplateInfo.ExternLoc,
1254 TemplateInfo.TemplateLoc,
1255 D);
1256 if (ThisRes.isInvalid()) {
1257 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001258 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001259 }
1260
1261 ThisDecl = ThisRes.get();
1262 break;
1263 }
1264 }
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Richard Smith34b41d92011-02-20 03:19:35 +00001266 bool TypeContainsAuto =
1267 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1268
Douglas Gregor1426e532009-05-12 21:31:51 +00001269 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001270 if (isTokenEqualOrMistypedEqualEqual(
1271 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001272 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001273 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001274 if (D.isFunctionDeclarator())
1275 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1276 << 1 /* delete */;
1277 else
1278 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001279 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001280 if (D.isFunctionDeclarator())
1281 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1282 << 1 /* delete */;
1283 else
1284 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001285 } else {
John McCall731ad842009-12-19 09:28:58 +00001286 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1287 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001288 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001289 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001290
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001291 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001292 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001293 cutOffParsing();
1294 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001295 }
1296
John McCall60d7b3a2010-08-24 06:29:42 +00001297 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001298
John McCall731ad842009-12-19 09:28:58 +00001299 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001300 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001301 ExitScope();
1302 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001303
Douglas Gregor1426e532009-05-12 21:31:51 +00001304 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001305 SkipUntil(tok::comma, true, true);
1306 Actions.ActOnInitializerError(ThisDecl);
1307 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001308 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1309 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001310 }
1311 } else if (Tok.is(tok::l_paren)) {
1312 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001313 BalancedDelimiterTracker T(*this, tok::l_paren);
1314 T.consumeOpen();
1315
Douglas Gregor1426e532009-05-12 21:31:51 +00001316 ExprVector Exprs(Actions);
1317 CommaLocsTy CommaLocs;
1318
Douglas Gregorb4debae2009-12-22 17:47:17 +00001319 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1320 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001321 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001322 }
1323
Douglas Gregor1426e532009-05-12 21:31:51 +00001324 if (ParseExpressionList(Exprs, CommaLocs)) {
1325 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001326
1327 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001329 ExitScope();
1330 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001331 } else {
1332 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001333 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001334
1335 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1336 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001337
1338 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001339 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001340 ExitScope();
1341 }
1342
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001343 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001344 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001345 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001346 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001347 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001348 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1349 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001350 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1351
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001352 if (D.getCXXScopeSpec().isSet()) {
1353 EnterScope(0);
1354 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1355 }
1356
1357 ExprResult Init(ParseBraceInitializer());
1358
1359 if (D.getCXXScopeSpec().isSet()) {
1360 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1361 ExitScope();
1362 }
1363
1364 if (Init.isInvalid()) {
1365 Actions.ActOnInitializerError(ThisDecl);
1366 } else
1367 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1368 /*DirectInit=*/true, TypeContainsAuto);
1369
Douglas Gregor1426e532009-05-12 21:31:51 +00001370 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001371 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001372 }
1373
Richard Smith483b9f32011-02-21 20:05:19 +00001374 Actions.FinalizeDeclaration(ThisDecl);
1375
Douglas Gregor1426e532009-05-12 21:31:51 +00001376 return ThisDecl;
1377}
1378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379/// ParseSpecifierQualifierList
1380/// specifier-qualifier-list:
1381/// type-specifier specifier-qualifier-list[opt]
1382/// type-qualifier specifier-qualifier-list[opt]
1383/// [GNU] attributes specifier-qualifier-list[opt]
1384///
Richard Smithc89edf52011-07-01 19:46:12 +00001385void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1387 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001388 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001389 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 // Validate declspec for type-name.
1392 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001393 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001394 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 // Issue diagnostic and remove storage class if present.
1398 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1399 if (DS.getStorageClassSpecLoc().isValid())
1400 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1401 else
1402 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1403 DS.ClearStorageClassSpecs();
1404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 // Issue diagnostic and remove function specfier if present.
1407 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001408 if (DS.isInlineSpecified())
1409 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1410 if (DS.isVirtualSpecified())
1411 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1412 if (DS.isExplicitSpecified())
1413 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 DS.ClearFunctionSpecs();
1415 }
1416}
1417
Chris Lattnerc199ab32009-04-12 20:42:31 +00001418/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1419/// specified token is valid after the identifier in a declarator which
1420/// immediately follows the declspec. For example, these things are valid:
1421///
1422/// int x [ 4]; // direct-declarator
1423/// int x ( int y); // direct-declarator
1424/// int(int x ) // direct-declarator
1425/// int x ; // simple-declaration
1426/// int x = 17; // init-declarator-list
1427/// int x , y; // init-declarator-list
1428/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001429/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001430/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001431///
1432/// This is not, because 'x' does not immediately follow the declspec (though
1433/// ')' happens to be valid anyway).
1434/// int (x)
1435///
1436static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1437 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1438 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001439 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001440}
1441
Chris Lattnere40c2952009-04-14 21:34:55 +00001442
1443/// ParseImplicitInt - This method is called when we have an non-typename
1444/// identifier in a declspec (which normally terminates the decl spec) when
1445/// the declspec has no type specifier. In this case, the declspec is either
1446/// malformed or is "implicit int" (in K&R and C89).
1447///
1448/// This method handles diagnosing this prettily and returns false if the
1449/// declspec is done being processed. If it recovers and thinks there may be
1450/// other pieces of declspec after it, it returns true.
1451///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001452bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001453 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001454 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001455 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Chris Lattnere40c2952009-04-14 21:34:55 +00001457 SourceLocation Loc = Tok.getLocation();
1458 // If we see an identifier that is not a type name, we normally would
1459 // parse it as the identifer being declared. However, when a typename
1460 // is typo'd or the definition is not included, this will incorrectly
1461 // parse the typename as the identifier name and fall over misparsing
1462 // later parts of the diagnostic.
1463 //
1464 // As such, we try to do some look-ahead in cases where this would
1465 // otherwise be an "implicit-int" case to see if this is invalid. For
1466 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1467 // an identifier with implicit int, we'd get a parse error because the
1468 // next token is obviously invalid for a type. Parse these as a case
1469 // with an invalid type specifier.
1470 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattnere40c2952009-04-14 21:34:55 +00001472 // Since we know that this either implicit int (which is rare) or an
1473 // error, we'd do lookahead to try to do better recovery.
1474 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1475 // If this token is valid for implicit int, e.g. "static x = 4", then
1476 // we just avoid eating the identifier, so it will be parsed as the
1477 // identifier in the declarator.
1478 return false;
1479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Chris Lattnere40c2952009-04-14 21:34:55 +00001481 // Otherwise, if we don't consume this token, we are going to emit an
1482 // error anyway. Try to recover from various common problems. Check
1483 // to see if this was a reference to a tag name without a tag specified.
1484 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001485 //
1486 // C++ doesn't need this, and isTagName doesn't take SS.
1487 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001488 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001489 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregor23c94db2010-07-02 17:43:08 +00001491 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001492 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001493 case DeclSpec::TST_enum:
1494 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1495 case DeclSpec::TST_union:
1496 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1497 case DeclSpec::TST_struct:
1498 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1499 case DeclSpec::TST_class:
1500 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Chris Lattnerf4382f52009-04-14 22:17:06 +00001503 if (TagName) {
1504 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001505 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001506 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Chris Lattnerf4382f52009-04-14 22:17:06 +00001508 // Parse this as a tag as if the missing tag were present.
1509 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001510 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001511 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001512 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001513 return true;
1514 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregora786fdb2009-10-13 23:27:22 +00001517 // This is almost certainly an invalid type name. Let the action emit a
1518 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001519 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001520 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001521 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001522 // The action emitted a diagnostic, so we don't have to.
1523 if (T) {
1524 // The action has suggested that the type T could be used. Set that as
1525 // the type in the declaration specifiers, consume the would-be type
1526 // name token, and we're done.
1527 const char *PrevSpec;
1528 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001529 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001530 DS.SetRangeEnd(Tok.getLocation());
1531 ConsumeToken();
1532
1533 // There may be other declaration specifiers after this.
1534 return true;
1535 }
1536
1537 // Fall through; the action had no suggestion for us.
1538 } else {
1539 // The action did not emit a diagnostic, so emit one now.
1540 SourceRange R;
1541 if (SS) R = SS->getRange();
1542 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Douglas Gregora786fdb2009-10-13 23:27:22 +00001545 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001546 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001547 unsigned DiagID;
1548 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001549 DS.SetRangeEnd(Tok.getLocation());
1550 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattnere40c2952009-04-14 21:34:55 +00001552 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1553 // avoid rippling error messages on subsequent uses of the same type,
1554 // could be useful if #include was forgotten.
1555 return false;
1556}
1557
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001558/// \brief Determine the declaration specifier context from the declarator
1559/// context.
1560///
1561/// \param Context the declarator context, which is one of the
1562/// Declarator::TheContext enumerator values.
1563Parser::DeclSpecContext
1564Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1565 if (Context == Declarator::MemberContext)
1566 return DSC_class;
1567 if (Context == Declarator::FileContext)
1568 return DSC_top_level;
1569 return DSC_normal;
1570}
1571
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001572/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1573///
1574/// FIXME: Simply returns an alignof() expression if the argument is a
1575/// type. Ideally, the type should be propagated directly into Sema.
1576///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001577/// [C11] type-id
1578/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001579/// [C++0x] type-id ...[opt]
1580/// [C++0x] assignment-expression ...[opt]
1581ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1582 SourceLocation &EllipsisLoc) {
1583 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001584 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001585 SourceLocation TypeLoc = Tok.getLocation();
1586 ParsedType Ty = ParseTypeName().get();
1587 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001588 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1589 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001590 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001591 ER = ParseConstantExpression();
1592
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001593 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1594 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001595
1596 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001597}
1598
1599/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1600/// attribute to Attrs.
1601///
1602/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001603/// [C11] '_Alignas' '(' type-id ')'
1604/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001605/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1606/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001607void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1608 SourceLocation *endLoc) {
1609 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1610 "Not an alignment-specifier!");
1611
1612 SourceLocation KWLoc = Tok.getLocation();
1613 ConsumeToken();
1614
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001615 BalancedDelimiterTracker T(*this, tok::l_paren);
1616 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001617 return;
1618
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001619 SourceLocation EllipsisLoc;
1620 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001621 if (ArgExpr.isInvalid()) {
1622 SkipUntil(tok::r_paren);
1623 return;
1624 }
1625
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001626 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001627 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001628 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001629
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001630 // FIXME: Handle pack-expansions here.
1631 if (EllipsisLoc.isValid()) {
1632 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1633 return;
1634 }
1635
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001636 ExprVector ArgExprs(Actions);
1637 ArgExprs.push_back(ArgExpr.release());
1638 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001639 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001640}
1641
Reid Spencer5f016e22007-07-11 17:01:13 +00001642/// ParseDeclarationSpecifiers
1643/// declaration-specifiers: [C99 6.7]
1644/// storage-class-specifier declaration-specifiers[opt]
1645/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001646/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001647/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001648/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001649/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001650///
1651/// storage-class-specifier: [C99 6.7.1]
1652/// 'typedef'
1653/// 'extern'
1654/// 'static'
1655/// 'auto'
1656/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001657/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001658/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001659/// function-specifier: [C99 6.7.4]
1660/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001661/// [C++] 'virtual'
1662/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001663/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001664/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001665/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001666
Reid Spencer5f016e22007-07-11 17:01:13 +00001667///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001668void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001669 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001670 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001671 DeclSpecContext DSContext) {
1672 if (DS.getSourceRange().isInvalid()) {
1673 DS.SetRangeStart(Tok.getLocation());
1674 DS.SetRangeEnd(Tok.getLocation());
1675 }
1676
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001677 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001679 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001681 unsigned DiagID = 0;
1682
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001684
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001686 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001687 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001688 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1689 MaybeParseCXX0XAttributes(DS.getAttributes());
1690
Reid Spencer5f016e22007-07-11 17:01:13 +00001691 // If this is not a declaration specifier token, we're done reading decl
1692 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001693 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001696 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001697 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001698 if (DS.hasTypeSpecifier()) {
1699 bool AllowNonIdentifiers
1700 = (getCurScope()->getFlags() & (Scope::ControlScope |
1701 Scope::BlockScope |
1702 Scope::TemplateParamScope |
1703 Scope::FunctionPrototypeScope |
1704 Scope::AtCatchScope)) == 0;
1705 bool AllowNestedNameSpecifiers
1706 = DSContext == DSC_top_level ||
1707 (DSContext == DSC_class && DS.isFriendSpecified());
1708
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001709 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1710 AllowNonIdentifiers,
1711 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001712 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001713 }
1714
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001715 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1716 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1717 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001718 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1719 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001720 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001721 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001722 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001723 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001724
1725 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001726 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001727 }
1728
Chris Lattner5e02c472009-01-05 00:07:25 +00001729 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001730 // C++ scope specifier. Annotate and loop, or bail out on error.
1731 if (TryAnnotateCXXScopeToken(true)) {
1732 if (!DS.hasTypeSpecifier())
1733 DS.SetTypeSpecError();
1734 goto DoneWithDeclSpec;
1735 }
John McCall2e0a7152010-03-01 18:20:46 +00001736 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1737 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001738 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001739
1740 case tok::annot_cxxscope: {
1741 if (DS.hasTypeSpecifier())
1742 goto DoneWithDeclSpec;
1743
John McCallaa87d332009-12-12 11:40:51 +00001744 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001745 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1746 Tok.getAnnotationRange(),
1747 SS);
John McCallaa87d332009-12-12 11:40:51 +00001748
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001749 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001750 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001751 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001752 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001753 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001754 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001755
1756 // C++ [class.qual]p2:
1757 // In a lookup in which the constructor is an acceptable lookup
1758 // result and the nested-name-specifier nominates a class C:
1759 //
1760 // - if the name specified after the
1761 // nested-name-specifier, when looked up in C, is the
1762 // injected-class-name of C (Clause 9), or
1763 //
1764 // - if the name specified after the nested-name-specifier
1765 // is the same as the identifier or the
1766 // simple-template-id's template-name in the last
1767 // component of the nested-name-specifier,
1768 //
1769 // the name is instead considered to name the constructor of
1770 // class C.
1771 //
1772 // Thus, if the template-name is actually the constructor
1773 // name, then the code is ill-formed; this interpretation is
1774 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001775 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001776 if ((DSContext == DSC_top_level ||
1777 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1778 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001779 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001780 if (isConstructorDeclarator()) {
1781 // The user meant this to be an out-of-line constructor
1782 // definition, but template arguments are not allowed
1783 // there. Just allow this as a constructor; we'll
1784 // complain about it later.
1785 goto DoneWithDeclSpec;
1786 }
1787
1788 // The user meant this to name a type, but it actually names
1789 // a constructor with some extraneous template
1790 // arguments. Complain, then parse it as a type as the user
1791 // intended.
1792 Diag(TemplateId->TemplateNameLoc,
1793 diag::err_out_of_line_template_id_names_constructor)
1794 << TemplateId->Name;
1795 }
1796
John McCallaa87d332009-12-12 11:40:51 +00001797 DS.getTypeSpecScope() = SS;
1798 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001799 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001800 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001801 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001802 continue;
1803 }
1804
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001805 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001806 DS.getTypeSpecScope() = SS;
1807 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001808 if (Tok.getAnnotationValue()) {
1809 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001810 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1811 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001812 PrevSpec, DiagID, T);
1813 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001814 else
1815 DS.SetTypeSpecError();
1816 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1817 ConsumeToken(); // The typename
1818 }
1819
Douglas Gregor9135c722009-03-25 15:40:00 +00001820 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001821 goto DoneWithDeclSpec;
1822
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001823 // If we're in a context where the identifier could be a class name,
1824 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001825 if ((DSContext == DSC_top_level ||
1826 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001827 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001828 &SS)) {
1829 if (isConstructorDeclarator())
1830 goto DoneWithDeclSpec;
1831
1832 // As noted in C++ [class.qual]p2 (cited above), when the name
1833 // of the class is qualified in a context where it could name
1834 // a constructor, its a constructor name. However, we've
1835 // looked at the declarator, and the user probably meant this
1836 // to be a type. Complain that it isn't supposed to be treated
1837 // as a type, then proceed to parse it as a type.
1838 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1839 << Next.getIdentifierInfo();
1840 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001841
John McCallb3d87482010-08-24 05:47:05 +00001842 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1843 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001844 getCurScope(), &SS,
1845 false, false, ParsedType(),
1846 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001847
Chris Lattnerf4382f52009-04-14 22:17:06 +00001848 // If the referenced identifier is not a type, then this declspec is
1849 // erroneous: We already checked about that it has no type specifier, and
1850 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001851 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001852 if (TypeRep == 0) {
1853 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001854 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001855 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001856 }
Mike Stump1eb44332009-09-09 15:08:12 +00001857
John McCallaa87d332009-12-12 11:40:51 +00001858 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001859 ConsumeToken(); // The C++ scope.
1860
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001861 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001862 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001863 if (isInvalid)
1864 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001866 DS.SetRangeEnd(Tok.getLocation());
1867 ConsumeToken(); // The typename.
1868
1869 continue;
1870 }
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Chris Lattner80d0c892009-01-21 19:48:37 +00001872 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001873 if (Tok.getAnnotationValue()) {
1874 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001876 DiagID, T);
1877 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001878 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001879
1880 if (isInvalid)
1881 break;
1882
Chris Lattner80d0c892009-01-21 19:48:37 +00001883 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1884 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Chris Lattner80d0c892009-01-21 19:48:37 +00001886 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1887 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001888 // Objective-C interface.
1889 if (Tok.is(tok::less) && getLang().ObjC1)
1890 ParseObjCProtocolQualifiers(DS);
1891
Chris Lattner80d0c892009-01-21 19:48:37 +00001892 continue;
1893 }
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregorbfad9152011-04-28 15:48:45 +00001895 case tok::kw___is_signed:
1896 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1897 // typically treats it as a trait. If we see __is_signed as it appears
1898 // in libstdc++, e.g.,
1899 //
1900 // static const bool __is_signed;
1901 //
1902 // then treat __is_signed as an identifier rather than as a keyword.
1903 if (DS.getTypeSpecType() == TST_bool &&
1904 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1905 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1906 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1907 Tok.setKind(tok::identifier);
1908 }
1909
1910 // We're done with the declaration-specifiers.
1911 goto DoneWithDeclSpec;
1912
Chris Lattner3bd934a2008-07-26 01:18:38 +00001913 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001914 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001915 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001916 // In C++, check to see if this is a scope specifier like foo::bar::, if
1917 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001918 if (getLang().CPlusPlus) {
1919 if (TryAnnotateCXXScopeToken(true)) {
1920 if (!DS.hasTypeSpecifier())
1921 DS.SetTypeSpecError();
1922 goto DoneWithDeclSpec;
1923 }
1924 if (!Tok.is(tok::identifier))
1925 continue;
1926 }
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattner3bd934a2008-07-26 01:18:38 +00001928 // This identifier can only be a typedef name if we haven't already seen
1929 // a type-specifier. Without this check we misparse:
1930 // typedef int X; struct Y { short X; }; as 'short int'.
1931 if (DS.hasTypeSpecifier())
1932 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001933
John Thompson82287d12010-02-05 00:12:22 +00001934 // Check for need to substitute AltiVec keyword tokens.
1935 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1936 break;
1937
Chris Lattner3bd934a2008-07-26 01:18:38 +00001938 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001939 ParsedType TypeRep =
1940 Actions.getTypeName(*Tok.getIdentifierInfo(),
1941 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001942
Chris Lattnerc199ab32009-04-12 20:42:31 +00001943 // If this is not a typedef name, don't parse it as part of the declspec,
1944 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001945 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001946 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001947 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001948 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001949
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001950 // If we're in a context where the identifier could be a class name,
1951 // check whether this is a constructor declaration.
1952 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001953 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001954 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001955 goto DoneWithDeclSpec;
1956
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001958 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001959 if (isInvalid)
1960 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Chris Lattner3bd934a2008-07-26 01:18:38 +00001962 DS.SetRangeEnd(Tok.getLocation());
1963 ConsumeToken(); // The identifier
1964
1965 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1966 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001967 // Objective-C interface.
1968 if (Tok.is(tok::less) && getLang().ObjC1)
1969 ParseObjCProtocolQualifiers(DS);
1970
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001971 // Need to support trailing type qualifiers (e.g. "id<p> const").
1972 // If a type specifier follows, it will be diagnosed elsewhere.
1973 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001974 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001975
1976 // type-name
1977 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001978 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001979 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001980 // This template-id does not refer to a type name, so we're
1981 // done with the type-specifiers.
1982 goto DoneWithDeclSpec;
1983 }
1984
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001985 // If we're in a context where the template-id could be a
1986 // constructor name or specialization, check whether this is a
1987 // constructor declaration.
1988 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001989 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001990 isConstructorDeclarator())
1991 goto DoneWithDeclSpec;
1992
Douglas Gregor39a8de12009-02-25 19:37:18 +00001993 // Turn the template-id annotation token into a type annotation
1994 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001995 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001996 continue;
1997 }
1998
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 // GNU attributes support.
2000 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00002001 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00002002 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002003
2004 // Microsoft declspec support.
2005 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002006 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002007 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Steve Naroff239f0732008-12-25 14:16:32 +00002009 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002010 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002011 // FIXME: Add handling here!
2012 break;
2013
2014 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002015 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002016 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002017 case tok::kw___cdecl:
2018 case tok::kw___stdcall:
2019 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002020 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002021 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002022 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002023 continue;
2024
Dawn Perchik52fc3142010-09-03 01:29:35 +00002025 // Borland single token adornments.
2026 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002027 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002028 continue;
2029
Peter Collingbournef315fa82011-02-14 01:42:53 +00002030 // OpenCL single token adornments.
2031 case tok::kw___kernel:
2032 ParseOpenCLAttributes(DS.getAttributes());
2033 continue;
2034
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 // storage-class-specifier
2036 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002037 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2038 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 break;
2040 case tok::kw_extern:
2041 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002042 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002043 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2044 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002046 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002047 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2048 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002049 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 case tok::kw_static:
2051 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002052 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002053 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2054 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 break;
2056 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002057 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002058 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002059 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2060 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002061 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002062 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002063 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002064 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002065 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2066 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002067 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002068 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2069 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 break;
2071 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002072 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2073 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002075 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002076 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2077 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002078 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 // function-specifier
2084 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002085 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002086 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002087 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002088 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002089 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002090 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002091 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002092 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002093
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002094 // alignment-specifier
2095 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002096 if (!getLang().C11)
2097 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002098 ParseAlignmentSpecifier(DS.getAttributes());
2099 continue;
2100
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002101 // friend
2102 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002103 if (DSContext == DSC_class)
2104 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2105 else {
2106 PrevSpec = ""; // not actually used by the diagnostic
2107 DiagID = diag::err_friend_invalid_in_context;
2108 isInvalid = true;
2109 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002110 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Douglas Gregor8d267c52011-09-09 02:06:17 +00002112 // Modules
2113 case tok::kw___module_private__:
2114 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2115 break;
2116
Sebastian Redl2ac67232009-11-05 15:47:02 +00002117 // constexpr
2118 case tok::kw_constexpr:
2119 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2120 break;
2121
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 // type-specifier
2123 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002124 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 break;
2127 case tok::kw_long:
2128 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002129 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2130 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002131 else
John McCallfec54012009-08-03 20:12:06 +00002132 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2133 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002134 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002135 case tok::kw___int64:
2136 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2137 DiagID);
2138 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002139 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002140 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2141 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002142 break;
2143 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002144 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2145 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002146 break;
2147 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2149 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002150 break;
2151 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2153 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002154 break;
2155 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2157 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002158 break;
2159 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2161 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002162 break;
2163 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002167 case tok::kw_half:
2168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2169 DiagID);
2170 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002171 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2173 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002174 break;
2175 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002176 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2177 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002178 break;
2179 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2181 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002182 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002183 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2185 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002186 break;
2187 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2189 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002190 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002191 case tok::kw_bool:
2192 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002193 if (Tok.is(tok::kw_bool) &&
2194 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2195 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2196 PrevSpec = ""; // Not used by the diagnostic.
2197 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002198 // For better error recovery.
2199 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002200 isInvalid = true;
2201 } else {
2202 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2203 DiagID);
2204 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002205 break;
2206 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2208 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002209 break;
2210 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002211 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2212 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002213 break;
2214 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002215 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2216 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002217 break;
John Thompson82287d12010-02-05 00:12:22 +00002218 case tok::kw___vector:
2219 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2220 break;
2221 case tok::kw___pixel:
2222 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2223 break;
John McCalla5fc4722011-04-09 22:50:59 +00002224 case tok::kw___unknown_anytype:
2225 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2226 PrevSpec, DiagID);
2227 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002228
2229 // class-specifier:
2230 case tok::kw_class:
2231 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002232 case tok::kw_union: {
2233 tok::TokenKind Kind = Tok.getKind();
2234 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002235 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002236 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002237 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002238
2239 // enum-specifier:
2240 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002241 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002242 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002243 continue;
2244
2245 // cv-qualifier:
2246 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002247 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2248 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 break;
2250 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002251 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2252 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002253 break;
2254 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002255 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2256 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002257 break;
2258
Douglas Gregord57959a2009-03-27 23:10:48 +00002259 // C++ typename-specifier:
2260 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002261 if (TryAnnotateTypeOrScopeToken()) {
2262 DS.SetTypeSpecError();
2263 goto DoneWithDeclSpec;
2264 }
2265 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002266 continue;
2267 break;
2268
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 // GNU typeof support.
2270 case tok::kw_typeof:
2271 ParseTypeofSpecifier(DS);
2272 continue;
2273
David Blaikie42d6d0c2011-12-04 05:04:18 +00002274 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002275 ParseDecltypeSpecifier(DS);
2276 continue;
2277
Sean Huntdb5d44b2011-05-19 05:37:45 +00002278 case tok::kw___underlying_type:
2279 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002280 continue;
2281
2282 case tok::kw__Atomic:
2283 ParseAtomicSpecifier(DS);
2284 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002285
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002286 // OpenCL qualifiers:
2287 case tok::kw_private:
2288 if (!getLang().OpenCL)
2289 goto DoneWithDeclSpec;
2290 case tok::kw___private:
2291 case tok::kw___global:
2292 case tok::kw___local:
2293 case tok::kw___constant:
2294 case tok::kw___read_only:
2295 case tok::kw___write_only:
2296 case tok::kw___read_write:
2297 ParseOpenCLQualifiers(DS);
2298 break;
2299
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002300 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002301 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002302 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2303 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002304 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002305 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Douglas Gregor46f936e2010-11-19 17:10:50 +00002307 if (!ParseObjCProtocolQualifiers(DS))
2308 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2309 << FixItHint::CreateInsertion(Loc, "id")
2310 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002311
2312 // Need to support trailing type qualifiers (e.g. "id<p> const").
2313 // If a type specifier follows, it will be diagnosed elsewhere.
2314 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 }
John McCallfec54012009-08-03 20:12:06 +00002316 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 if (isInvalid) {
2318 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002319 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002320
2321 if (DiagID == diag::ext_duplicate_declspec)
2322 Diag(Tok, DiagID)
2323 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2324 else
2325 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002327
Chris Lattner81c018d2008-03-13 06:29:04 +00002328 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002329 if (DiagID != diag::err_bool_redeclaration)
2330 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002331 }
2332}
Douglas Gregoradcac882008-12-01 23:54:00 +00002333
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002334/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002335/// primarily follow the C++ grammar with additions for C99 and GNU,
2336/// which together subsume the C grammar. Note that the C++
2337/// type-specifier also includes the C type-qualifier (for const,
2338/// volatile, and C99 restrict). Returns true if a type-specifier was
2339/// found (and parsed), false otherwise.
2340///
2341/// type-specifier: [C++ 7.1.5]
2342/// simple-type-specifier
2343/// class-specifier
2344/// enum-specifier
2345/// elaborated-type-specifier [TODO]
2346/// cv-qualifier
2347///
2348/// cv-qualifier: [C++ 7.1.5.1]
2349/// 'const'
2350/// 'volatile'
2351/// [C99] 'restrict'
2352///
2353/// simple-type-specifier: [ C++ 7.1.5.2]
2354/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2355/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2356/// 'char'
2357/// 'wchar_t'
2358/// 'bool'
2359/// 'short'
2360/// 'int'
2361/// 'long'
2362/// 'signed'
2363/// 'unsigned'
2364/// 'float'
2365/// 'double'
2366/// 'void'
2367/// [C99] '_Bool'
2368/// [C99] '_Complex'
2369/// [C99] '_Imaginary' // Removed in TC2?
2370/// [GNU] '_Decimal32'
2371/// [GNU] '_Decimal64'
2372/// [GNU] '_Decimal128'
2373/// [GNU] typeof-specifier
2374/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2375/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002376/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002377/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002378bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002379 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002380 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002381 const ParsedTemplateInfo &TemplateInfo,
2382 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002383 SourceLocation Loc = Tok.getLocation();
2384
2385 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002386 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002387 // If we already have a type specifier, this identifier is not a type.
2388 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2389 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2390 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2391 return false;
John Thompson82287d12010-02-05 00:12:22 +00002392 // Check for need to substitute AltiVec keyword tokens.
2393 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2394 break;
2395 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002396 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002397 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002398 // Annotate typenames and C++ scope specifiers. If we get one, just
2399 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002400 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2401 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002402 return true;
2403 if (Tok.is(tok::identifier))
2404 return false;
2405 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2406 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002407 case tok::coloncolon: // ::foo::bar
2408 if (NextToken().is(tok::kw_new) || // ::new
2409 NextToken().is(tok::kw_delete)) // ::delete
2410 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattner166a8fc2009-01-04 23:41:41 +00002412 // Annotate typenames and C++ scope specifiers. If we get one, just
2413 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002414 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2415 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002416 return true;
2417 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2418 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Douglas Gregor12e083c2008-11-07 15:42:26 +00002420 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002421 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002422 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002423 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2424 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002425 DiagID, T);
2426 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002427 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002428 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2429 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Douglas Gregor12e083c2008-11-07 15:42:26 +00002431 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2432 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2433 // Objective-C interface. If we don't have Objective-C or a '<', this is
2434 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002435 if (Tok.is(tok::less) && getLang().ObjC1)
2436 ParseObjCProtocolQualifiers(DS);
2437
Douglas Gregor12e083c2008-11-07 15:42:26 +00002438 return true;
2439 }
2440
2441 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002442 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002443 break;
2444 case tok::kw_long:
2445 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2447 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002448 else
John McCallfec54012009-08-03 20:12:06 +00002449 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2450 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002451 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002452 case tok::kw___int64:
2453 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2454 DiagID);
2455 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002456 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002457 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002458 break;
2459 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002460 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2461 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 break;
2463 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2465 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002466 break;
2467 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002468 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2469 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002470 break;
2471 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002473 break;
2474 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002476 break;
2477 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002479 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002480 case tok::kw_half:
2481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2482 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002483 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002485 break;
2486 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002487 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002488 break;
2489 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002491 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002492 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002494 break;
2495 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002497 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002498 case tok::kw_bool:
2499 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002501 break;
2502 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2504 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002505 break;
2506 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2508 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002509 break;
2510 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2512 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002513 break;
John Thompson82287d12010-02-05 00:12:22 +00002514 case tok::kw___vector:
2515 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2516 break;
2517 case tok::kw___pixel:
2518 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2519 break;
2520
Douglas Gregor12e083c2008-11-07 15:42:26 +00002521 // class-specifier:
2522 case tok::kw_class:
2523 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002524 case tok::kw_union: {
2525 tok::TokenKind Kind = Tok.getKind();
2526 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002527 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002528 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002529 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002530 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002531 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002532
2533 // enum-specifier:
2534 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002535 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002536 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002537 return true;
2538
2539 // cv-qualifier:
2540 case tok::kw_const:
2541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002542 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002543 break;
2544 case tok::kw_volatile:
2545 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002546 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002547 break;
2548 case tok::kw_restrict:
2549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002550 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002551 break;
2552
2553 // GNU typeof support.
2554 case tok::kw_typeof:
2555 ParseTypeofSpecifier(DS);
2556 return true;
2557
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002558 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002559 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002560 ParseDecltypeSpecifier(DS);
2561 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002562
Sean Huntdb5d44b2011-05-19 05:37:45 +00002563 // C++0x type traits support.
2564 case tok::kw___underlying_type:
2565 ParseUnderlyingTypeSpecifier(DS);
2566 return true;
2567
Eli Friedmanb001de72011-10-06 23:00:33 +00002568 case tok::kw__Atomic:
2569 ParseAtomicSpecifier(DS);
2570 return true;
2571
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002572 // OpenCL qualifiers:
2573 case tok::kw_private:
2574 if (!getLang().OpenCL)
2575 return false;
2576 case tok::kw___private:
2577 case tok::kw___global:
2578 case tok::kw___local:
2579 case tok::kw___constant:
2580 case tok::kw___read_only:
2581 case tok::kw___write_only:
2582 case tok::kw___read_write:
2583 ParseOpenCLQualifiers(DS);
2584 break;
2585
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002586 // C++0x auto support.
2587 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002588 // This is only called in situations where a storage-class specifier is
2589 // illegal, so we can assume an auto type specifier was intended even in
2590 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2591 // extension diagnostic.
2592 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002593 return false;
2594
John McCallfec54012009-08-03 20:12:06 +00002595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002596 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002597
Eli Friedman290eeb02009-06-08 23:27:34 +00002598 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002599 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002600 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002601 case tok::kw___cdecl:
2602 case tok::kw___stdcall:
2603 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002604 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002605 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002606 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002607 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002608
Dawn Perchik52fc3142010-09-03 01:29:35 +00002609 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002610 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002611 return true;
2612
Douglas Gregor12e083c2008-11-07 15:42:26 +00002613 default:
2614 // Not a type-specifier; do nothing.
2615 return false;
2616 }
2617
2618 // If the specifier combination wasn't legal, issue a diagnostic.
2619 if (isInvalid) {
2620 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002621 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002622 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002623 }
2624 DS.SetRangeEnd(Tok.getLocation());
2625 ConsumeToken(); // whatever we parsed above.
2626 return true;
2627}
Reid Spencer5f016e22007-07-11 17:01:13 +00002628
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002629/// ParseStructDeclaration - Parse a struct declaration without the terminating
2630/// semicolon.
2631///
Reid Spencer5f016e22007-07-11 17:01:13 +00002632/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002633/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002634/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002635/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002636/// struct-declarator-list:
2637/// struct-declarator
2638/// struct-declarator-list ',' struct-declarator
2639/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2640/// struct-declarator:
2641/// declarator
2642/// [GNU] declarator attributes[opt]
2643/// declarator[opt] ':' constant-expression
2644/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2645///
Chris Lattnere1359422008-04-10 06:46:29 +00002646void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002647ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002648
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002649 if (Tok.is(tok::kw___extension__)) {
2650 // __extension__ silences extension warnings in the subexpression.
2651 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002652 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002653 return ParseStructDeclaration(DS, Fields);
2654 }
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Steve Naroff28a7ca82007-08-20 22:28:22 +00002656 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002659 // If there are no declarators, this is a free-standing declaration
2660 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002661 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002662 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002663 return;
2664 }
2665
2666 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002667 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002668 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002669 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002670 FieldDeclarator DeclaratorInfo(DS);
2671
2672 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002673 if (!FirstDeclarator)
2674 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Steve Naroff28a7ca82007-08-20 22:28:22 +00002676 /// struct-declarator: declarator
2677 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002678 if (Tok.isNot(tok::colon)) {
2679 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2680 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002681 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002682 }
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Chris Lattner04d66662007-10-09 17:33:22 +00002684 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002685 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002686 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002687 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002688 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002689 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002690 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002691 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002692
Steve Naroff28a7ca82007-08-20 22:28:22 +00002693 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002694 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002695
John McCallbdd563e2009-11-03 02:38:08 +00002696 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002697 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002698 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002699
Steve Naroff28a7ca82007-08-20 22:28:22 +00002700 // If we don't have a comma, it is either the end of the list (a ';')
2701 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002702 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002703 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002704
Steve Naroff28a7ca82007-08-20 22:28:22 +00002705 // Consume the comma.
2706 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002707
John McCallbdd563e2009-11-03 02:38:08 +00002708 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002709 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002710}
2711
2712/// ParseStructUnionBody
2713/// struct-contents:
2714/// struct-declaration-list
2715/// [EXT] empty
2716/// [GNU] "struct-declaration-list" without terminatoring ';'
2717/// struct-declaration-list:
2718/// struct-declaration
2719/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002720/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002721///
Reid Spencer5f016e22007-07-11 17:01:13 +00002722void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002723 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002724 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2725 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002727 BalancedDelimiterTracker T(*this, tok::l_brace);
2728 if (T.consumeOpen())
2729 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002731 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002732 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002733
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2735 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002736 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2737 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2738 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2739 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002740
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002742
Reid Spencer5f016e22007-07-11 17:01:13 +00002743 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002744 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002745 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002746
Reid Spencer5f016e22007-07-11 17:01:13 +00002747 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002748 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002749 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002750 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002751 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002752 ConsumeToken();
2753 continue;
2754 }
Chris Lattnere1359422008-04-10 06:46:29 +00002755
2756 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002757 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002758
John McCallbdd563e2009-11-03 02:38:08 +00002759 if (!Tok.is(tok::at)) {
2760 struct CFieldCallback : FieldCallback {
2761 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002762 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002763 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002764
John McCalld226f652010-08-21 09:40:31 +00002765 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002766 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002767 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2768
John McCalld226f652010-08-21 09:40:31 +00002769 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002770 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002771 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002772 FD.D.getDeclSpec().getSourceRange().getBegin(),
2773 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002774 FieldDecls.push_back(Field);
2775 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002776 }
John McCallbdd563e2009-11-03 02:38:08 +00002777 } Callback(*this, TagDecl, FieldDecls);
2778
2779 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002780 } else { // Handle @defs
2781 ConsumeToken();
2782 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2783 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002784 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002785 continue;
2786 }
2787 ConsumeToken();
2788 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2789 if (!Tok.is(tok::identifier)) {
2790 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002791 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002792 continue;
2793 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002794 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002795 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002796 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002797 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2798 ConsumeToken();
2799 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002800 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002801
Chris Lattner04d66662007-10-09 17:33:22 +00002802 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002803 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002804 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002805 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 break;
2807 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002808 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2809 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002811 // If we stopped at a ';', eat it.
2812 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002813 }
2814 }
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002816 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002817
John McCall0b7e6782011-03-24 11:26:52 +00002818 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002820 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002821
Douglas Gregor23c94db2010-07-02 17:43:08 +00002822 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002823 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002824 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002825 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002826 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002827 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2828 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002829}
2830
Reid Spencer5f016e22007-07-11 17:01:13 +00002831/// ParseEnumSpecifier
2832/// enum-specifier: [C99 6.7.2.2]
2833/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002834///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002835/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2836/// '}' attributes[opt]
2837/// 'enum' identifier
2838/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002839///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002840/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2841/// [C++0x] enum-head '{' enumerator-list ',' '}'
2842///
2843/// enum-head: [C++0x]
2844/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2845/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2846///
2847/// enum-key: [C++0x]
2848/// 'enum'
2849/// 'enum' 'class'
2850/// 'enum' 'struct'
2851///
2852/// enum-base: [C++0x]
2853/// ':' type-specifier-seq
2854///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002855/// [C++] elaborated-type-specifier:
2856/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2857///
Chris Lattner4c97d762009-04-12 21:49:30 +00002858void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002859 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002860 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002861 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002862 if (Tok.is(tok::code_completion)) {
2863 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002864 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002865 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002866 }
John McCall57c13002011-07-06 05:58:41 +00002867
Richard Smithbdad7a22012-01-10 01:33:14 +00002868 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002869 bool IsScopedUsingClassTag = false;
2870
2871 if (getLang().CPlusPlus0x &&
2872 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002873 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002874 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002875 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002876 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002877
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002878 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002879 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002880 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002881
Douglas Gregor5471bc82011-09-08 17:18:35 +00002882 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002883 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002884
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002885 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002886 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002887 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2888 // if a fixed underlying type is allowed.
2889 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2890
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002891 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2892 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002893 return;
2894
2895 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002896 Diag(Tok, diag::err_expected_ident);
2897 if (Tok.isNot(tok::l_brace)) {
2898 // Has no name and is not a definition.
2899 // Skip the rest of this declarator, up until the comma or semicolon.
2900 SkipUntil(tok::comma, true);
2901 return;
2902 }
2903 }
2904 }
Mike Stump1eb44332009-09-09 15:08:12 +00002905
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002906 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002907 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2908 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002909 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002911 // Skip the rest of this declarator, up until the comma or semicolon.
2912 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002914 }
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002916 // If an identifier is present, consume and remember it.
2917 IdentifierInfo *Name = 0;
2918 SourceLocation NameLoc;
2919 if (Tok.is(tok::identifier)) {
2920 Name = Tok.getIdentifierInfo();
2921 NameLoc = ConsumeToken();
2922 }
Mike Stump1eb44332009-09-09 15:08:12 +00002923
Richard Smithbdad7a22012-01-10 01:33:14 +00002924 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002925 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2926 // declaration of a scoped enumeration.
2927 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002928 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002929 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002930 }
2931
2932 TypeResult BaseType;
2933
Douglas Gregora61b3e72010-12-01 17:42:47 +00002934 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002935 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002936 bool PossibleBitfield = false;
2937 if (getCurScope()->getFlags() & Scope::ClassScope) {
2938 // If we're in class scope, this can either be an enum declaration with
2939 // an underlying type, or a declaration of a bitfield member. We try to
2940 // use a simple disambiguation scheme first to catch the common cases
2941 // (integer literal, sizeof); if it's still ambiguous, we then consider
2942 // anything that's a simple-type-specifier followed by '(' as an
2943 // expression. This suffices because function types are not valid
2944 // underlying types anyway.
2945 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2946 // If the next token starts an expression, we know we're parsing a
2947 // bit-field. This is the common case.
2948 if (TPR == TPResult::True())
2949 PossibleBitfield = true;
2950 // If the next token starts a type-specifier-seq, it may be either a
2951 // a fixed underlying type or the start of a function-style cast in C++;
2952 // lookahead one more token to see if it's obvious that we have a
2953 // fixed underlying type.
2954 else if (TPR == TPResult::False() &&
2955 GetLookAheadToken(2).getKind() == tok::semi) {
2956 // Consume the ':'.
2957 ConsumeToken();
2958 } else {
2959 // We have the start of a type-specifier-seq, so we have to perform
2960 // tentative parsing to determine whether we have an expression or a
2961 // type.
2962 TentativeParsingAction TPA(*this);
2963
2964 // Consume the ':'.
2965 ConsumeToken();
2966
Douglas Gregor86f208c2011-02-22 20:32:04 +00002967 if ((getLang().CPlusPlus &&
2968 isCXXDeclarationSpecifier() != TPResult::True()) ||
2969 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002970 // We'll parse this as a bitfield later.
2971 PossibleBitfield = true;
2972 TPA.Revert();
2973 } else {
2974 // We have a type-specifier-seq.
2975 TPA.Commit();
2976 }
2977 }
2978 } else {
2979 // Consume the ':'.
2980 ConsumeToken();
2981 }
2982
2983 if (!PossibleBitfield) {
2984 SourceRange Range;
2985 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002986
Douglas Gregor5471bc82011-09-08 17:18:35 +00002987 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002988 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2989 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002990 if (getLang().CPlusPlus0x)
2991 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002992 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002993 }
2994
Richard Smithbdad7a22012-01-10 01:33:14 +00002995 // There are four options here. If we have 'friend enum foo;' then this is a
2996 // friend declaration, and cannot have an accompanying definition. If we have
2997 // 'enum foo;', then this is a forward declaration. If we have
2998 // 'enum foo {...' then this is a definition. Otherwise we have something
2999 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003000 //
3001 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3002 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3003 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3004 //
John McCallf312b1e2010-08-26 23:41:50 +00003005 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00003006 if (DS.isFriendSpecified())
3007 TUK = Sema::TUK_Friend;
3008 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00003009 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003010 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00003011 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003012 else
John McCallf312b1e2010-08-26 23:41:50 +00003013 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003014
3015 // enums cannot be templates, although they can be referenced from a
3016 // template.
3017 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003018 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003019 Diag(Tok, diag::err_enum_template);
3020
3021 // Skip the rest of this declarator, up until the comma or semicolon.
3022 SkipUntil(tok::comma, true);
3023 return;
3024 }
3025
Douglas Gregorb9075602011-02-22 02:55:24 +00003026 if (!Name && TUK != Sema::TUK_Definition) {
3027 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3028
3029 // Skip the rest of this declarator, up until the comma or semicolon.
3030 SkipUntil(tok::comma, true);
3031 return;
3032 }
3033
Douglas Gregor402abb52009-05-28 23:31:59 +00003034 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003035 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003036 const char *PrevSpec = 0;
3037 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003038 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003039 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003040 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003041 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00003042 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003043 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003044
Douglas Gregor48c89f42010-04-24 16:38:41 +00003045 if (IsDependent) {
3046 // This enum has a dependent nested-name-specifier. Handle it as a
3047 // dependent tag.
3048 if (!Name) {
3049 DS.SetTypeSpecError();
3050 Diag(Tok, diag::err_expected_type_name_after_typename);
3051 return;
3052 }
3053
Douglas Gregor23c94db2010-07-02 17:43:08 +00003054 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003055 TUK, SS, Name, StartLoc,
3056 NameLoc);
3057 if (Type.isInvalid()) {
3058 DS.SetTypeSpecError();
3059 return;
3060 }
3061
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003062 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3063 NameLoc.isValid() ? NameLoc : StartLoc,
3064 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003065 Diag(StartLoc, DiagID) << PrevSpec;
3066
3067 return;
3068 }
Mike Stump1eb44332009-09-09 15:08:12 +00003069
John McCalld226f652010-08-21 09:40:31 +00003070 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003071 // The action failed to produce an enumeration tag. If this is a
3072 // definition, consume the entire definition.
3073 if (Tok.is(tok::l_brace)) {
3074 ConsumeBrace();
3075 SkipUntil(tok::r_brace);
3076 }
3077
3078 DS.SetTypeSpecError();
3079 return;
3080 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003081
3082 if (Tok.is(tok::l_brace)) {
3083 if (TUK == Sema::TUK_Friend)
3084 Diag(Tok, diag::err_friend_decl_defines_type)
3085 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00003086 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003089 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3090 NameLoc.isValid() ? NameLoc : StartLoc,
3091 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003092 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003093}
3094
3095/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3096/// enumerator-list:
3097/// enumerator
3098/// enumerator-list ',' enumerator
3099/// enumerator:
3100/// enumeration-constant
3101/// enumeration-constant '=' constant-expression
3102/// enumeration-constant:
3103/// identifier
3104///
John McCalld226f652010-08-21 09:40:31 +00003105void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003106 // Enter the scope of the enum body and start the definition.
3107 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003108 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003109
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003110 BalancedDelimiterTracker T(*this, tok::l_brace);
3111 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003112
Chris Lattner7946dd32007-08-27 17:24:30 +00003113 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003114 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003115 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Chris Lattner5f9e2722011-07-23 10:55:15 +00003117 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003118
John McCalld226f652010-08-21 09:40:31 +00003119 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Reid Spencer5f016e22007-07-11 17:01:13 +00003121 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003122 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3124 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003125
John McCall5b629aa2010-10-22 23:36:17 +00003126 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003127 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003128 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003129
Reid Spencer5f016e22007-07-11 17:01:13 +00003130 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003131 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003132 ParsingDeclRAIIObject PD(*this);
3133
Chris Lattner04d66662007-10-09 17:33:22 +00003134 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003136 AssignedVal = ParseConstantExpression();
3137 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003138 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 }
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003142 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3143 LastEnumConstDecl,
3144 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003145 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003146 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003147 PD.complete(EnumConstDecl);
3148
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 EnumConstantDecls.push_back(EnumConstDecl);
3150 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Douglas Gregor751f6922010-09-07 14:51:08 +00003152 if (Tok.is(tok::identifier)) {
3153 // We're missing a comma between enumerators.
3154 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3155 Diag(Loc, diag::err_enumerator_list_missing_comma)
3156 << FixItHint::CreateInsertion(Loc, ", ");
3157 continue;
3158 }
3159
Chris Lattner04d66662007-10-09 17:33:22 +00003160 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 break;
3162 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Richard Smith7fe62082011-10-15 05:09:34 +00003164 if (Tok.isNot(tok::identifier)) {
3165 if (!getLang().C99 && !getLang().CPlusPlus0x)
3166 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3167 << getLang().CPlusPlus
3168 << FixItHint::CreateRemoval(CommaLoc);
3169 else if (getLang().CPlusPlus0x)
3170 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3171 << FixItHint::CreateRemoval(CommaLoc);
3172 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003173 }
Mike Stump1eb44332009-09-09 15:08:12 +00003174
Reid Spencer5f016e22007-07-11 17:01:13 +00003175 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003176 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003177
Reid Spencer5f016e22007-07-11 17:01:13 +00003178 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003179 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003180 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003181
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003182 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3183 EnumDecl, EnumConstantDecls.data(),
3184 EnumConstantDecls.size(), getCurScope(),
3185 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003186
Douglas Gregor72de6672009-01-08 20:45:30 +00003187 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003188 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3189 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003190}
3191
3192/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003193/// start of a type-qualifier-list.
3194bool Parser::isTypeQualifier() const {
3195 switch (Tok.getKind()) {
3196 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003197
3198 // type-qualifier only in OpenCL
3199 case tok::kw_private:
3200 return getLang().OpenCL;
3201
Steve Naroff5f8aa692008-02-11 23:15:56 +00003202 // type-qualifier
3203 case tok::kw_const:
3204 case tok::kw_volatile:
3205 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003206 case tok::kw___private:
3207 case tok::kw___local:
3208 case tok::kw___global:
3209 case tok::kw___constant:
3210 case tok::kw___read_only:
3211 case tok::kw___read_write:
3212 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003213 return true;
3214 }
3215}
3216
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003217/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3218/// is definitely a type-specifier. Return false if it isn't part of a type
3219/// specifier or if we're not sure.
3220bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3221 switch (Tok.getKind()) {
3222 default: return false;
3223 // type-specifiers
3224 case tok::kw_short:
3225 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003226 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003227 case tok::kw_signed:
3228 case tok::kw_unsigned:
3229 case tok::kw__Complex:
3230 case tok::kw__Imaginary:
3231 case tok::kw_void:
3232 case tok::kw_char:
3233 case tok::kw_wchar_t:
3234 case tok::kw_char16_t:
3235 case tok::kw_char32_t:
3236 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003237 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003238 case tok::kw_float:
3239 case tok::kw_double:
3240 case tok::kw_bool:
3241 case tok::kw__Bool:
3242 case tok::kw__Decimal32:
3243 case tok::kw__Decimal64:
3244 case tok::kw__Decimal128:
3245 case tok::kw___vector:
3246
3247 // struct-or-union-specifier (C99) or class-specifier (C++)
3248 case tok::kw_class:
3249 case tok::kw_struct:
3250 case tok::kw_union:
3251 // enum-specifier
3252 case tok::kw_enum:
3253
3254 // typedef-name
3255 case tok::annot_typename:
3256 return true;
3257 }
3258}
3259
Steve Naroff5f8aa692008-02-11 23:15:56 +00003260/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003261/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003262bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 switch (Tok.getKind()) {
3264 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003265
Chris Lattner166a8fc2009-01-04 23:41:41 +00003266 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003267 if (TryAltiVecVectorToken())
3268 return true;
3269 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003270 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003271 // Annotate typenames and C++ scope specifiers. If we get one, just
3272 // recurse to handle whatever we get.
3273 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003274 return true;
3275 if (Tok.is(tok::identifier))
3276 return false;
3277 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003278
Chris Lattner166a8fc2009-01-04 23:41:41 +00003279 case tok::coloncolon: // ::foo::bar
3280 if (NextToken().is(tok::kw_new) || // ::new
3281 NextToken().is(tok::kw_delete)) // ::delete
3282 return false;
3283
Chris Lattner166a8fc2009-01-04 23:41:41 +00003284 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003285 return true;
3286 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 // GNU attributes support.
3289 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003290 // GNU typeof support.
3291 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003292
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 // type-specifiers
3294 case tok::kw_short:
3295 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003296 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003297 case tok::kw_signed:
3298 case tok::kw_unsigned:
3299 case tok::kw__Complex:
3300 case tok::kw__Imaginary:
3301 case tok::kw_void:
3302 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003303 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003304 case tok::kw_char16_t:
3305 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003307 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 case tok::kw_float:
3309 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003310 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003311 case tok::kw__Bool:
3312 case tok::kw__Decimal32:
3313 case tok::kw__Decimal64:
3314 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003315 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Chris Lattner99dc9142008-04-13 18:59:07 +00003317 // struct-or-union-specifier (C99) or class-specifier (C++)
3318 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 case tok::kw_struct:
3320 case tok::kw_union:
3321 // enum-specifier
3322 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003323
Reid Spencer5f016e22007-07-11 17:01:13 +00003324 // type-qualifier
3325 case tok::kw_const:
3326 case tok::kw_volatile:
3327 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003328
3329 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003330 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Chris Lattner7c186be2008-10-20 00:25:30 +00003333 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3334 case tok::less:
3335 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003336
Steve Naroff239f0732008-12-25 14:16:32 +00003337 case tok::kw___cdecl:
3338 case tok::kw___stdcall:
3339 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003340 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003341 case tok::kw___w64:
3342 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003343 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003344 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003345 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003346
3347 case tok::kw___private:
3348 case tok::kw___local:
3349 case tok::kw___global:
3350 case tok::kw___constant:
3351 case tok::kw___read_only:
3352 case tok::kw___read_write:
3353 case tok::kw___write_only:
3354
Eli Friedman290eeb02009-06-08 23:27:34 +00003355 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003356
3357 case tok::kw_private:
3358 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003359
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003360 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003361 case tok::kw__Atomic:
3362 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003363 }
3364}
3365
3366/// isDeclarationSpecifier() - Return true if the current token is part of a
3367/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003368///
3369/// \param DisambiguatingWithExpression True to indicate that the purpose of
3370/// this check is to disambiguate between an expression and a declaration.
3371bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 switch (Tok.getKind()) {
3373 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003374
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003375 case tok::kw_private:
3376 return getLang().OpenCL;
3377
Chris Lattner166a8fc2009-01-04 23:41:41 +00003378 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003379 // Unfortunate hack to support "Class.factoryMethod" notation.
3380 if (getLang().ObjC1 && NextToken().is(tok::period))
3381 return false;
John Thompson82287d12010-02-05 00:12:22 +00003382 if (TryAltiVecVectorToken())
3383 return true;
3384 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003385 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003386 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003387 // Annotate typenames and C++ scope specifiers. If we get one, just
3388 // recurse to handle whatever we get.
3389 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003390 return true;
3391 if (Tok.is(tok::identifier))
3392 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003393
3394 // If we're in Objective-C and we have an Objective-C class type followed
3395 // by an identifier and then either ':' or ']', in a place where an
3396 // expression is permitted, then this is probably a class message send
3397 // missing the initial '['. In this case, we won't consider this to be
3398 // the start of a declaration.
3399 if (DisambiguatingWithExpression &&
3400 isStartOfObjCClassMessageMissingOpenBracket())
3401 return false;
3402
John McCall9ba61662010-02-26 08:45:28 +00003403 return isDeclarationSpecifier();
3404
Chris Lattner166a8fc2009-01-04 23:41:41 +00003405 case tok::coloncolon: // ::foo::bar
3406 if (NextToken().is(tok::kw_new) || // ::new
3407 NextToken().is(tok::kw_delete)) // ::delete
3408 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003409
Chris Lattner166a8fc2009-01-04 23:41:41 +00003410 // Annotate typenames and C++ scope specifiers. If we get one, just
3411 // recurse to handle whatever we get.
3412 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003413 return true;
3414 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003415
Reid Spencer5f016e22007-07-11 17:01:13 +00003416 // storage-class-specifier
3417 case tok::kw_typedef:
3418 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003419 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003420 case tok::kw_static:
3421 case tok::kw_auto:
3422 case tok::kw_register:
3423 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Douglas Gregor8d267c52011-09-09 02:06:17 +00003425 // Modules
3426 case tok::kw___module_private__:
3427
Reid Spencer5f016e22007-07-11 17:01:13 +00003428 // type-specifiers
3429 case tok::kw_short:
3430 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003431 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003432 case tok::kw_signed:
3433 case tok::kw_unsigned:
3434 case tok::kw__Complex:
3435 case tok::kw__Imaginary:
3436 case tok::kw_void:
3437 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003438 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003439 case tok::kw_char16_t:
3440 case tok::kw_char32_t:
3441
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003443 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 case tok::kw_float:
3445 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003446 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003447 case tok::kw__Bool:
3448 case tok::kw__Decimal32:
3449 case tok::kw__Decimal64:
3450 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003451 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Chris Lattner99dc9142008-04-13 18:59:07 +00003453 // struct-or-union-specifier (C99) or class-specifier (C++)
3454 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 case tok::kw_struct:
3456 case tok::kw_union:
3457 // enum-specifier
3458 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003459
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 // type-qualifier
3461 case tok::kw_const:
3462 case tok::kw_volatile:
3463 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003464
Reid Spencer5f016e22007-07-11 17:01:13 +00003465 // function-specifier
3466 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003467 case tok::kw_virtual:
3468 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003469
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003470 // static_assert-declaration
3471 case tok::kw__Static_assert:
3472
Chris Lattner1ef08762007-08-09 17:01:07 +00003473 // GNU typeof support.
3474 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Chris Lattner1ef08762007-08-09 17:01:07 +00003476 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003477 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Francois Pichete3d49b42011-06-19 08:02:06 +00003480 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003481 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003482 return true;
3483
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003484 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003485 case tok::kw__Atomic:
3486 return true;
3487
Chris Lattnerf3948c42008-07-26 03:38:44 +00003488 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3489 case tok::less:
3490 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Douglas Gregord9d75e52011-04-27 05:41:15 +00003492 // typedef-name
3493 case tok::annot_typename:
3494 return !DisambiguatingWithExpression ||
3495 !isStartOfObjCClassMessageMissingOpenBracket();
3496
Steve Naroff47f52092009-01-06 19:34:12 +00003497 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003498 case tok::kw___cdecl:
3499 case tok::kw___stdcall:
3500 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003501 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003502 case tok::kw___w64:
3503 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003504 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003505 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003506 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003507 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003508
3509 case tok::kw___private:
3510 case tok::kw___local:
3511 case tok::kw___global:
3512 case tok::kw___constant:
3513 case tok::kw___read_only:
3514 case tok::kw___read_write:
3515 case tok::kw___write_only:
3516
Eli Friedman290eeb02009-06-08 23:27:34 +00003517 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003518 }
3519}
3520
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003521bool Parser::isConstructorDeclarator() {
3522 TentativeParsingAction TPA(*this);
3523
3524 // Parse the C++ scope specifier.
3525 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003526 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3527 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003528 TPA.Revert();
3529 return false;
3530 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003531
3532 // Parse the constructor name.
3533 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3534 // We already know that we have a constructor name; just consume
3535 // the token.
3536 ConsumeToken();
3537 } else {
3538 TPA.Revert();
3539 return false;
3540 }
3541
3542 // Current class name must be followed by a left parentheses.
3543 if (Tok.isNot(tok::l_paren)) {
3544 TPA.Revert();
3545 return false;
3546 }
3547 ConsumeParen();
3548
3549 // A right parentheses or ellipsis signals that we have a constructor.
3550 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3551 TPA.Revert();
3552 return true;
3553 }
3554
3555 // If we need to, enter the specified scope.
3556 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003557 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003558 DeclScopeObj.EnterDeclaratorScope();
3559
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003560 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003561 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003562 MaybeParseMicrosoftAttributes(Attrs);
3563
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003564 // Check whether the next token(s) are part of a declaration
3565 // specifier, in which case we have the start of a parameter and,
3566 // therefore, we know that this is a constructor.
3567 bool IsConstructor = isDeclarationSpecifier();
3568 TPA.Revert();
3569 return IsConstructor;
3570}
Reid Spencer5f016e22007-07-11 17:01:13 +00003571
3572/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003573/// type-qualifier-list: [C99 6.7.5]
3574/// type-qualifier
3575/// [vendor] attributes
3576/// [ only if VendorAttributesAllowed=true ]
3577/// type-qualifier-list type-qualifier
3578/// [vendor] type-qualifier-list attributes
3579/// [ only if VendorAttributesAllowed=true ]
3580/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3581/// [ only if CXX0XAttributesAllowed=true ]
3582/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003583///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003584void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3585 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003586 bool CXX0XAttributesAllowed) {
3587 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3588 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003589 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003590 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003591 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003592 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003593 else
3594 Diag(Loc, diag::err_attributes_not_allowed);
3595 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003596
3597 SourceLocation EndLoc;
3598
Reid Spencer5f016e22007-07-11 17:01:13 +00003599 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003600 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003601 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003602 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003603 SourceLocation Loc = Tok.getLocation();
3604
3605 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003606 case tok::code_completion:
3607 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003608 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003609
Reid Spencer5f016e22007-07-11 17:01:13 +00003610 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3612 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003613 break;
3614 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003615 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3616 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003617 break;
3618 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3620 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003622
3623 // OpenCL qualifiers:
3624 case tok::kw_private:
3625 if (!getLang().OpenCL)
3626 goto DoneWithTypeQuals;
3627 case tok::kw___private:
3628 case tok::kw___global:
3629 case tok::kw___local:
3630 case tok::kw___constant:
3631 case tok::kw___read_only:
3632 case tok::kw___write_only:
3633 case tok::kw___read_write:
3634 ParseOpenCLQualifiers(DS);
3635 break;
3636
Eli Friedman290eeb02009-06-08 23:27:34 +00003637 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003638 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003639 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003640 case tok::kw___cdecl:
3641 case tok::kw___stdcall:
3642 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003643 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003644 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003645 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003646 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003647 continue;
3648 }
3649 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003650 case tok::kw___pascal:
3651 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003652 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003653 continue;
3654 }
3655 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003657 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003658 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003659 continue; // do *not* consume the next token!
3660 }
3661 // otherwise, FALL THROUGH!
3662 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003663 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003664 // If this is not a type-qualifier token, we're done reading type
3665 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003666 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003667 if (EndLoc.isValid())
3668 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003669 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003670 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003671
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 // If the specifier combination wasn't legal, issue a diagnostic.
3673 if (isInvalid) {
3674 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003675 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003676 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003677 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 }
3679}
3680
3681
3682/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3683///
3684void Parser::ParseDeclarator(Declarator &D) {
3685 /// This implements the 'declarator' production in the C grammar, then checks
3686 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003687 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003688}
3689
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003690/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3691/// is parsed by the function passed to it. Pass null, and the direct-declarator
3692/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003693/// ptr-operator production.
3694///
Richard Smith0706df42011-10-19 21:33:05 +00003695/// If the grammar of this construct is extended, matching changes must also be
3696/// made to TryParseDeclarator and MightBeDeclarator.
3697///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003698/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3699/// [C] pointer[opt] direct-declarator
3700/// [C++] direct-declarator
3701/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003702///
3703/// pointer: [C99 6.7.5]
3704/// '*' type-qualifier-list[opt]
3705/// '*' type-qualifier-list[opt] pointer
3706///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003707/// ptr-operator:
3708/// '*' cv-qualifier-seq[opt]
3709/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003710/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003711/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003712/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003713/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003714void Parser::ParseDeclaratorInternal(Declarator &D,
3715 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003716 if (Diags.hasAllExtensionsSilenced())
3717 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003718
Sebastian Redlf30208a2009-01-24 21:16:55 +00003719 // C++ member pointers start with a '::' or a nested-name.
3720 // Member pointers get special handling, since there's no place for the
3721 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003722 if (getLang().CPlusPlus &&
3723 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3724 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003725 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3726 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003727 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003728 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003729
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003730 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003731 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003732 // The scope spec really belongs to the direct-declarator.
3733 D.getCXXScopeSpec() = SS;
3734 if (DirectDeclParser)
3735 (this->*DirectDeclParser)(D);
3736 return;
3737 }
3738
3739 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003740 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003741 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003742 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003743 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003744
3745 // Recurse to parse whatever is left.
3746 ParseDeclaratorInternal(D, DirectDeclParser);
3747
3748 // Sema will have to catch (syntactically invalid) pointers into global
3749 // scope. It has to catch pointers into namespace scope anyway.
3750 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003751 Loc),
3752 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003753 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003754 return;
3755 }
3756 }
3757
3758 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003759 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003760 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003761 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003762 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003763 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003764 if (DirectDeclParser)
3765 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003766 return;
3767 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003768
Sebastian Redl05532f22009-03-15 22:02:01 +00003769 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3770 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003771 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003772 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003773
Chris Lattner9af55002009-03-27 04:18:06 +00003774 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003775 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003776 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003777
Reid Spencer5f016e22007-07-11 17:01:13 +00003778 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003779 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003780
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003782 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003783 if (Kind == tok::star)
3784 // Remember that we parsed a pointer type, and remember the type-quals.
3785 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003786 DS.getConstSpecLoc(),
3787 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003788 DS.getRestrictSpecLoc()),
3789 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003790 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003791 else
3792 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003793 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003794 Loc),
3795 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003796 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003797 } else {
3798 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003799 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003800
Sebastian Redl743de1f2009-03-23 00:00:23 +00003801 // Complain about rvalue references in C++03, but then go on and build
3802 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003803 if (Kind == tok::ampamp)
3804 Diag(Loc, getLang().CPlusPlus0x ?
3805 diag::warn_cxx98_compat_rvalue_reference :
3806 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003807
Reid Spencer5f016e22007-07-11 17:01:13 +00003808 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3809 // cv-qualifiers are introduced through the use of a typedef or of a
3810 // template type argument, in which case the cv-qualifiers are ignored.
3811 //
3812 // [GNU] Retricted references are allowed.
3813 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003814 // [C++0x] Attributes on references are not allowed.
3815 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003816 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003817
3818 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3819 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3820 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003821 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003822 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3823 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003824 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003825 }
3826
3827 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003828 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003829
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003830 if (D.getNumTypeObjects() > 0) {
3831 // C++ [dcl.ref]p4: There shall be no references to references.
3832 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3833 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003834 if (const IdentifierInfo *II = D.getIdentifier())
3835 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3836 << II;
3837 else
3838 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3839 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003840
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003841 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003842 // can go ahead and build the (technically ill-formed)
3843 // declarator: reference collapsing will take care of it.
3844 }
3845 }
3846
Reid Spencer5f016e22007-07-11 17:01:13 +00003847 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003848 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003849 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003850 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003851 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 }
3853}
3854
3855/// ParseDirectDeclarator
3856/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003857/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003858/// '(' declarator ')'
3859/// [GNU] '(' attributes declarator ')'
3860/// [C90] direct-declarator '[' constant-expression[opt] ']'
3861/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3862/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3863/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3864/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3865/// direct-declarator '(' parameter-type-list ')'
3866/// direct-declarator '(' identifier-list[opt] ')'
3867/// [GNU] direct-declarator '(' parameter-forward-declarations
3868/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003869/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3870/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003871/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003872///
3873/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003874/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003875/// '::'[opt] nested-name-specifier[opt] type-name
3876///
3877/// id-expression: [C++ 5.1]
3878/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003879/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003880///
3881/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003882/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003883/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003884/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003885/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003886/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003887///
Reid Spencer5f016e22007-07-11 17:01:13 +00003888void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003889 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003890
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003891 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3892 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003893 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003894 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3895 D.getContext() == Declarator::MemberContext;
3896 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3897 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003898 }
3899
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003900 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003901 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003902 // Change the declaration context for name lookup, until this function
3903 // is exited (and the declarator has been parsed).
3904 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003905 }
3906
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003907 // C++0x [dcl.fct]p14:
3908 // There is a syntactic ambiguity when an ellipsis occurs at the end
3909 // of a parameter-declaration-clause without a preceding comma. In
3910 // this case, the ellipsis is parsed as part of the
3911 // abstract-declarator if the type of the parameter names a template
3912 // parameter pack that has not been expanded; otherwise, it is parsed
3913 // as part of the parameter-declaration-clause.
3914 if (Tok.is(tok::ellipsis) &&
3915 !((D.getContext() == Declarator::PrototypeContext ||
3916 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003917 NextToken().is(tok::r_paren) &&
3918 !Actions.containsUnexpandedParameterPacks(D)))
3919 D.setEllipsisLoc(ConsumeToken());
3920
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003921 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3922 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3923 // We found something that indicates the start of an unqualified-id.
3924 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003925 bool AllowConstructorName;
3926 if (D.getDeclSpec().hasTypeSpecifier())
3927 AllowConstructorName = false;
3928 else if (D.getCXXScopeSpec().isSet())
3929 AllowConstructorName =
3930 (D.getContext() == Declarator::FileContext ||
3931 (D.getContext() == Declarator::MemberContext &&
3932 D.getDeclSpec().isFriendSpecified()));
3933 else
3934 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3935
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003936 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3937 /*EnteringContext=*/true,
3938 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003939 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003940 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003941 D.getName()) ||
3942 // Once we're past the identifier, if the scope was bad, mark the
3943 // whole declarator bad.
3944 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003945 D.SetIdentifier(0, Tok.getLocation());
3946 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003947 } else {
3948 // Parsed the unqualified-id; update range information and move along.
3949 if (D.getSourceRange().getBegin().isInvalid())
3950 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3951 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003952 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003953 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003954 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003955 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003956 assert(!getLang().CPlusPlus &&
3957 "There's a C++-specific check for tok::identifier above");
3958 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3959 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3960 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003961 goto PastIdentifier;
3962 }
3963
3964 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003965 // direct-declarator: '(' declarator ')'
3966 // direct-declarator: '(' attributes declarator ')'
3967 // Example: 'char (*X)' or 'int (*XX)(void)'
3968 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003969
3970 // If the declarator was parenthesized, we entered the declarator
3971 // scope when parsing the parenthesized declarator, then exited
3972 // the scope already. Re-enter the scope, if we need to.
3973 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003974 // If there was an error parsing parenthesized declarator, declarator
3975 // scope may have been enterred before. Don't do it again.
3976 if (!D.isInvalidType() &&
3977 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003978 // Change the declaration context for name lookup, until this function
3979 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003980 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003981 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003982 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003983 // This could be something simple like "int" (in which case the declarator
3984 // portion is empty), if an abstract-declarator is allowed.
3985 D.SetIdentifier(0, Tok.getLocation());
3986 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003987 if (D.getContext() == Declarator::MemberContext)
3988 Diag(Tok, diag::err_expected_member_name_or_semi)
3989 << D.getDeclSpec().getSourceRange();
3990 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003991 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003992 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003993 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003994 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003995 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003996 }
Mike Stump1eb44332009-09-09 15:08:12 +00003997
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003998 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003999 assert(D.isPastIdentifier() &&
4000 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004001
Sean Huntbbd37c62009-11-21 08:43:09 +00004002 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00004003 if (D.getIdentifier())
4004 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004005
Reid Spencer5f016e22007-07-11 17:01:13 +00004006 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004007 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004008 // Enter function-declaration scope, limiting any declarators to the
4009 // function prototype scope, including parameter declarators.
4010 ParseScope PrototypeScope(this,
4011 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004012 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4013 // In such a case, check if we actually have a function declarator; if it
4014 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00004015 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4016 // When not in file scope, warn for ambiguous function declarators, just
4017 // in case the author intended it as a variable definition.
4018 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4019 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4020 break;
4021 }
John McCall0b7e6782011-03-24 11:26:52 +00004022 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004023 BalancedDelimiterTracker T(*this, tok::l_paren);
4024 T.consumeOpen();
4025 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004026 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004027 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004028 ParseBracketDeclarator(D);
4029 } else {
4030 break;
4031 }
4032 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004033}
Reid Spencer5f016e22007-07-11 17:01:13 +00004034
Chris Lattneref4715c2008-04-06 05:45:57 +00004035/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4036/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004037/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004038/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4039///
4040/// direct-declarator:
4041/// '(' declarator ')'
4042/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004043/// direct-declarator '(' parameter-type-list ')'
4044/// direct-declarator '(' identifier-list[opt] ')'
4045/// [GNU] direct-declarator '(' parameter-forward-declarations
4046/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004047///
4048void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004049 BalancedDelimiterTracker T(*this, tok::l_paren);
4050 T.consumeOpen();
4051
Chris Lattneref4715c2008-04-06 05:45:57 +00004052 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004053
Chris Lattner7399ee02008-10-20 02:05:46 +00004054 // Eat any attributes before we look at whether this is a grouping or function
4055 // declarator paren. If this is a grouping paren, the attribute applies to
4056 // the type being built up, for example:
4057 // int (__attribute__(()) *x)(long y)
4058 // If this ends up not being a grouping paren, the attribute applies to the
4059 // first argument, for example:
4060 // int (__attribute__(()) int x)
4061 // In either case, we need to eat any attributes to be able to determine what
4062 // sort of paren this is.
4063 //
John McCall0b7e6782011-03-24 11:26:52 +00004064 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004065 bool RequiresArg = false;
4066 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004067 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004068
Chris Lattner7399ee02008-10-20 02:05:46 +00004069 // We require that the argument list (if this is a non-grouping paren) be
4070 // present even if the attribute list was empty.
4071 RequiresArg = true;
4072 }
Steve Naroff239f0732008-12-25 14:16:32 +00004073 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004074 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004075 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004076 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004077 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004078 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004079 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004080 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004081 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004082 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004083
Chris Lattneref4715c2008-04-06 05:45:57 +00004084 // If we haven't past the identifier yet (or where the identifier would be
4085 // stored, if this is an abstract declarator), then this is probably just
4086 // grouping parens. However, if this could be an abstract-declarator, then
4087 // this could also be the start of function arguments (consider 'void()').
4088 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004089
Chris Lattneref4715c2008-04-06 05:45:57 +00004090 if (!D.mayOmitIdentifier()) {
4091 // If this can't be an abstract-declarator, this *must* be a grouping
4092 // paren, because we haven't seen the identifier yet.
4093 isGrouping = true;
4094 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004095 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004096 isDeclarationSpecifier()) { // 'int(int)' is a function.
4097 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4098 // considered to be a type, not a K&R identifier-list.
4099 isGrouping = false;
4100 } else {
4101 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4102 isGrouping = true;
4103 }
Mike Stump1eb44332009-09-09 15:08:12 +00004104
Chris Lattneref4715c2008-04-06 05:45:57 +00004105 // If this is a grouping paren, handle:
4106 // direct-declarator: '(' declarator ')'
4107 // direct-declarator: '(' attributes declarator ')'
4108 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004109 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004110 D.setGroupingParens(true);
4111
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004112 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004113 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004114 T.consumeClose();
4115 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4116 T.getCloseLocation()),
4117 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004118
4119 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004120 return;
4121 }
Mike Stump1eb44332009-09-09 15:08:12 +00004122
Chris Lattneref4715c2008-04-06 05:45:57 +00004123 // Okay, if this wasn't a grouping paren, it must be the start of a function
4124 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004125 // identifier (and remember where it would have been), then call into
4126 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004127 D.SetIdentifier(0, Tok.getLocation());
4128
David Blaikie42d6d0c2011-12-04 05:04:18 +00004129 // Enter function-declaration scope, limiting any declarators to the
4130 // function prototype scope, including parameter declarators.
4131 ParseScope PrototypeScope(this,
4132 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004133 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004134 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004135}
4136
4137/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4138/// declarator D up to a paren, which indicates that we are parsing function
4139/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004140///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004141/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004142/// after the open paren - they should be considered to be the first argument of
4143/// a parameter. If RequiresArg is true, then the first argument of the
4144/// function is required to be present and required to not be an identifier
4145/// list.
4146///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004147/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4148/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4149/// (C++0x) trailing-return-type[opt].
4150///
4151/// [C++0x] exception-specification:
4152/// dynamic-exception-specification
4153/// noexcept-specification
4154///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004155void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004156 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004157 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004158 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004159 assert(getCurScope()->isFunctionPrototypeScope() &&
4160 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004161 // lparen is already consumed!
4162 assert(D.isPastIdentifier() && "Should not call before identifier!");
4163
4164 // This should be true when the function has typed arguments.
4165 // Otherwise, it is treated as a K&R-style function.
4166 bool HasProto = false;
4167 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004168 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004169 // Remember where we see an ellipsis, if any.
4170 SourceLocation EllipsisLoc;
4171
4172 DeclSpec DS(AttrFactory);
4173 bool RefQualifierIsLValueRef = true;
4174 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004175 SourceLocation ConstQualifierLoc;
4176 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004177 ExceptionSpecificationType ESpecType = EST_None;
4178 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004179 SmallVector<ParsedType, 2> DynamicExceptions;
4180 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004181 ExprResult NoexceptExpr;
4182 ParsedType TrailingReturnType;
4183
4184 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004185 if (isFunctionDeclaratorIdentifierList()) {
4186 if (RequiresArg)
4187 Diag(Tok, diag::err_argument_required_after_attribute);
4188
4189 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4190
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004191 Tracker.consumeClose();
4192 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004193 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004194 if (Tok.isNot(tok::r_paren))
4195 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4196 else if (RequiresArg)
4197 Diag(Tok, diag::err_argument_required_after_attribute);
4198
4199 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4200
4201 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004202 Tracker.consumeClose();
4203 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004204
4205 if (getLang().CPlusPlus) {
4206 MaybeParseCXX0XAttributes(attrs);
4207
4208 // Parse cv-qualifier-seq[opt].
4209 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004210 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004211 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004212 ConstQualifierLoc = DS.getConstSpecLoc();
4213 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4214 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004215
4216 // Parse ref-qualifier[opt].
4217 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004218 Diag(Tok, getLang().CPlusPlus0x ?
4219 diag::warn_cxx98_compat_ref_qualifier :
4220 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004221
4222 RefQualifierIsLValueRef = Tok.is(tok::amp);
4223 RefQualifierLoc = ConsumeToken();
4224 EndLoc = RefQualifierLoc;
4225 }
4226
4227 // Parse exception-specification[opt].
4228 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4229 DynamicExceptions,
4230 DynamicExceptionRanges,
4231 NoexceptExpr);
4232 if (ESpecType != EST_None)
4233 EndLoc = ESpecRange.getEnd();
4234
4235 // Parse trailing-return-type[opt].
4236 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004237 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004238 SourceRange Range;
4239 TrailingReturnType = ParseTrailingReturnType(Range).get();
4240 if (Range.getEnd().isValid())
4241 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004242 }
4243 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004244 }
4245
4246 // Remember that we parsed a function type, and remember the attributes.
4247 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4248 /*isVariadic=*/EllipsisLoc.isValid(),
4249 EllipsisLoc,
4250 ParamInfo.data(), ParamInfo.size(),
4251 DS.getTypeQualifiers(),
4252 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004253 RefQualifierLoc, ConstQualifierLoc,
4254 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004255 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004256 ESpecType, ESpecRange.getBegin(),
4257 DynamicExceptions.data(),
4258 DynamicExceptionRanges.data(),
4259 DynamicExceptions.size(),
4260 NoexceptExpr.isUsable() ?
4261 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004262 Tracker.getOpenLocation(),
4263 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004264 TrailingReturnType),
4265 attrs, EndLoc);
4266}
4267
4268/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4269/// identifier list form for a K&R-style function: void foo(a,b,c)
4270///
4271/// Note that identifier-lists are only allowed for normal declarators, not for
4272/// abstract-declarators.
4273bool Parser::isFunctionDeclaratorIdentifierList() {
4274 return !getLang().CPlusPlus
4275 && Tok.is(tok::identifier)
4276 && !TryAltiVecVectorToken()
4277 // K&R identifier lists can't have typedefs as identifiers, per C99
4278 // 6.7.5.3p11.
4279 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4280 // Identifier lists follow a really simple grammar: the identifiers can
4281 // be followed *only* by a ", identifier" or ")". However, K&R
4282 // identifier lists are really rare in the brave new modern world, and
4283 // it is very common for someone to typo a type in a non-K&R style
4284 // list. If we are presented with something like: "void foo(intptr x,
4285 // float y)", we don't want to start parsing the function declarator as
4286 // though it is a K&R style declarator just because intptr is an
4287 // invalid type.
4288 //
4289 // To handle this, we check to see if the token after the first
4290 // identifier is a "," or ")". Only then do we parse it as an
4291 // identifier list.
4292 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4293}
4294
4295/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4296/// we found a K&R-style identifier list instead of a typed parameter list.
4297///
4298/// After returning, ParamInfo will hold the parsed parameters.
4299///
4300/// identifier-list: [C99 6.7.5]
4301/// identifier
4302/// identifier-list ',' identifier
4303///
4304void Parser::ParseFunctionDeclaratorIdentifierList(
4305 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004306 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004307 // If there was no identifier specified for the declarator, either we are in
4308 // an abstract-declarator, or we are in a parameter declarator which was found
4309 // to be abstract. In abstract-declarators, identifier lists are not valid:
4310 // diagnose this.
4311 if (!D.getIdentifier())
4312 Diag(Tok, diag::ext_ident_list_in_param);
4313
4314 // Maintain an efficient lookup of params we have seen so far.
4315 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4316
4317 while (1) {
4318 // If this isn't an identifier, report the error and skip until ')'.
4319 if (Tok.isNot(tok::identifier)) {
4320 Diag(Tok, diag::err_expected_ident);
4321 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4322 // Forget we parsed anything.
4323 ParamInfo.clear();
4324 return;
4325 }
4326
4327 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4328
4329 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4330 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4331 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4332
4333 // Verify that the argument identifier has not already been mentioned.
4334 if (!ParamsSoFar.insert(ParmII)) {
4335 Diag(Tok, diag::err_param_redefinition) << ParmII;
4336 } else {
4337 // Remember this identifier in ParamInfo.
4338 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4339 Tok.getLocation(),
4340 0));
4341 }
4342
4343 // Eat the identifier.
4344 ConsumeToken();
4345
4346 // The list continues if we see a comma.
4347 if (Tok.isNot(tok::comma))
4348 break;
4349 ConsumeToken();
4350 }
4351}
4352
4353/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4354/// after the opening parenthesis. This function will not parse a K&R-style
4355/// identifier list.
4356///
4357/// D is the declarator being parsed. If attrs is non-null, then the caller
4358/// parsed those arguments immediately after the open paren - they should be
4359/// considered to be the first argument of a parameter.
4360///
4361/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4362/// be the location of the ellipsis, if any was parsed.
4363///
Reid Spencer5f016e22007-07-11 17:01:13 +00004364/// parameter-type-list: [C99 6.7.5]
4365/// parameter-list
4366/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004367/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004368///
4369/// parameter-list: [C99 6.7.5]
4370/// parameter-declaration
4371/// parameter-list ',' parameter-declaration
4372///
4373/// parameter-declaration: [C99 6.7.5]
4374/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004375/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004376/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004377/// declaration-specifiers abstract-declarator[opt]
4378/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004379/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004380/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4381///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004382void Parser::ParseParameterDeclarationClause(
4383 Declarator &D,
4384 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004385 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004386 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004387
Chris Lattnerf97409f2008-04-06 06:57:35 +00004388 while (1) {
4389 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004390 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004391 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004392 }
Mike Stump1eb44332009-09-09 15:08:12 +00004393
Chris Lattnerf97409f2008-04-06 06:57:35 +00004394 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004395 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004396 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004397
John McCall7f040a92010-12-24 02:08:15 +00004398 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004399 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004400 ParseMicrosoftAttributes(DS.getAttributes());
4401
4402 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004403
4404 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004405 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004406 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4407 // attributes lost? Should they even be allowed?
4408 // FIXME: If we can leave the attributes in the token stream somehow, we can
4409 // get rid of a parameter (attrs) and this statement. It might be too much
4410 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004411 DS.takeAttributesFrom(attrs);
4412
Chris Lattnere64c5492009-02-27 18:38:20 +00004413 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004414
Chris Lattnerf97409f2008-04-06 06:57:35 +00004415 // Parse the declarator. This is "PrototypeContext", because we must
4416 // accept either 'declarator' or 'abstract-declarator' here.
4417 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4418 ParseDeclarator(ParmDecl);
4419
4420 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004421 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004422
Chris Lattnerf97409f2008-04-06 06:57:35 +00004423 // Remember this parsed parameter in ParamInfo.
4424 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004425
Douglas Gregor72b505b2008-12-16 21:30:33 +00004426 // DefArgToks is used when the parsing of default arguments needs
4427 // to be delayed.
4428 CachedTokens *DefArgToks = 0;
4429
Chris Lattnerf97409f2008-04-06 06:57:35 +00004430 // If no parameter was specified, verify that *something* was specified,
4431 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004432 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4433 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004434 // Completely missing, emit error.
4435 Diag(DSStart, diag::err_missing_param);
4436 } else {
4437 // Otherwise, we have something. Add it and let semantic analysis try
4438 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004439
Chris Lattnerf97409f2008-04-06 06:57:35 +00004440 // Inform the actions module about the parameter declarator, so it gets
4441 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004442 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004443
4444 // Parse the default argument, if any. We parse the default
4445 // arguments in all dialects; the semantic analysis in
4446 // ActOnParamDefaultArgument will reject the default argument in
4447 // C.
4448 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004449 SourceLocation EqualLoc = Tok.getLocation();
4450
Chris Lattner04421082008-04-08 04:40:51 +00004451 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004452 if (D.getContext() == Declarator::MemberContext) {
4453 // If we're inside a class definition, cache the tokens
4454 // corresponding to the default argument. We'll actually parse
4455 // them when we see the end of the class definition.
4456 // FIXME: Templates will require something similar.
4457 // FIXME: Can we use a smart pointer for Toks?
4458 DefArgToks = new CachedTokens;
4459
Mike Stump1eb44332009-09-09 15:08:12 +00004460 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004461 /*StopAtSemi=*/true,
4462 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004463 delete DefArgToks;
4464 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004465 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004466 } else {
4467 // Mark the end of the default argument so that we know when to
4468 // stop when we parse it later on.
4469 Token DefArgEnd;
4470 DefArgEnd.startToken();
4471 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4472 DefArgEnd.setLocation(Tok.getLocation());
4473 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004474 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004475 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004476 }
Chris Lattner04421082008-04-08 04:40:51 +00004477 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004478 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004479 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004480
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004481 // The argument isn't actually potentially evaluated unless it is
4482 // used.
4483 EnterExpressionEvaluationContext Eval(Actions,
4484 Sema::PotentiallyEvaluatedIfUsed);
4485
John McCall60d7b3a2010-08-24 06:29:42 +00004486 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004487 if (DefArgResult.isInvalid()) {
4488 Actions.ActOnParamDefaultArgumentError(Param);
4489 SkipUntil(tok::comma, tok::r_paren, true, true);
4490 } else {
4491 // Inform the actions module about the default argument
4492 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004493 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004494 }
Chris Lattner04421082008-04-08 04:40:51 +00004495 }
4496 }
Mike Stump1eb44332009-09-09 15:08:12 +00004497
4498 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4499 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004500 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004501 }
4502
4503 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004504 if (Tok.isNot(tok::comma)) {
4505 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004506 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4507
4508 if (!getLang().CPlusPlus) {
4509 // We have ellipsis without a preceding ',', which is ill-formed
4510 // in C. Complain and provide the fix.
4511 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004512 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004513 }
4514 }
4515
4516 break;
4517 }
Mike Stump1eb44332009-09-09 15:08:12 +00004518
Chris Lattnerf97409f2008-04-06 06:57:35 +00004519 // Consume the comma.
4520 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004521 }
Mike Stump1eb44332009-09-09 15:08:12 +00004522
Chris Lattner66d28652008-04-06 06:34:08 +00004523}
Chris Lattneref4715c2008-04-06 05:45:57 +00004524
Reid Spencer5f016e22007-07-11 17:01:13 +00004525/// [C90] direct-declarator '[' constant-expression[opt] ']'
4526/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4527/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4528/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4529/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4530void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004531 BalancedDelimiterTracker T(*this, tok::l_square);
4532 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004533
Chris Lattner378c7e42008-12-18 07:27:21 +00004534 // C array syntax has many features, but by-far the most common is [] and [4].
4535 // This code does a fast path to handle some of the most obvious cases.
4536 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004537 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004538 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004539 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004540
Chris Lattner378c7e42008-12-18 07:27:21 +00004541 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004542 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004543 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004544 T.getOpenLocation(),
4545 T.getCloseLocation()),
4546 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004547 return;
4548 } else if (Tok.getKind() == tok::numeric_constant &&
4549 GetLookAheadToken(1).is(tok::r_square)) {
4550 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004551 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004552 ConsumeToken();
4553
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004554 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004555 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004556 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004557
Chris Lattner378c7e42008-12-18 07:27:21 +00004558 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004559 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004560 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004561 T.getOpenLocation(),
4562 T.getCloseLocation()),
4563 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004564 return;
4565 }
Mike Stump1eb44332009-09-09 15:08:12 +00004566
Reid Spencer5f016e22007-07-11 17:01:13 +00004567 // If valid, this location is the position where we read the 'static' keyword.
4568 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004569 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004570 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004571
Reid Spencer5f016e22007-07-11 17:01:13 +00004572 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004573 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004574 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004575 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004576
Reid Spencer5f016e22007-07-11 17:01:13 +00004577 // If we haven't already read 'static', check to see if there is one after the
4578 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004579 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004580 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004581
Reid Spencer5f016e22007-07-11 17:01:13 +00004582 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4583 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004584 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004585
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004586 // Handle the case where we have '[*]' as the array size. However, a leading
4587 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4588 // the the token after the star is a ']'. Since stars in arrays are
4589 // infrequent, use of lookahead is not costly here.
4590 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004591 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004592
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004593 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004594 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004595 StaticLoc = SourceLocation(); // Drop the static.
4596 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004597 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004598 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004599 // Note, in C89, this production uses the constant-expr production instead
4600 // of assignment-expr. The only difference is that assignment-expr allows
4601 // things like '=' and '*='. Sema rejects these in C89 mode because they
4602 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004603
Douglas Gregore0762c92009-06-19 23:52:42 +00004604 // Parse the constant-expression or assignment-expression now (depending
4605 // on dialect).
4606 if (getLang().CPlusPlus)
4607 NumElements = ParseConstantExpression();
4608 else
4609 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004610 }
Mike Stump1eb44332009-09-09 15:08:12 +00004611
Reid Spencer5f016e22007-07-11 17:01:13 +00004612 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004613 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004614 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004615 // If the expression was invalid, skip it.
4616 SkipUntil(tok::r_square);
4617 return;
4618 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004619
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004620 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004621
John McCall0b7e6782011-03-24 11:26:52 +00004622 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004623 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004624
Chris Lattner378c7e42008-12-18 07:27:21 +00004625 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004626 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004627 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004628 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004629 T.getOpenLocation(),
4630 T.getCloseLocation()),
4631 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004632}
4633
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004634/// [GNU] typeof-specifier:
4635/// typeof ( expressions )
4636/// typeof ( type-name )
4637/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004638///
4639void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004640 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004641 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004642 SourceLocation StartLoc = ConsumeToken();
4643
John McCallcfb708c2010-01-13 20:03:27 +00004644 const bool hasParens = Tok.is(tok::l_paren);
4645
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004646 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004647 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004648 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004649 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4650 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004651 if (hasParens)
4652 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004653
4654 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004655 // FIXME: Not accurate, the range gets one token more than it should.
4656 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004657 else
4658 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004659
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004660 if (isCastExpr) {
4661 if (!CastTy) {
4662 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004663 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004664 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004665
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004666 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004667 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004668 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4669 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004670 DiagID, CastTy))
4671 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004672 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004673 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004674
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004675 // If we get here, the operand to the typeof was an expresion.
4676 if (Operand.isInvalid()) {
4677 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004678 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004679 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004680
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004681 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004682 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004683 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4684 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004685 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004686 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004687}
Chris Lattner1b492422010-02-28 18:33:55 +00004688
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004689/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004690/// _Atomic ( type-name )
4691///
4692void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4693 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4694
4695 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004696 BalancedDelimiterTracker T(*this, tok::l_paren);
4697 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004698 SkipUntil(tok::r_paren);
4699 return;
4700 }
4701
4702 TypeResult Result = ParseTypeName();
4703 if (Result.isInvalid()) {
4704 SkipUntil(tok::r_paren);
4705 return;
4706 }
4707
4708 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004709 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004710
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004711 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004712 return;
4713
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004714 DS.setTypeofParensRange(T.getRange());
4715 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004716
4717 const char *PrevSpec = 0;
4718 unsigned DiagID;
4719 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4720 DiagID, Result.release()))
4721 Diag(StartLoc, DiagID) << PrevSpec;
4722}
4723
Chris Lattner1b492422010-02-28 18:33:55 +00004724
4725/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4726/// from TryAltiVecVectorToken.
4727bool Parser::TryAltiVecVectorTokenOutOfLine() {
4728 Token Next = NextToken();
4729 switch (Next.getKind()) {
4730 default: return false;
4731 case tok::kw_short:
4732 case tok::kw_long:
4733 case tok::kw_signed:
4734 case tok::kw_unsigned:
4735 case tok::kw_void:
4736 case tok::kw_char:
4737 case tok::kw_int:
4738 case tok::kw_float:
4739 case tok::kw_double:
4740 case tok::kw_bool:
4741 case tok::kw___pixel:
4742 Tok.setKind(tok::kw___vector);
4743 return true;
4744 case tok::identifier:
4745 if (Next.getIdentifierInfo() == Ident_pixel) {
4746 Tok.setKind(tok::kw___vector);
4747 return true;
4748 }
4749 return false;
4750 }
4751}
4752
4753bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4754 const char *&PrevSpec, unsigned &DiagID,
4755 bool &isInvalid) {
4756 if (Tok.getIdentifierInfo() == Ident_vector) {
4757 Token Next = NextToken();
4758 switch (Next.getKind()) {
4759 case tok::kw_short:
4760 case tok::kw_long:
4761 case tok::kw_signed:
4762 case tok::kw_unsigned:
4763 case tok::kw_void:
4764 case tok::kw_char:
4765 case tok::kw_int:
4766 case tok::kw_float:
4767 case tok::kw_double:
4768 case tok::kw_bool:
4769 case tok::kw___pixel:
4770 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4771 return true;
4772 case tok::identifier:
4773 if (Next.getIdentifierInfo() == Ident_pixel) {
4774 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4775 return true;
4776 }
4777 break;
4778 default:
4779 break;
4780 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004781 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004782 DS.isTypeAltiVecVector()) {
4783 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4784 return true;
4785 }
4786 return false;
4787}