blob: 16321491ee8c65a4dd0396b20f081bed6da3c588 [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:
997 case tok::colon: // Might be a typo for '::'.
998 return getLang().CPlusPlus;
999
1000 case tok::identifier:
1001 switch (NextToken().getKind()) {
1002 case tok::code_completion:
1003 case tok::coloncolon:
1004 case tok::comma:
1005 case tok::equal:
1006 case tok::equalequal: // Might be a typo for '='.
1007 case tok::kw_alignas:
1008 case tok::kw_asm:
1009 case tok::kw___attribute:
1010 case tok::l_brace:
1011 case tok::l_paren:
1012 case tok::l_square:
1013 case tok::less:
1014 case tok::r_brace:
1015 case tok::r_paren:
1016 case tok::r_square:
1017 case tok::semi:
1018 return true;
1019
1020 case tok::colon:
1021 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
1022 // and in block scope it's probably a label.
1023 return getLang().CPlusPlus && Context == Declarator::FileContext;
1024
1025 default:
1026 return false;
1027 }
1028
1029 default:
1030 return false;
1031 }
1032}
1033
John McCalld8ac0572009-11-03 19:26:08 +00001034/// ParseDeclGroup - Having concluded that this is either a function
1035/// definition or a group of object declarations, actually parse the
1036/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001037Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1038 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001039 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001040 SourceLocation *DeclEnd,
1041 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001042 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001043 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001044 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001045
John McCalld8ac0572009-11-03 19:26:08 +00001046 // Bail out if the first declarator didn't seem well-formed.
1047 if (!D.hasName() && !D.mayOmitIdentifier()) {
1048 // Skip until ; or }.
1049 SkipUntil(tok::r_brace, true, true);
1050 if (Tok.is(tok::semi))
1051 ConsumeToken();
1052 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattnerc82daef2010-07-11 22:24:20 +00001055 // Check to see if we have a function *definition* which must have a body.
1056 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1057 // Look at the next token to make sure that this isn't a function
1058 // declaration. We have to check this because __attribute__ might be the
1059 // start of a function definition in GCC-extended K&R C.
1060 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001061
Chris Lattner004659a2010-07-11 22:42:07 +00001062 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001063 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1064 Diag(Tok, diag::err_function_declared_typedef);
1065
1066 // Recover by treating the 'typedef' as spurious.
1067 DS.ClearStorageClassSpecs();
1068 }
1069
John McCalld226f652010-08-21 09:40:31 +00001070 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001071 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001072 }
1073
1074 if (isDeclarationSpecifier()) {
1075 // If there is an invalid declaration specifier right after the function
1076 // prototype, then we must be in a missing semicolon case where this isn't
1077 // actually a body. Just fall through into the code that handles it as a
1078 // prototype, and let the top-level code handle the erroneous declspec
1079 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001080 } else {
1081 Diag(Tok, diag::err_expected_fn_body);
1082 SkipUntil(tok::semi);
1083 return DeclGroupPtrTy();
1084 }
1085 }
1086
Richard Smithad762fc2011-04-14 22:09:26 +00001087 if (ParseAttributesAfterDeclarator(D))
1088 return DeclGroupPtrTy();
1089
1090 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1091 // must parse and analyze the for-range-initializer before the declaration is
1092 // analyzed.
1093 if (FRI && Tok.is(tok::colon)) {
1094 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001095 if (Tok.is(tok::l_brace))
1096 FRI->RangeExpr = ParseBraceInitializer();
1097 else
1098 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001099 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1100 Actions.ActOnCXXForRangeDecl(ThisDecl);
1101 Actions.FinalizeDeclaration(ThisDecl);
1102 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1103 }
1104
Chris Lattner5f9e2722011-07-23 10:55:15 +00001105 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001106 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001107 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001108 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001109 DeclsInGroup.push_back(FirstDecl);
1110
Richard Smith0706df42011-10-19 21:33:05 +00001111 bool ExpectSemi = Context != Declarator::ForContext;
1112
John McCalld8ac0572009-11-03 19:26:08 +00001113 // If we don't have a comma, it is either the end of the list (a ';') or an
1114 // error, bail out.
1115 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001116 SourceLocation CommaLoc = ConsumeToken();
1117
1118 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1119 // This comma was followed by a line-break and something which can't be
1120 // the start of a declarator. The comma was probably a typo for a
1121 // semicolon.
1122 Diag(CommaLoc, diag::err_expected_semi_declaration)
1123 << FixItHint::CreateReplacement(CommaLoc, ";");
1124 ExpectSemi = false;
1125 break;
1126 }
John McCalld8ac0572009-11-03 19:26:08 +00001127
1128 // Parse the next declarator.
1129 D.clear();
1130
1131 // Accept attributes in an init-declarator. In the first declarator in a
1132 // declaration, these would be part of the declspec. In subsequent
1133 // declarators, they become part of the declarator itself, so that they
1134 // don't apply to declarators after *this* one. Examples:
1135 // short __attribute__((common)) var; -> declspec
1136 // short var __attribute__((common)); -> declarator
1137 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001138 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001139
1140 ParseDeclarator(D);
1141
John McCalld226f652010-08-21 09:40:31 +00001142 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001143 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001144 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001145 DeclsInGroup.push_back(ThisDecl);
1146 }
1147
1148 if (DeclEnd)
1149 *DeclEnd = Tok.getLocation();
1150
Richard Smith0706df42011-10-19 21:33:05 +00001151 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001152 ExpectAndConsume(tok::semi,
1153 Context == Declarator::FileContext
1154 ? diag::err_invalid_token_after_toplevel_declarator
1155 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001156 // Okay, there was no semicolon and one was expected. If we see a
1157 // declaration specifier, just assume it was missing and continue parsing.
1158 // Otherwise things are very confused and we skip to recover.
1159 if (!isDeclarationSpecifier()) {
1160 SkipUntil(tok::r_brace, true, true);
1161 if (Tok.is(tok::semi))
1162 ConsumeToken();
1163 }
John McCalld8ac0572009-11-03 19:26:08 +00001164 }
1165
Douglas Gregor23c94db2010-07-02 17:43:08 +00001166 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001167 DeclsInGroup.data(),
1168 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001169}
1170
Richard Smithad762fc2011-04-14 22:09:26 +00001171/// Parse an optional simple-asm-expr and attributes, and attach them to a
1172/// declarator. Returns true on an error.
1173bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1174 // If a simple-asm-expr is present, parse it.
1175 if (Tok.is(tok::kw_asm)) {
1176 SourceLocation Loc;
1177 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1178 if (AsmLabel.isInvalid()) {
1179 SkipUntil(tok::semi, true, true);
1180 return true;
1181 }
1182
1183 D.setAsmLabel(AsmLabel.release());
1184 D.SetRangeEnd(Loc);
1185 }
1186
1187 MaybeParseGNUAttributes(D);
1188 return false;
1189}
1190
Douglas Gregor1426e532009-05-12 21:31:51 +00001191/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1192/// declarator'. This method parses the remainder of the declaration
1193/// (including any attributes or initializer, among other things) and
1194/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001195///
Reid Spencer5f016e22007-07-11 17:01:13 +00001196/// init-declarator: [C99 6.7]
1197/// declarator
1198/// declarator '=' initializer
1199/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1200/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001201/// [C++] declarator initializer[opt]
1202///
1203/// [C++] initializer:
1204/// [C++] '=' initializer-clause
1205/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001206/// [C++0x] '=' 'default' [TODO]
1207/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001208/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001209///
1210/// According to the standard grammar, =default and =delete are function
1211/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001212///
John McCalld226f652010-08-21 09:40:31 +00001213Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001214 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001215 if (ParseAttributesAfterDeclarator(D))
1216 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Richard Smithad762fc2011-04-14 22:09:26 +00001218 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1219}
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Richard Smithad762fc2011-04-14 22:09:26 +00001221Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1222 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001223 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001224 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001225 switch (TemplateInfo.Kind) {
1226 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001227 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001228 break;
1229
1230 case ParsedTemplateInfo::Template:
1231 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001232 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001233 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001234 TemplateInfo.TemplateParams->data(),
1235 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001236 D);
1237 break;
1238
1239 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001240 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001241 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001242 TemplateInfo.ExternLoc,
1243 TemplateInfo.TemplateLoc,
1244 D);
1245 if (ThisRes.isInvalid()) {
1246 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001247 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001248 }
1249
1250 ThisDecl = ThisRes.get();
1251 break;
1252 }
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Richard Smith34b41d92011-02-20 03:19:35 +00001255 bool TypeContainsAuto =
1256 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1257
Douglas Gregor1426e532009-05-12 21:31:51 +00001258 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001259 if (isTokenEqualOrMistypedEqualEqual(
1260 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001261 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001262 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001263 if (D.isFunctionDeclarator())
1264 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1265 << 1 /* delete */;
1266 else
1267 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001268 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001269 if (D.isFunctionDeclarator())
1270 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1271 << 1 /* delete */;
1272 else
1273 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001274 } else {
John McCall731ad842009-12-19 09:28:58 +00001275 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1276 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001277 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001278 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001279
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001280 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001281 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001282 cutOffParsing();
1283 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001284 }
1285
John McCall60d7b3a2010-08-24 06:29:42 +00001286 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001287
John McCall731ad842009-12-19 09:28:58 +00001288 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001289 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001290 ExitScope();
1291 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001292
Douglas Gregor1426e532009-05-12 21:31:51 +00001293 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001294 SkipUntil(tok::comma, true, true);
1295 Actions.ActOnInitializerError(ThisDecl);
1296 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001297 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1298 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001299 }
1300 } else if (Tok.is(tok::l_paren)) {
1301 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001302 BalancedDelimiterTracker T(*this, tok::l_paren);
1303 T.consumeOpen();
1304
Douglas Gregor1426e532009-05-12 21:31:51 +00001305 ExprVector Exprs(Actions);
1306 CommaLocsTy CommaLocs;
1307
Douglas Gregorb4debae2009-12-22 17:47:17 +00001308 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1309 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001310 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001311 }
1312
Douglas Gregor1426e532009-05-12 21:31:51 +00001313 if (ParseExpressionList(Exprs, CommaLocs)) {
1314 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001315
1316 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001317 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001318 ExitScope();
1319 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001320 } else {
1321 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001322 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001323
1324 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1325 "Unexpected number of commas!");
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 }
1331
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001332 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001333 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001334 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001335 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001336 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001337 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1338 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001339 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1340
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001341 if (D.getCXXScopeSpec().isSet()) {
1342 EnterScope(0);
1343 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1344 }
1345
1346 ExprResult Init(ParseBraceInitializer());
1347
1348 if (D.getCXXScopeSpec().isSet()) {
1349 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1350 ExitScope();
1351 }
1352
1353 if (Init.isInvalid()) {
1354 Actions.ActOnInitializerError(ThisDecl);
1355 } else
1356 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1357 /*DirectInit=*/true, TypeContainsAuto);
1358
Douglas Gregor1426e532009-05-12 21:31:51 +00001359 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001360 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001361 }
1362
Richard Smith483b9f32011-02-21 20:05:19 +00001363 Actions.FinalizeDeclaration(ThisDecl);
1364
Douglas Gregor1426e532009-05-12 21:31:51 +00001365 return ThisDecl;
1366}
1367
Reid Spencer5f016e22007-07-11 17:01:13 +00001368/// ParseSpecifierQualifierList
1369/// specifier-qualifier-list:
1370/// type-specifier specifier-qualifier-list[opt]
1371/// type-qualifier specifier-qualifier-list[opt]
1372/// [GNU] attributes specifier-qualifier-list[opt]
1373///
Richard Smithc89edf52011-07-01 19:46:12 +00001374void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1376 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001377 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001378 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 // Validate declspec for type-name.
1381 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001382 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001383 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 // Issue diagnostic and remove storage class if present.
1387 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1388 if (DS.getStorageClassSpecLoc().isValid())
1389 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1390 else
1391 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1392 DS.ClearStorageClassSpecs();
1393 }
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 // Issue diagnostic and remove function specfier if present.
1396 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001397 if (DS.isInlineSpecified())
1398 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1399 if (DS.isVirtualSpecified())
1400 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1401 if (DS.isExplicitSpecified())
1402 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 DS.ClearFunctionSpecs();
1404 }
1405}
1406
Chris Lattnerc199ab32009-04-12 20:42:31 +00001407/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1408/// specified token is valid after the identifier in a declarator which
1409/// immediately follows the declspec. For example, these things are valid:
1410///
1411/// int x [ 4]; // direct-declarator
1412/// int x ( int y); // direct-declarator
1413/// int(int x ) // direct-declarator
1414/// int x ; // simple-declaration
1415/// int x = 17; // init-declarator-list
1416/// int x , y; // init-declarator-list
1417/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001418/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001419/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001420///
1421/// This is not, because 'x' does not immediately follow the declspec (though
1422/// ')' happens to be valid anyway).
1423/// int (x)
1424///
1425static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1426 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1427 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001428 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001429}
1430
Chris Lattnere40c2952009-04-14 21:34:55 +00001431
1432/// ParseImplicitInt - This method is called when we have an non-typename
1433/// identifier in a declspec (which normally terminates the decl spec) when
1434/// the declspec has no type specifier. In this case, the declspec is either
1435/// malformed or is "implicit int" (in K&R and C89).
1436///
1437/// This method handles diagnosing this prettily and returns false if the
1438/// declspec is done being processed. If it recovers and thinks there may be
1439/// other pieces of declspec after it, it returns true.
1440///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001441bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001443 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001444 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Chris Lattnere40c2952009-04-14 21:34:55 +00001446 SourceLocation Loc = Tok.getLocation();
1447 // If we see an identifier that is not a type name, we normally would
1448 // parse it as the identifer being declared. However, when a typename
1449 // is typo'd or the definition is not included, this will incorrectly
1450 // parse the typename as the identifier name and fall over misparsing
1451 // later parts of the diagnostic.
1452 //
1453 // As such, we try to do some look-ahead in cases where this would
1454 // otherwise be an "implicit-int" case to see if this is invalid. For
1455 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1456 // an identifier with implicit int, we'd get a parse error because the
1457 // next token is obviously invalid for a type. Parse these as a case
1458 // with an invalid type specifier.
1459 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Chris Lattnere40c2952009-04-14 21:34:55 +00001461 // Since we know that this either implicit int (which is rare) or an
1462 // error, we'd do lookahead to try to do better recovery.
1463 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1464 // If this token is valid for implicit int, e.g. "static x = 4", then
1465 // we just avoid eating the identifier, so it will be parsed as the
1466 // identifier in the declarator.
1467 return false;
1468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Chris Lattnere40c2952009-04-14 21:34:55 +00001470 // Otherwise, if we don't consume this token, we are going to emit an
1471 // error anyway. Try to recover from various common problems. Check
1472 // to see if this was a reference to a tag name without a tag specified.
1473 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001474 //
1475 // C++ doesn't need this, and isTagName doesn't take SS.
1476 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001477 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001478 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Douglas Gregor23c94db2010-07-02 17:43:08 +00001480 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001481 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001482 case DeclSpec::TST_enum:
1483 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1484 case DeclSpec::TST_union:
1485 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1486 case DeclSpec::TST_struct:
1487 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1488 case DeclSpec::TST_class:
1489 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Chris Lattnerf4382f52009-04-14 22:17:06 +00001492 if (TagName) {
1493 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001494 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001495 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattnerf4382f52009-04-14 22:17:06 +00001497 // Parse this as a tag as if the missing tag were present.
1498 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001499 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001500 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001501 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001502 return true;
1503 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregora786fdb2009-10-13 23:27:22 +00001506 // This is almost certainly an invalid type name. Let the action emit a
1507 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001508 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001509 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001511 // The action emitted a diagnostic, so we don't have to.
1512 if (T) {
1513 // The action has suggested that the type T could be used. Set that as
1514 // the type in the declaration specifiers, consume the would-be type
1515 // name token, and we're done.
1516 const char *PrevSpec;
1517 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001518 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001519 DS.SetRangeEnd(Tok.getLocation());
1520 ConsumeToken();
1521
1522 // There may be other declaration specifiers after this.
1523 return true;
1524 }
1525
1526 // Fall through; the action had no suggestion for us.
1527 } else {
1528 // The action did not emit a diagnostic, so emit one now.
1529 SourceRange R;
1530 if (SS) R = SS->getRange();
1531 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Douglas Gregora786fdb2009-10-13 23:27:22 +00001534 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001535 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001536 unsigned DiagID;
1537 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001538 DS.SetRangeEnd(Tok.getLocation());
1539 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Chris Lattnere40c2952009-04-14 21:34:55 +00001541 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1542 // avoid rippling error messages on subsequent uses of the same type,
1543 // could be useful if #include was forgotten.
1544 return false;
1545}
1546
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001547/// \brief Determine the declaration specifier context from the declarator
1548/// context.
1549///
1550/// \param Context the declarator context, which is one of the
1551/// Declarator::TheContext enumerator values.
1552Parser::DeclSpecContext
1553Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1554 if (Context == Declarator::MemberContext)
1555 return DSC_class;
1556 if (Context == Declarator::FileContext)
1557 return DSC_top_level;
1558 return DSC_normal;
1559}
1560
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001561/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1562///
1563/// FIXME: Simply returns an alignof() expression if the argument is a
1564/// type. Ideally, the type should be propagated directly into Sema.
1565///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001566/// [C11] type-id
1567/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001568/// [C++0x] type-id ...[opt]
1569/// [C++0x] assignment-expression ...[opt]
1570ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1571 SourceLocation &EllipsisLoc) {
1572 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001573 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001574 SourceLocation TypeLoc = Tok.getLocation();
1575 ParsedType Ty = ParseTypeName().get();
1576 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001577 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1578 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001579 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001580 ER = ParseConstantExpression();
1581
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001582 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1583 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001584
1585 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001586}
1587
1588/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1589/// attribute to Attrs.
1590///
1591/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001592/// [C11] '_Alignas' '(' type-id ')'
1593/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001594/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1595/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001596void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1597 SourceLocation *endLoc) {
1598 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1599 "Not an alignment-specifier!");
1600
1601 SourceLocation KWLoc = Tok.getLocation();
1602 ConsumeToken();
1603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001604 BalancedDelimiterTracker T(*this, tok::l_paren);
1605 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001606 return;
1607
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001608 SourceLocation EllipsisLoc;
1609 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001610 if (ArgExpr.isInvalid()) {
1611 SkipUntil(tok::r_paren);
1612 return;
1613 }
1614
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001615 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001616 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001617 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001618
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001619 // FIXME: Handle pack-expansions here.
1620 if (EllipsisLoc.isValid()) {
1621 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1622 return;
1623 }
1624
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001625 ExprVector ArgExprs(Actions);
1626 ArgExprs.push_back(ArgExpr.release());
1627 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001628 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001629}
1630
Reid Spencer5f016e22007-07-11 17:01:13 +00001631/// ParseDeclarationSpecifiers
1632/// declaration-specifiers: [C99 6.7]
1633/// storage-class-specifier declaration-specifiers[opt]
1634/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001635/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001636/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001637/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001638/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001639///
1640/// storage-class-specifier: [C99 6.7.1]
1641/// 'typedef'
1642/// 'extern'
1643/// 'static'
1644/// 'auto'
1645/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001646/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001647/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001648/// function-specifier: [C99 6.7.4]
1649/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001650/// [C++] 'virtual'
1651/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001652/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001653/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001654/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001655
Reid Spencer5f016e22007-07-11 17:01:13 +00001656///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001657void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001658 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001659 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001660 DeclSpecContext DSContext) {
1661 if (DS.getSourceRange().isInvalid()) {
1662 DS.SetRangeStart(Tok.getLocation());
1663 DS.SetRangeEnd(Tok.getLocation());
1664 }
1665
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001666 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001668 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001670 unsigned DiagID = 0;
1671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001673
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001675 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001676 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001677 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1678 MaybeParseCXX0XAttributes(DS.getAttributes());
1679
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 // If this is not a declaration specifier token, we're done reading decl
1681 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001682 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001685 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001686 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001687 if (DS.hasTypeSpecifier()) {
1688 bool AllowNonIdentifiers
1689 = (getCurScope()->getFlags() & (Scope::ControlScope |
1690 Scope::BlockScope |
1691 Scope::TemplateParamScope |
1692 Scope::FunctionPrototypeScope |
1693 Scope::AtCatchScope)) == 0;
1694 bool AllowNestedNameSpecifiers
1695 = DSContext == DSC_top_level ||
1696 (DSContext == DSC_class && DS.isFriendSpecified());
1697
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001698 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1699 AllowNonIdentifiers,
1700 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001701 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001702 }
1703
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001704 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1705 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1706 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001707 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1708 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001709 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001710 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001711 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001712 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001713
1714 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001715 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001716 }
1717
Chris Lattner5e02c472009-01-05 00:07:25 +00001718 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001719 // C++ scope specifier. Annotate and loop, or bail out on error.
1720 if (TryAnnotateCXXScopeToken(true)) {
1721 if (!DS.hasTypeSpecifier())
1722 DS.SetTypeSpecError();
1723 goto DoneWithDeclSpec;
1724 }
John McCall2e0a7152010-03-01 18:20:46 +00001725 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1726 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001727 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001728
1729 case tok::annot_cxxscope: {
1730 if (DS.hasTypeSpecifier())
1731 goto DoneWithDeclSpec;
1732
John McCallaa87d332009-12-12 11:40:51 +00001733 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001734 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1735 Tok.getAnnotationRange(),
1736 SS);
John McCallaa87d332009-12-12 11:40:51 +00001737
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001738 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001739 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001740 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001741 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001742 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001743 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001744
1745 // C++ [class.qual]p2:
1746 // In a lookup in which the constructor is an acceptable lookup
1747 // result and the nested-name-specifier nominates a class C:
1748 //
1749 // - if the name specified after the
1750 // nested-name-specifier, when looked up in C, is the
1751 // injected-class-name of C (Clause 9), or
1752 //
1753 // - if the name specified after the nested-name-specifier
1754 // is the same as the identifier or the
1755 // simple-template-id's template-name in the last
1756 // component of the nested-name-specifier,
1757 //
1758 // the name is instead considered to name the constructor of
1759 // class C.
1760 //
1761 // Thus, if the template-name is actually the constructor
1762 // name, then the code is ill-formed; this interpretation is
1763 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001764 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001765 if ((DSContext == DSC_top_level ||
1766 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1767 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001768 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001769 if (isConstructorDeclarator()) {
1770 // The user meant this to be an out-of-line constructor
1771 // definition, but template arguments are not allowed
1772 // there. Just allow this as a constructor; we'll
1773 // complain about it later.
1774 goto DoneWithDeclSpec;
1775 }
1776
1777 // The user meant this to name a type, but it actually names
1778 // a constructor with some extraneous template
1779 // arguments. Complain, then parse it as a type as the user
1780 // intended.
1781 Diag(TemplateId->TemplateNameLoc,
1782 diag::err_out_of_line_template_id_names_constructor)
1783 << TemplateId->Name;
1784 }
1785
John McCallaa87d332009-12-12 11:40:51 +00001786 DS.getTypeSpecScope() = SS;
1787 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001788 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001789 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001790 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001791 continue;
1792 }
1793
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001794 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001795 DS.getTypeSpecScope() = SS;
1796 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001797 if (Tok.getAnnotationValue()) {
1798 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1800 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001801 PrevSpec, DiagID, T);
1802 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001803 else
1804 DS.SetTypeSpecError();
1805 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1806 ConsumeToken(); // The typename
1807 }
1808
Douglas Gregor9135c722009-03-25 15:40:00 +00001809 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001810 goto DoneWithDeclSpec;
1811
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001812 // If we're in a context where the identifier could be a class name,
1813 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001814 if ((DSContext == DSC_top_level ||
1815 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001816 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001817 &SS)) {
1818 if (isConstructorDeclarator())
1819 goto DoneWithDeclSpec;
1820
1821 // As noted in C++ [class.qual]p2 (cited above), when the name
1822 // of the class is qualified in a context where it could name
1823 // a constructor, its a constructor name. However, we've
1824 // looked at the declarator, and the user probably meant this
1825 // to be a type. Complain that it isn't supposed to be treated
1826 // as a type, then proceed to parse it as a type.
1827 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1828 << Next.getIdentifierInfo();
1829 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001830
John McCallb3d87482010-08-24 05:47:05 +00001831 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1832 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001833 getCurScope(), &SS,
1834 false, false, ParsedType(),
1835 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001836
Chris Lattnerf4382f52009-04-14 22:17:06 +00001837 // If the referenced identifier is not a type, then this declspec is
1838 // erroneous: We already checked about that it has no type specifier, and
1839 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001840 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001841 if (TypeRep == 0) {
1842 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001843 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001844 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
John McCallaa87d332009-12-12 11:40:51 +00001847 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001848 ConsumeToken(); // The C++ scope.
1849
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001850 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001851 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001852 if (isInvalid)
1853 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001855 DS.SetRangeEnd(Tok.getLocation());
1856 ConsumeToken(); // The typename.
1857
1858 continue;
1859 }
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Chris Lattner80d0c892009-01-21 19:48:37 +00001861 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001862 if (Tok.getAnnotationValue()) {
1863 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001864 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001865 DiagID, T);
1866 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001867 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001868
1869 if (isInvalid)
1870 break;
1871
Chris Lattner80d0c892009-01-21 19:48:37 +00001872 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1873 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Chris Lattner80d0c892009-01-21 19:48:37 +00001875 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1876 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001877 // Objective-C interface.
1878 if (Tok.is(tok::less) && getLang().ObjC1)
1879 ParseObjCProtocolQualifiers(DS);
1880
Chris Lattner80d0c892009-01-21 19:48:37 +00001881 continue;
1882 }
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Douglas Gregorbfad9152011-04-28 15:48:45 +00001884 case tok::kw___is_signed:
1885 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1886 // typically treats it as a trait. If we see __is_signed as it appears
1887 // in libstdc++, e.g.,
1888 //
1889 // static const bool __is_signed;
1890 //
1891 // then treat __is_signed as an identifier rather than as a keyword.
1892 if (DS.getTypeSpecType() == TST_bool &&
1893 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1894 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1895 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1896 Tok.setKind(tok::identifier);
1897 }
1898
1899 // We're done with the declaration-specifiers.
1900 goto DoneWithDeclSpec;
1901
Chris Lattner3bd934a2008-07-26 01:18:38 +00001902 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001903 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001904 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001905 // In C++, check to see if this is a scope specifier like foo::bar::, if
1906 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001907 if (getLang().CPlusPlus) {
1908 if (TryAnnotateCXXScopeToken(true)) {
1909 if (!DS.hasTypeSpecifier())
1910 DS.SetTypeSpecError();
1911 goto DoneWithDeclSpec;
1912 }
1913 if (!Tok.is(tok::identifier))
1914 continue;
1915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Chris Lattner3bd934a2008-07-26 01:18:38 +00001917 // This identifier can only be a typedef name if we haven't already seen
1918 // a type-specifier. Without this check we misparse:
1919 // typedef int X; struct Y { short X; }; as 'short int'.
1920 if (DS.hasTypeSpecifier())
1921 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001922
John Thompson82287d12010-02-05 00:12:22 +00001923 // Check for need to substitute AltiVec keyword tokens.
1924 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1925 break;
1926
Chris Lattner3bd934a2008-07-26 01:18:38 +00001927 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001928 ParsedType TypeRep =
1929 Actions.getTypeName(*Tok.getIdentifierInfo(),
1930 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001931
Chris Lattnerc199ab32009-04-12 20:42:31 +00001932 // If this is not a typedef name, don't parse it as part of the declspec,
1933 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001934 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001935 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001936 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001937 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001938
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001939 // If we're in a context where the identifier could be a class name,
1940 // check whether this is a constructor declaration.
1941 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001942 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001943 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001944 goto DoneWithDeclSpec;
1945
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001947 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001948 if (isInvalid)
1949 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Chris Lattner3bd934a2008-07-26 01:18:38 +00001951 DS.SetRangeEnd(Tok.getLocation());
1952 ConsumeToken(); // The identifier
1953
1954 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1955 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001956 // Objective-C interface.
1957 if (Tok.is(tok::less) && getLang().ObjC1)
1958 ParseObjCProtocolQualifiers(DS);
1959
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001960 // Need to support trailing type qualifiers (e.g. "id<p> const").
1961 // If a type specifier follows, it will be diagnosed elsewhere.
1962 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001963 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001964
1965 // type-name
1966 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001967 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001968 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001969 // This template-id does not refer to a type name, so we're
1970 // done with the type-specifiers.
1971 goto DoneWithDeclSpec;
1972 }
1973
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001974 // If we're in a context where the template-id could be a
1975 // constructor name or specialization, check whether this is a
1976 // constructor declaration.
1977 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001978 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001979 isConstructorDeclarator())
1980 goto DoneWithDeclSpec;
1981
Douglas Gregor39a8de12009-02-25 19:37:18 +00001982 // Turn the template-id annotation token into a type annotation
1983 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001984 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001985 continue;
1986 }
1987
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 // GNU attributes support.
1989 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001990 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001992
1993 // Microsoft declspec support.
1994 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001995 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001996 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Steve Naroff239f0732008-12-25 14:16:32 +00001998 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001999 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002000 // FIXME: Add handling here!
2001 break;
2002
2003 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002004 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002005 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002006 case tok::kw___cdecl:
2007 case tok::kw___stdcall:
2008 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002009 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002010 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002011 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002012 continue;
2013
Dawn Perchik52fc3142010-09-03 01:29:35 +00002014 // Borland single token adornments.
2015 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002016 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002017 continue;
2018
Peter Collingbournef315fa82011-02-14 01:42:53 +00002019 // OpenCL single token adornments.
2020 case tok::kw___kernel:
2021 ParseOpenCLAttributes(DS.getAttributes());
2022 continue;
2023
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 // storage-class-specifier
2025 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002026 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2027 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 break;
2029 case tok::kw_extern:
2030 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002031 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002032 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2033 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002035 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002036 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2037 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002038 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 case tok::kw_static:
2040 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002041 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002042 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2043 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 break;
2045 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002046 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002047 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002048 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2049 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002050 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002051 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002052 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002053 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2055 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002056 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002057 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2058 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 break;
2060 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002061 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2062 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002064 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002065 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2066 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002067 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Reid Spencer5f016e22007-07-11 17:01:13 +00002072 // function-specifier
2073 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002074 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002076 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002078 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002079 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002081 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002082
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002083 // alignment-specifier
2084 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002085 if (!getLang().C11)
2086 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002087 ParseAlignmentSpecifier(DS.getAttributes());
2088 continue;
2089
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002090 // friend
2091 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002092 if (DSContext == DSC_class)
2093 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2094 else {
2095 PrevSpec = ""; // not actually used by the diagnostic
2096 DiagID = diag::err_friend_invalid_in_context;
2097 isInvalid = true;
2098 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002099 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Douglas Gregor8d267c52011-09-09 02:06:17 +00002101 // Modules
2102 case tok::kw___module_private__:
2103 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2104 break;
2105
Sebastian Redl2ac67232009-11-05 15:47:02 +00002106 // constexpr
2107 case tok::kw_constexpr:
2108 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2109 break;
2110
Chris Lattner80d0c892009-01-21 19:48:37 +00002111 // type-specifier
2112 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002113 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2114 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002115 break;
2116 case tok::kw_long:
2117 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002118 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2119 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002120 else
John McCallfec54012009-08-03 20:12:06 +00002121 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2122 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002123 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002124 case tok::kw___int64:
2125 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2126 DiagID);
2127 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002129 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2130 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002131 break;
2132 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002133 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2134 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002135 break;
2136 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002137 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2138 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002139 break;
2140 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002141 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2142 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002143 break;
2144 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002145 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2146 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002147 break;
2148 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002149 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2150 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002151 break;
2152 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002153 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2154 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002155 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002156 case tok::kw_half:
2157 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2158 DiagID);
2159 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002160 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002161 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2162 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002163 break;
2164 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2166 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002167 break;
2168 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002169 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2170 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002171 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002172 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002173 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2174 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002175 break;
2176 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2178 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002179 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002180 case tok::kw_bool:
2181 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002182 if (Tok.is(tok::kw_bool) &&
2183 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2184 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2185 PrevSpec = ""; // Not used by the diagnostic.
2186 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002187 // For better error recovery.
2188 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002189 isInvalid = true;
2190 } else {
2191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2192 DiagID);
2193 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002194 break;
2195 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002196 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2197 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002198 break;
2199 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002200 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2201 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002202 break;
2203 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002204 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2205 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002206 break;
John Thompson82287d12010-02-05 00:12:22 +00002207 case tok::kw___vector:
2208 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2209 break;
2210 case tok::kw___pixel:
2211 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2212 break;
John McCalla5fc4722011-04-09 22:50:59 +00002213 case tok::kw___unknown_anytype:
2214 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2215 PrevSpec, DiagID);
2216 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002217
2218 // class-specifier:
2219 case tok::kw_class:
2220 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002221 case tok::kw_union: {
2222 tok::TokenKind Kind = Tok.getKind();
2223 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002224 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002225 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002226 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002227
2228 // enum-specifier:
2229 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002230 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002231 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002232 continue;
2233
2234 // cv-qualifier:
2235 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002236 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2237 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002238 break;
2239 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002240 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2241 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002242 break;
2243 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002244 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2245 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002246 break;
2247
Douglas Gregord57959a2009-03-27 23:10:48 +00002248 // C++ typename-specifier:
2249 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002250 if (TryAnnotateTypeOrScopeToken()) {
2251 DS.SetTypeSpecError();
2252 goto DoneWithDeclSpec;
2253 }
2254 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002255 continue;
2256 break;
2257
Chris Lattner80d0c892009-01-21 19:48:37 +00002258 // GNU typeof support.
2259 case tok::kw_typeof:
2260 ParseTypeofSpecifier(DS);
2261 continue;
2262
David Blaikie42d6d0c2011-12-04 05:04:18 +00002263 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002264 ParseDecltypeSpecifier(DS);
2265 continue;
2266
Sean Huntdb5d44b2011-05-19 05:37:45 +00002267 case tok::kw___underlying_type:
2268 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002269 continue;
2270
2271 case tok::kw__Atomic:
2272 ParseAtomicSpecifier(DS);
2273 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002274
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002275 // OpenCL qualifiers:
2276 case tok::kw_private:
2277 if (!getLang().OpenCL)
2278 goto DoneWithDeclSpec;
2279 case tok::kw___private:
2280 case tok::kw___global:
2281 case tok::kw___local:
2282 case tok::kw___constant:
2283 case tok::kw___read_only:
2284 case tok::kw___write_only:
2285 case tok::kw___read_write:
2286 ParseOpenCLQualifiers(DS);
2287 break;
2288
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002289 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002290 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002291 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2292 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002293 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002294 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Douglas Gregor46f936e2010-11-19 17:10:50 +00002296 if (!ParseObjCProtocolQualifiers(DS))
2297 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2298 << FixItHint::CreateInsertion(Loc, "id")
2299 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002300
2301 // Need to support trailing type qualifiers (e.g. "id<p> const").
2302 // If a type specifier follows, it will be diagnosed elsewhere.
2303 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002304 }
John McCallfec54012009-08-03 20:12:06 +00002305 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002306 if (isInvalid) {
2307 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002308 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002309
2310 if (DiagID == diag::ext_duplicate_declspec)
2311 Diag(Tok, DiagID)
2312 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2313 else
2314 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002316
Chris Lattner81c018d2008-03-13 06:29:04 +00002317 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002318 if (DiagID != diag::err_bool_redeclaration)
2319 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002320 }
2321}
Douglas Gregoradcac882008-12-01 23:54:00 +00002322
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002323/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002324/// primarily follow the C++ grammar with additions for C99 and GNU,
2325/// which together subsume the C grammar. Note that the C++
2326/// type-specifier also includes the C type-qualifier (for const,
2327/// volatile, and C99 restrict). Returns true if a type-specifier was
2328/// found (and parsed), false otherwise.
2329///
2330/// type-specifier: [C++ 7.1.5]
2331/// simple-type-specifier
2332/// class-specifier
2333/// enum-specifier
2334/// elaborated-type-specifier [TODO]
2335/// cv-qualifier
2336///
2337/// cv-qualifier: [C++ 7.1.5.1]
2338/// 'const'
2339/// 'volatile'
2340/// [C99] 'restrict'
2341///
2342/// simple-type-specifier: [ C++ 7.1.5.2]
2343/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2344/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2345/// 'char'
2346/// 'wchar_t'
2347/// 'bool'
2348/// 'short'
2349/// 'int'
2350/// 'long'
2351/// 'signed'
2352/// 'unsigned'
2353/// 'float'
2354/// 'double'
2355/// 'void'
2356/// [C99] '_Bool'
2357/// [C99] '_Complex'
2358/// [C99] '_Imaginary' // Removed in TC2?
2359/// [GNU] '_Decimal32'
2360/// [GNU] '_Decimal64'
2361/// [GNU] '_Decimal128'
2362/// [GNU] typeof-specifier
2363/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2364/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002365/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002366/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002367bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002368 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002369 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002370 const ParsedTemplateInfo &TemplateInfo,
2371 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002372 SourceLocation Loc = Tok.getLocation();
2373
2374 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002375 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002376 // If we already have a type specifier, this identifier is not a type.
2377 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2378 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2379 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2380 return false;
John Thompson82287d12010-02-05 00:12:22 +00002381 // Check for need to substitute AltiVec keyword tokens.
2382 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2383 break;
2384 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002385 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002386 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002387 // Annotate typenames and C++ scope specifiers. If we get one, just
2388 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002389 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2390 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002391 return true;
2392 if (Tok.is(tok::identifier))
2393 return false;
2394 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2395 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002396 case tok::coloncolon: // ::foo::bar
2397 if (NextToken().is(tok::kw_new) || // ::new
2398 NextToken().is(tok::kw_delete)) // ::delete
2399 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Chris Lattner166a8fc2009-01-04 23:41:41 +00002401 // Annotate typenames and C++ scope specifiers. If we get one, just
2402 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002403 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2404 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002405 return true;
2406 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2407 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002408
Douglas Gregor12e083c2008-11-07 15:42:26 +00002409 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002410 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002411 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2413 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002414 DiagID, T);
2415 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002416 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002417 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2418 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Douglas Gregor12e083c2008-11-07 15:42:26 +00002420 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2421 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2422 // Objective-C interface. If we don't have Objective-C or a '<', this is
2423 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002424 if (Tok.is(tok::less) && getLang().ObjC1)
2425 ParseObjCProtocolQualifiers(DS);
2426
Douglas Gregor12e083c2008-11-07 15:42:26 +00002427 return true;
2428 }
2429
2430 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002431 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002432 break;
2433 case tok::kw_long:
2434 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002435 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2436 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002437 else
John McCallfec54012009-08-03 20:12:06 +00002438 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2439 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002440 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002441 case tok::kw___int64:
2442 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2443 DiagID);
2444 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002445 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002447 break;
2448 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002449 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2450 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002451 break;
2452 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002453 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2454 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002455 break;
2456 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002457 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2458 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002459 break;
2460 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 break;
2463 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002465 break;
2466 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002467 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002468 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002469 case tok::kw_half:
2470 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2471 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002472 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002474 break;
2475 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002477 break;
2478 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002480 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002481 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002483 break;
2484 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002486 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002487 case tok::kw_bool:
2488 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002490 break;
2491 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2493 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002494 break;
2495 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2497 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002498 break;
2499 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2501 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002502 break;
John Thompson82287d12010-02-05 00:12:22 +00002503 case tok::kw___vector:
2504 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2505 break;
2506 case tok::kw___pixel:
2507 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2508 break;
2509
Douglas Gregor12e083c2008-11-07 15:42:26 +00002510 // class-specifier:
2511 case tok::kw_class:
2512 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002513 case tok::kw_union: {
2514 tok::TokenKind Kind = Tok.getKind();
2515 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002516 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002517 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002518 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002519 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002520 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002521
2522 // enum-specifier:
2523 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002524 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002525 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002526 return true;
2527
2528 // cv-qualifier:
2529 case tok::kw_const:
2530 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002531 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002532 break;
2533 case tok::kw_volatile:
2534 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002535 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002536 break;
2537 case tok::kw_restrict:
2538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002539 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002540 break;
2541
2542 // GNU typeof support.
2543 case tok::kw_typeof:
2544 ParseTypeofSpecifier(DS);
2545 return true;
2546
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002547 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002548 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002549 ParseDecltypeSpecifier(DS);
2550 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Sean Huntdb5d44b2011-05-19 05:37:45 +00002552 // C++0x type traits support.
2553 case tok::kw___underlying_type:
2554 ParseUnderlyingTypeSpecifier(DS);
2555 return true;
2556
Eli Friedmanb001de72011-10-06 23:00:33 +00002557 case tok::kw__Atomic:
2558 ParseAtomicSpecifier(DS);
2559 return true;
2560
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002561 // OpenCL qualifiers:
2562 case tok::kw_private:
2563 if (!getLang().OpenCL)
2564 return false;
2565 case tok::kw___private:
2566 case tok::kw___global:
2567 case tok::kw___local:
2568 case tok::kw___constant:
2569 case tok::kw___read_only:
2570 case tok::kw___write_only:
2571 case tok::kw___read_write:
2572 ParseOpenCLQualifiers(DS);
2573 break;
2574
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002575 // C++0x auto support.
2576 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002577 // This is only called in situations where a storage-class specifier is
2578 // illegal, so we can assume an auto type specifier was intended even in
2579 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2580 // extension diagnostic.
2581 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002582 return false;
2583
John McCallfec54012009-08-03 20:12:06 +00002584 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002585 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002586
Eli Friedman290eeb02009-06-08 23:27:34 +00002587 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002588 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002589 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002590 case tok::kw___cdecl:
2591 case tok::kw___stdcall:
2592 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002593 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002594 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002595 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002596 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002597
Dawn Perchik52fc3142010-09-03 01:29:35 +00002598 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002599 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002600 return true;
2601
Douglas Gregor12e083c2008-11-07 15:42:26 +00002602 default:
2603 // Not a type-specifier; do nothing.
2604 return false;
2605 }
2606
2607 // If the specifier combination wasn't legal, issue a diagnostic.
2608 if (isInvalid) {
2609 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002610 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002611 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002612 }
2613 DS.SetRangeEnd(Tok.getLocation());
2614 ConsumeToken(); // whatever we parsed above.
2615 return true;
2616}
Reid Spencer5f016e22007-07-11 17:01:13 +00002617
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002618/// ParseStructDeclaration - Parse a struct declaration without the terminating
2619/// semicolon.
2620///
Reid Spencer5f016e22007-07-11 17:01:13 +00002621/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002622/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002623/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002624/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002625/// struct-declarator-list:
2626/// struct-declarator
2627/// struct-declarator-list ',' struct-declarator
2628/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2629/// struct-declarator:
2630/// declarator
2631/// [GNU] declarator attributes[opt]
2632/// declarator[opt] ':' constant-expression
2633/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2634///
Chris Lattnere1359422008-04-10 06:46:29 +00002635void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002636ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002637
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002638 if (Tok.is(tok::kw___extension__)) {
2639 // __extension__ silences extension warnings in the subexpression.
2640 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002641 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002642 return ParseStructDeclaration(DS, Fields);
2643 }
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Steve Naroff28a7ca82007-08-20 22:28:22 +00002645 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002646 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002647
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002648 // If there are no declarators, this is a free-standing declaration
2649 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002650 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002651 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002652 return;
2653 }
2654
2655 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002656 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002658 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002659 FieldDeclarator DeclaratorInfo(DS);
2660
2661 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002662 if (!FirstDeclarator)
2663 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002664
Steve Naroff28a7ca82007-08-20 22:28:22 +00002665 /// struct-declarator: declarator
2666 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002667 if (Tok.isNot(tok::colon)) {
2668 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2669 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002670 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002671 }
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Chris Lattner04d66662007-10-09 17:33:22 +00002673 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002674 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002675 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002676 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002677 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002678 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002679 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002680 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002681
Steve Naroff28a7ca82007-08-20 22:28:22 +00002682 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002683 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002684
John McCallbdd563e2009-11-03 02:38:08 +00002685 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002686 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002687 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002688
Steve Naroff28a7ca82007-08-20 22:28:22 +00002689 // If we don't have a comma, it is either the end of the list (a ';')
2690 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002691 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002692 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002693
Steve Naroff28a7ca82007-08-20 22:28:22 +00002694 // Consume the comma.
2695 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002696
John McCallbdd563e2009-11-03 02:38:08 +00002697 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002698 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002699}
2700
2701/// ParseStructUnionBody
2702/// struct-contents:
2703/// struct-declaration-list
2704/// [EXT] empty
2705/// [GNU] "struct-declaration-list" without terminatoring ';'
2706/// struct-declaration-list:
2707/// struct-declaration
2708/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002709/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002710///
Reid Spencer5f016e22007-07-11 17:01:13 +00002711void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002712 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002713 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2714 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002716 BalancedDelimiterTracker T(*this, tok::l_brace);
2717 if (T.consumeOpen())
2718 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002720 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002721 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002722
Reid Spencer5f016e22007-07-11 17:01:13 +00002723 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2724 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002725 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2726 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2727 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2728 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002729
Chris Lattner5f9e2722011-07-23 10:55:15 +00002730 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002731
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002733 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Reid Spencer5f016e22007-07-11 17:01:13 +00002736 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002737 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002738 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002739 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002740 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 ConsumeToken();
2742 continue;
2743 }
Chris Lattnere1359422008-04-10 06:46:29 +00002744
2745 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002746 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002747
John McCallbdd563e2009-11-03 02:38:08 +00002748 if (!Tok.is(tok::at)) {
2749 struct CFieldCallback : FieldCallback {
2750 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002751 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002752 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002753
John McCalld226f652010-08-21 09:40:31 +00002754 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002755 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002756 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2757
John McCalld226f652010-08-21 09:40:31 +00002758 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002759 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002760 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002761 FD.D.getDeclSpec().getSourceRange().getBegin(),
2762 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002763 FieldDecls.push_back(Field);
2764 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002765 }
John McCallbdd563e2009-11-03 02:38:08 +00002766 } Callback(*this, TagDecl, FieldDecls);
2767
2768 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002769 } else { // Handle @defs
2770 ConsumeToken();
2771 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2772 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002773 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002774 continue;
2775 }
2776 ConsumeToken();
2777 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2778 if (!Tok.is(tok::identifier)) {
2779 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002780 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002781 continue;
2782 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002783 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002784 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002785 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002786 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2787 ConsumeToken();
2788 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002789 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002790
Chris Lattner04d66662007-10-09 17:33:22 +00002791 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002792 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002793 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002794 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002795 break;
2796 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002797 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2798 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002799 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002800 // If we stopped at a ';', eat it.
2801 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002802 }
2803 }
Mike Stump1eb44332009-09-09 15:08:12 +00002804
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002805 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002806
John McCall0b7e6782011-03-24 11:26:52 +00002807 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002808 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002809 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002810
Douglas Gregor23c94db2010-07-02 17:43:08 +00002811 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002812 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002813 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002814 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002815 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002816 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2817 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002818}
2819
Reid Spencer5f016e22007-07-11 17:01:13 +00002820/// ParseEnumSpecifier
2821/// enum-specifier: [C99 6.7.2.2]
2822/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002823///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002824/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2825/// '}' attributes[opt]
2826/// 'enum' identifier
2827/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002828///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002829/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2830/// [C++0x] enum-head '{' enumerator-list ',' '}'
2831///
2832/// enum-head: [C++0x]
2833/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2834/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2835///
2836/// enum-key: [C++0x]
2837/// 'enum'
2838/// 'enum' 'class'
2839/// 'enum' 'struct'
2840///
2841/// enum-base: [C++0x]
2842/// ':' type-specifier-seq
2843///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002844/// [C++] elaborated-type-specifier:
2845/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2846///
Chris Lattner4c97d762009-04-12 21:49:30 +00002847void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002848 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002849 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002850 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002851 if (Tok.is(tok::code_completion)) {
2852 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002853 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002854 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002855 }
John McCall57c13002011-07-06 05:58:41 +00002856
2857 bool IsScopedEnum = false;
2858 bool IsScopedUsingClassTag = false;
2859
2860 if (getLang().CPlusPlus0x &&
2861 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002862 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002863 IsScopedEnum = true;
2864 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2865 ConsumeToken();
2866 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002867
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002868 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002869 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002870 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002871
Douglas Gregor5471bc82011-09-08 17:18:35 +00002872 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002873 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002874
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002875 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002876 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002877 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2878 // if a fixed underlying type is allowed.
2879 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2880
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002881 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2882 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002883 return;
2884
2885 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002886 Diag(Tok, diag::err_expected_ident);
2887 if (Tok.isNot(tok::l_brace)) {
2888 // Has no name and is not a definition.
2889 // Skip the rest of this declarator, up until the comma or semicolon.
2890 SkipUntil(tok::comma, true);
2891 return;
2892 }
2893 }
2894 }
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002896 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002897 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2898 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002899 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002900
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002901 // Skip the rest of this declarator, up until the comma or semicolon.
2902 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002903 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002904 }
Mike Stump1eb44332009-09-09 15:08:12 +00002905
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002906 // If an identifier is present, consume and remember it.
2907 IdentifierInfo *Name = 0;
2908 SourceLocation NameLoc;
2909 if (Tok.is(tok::identifier)) {
2910 Name = Tok.getIdentifierInfo();
2911 NameLoc = ConsumeToken();
2912 }
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002914 if (!Name && IsScopedEnum) {
2915 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2916 // declaration of a scoped enumeration.
2917 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2918 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002919 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002920 }
2921
2922 TypeResult BaseType;
2923
Douglas Gregora61b3e72010-12-01 17:42:47 +00002924 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002925 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002926 bool PossibleBitfield = false;
2927 if (getCurScope()->getFlags() & Scope::ClassScope) {
2928 // If we're in class scope, this can either be an enum declaration with
2929 // an underlying type, or a declaration of a bitfield member. We try to
2930 // use a simple disambiguation scheme first to catch the common cases
2931 // (integer literal, sizeof); if it's still ambiguous, we then consider
2932 // anything that's a simple-type-specifier followed by '(' as an
2933 // expression. This suffices because function types are not valid
2934 // underlying types anyway.
2935 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2936 // If the next token starts an expression, we know we're parsing a
2937 // bit-field. This is the common case.
2938 if (TPR == TPResult::True())
2939 PossibleBitfield = true;
2940 // If the next token starts a type-specifier-seq, it may be either a
2941 // a fixed underlying type or the start of a function-style cast in C++;
2942 // lookahead one more token to see if it's obvious that we have a
2943 // fixed underlying type.
2944 else if (TPR == TPResult::False() &&
2945 GetLookAheadToken(2).getKind() == tok::semi) {
2946 // Consume the ':'.
2947 ConsumeToken();
2948 } else {
2949 // We have the start of a type-specifier-seq, so we have to perform
2950 // tentative parsing to determine whether we have an expression or a
2951 // type.
2952 TentativeParsingAction TPA(*this);
2953
2954 // Consume the ':'.
2955 ConsumeToken();
2956
Douglas Gregor86f208c2011-02-22 20:32:04 +00002957 if ((getLang().CPlusPlus &&
2958 isCXXDeclarationSpecifier() != TPResult::True()) ||
2959 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002960 // We'll parse this as a bitfield later.
2961 PossibleBitfield = true;
2962 TPA.Revert();
2963 } else {
2964 // We have a type-specifier-seq.
2965 TPA.Commit();
2966 }
2967 }
2968 } else {
2969 // Consume the ':'.
2970 ConsumeToken();
2971 }
2972
2973 if (!PossibleBitfield) {
2974 SourceRange Range;
2975 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002976
Douglas Gregor5471bc82011-09-08 17:18:35 +00002977 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002978 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2979 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002980 if (getLang().CPlusPlus0x)
2981 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002982 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002983 }
2984
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002985 // There are three options here. If we have 'enum foo;', then this is a
2986 // forward declaration. If we have 'enum foo {...' then this is a
2987 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2988 //
2989 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2990 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2991 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2992 //
John McCallf312b1e2010-08-26 23:41:50 +00002993 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002994 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002995 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002996 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002997 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002998 else
John McCallf312b1e2010-08-26 23:41:50 +00002999 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003000
3001 // enums cannot be templates, although they can be referenced from a
3002 // template.
3003 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003004 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003005 Diag(Tok, diag::err_enum_template);
3006
3007 // Skip the rest of this declarator, up until the comma or semicolon.
3008 SkipUntil(tok::comma, true);
3009 return;
3010 }
3011
Douglas Gregorb9075602011-02-22 02:55:24 +00003012 if (!Name && TUK != Sema::TUK_Definition) {
3013 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3014
3015 // Skip the rest of this declarator, up until the comma or semicolon.
3016 SkipUntil(tok::comma, true);
3017 return;
3018 }
3019
Douglas Gregor402abb52009-05-28 23:31:59 +00003020 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003021 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003022 const char *PrevSpec = 0;
3023 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003024 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003025 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003026 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003027 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003028 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003029 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003030
Douglas Gregor48c89f42010-04-24 16:38:41 +00003031 if (IsDependent) {
3032 // This enum has a dependent nested-name-specifier. Handle it as a
3033 // dependent tag.
3034 if (!Name) {
3035 DS.SetTypeSpecError();
3036 Diag(Tok, diag::err_expected_type_name_after_typename);
3037 return;
3038 }
3039
Douglas Gregor23c94db2010-07-02 17:43:08 +00003040 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003041 TUK, SS, Name, StartLoc,
3042 NameLoc);
3043 if (Type.isInvalid()) {
3044 DS.SetTypeSpecError();
3045 return;
3046 }
3047
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003048 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3049 NameLoc.isValid() ? NameLoc : StartLoc,
3050 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003051 Diag(StartLoc, DiagID) << PrevSpec;
3052
3053 return;
3054 }
Mike Stump1eb44332009-09-09 15:08:12 +00003055
John McCalld226f652010-08-21 09:40:31 +00003056 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003057 // The action failed to produce an enumeration tag. If this is a
3058 // definition, consume the entire definition.
3059 if (Tok.is(tok::l_brace)) {
3060 ConsumeBrace();
3061 SkipUntil(tok::r_brace);
3062 }
3063
3064 DS.SetTypeSpecError();
3065 return;
3066 }
3067
Chris Lattner04d66662007-10-09 17:33:22 +00003068 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00003069 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003070
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003071 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3072 NameLoc.isValid() ? NameLoc : StartLoc,
3073 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003074 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003075}
3076
3077/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3078/// enumerator-list:
3079/// enumerator
3080/// enumerator-list ',' enumerator
3081/// enumerator:
3082/// enumeration-constant
3083/// enumeration-constant '=' constant-expression
3084/// enumeration-constant:
3085/// identifier
3086///
John McCalld226f652010-08-21 09:40:31 +00003087void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003088 // Enter the scope of the enum body and start the definition.
3089 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003090 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003091
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003092 BalancedDelimiterTracker T(*this, tok::l_brace);
3093 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Chris Lattner7946dd32007-08-27 17:24:30 +00003095 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003096 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003097 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Chris Lattner5f9e2722011-07-23 10:55:15 +00003099 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003100
John McCalld226f652010-08-21 09:40:31 +00003101 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Reid Spencer5f016e22007-07-11 17:01:13 +00003103 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003104 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003105 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3106 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003107
John McCall5b629aa2010-10-22 23:36:17 +00003108 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003109 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003110 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003111
Reid Spencer5f016e22007-07-11 17:01:13 +00003112 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003113 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003114 ParsingDeclRAIIObject PD(*this);
3115
Chris Lattner04d66662007-10-09 17:33:22 +00003116 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003117 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003118 AssignedVal = ParseConstantExpression();
3119 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003120 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003121 }
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003124 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3125 LastEnumConstDecl,
3126 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003127 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003128 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003129 PD.complete(EnumConstDecl);
3130
Reid Spencer5f016e22007-07-11 17:01:13 +00003131 EnumConstantDecls.push_back(EnumConstDecl);
3132 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003133
Douglas Gregor751f6922010-09-07 14:51:08 +00003134 if (Tok.is(tok::identifier)) {
3135 // We're missing a comma between enumerators.
3136 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3137 Diag(Loc, diag::err_enumerator_list_missing_comma)
3138 << FixItHint::CreateInsertion(Loc, ", ");
3139 continue;
3140 }
3141
Chris Lattner04d66662007-10-09 17:33:22 +00003142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 break;
3144 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Richard Smith7fe62082011-10-15 05:09:34 +00003146 if (Tok.isNot(tok::identifier)) {
3147 if (!getLang().C99 && !getLang().CPlusPlus0x)
3148 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3149 << getLang().CPlusPlus
3150 << FixItHint::CreateRemoval(CommaLoc);
3151 else if (getLang().CPlusPlus0x)
3152 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3153 << FixItHint::CreateRemoval(CommaLoc);
3154 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 }
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003158 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003159
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003161 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003162 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003163
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003164 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3165 EnumDecl, EnumConstantDecls.data(),
3166 EnumConstantDecls.size(), getCurScope(),
3167 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Douglas Gregor72de6672009-01-08 20:45:30 +00003169 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003170 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3171 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003172}
3173
3174/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003175/// start of a type-qualifier-list.
3176bool Parser::isTypeQualifier() const {
3177 switch (Tok.getKind()) {
3178 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003179
3180 // type-qualifier only in OpenCL
3181 case tok::kw_private:
3182 return getLang().OpenCL;
3183
Steve Naroff5f8aa692008-02-11 23:15:56 +00003184 // type-qualifier
3185 case tok::kw_const:
3186 case tok::kw_volatile:
3187 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003188 case tok::kw___private:
3189 case tok::kw___local:
3190 case tok::kw___global:
3191 case tok::kw___constant:
3192 case tok::kw___read_only:
3193 case tok::kw___read_write:
3194 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003195 return true;
3196 }
3197}
3198
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003199/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3200/// is definitely a type-specifier. Return false if it isn't part of a type
3201/// specifier or if we're not sure.
3202bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3203 switch (Tok.getKind()) {
3204 default: return false;
3205 // type-specifiers
3206 case tok::kw_short:
3207 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003208 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003209 case tok::kw_signed:
3210 case tok::kw_unsigned:
3211 case tok::kw__Complex:
3212 case tok::kw__Imaginary:
3213 case tok::kw_void:
3214 case tok::kw_char:
3215 case tok::kw_wchar_t:
3216 case tok::kw_char16_t:
3217 case tok::kw_char32_t:
3218 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003219 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003220 case tok::kw_float:
3221 case tok::kw_double:
3222 case tok::kw_bool:
3223 case tok::kw__Bool:
3224 case tok::kw__Decimal32:
3225 case tok::kw__Decimal64:
3226 case tok::kw__Decimal128:
3227 case tok::kw___vector:
3228
3229 // struct-or-union-specifier (C99) or class-specifier (C++)
3230 case tok::kw_class:
3231 case tok::kw_struct:
3232 case tok::kw_union:
3233 // enum-specifier
3234 case tok::kw_enum:
3235
3236 // typedef-name
3237 case tok::annot_typename:
3238 return true;
3239 }
3240}
3241
Steve Naroff5f8aa692008-02-11 23:15:56 +00003242/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003243/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003244bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003245 switch (Tok.getKind()) {
3246 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Chris Lattner166a8fc2009-01-04 23:41:41 +00003248 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003249 if (TryAltiVecVectorToken())
3250 return true;
3251 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003252 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003253 // Annotate typenames and C++ scope specifiers. If we get one, just
3254 // recurse to handle whatever we get.
3255 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003256 return true;
3257 if (Tok.is(tok::identifier))
3258 return false;
3259 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003260
Chris Lattner166a8fc2009-01-04 23:41:41 +00003261 case tok::coloncolon: // ::foo::bar
3262 if (NextToken().is(tok::kw_new) || // ::new
3263 NextToken().is(tok::kw_delete)) // ::delete
3264 return false;
3265
Chris Lattner166a8fc2009-01-04 23:41:41 +00003266 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003267 return true;
3268 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003269
Reid Spencer5f016e22007-07-11 17:01:13 +00003270 // GNU attributes support.
3271 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003272 // GNU typeof support.
3273 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Reid Spencer5f016e22007-07-11 17:01:13 +00003275 // type-specifiers
3276 case tok::kw_short:
3277 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003278 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003279 case tok::kw_signed:
3280 case tok::kw_unsigned:
3281 case tok::kw__Complex:
3282 case tok::kw__Imaginary:
3283 case tok::kw_void:
3284 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003285 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003286 case tok::kw_char16_t:
3287 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003289 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 case tok::kw_float:
3291 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003292 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 case tok::kw__Bool:
3294 case tok::kw__Decimal32:
3295 case tok::kw__Decimal64:
3296 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003297 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Chris Lattner99dc9142008-04-13 18:59:07 +00003299 // struct-or-union-specifier (C99) or class-specifier (C++)
3300 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 case tok::kw_struct:
3302 case tok::kw_union:
3303 // enum-specifier
3304 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003305
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 // type-qualifier
3307 case tok::kw_const:
3308 case tok::kw_volatile:
3309 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003310
3311 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003312 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003314
Chris Lattner7c186be2008-10-20 00:25:30 +00003315 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3316 case tok::less:
3317 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Steve Naroff239f0732008-12-25 14:16:32 +00003319 case tok::kw___cdecl:
3320 case tok::kw___stdcall:
3321 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003322 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003323 case tok::kw___w64:
3324 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003325 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003326 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003327 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003328
3329 case tok::kw___private:
3330 case tok::kw___local:
3331 case tok::kw___global:
3332 case tok::kw___constant:
3333 case tok::kw___read_only:
3334 case tok::kw___read_write:
3335 case tok::kw___write_only:
3336
Eli Friedman290eeb02009-06-08 23:27:34 +00003337 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003338
3339 case tok::kw_private:
3340 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003341
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003342 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003343 case tok::kw__Atomic:
3344 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 }
3346}
3347
3348/// isDeclarationSpecifier() - Return true if the current token is part of a
3349/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003350///
3351/// \param DisambiguatingWithExpression True to indicate that the purpose of
3352/// this check is to disambiguate between an expression and a declaration.
3353bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003354 switch (Tok.getKind()) {
3355 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003357 case tok::kw_private:
3358 return getLang().OpenCL;
3359
Chris Lattner166a8fc2009-01-04 23:41:41 +00003360 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003361 // Unfortunate hack to support "Class.factoryMethod" notation.
3362 if (getLang().ObjC1 && NextToken().is(tok::period))
3363 return false;
John Thompson82287d12010-02-05 00:12:22 +00003364 if (TryAltiVecVectorToken())
3365 return true;
3366 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003367 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003368 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003369 // Annotate typenames and C++ scope specifiers. If we get one, just
3370 // recurse to handle whatever we get.
3371 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003372 return true;
3373 if (Tok.is(tok::identifier))
3374 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003375
3376 // If we're in Objective-C and we have an Objective-C class type followed
3377 // by an identifier and then either ':' or ']', in a place where an
3378 // expression is permitted, then this is probably a class message send
3379 // missing the initial '['. In this case, we won't consider this to be
3380 // the start of a declaration.
3381 if (DisambiguatingWithExpression &&
3382 isStartOfObjCClassMessageMissingOpenBracket())
3383 return false;
3384
John McCall9ba61662010-02-26 08:45:28 +00003385 return isDeclarationSpecifier();
3386
Chris Lattner166a8fc2009-01-04 23:41:41 +00003387 case tok::coloncolon: // ::foo::bar
3388 if (NextToken().is(tok::kw_new) || // ::new
3389 NextToken().is(tok::kw_delete)) // ::delete
3390 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003391
Chris Lattner166a8fc2009-01-04 23:41:41 +00003392 // Annotate typenames and C++ scope specifiers. If we get one, just
3393 // recurse to handle whatever we get.
3394 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003395 return true;
3396 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003397
Reid Spencer5f016e22007-07-11 17:01:13 +00003398 // storage-class-specifier
3399 case tok::kw_typedef:
3400 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003401 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003402 case tok::kw_static:
3403 case tok::kw_auto:
3404 case tok::kw_register:
3405 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003406
Douglas Gregor8d267c52011-09-09 02:06:17 +00003407 // Modules
3408 case tok::kw___module_private__:
3409
Reid Spencer5f016e22007-07-11 17:01:13 +00003410 // type-specifiers
3411 case tok::kw_short:
3412 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003413 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003414 case tok::kw_signed:
3415 case tok::kw_unsigned:
3416 case tok::kw__Complex:
3417 case tok::kw__Imaginary:
3418 case tok::kw_void:
3419 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003420 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003421 case tok::kw_char16_t:
3422 case tok::kw_char32_t:
3423
Reid Spencer5f016e22007-07-11 17:01:13 +00003424 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003425 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003426 case tok::kw_float:
3427 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003428 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003429 case tok::kw__Bool:
3430 case tok::kw__Decimal32:
3431 case tok::kw__Decimal64:
3432 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003433 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003434
Chris Lattner99dc9142008-04-13 18:59:07 +00003435 // struct-or-union-specifier (C99) or class-specifier (C++)
3436 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 case tok::kw_struct:
3438 case tok::kw_union:
3439 // enum-specifier
3440 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003441
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 // type-qualifier
3443 case tok::kw_const:
3444 case tok::kw_volatile:
3445 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003446
Reid Spencer5f016e22007-07-11 17:01:13 +00003447 // function-specifier
3448 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003449 case tok::kw_virtual:
3450 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003451
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003452 // static_assert-declaration
3453 case tok::kw__Static_assert:
3454
Chris Lattner1ef08762007-08-09 17:01:07 +00003455 // GNU typeof support.
3456 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Chris Lattner1ef08762007-08-09 17:01:07 +00003458 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003459 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003461
Francois Pichete3d49b42011-06-19 08:02:06 +00003462 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003463 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003464 return true;
3465
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003466 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003467 case tok::kw__Atomic:
3468 return true;
3469
Chris Lattnerf3948c42008-07-26 03:38:44 +00003470 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3471 case tok::less:
3472 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003473
Douglas Gregord9d75e52011-04-27 05:41:15 +00003474 // typedef-name
3475 case tok::annot_typename:
3476 return !DisambiguatingWithExpression ||
3477 !isStartOfObjCClassMessageMissingOpenBracket();
3478
Steve Naroff47f52092009-01-06 19:34:12 +00003479 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003480 case tok::kw___cdecl:
3481 case tok::kw___stdcall:
3482 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003483 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003484 case tok::kw___w64:
3485 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003486 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003487 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003488 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003489 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003490
3491 case tok::kw___private:
3492 case tok::kw___local:
3493 case tok::kw___global:
3494 case tok::kw___constant:
3495 case tok::kw___read_only:
3496 case tok::kw___read_write:
3497 case tok::kw___write_only:
3498
Eli Friedman290eeb02009-06-08 23:27:34 +00003499 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 }
3501}
3502
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003503bool Parser::isConstructorDeclarator() {
3504 TentativeParsingAction TPA(*this);
3505
3506 // Parse the C++ scope specifier.
3507 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003508 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3509 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003510 TPA.Revert();
3511 return false;
3512 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003513
3514 // Parse the constructor name.
3515 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3516 // We already know that we have a constructor name; just consume
3517 // the token.
3518 ConsumeToken();
3519 } else {
3520 TPA.Revert();
3521 return false;
3522 }
3523
3524 // Current class name must be followed by a left parentheses.
3525 if (Tok.isNot(tok::l_paren)) {
3526 TPA.Revert();
3527 return false;
3528 }
3529 ConsumeParen();
3530
3531 // A right parentheses or ellipsis signals that we have a constructor.
3532 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3533 TPA.Revert();
3534 return true;
3535 }
3536
3537 // If we need to, enter the specified scope.
3538 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003539 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003540 DeclScopeObj.EnterDeclaratorScope();
3541
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003542 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003543 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003544 MaybeParseMicrosoftAttributes(Attrs);
3545
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003546 // Check whether the next token(s) are part of a declaration
3547 // specifier, in which case we have the start of a parameter and,
3548 // therefore, we know that this is a constructor.
3549 bool IsConstructor = isDeclarationSpecifier();
3550 TPA.Revert();
3551 return IsConstructor;
3552}
Reid Spencer5f016e22007-07-11 17:01:13 +00003553
3554/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003555/// type-qualifier-list: [C99 6.7.5]
3556/// type-qualifier
3557/// [vendor] attributes
3558/// [ only if VendorAttributesAllowed=true ]
3559/// type-qualifier-list type-qualifier
3560/// [vendor] type-qualifier-list attributes
3561/// [ only if VendorAttributesAllowed=true ]
3562/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3563/// [ only if CXX0XAttributesAllowed=true ]
3564/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003565///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003566void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3567 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003568 bool CXX0XAttributesAllowed) {
3569 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3570 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003571 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003572 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003573 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003574 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003575 else
3576 Diag(Loc, diag::err_attributes_not_allowed);
3577 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003578
3579 SourceLocation EndLoc;
3580
Reid Spencer5f016e22007-07-11 17:01:13 +00003581 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003582 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003584 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003585 SourceLocation Loc = Tok.getLocation();
3586
3587 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003588 case tok::code_completion:
3589 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003590 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003591
Reid Spencer5f016e22007-07-11 17:01:13 +00003592 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003593 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3594 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003595 break;
3596 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003597 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3598 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003599 break;
3600 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003601 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3602 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003603 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003604
3605 // OpenCL qualifiers:
3606 case tok::kw_private:
3607 if (!getLang().OpenCL)
3608 goto DoneWithTypeQuals;
3609 case tok::kw___private:
3610 case tok::kw___global:
3611 case tok::kw___local:
3612 case tok::kw___constant:
3613 case tok::kw___read_only:
3614 case tok::kw___write_only:
3615 case tok::kw___read_write:
3616 ParseOpenCLQualifiers(DS);
3617 break;
3618
Eli Friedman290eeb02009-06-08 23:27:34 +00003619 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003620 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003621 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003622 case tok::kw___cdecl:
3623 case tok::kw___stdcall:
3624 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003625 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003626 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003627 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003628 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003629 continue;
3630 }
3631 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003632 case tok::kw___pascal:
3633 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003634 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003635 continue;
3636 }
3637 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003638 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003639 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003640 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003641 continue; // do *not* consume the next token!
3642 }
3643 // otherwise, FALL THROUGH!
3644 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003645 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003646 // If this is not a type-qualifier token, we're done reading type
3647 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003648 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003649 if (EndLoc.isValid())
3650 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003651 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003652 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003653
Reid Spencer5f016e22007-07-11 17:01:13 +00003654 // If the specifier combination wasn't legal, issue a diagnostic.
3655 if (isInvalid) {
3656 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003657 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003658 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003659 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 }
3661}
3662
3663
3664/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3665///
3666void Parser::ParseDeclarator(Declarator &D) {
3667 /// This implements the 'declarator' production in the C grammar, then checks
3668 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003669 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003670}
3671
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003672/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3673/// is parsed by the function passed to it. Pass null, and the direct-declarator
3674/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003675/// ptr-operator production.
3676///
Richard Smith0706df42011-10-19 21:33:05 +00003677/// If the grammar of this construct is extended, matching changes must also be
3678/// made to TryParseDeclarator and MightBeDeclarator.
3679///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003680/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3681/// [C] pointer[opt] direct-declarator
3682/// [C++] direct-declarator
3683/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003684///
3685/// pointer: [C99 6.7.5]
3686/// '*' type-qualifier-list[opt]
3687/// '*' type-qualifier-list[opt] pointer
3688///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003689/// ptr-operator:
3690/// '*' cv-qualifier-seq[opt]
3691/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003692/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003693/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003694/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003695/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003696void Parser::ParseDeclaratorInternal(Declarator &D,
3697 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003698 if (Diags.hasAllExtensionsSilenced())
3699 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003700
Sebastian Redlf30208a2009-01-24 21:16:55 +00003701 // C++ member pointers start with a '::' or a nested-name.
3702 // Member pointers get special handling, since there's no place for the
3703 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003704 if (getLang().CPlusPlus &&
3705 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3706 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003707 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3708 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003709 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003710 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003711
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003712 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003713 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003714 // The scope spec really belongs to the direct-declarator.
3715 D.getCXXScopeSpec() = SS;
3716 if (DirectDeclParser)
3717 (this->*DirectDeclParser)(D);
3718 return;
3719 }
3720
3721 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003722 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003723 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003724 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003725 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003726
3727 // Recurse to parse whatever is left.
3728 ParseDeclaratorInternal(D, DirectDeclParser);
3729
3730 // Sema will have to catch (syntactically invalid) pointers into global
3731 // scope. It has to catch pointers into namespace scope anyway.
3732 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003733 Loc),
3734 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003735 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003736 return;
3737 }
3738 }
3739
3740 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003741 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003742 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003743 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003744 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003745 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003746 if (DirectDeclParser)
3747 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003748 return;
3749 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003750
Sebastian Redl05532f22009-03-15 22:02:01 +00003751 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3752 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003753 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003754 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003755
Chris Lattner9af55002009-03-27 04:18:06 +00003756 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003757 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003758 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003759
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003761 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003762
Reid Spencer5f016e22007-07-11 17:01:13 +00003763 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003764 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003765 if (Kind == tok::star)
3766 // Remember that we parsed a pointer type, and remember the type-quals.
3767 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003768 DS.getConstSpecLoc(),
3769 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003770 DS.getRestrictSpecLoc()),
3771 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003772 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003773 else
3774 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003775 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003776 Loc),
3777 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003778 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003779 } else {
3780 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003781 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003782
Sebastian Redl743de1f2009-03-23 00:00:23 +00003783 // Complain about rvalue references in C++03, but then go on and build
3784 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003785 if (Kind == tok::ampamp)
3786 Diag(Loc, getLang().CPlusPlus0x ?
3787 diag::warn_cxx98_compat_rvalue_reference :
3788 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003789
Reid Spencer5f016e22007-07-11 17:01:13 +00003790 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3791 // cv-qualifiers are introduced through the use of a typedef or of a
3792 // template type argument, in which case the cv-qualifiers are ignored.
3793 //
3794 // [GNU] Retricted references are allowed.
3795 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003796 // [C++0x] Attributes on references are not allowed.
3797 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003798 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003799
3800 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3801 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3802 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003803 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003804 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3805 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003806 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003807 }
3808
3809 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003810 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003811
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003812 if (D.getNumTypeObjects() > 0) {
3813 // C++ [dcl.ref]p4: There shall be no references to references.
3814 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3815 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003816 if (const IdentifierInfo *II = D.getIdentifier())
3817 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3818 << II;
3819 else
3820 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3821 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003822
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003823 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003824 // can go ahead and build the (technically ill-formed)
3825 // declarator: reference collapsing will take care of it.
3826 }
3827 }
3828
Reid Spencer5f016e22007-07-11 17:01:13 +00003829 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003830 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003831 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003832 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003833 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003834 }
3835}
3836
3837/// ParseDirectDeclarator
3838/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003839/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003840/// '(' declarator ')'
3841/// [GNU] '(' attributes declarator ')'
3842/// [C90] direct-declarator '[' constant-expression[opt] ']'
3843/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3844/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3845/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3846/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3847/// direct-declarator '(' parameter-type-list ')'
3848/// direct-declarator '(' identifier-list[opt] ')'
3849/// [GNU] direct-declarator '(' parameter-forward-declarations
3850/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003851/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3852/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003853/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003854///
3855/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003856/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003857/// '::'[opt] nested-name-specifier[opt] type-name
3858///
3859/// id-expression: [C++ 5.1]
3860/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003861/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003862///
3863/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003864/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003865/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003866/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003867/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003868/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003869///
Reid Spencer5f016e22007-07-11 17:01:13 +00003870void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003871 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003872
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003873 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3874 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003875 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003876 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3877 D.getContext() == Declarator::MemberContext;
3878 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3879 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003880 }
3881
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003882 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003883 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003884 // Change the declaration context for name lookup, until this function
3885 // is exited (and the declarator has been parsed).
3886 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003887 }
3888
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003889 // C++0x [dcl.fct]p14:
3890 // There is a syntactic ambiguity when an ellipsis occurs at the end
3891 // of a parameter-declaration-clause without a preceding comma. In
3892 // this case, the ellipsis is parsed as part of the
3893 // abstract-declarator if the type of the parameter names a template
3894 // parameter pack that has not been expanded; otherwise, it is parsed
3895 // as part of the parameter-declaration-clause.
3896 if (Tok.is(tok::ellipsis) &&
3897 !((D.getContext() == Declarator::PrototypeContext ||
3898 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003899 NextToken().is(tok::r_paren) &&
3900 !Actions.containsUnexpandedParameterPacks(D)))
3901 D.setEllipsisLoc(ConsumeToken());
3902
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003903 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3904 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3905 // We found something that indicates the start of an unqualified-id.
3906 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003907 bool AllowConstructorName;
3908 if (D.getDeclSpec().hasTypeSpecifier())
3909 AllowConstructorName = false;
3910 else if (D.getCXXScopeSpec().isSet())
3911 AllowConstructorName =
3912 (D.getContext() == Declarator::FileContext ||
3913 (D.getContext() == Declarator::MemberContext &&
3914 D.getDeclSpec().isFriendSpecified()));
3915 else
3916 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3917
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003918 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3919 /*EnteringContext=*/true,
3920 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003921 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003922 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003923 D.getName()) ||
3924 // Once we're past the identifier, if the scope was bad, mark the
3925 // whole declarator bad.
3926 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003927 D.SetIdentifier(0, Tok.getLocation());
3928 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003929 } else {
3930 // Parsed the unqualified-id; update range information and move along.
3931 if (D.getSourceRange().getBegin().isInvalid())
3932 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3933 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003934 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003935 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003936 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003937 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003938 assert(!getLang().CPlusPlus &&
3939 "There's a C++-specific check for tok::identifier above");
3940 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3941 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3942 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003943 goto PastIdentifier;
3944 }
3945
3946 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003947 // direct-declarator: '(' declarator ')'
3948 // direct-declarator: '(' attributes declarator ')'
3949 // Example: 'char (*X)' or 'int (*XX)(void)'
3950 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003951
3952 // If the declarator was parenthesized, we entered the declarator
3953 // scope when parsing the parenthesized declarator, then exited
3954 // the scope already. Re-enter the scope, if we need to.
3955 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003956 // If there was an error parsing parenthesized declarator, declarator
3957 // scope may have been enterred before. Don't do it again.
3958 if (!D.isInvalidType() &&
3959 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003960 // Change the declaration context for name lookup, until this function
3961 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003962 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003963 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003964 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003965 // This could be something simple like "int" (in which case the declarator
3966 // portion is empty), if an abstract-declarator is allowed.
3967 D.SetIdentifier(0, Tok.getLocation());
3968 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003969 if (D.getContext() == Declarator::MemberContext)
3970 Diag(Tok, diag::err_expected_member_name_or_semi)
3971 << D.getDeclSpec().getSourceRange();
3972 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003973 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003974 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003975 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003976 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003977 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003978 }
Mike Stump1eb44332009-09-09 15:08:12 +00003979
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003980 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003981 assert(D.isPastIdentifier() &&
3982 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003983
Sean Huntbbd37c62009-11-21 08:43:09 +00003984 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003985 if (D.getIdentifier())
3986 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003987
Reid Spencer5f016e22007-07-11 17:01:13 +00003988 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003989 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003990 // Enter function-declaration scope, limiting any declarators to the
3991 // function prototype scope, including parameter declarators.
3992 ParseScope PrototypeScope(this,
3993 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003994 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3995 // In such a case, check if we actually have a function declarator; if it
3996 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003997 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3998 // When not in file scope, warn for ambiguous function declarators, just
3999 // in case the author intended it as a variable definition.
4000 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4001 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4002 break;
4003 }
John McCall0b7e6782011-03-24 11:26:52 +00004004 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004005 BalancedDelimiterTracker T(*this, tok::l_paren);
4006 T.consumeOpen();
4007 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004008 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004009 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004010 ParseBracketDeclarator(D);
4011 } else {
4012 break;
4013 }
4014 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004015}
Reid Spencer5f016e22007-07-11 17:01:13 +00004016
Chris Lattneref4715c2008-04-06 05:45:57 +00004017/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4018/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004019/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004020/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4021///
4022/// direct-declarator:
4023/// '(' declarator ')'
4024/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004025/// direct-declarator '(' parameter-type-list ')'
4026/// direct-declarator '(' identifier-list[opt] ')'
4027/// [GNU] direct-declarator '(' parameter-forward-declarations
4028/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004029///
4030void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004031 BalancedDelimiterTracker T(*this, tok::l_paren);
4032 T.consumeOpen();
4033
Chris Lattneref4715c2008-04-06 05:45:57 +00004034 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004035
Chris Lattner7399ee02008-10-20 02:05:46 +00004036 // Eat any attributes before we look at whether this is a grouping or function
4037 // declarator paren. If this is a grouping paren, the attribute applies to
4038 // the type being built up, for example:
4039 // int (__attribute__(()) *x)(long y)
4040 // If this ends up not being a grouping paren, the attribute applies to the
4041 // first argument, for example:
4042 // int (__attribute__(()) int x)
4043 // In either case, we need to eat any attributes to be able to determine what
4044 // sort of paren this is.
4045 //
John McCall0b7e6782011-03-24 11:26:52 +00004046 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004047 bool RequiresArg = false;
4048 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004049 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004050
Chris Lattner7399ee02008-10-20 02:05:46 +00004051 // We require that the argument list (if this is a non-grouping paren) be
4052 // present even if the attribute list was empty.
4053 RequiresArg = true;
4054 }
Steve Naroff239f0732008-12-25 14:16:32 +00004055 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004056 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004057 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004058 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004059 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004060 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004061 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004062 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004063 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004064 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004065
Chris Lattneref4715c2008-04-06 05:45:57 +00004066 // If we haven't past the identifier yet (or where the identifier would be
4067 // stored, if this is an abstract declarator), then this is probably just
4068 // grouping parens. However, if this could be an abstract-declarator, then
4069 // this could also be the start of function arguments (consider 'void()').
4070 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Chris Lattneref4715c2008-04-06 05:45:57 +00004072 if (!D.mayOmitIdentifier()) {
4073 // If this can't be an abstract-declarator, this *must* be a grouping
4074 // paren, because we haven't seen the identifier yet.
4075 isGrouping = true;
4076 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004077 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004078 isDeclarationSpecifier()) { // 'int(int)' is a function.
4079 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4080 // considered to be a type, not a K&R identifier-list.
4081 isGrouping = false;
4082 } else {
4083 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4084 isGrouping = true;
4085 }
Mike Stump1eb44332009-09-09 15:08:12 +00004086
Chris Lattneref4715c2008-04-06 05:45:57 +00004087 // If this is a grouping paren, handle:
4088 // direct-declarator: '(' declarator ')'
4089 // direct-declarator: '(' attributes declarator ')'
4090 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004091 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004092 D.setGroupingParens(true);
4093
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004094 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004095 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004096 T.consumeClose();
4097 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4098 T.getCloseLocation()),
4099 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004100
4101 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004102 return;
4103 }
Mike Stump1eb44332009-09-09 15:08:12 +00004104
Chris Lattneref4715c2008-04-06 05:45:57 +00004105 // Okay, if this wasn't a grouping paren, it must be the start of a function
4106 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004107 // identifier (and remember where it would have been), then call into
4108 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004109 D.SetIdentifier(0, Tok.getLocation());
4110
David Blaikie42d6d0c2011-12-04 05:04:18 +00004111 // Enter function-declaration scope, limiting any declarators to the
4112 // function prototype scope, including parameter declarators.
4113 ParseScope PrototypeScope(this,
4114 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004115 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004116 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004117}
4118
4119/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4120/// declarator D up to a paren, which indicates that we are parsing function
4121/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004122///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004123/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004124/// after the open paren - they should be considered to be the first argument of
4125/// a parameter. If RequiresArg is true, then the first argument of the
4126/// function is required to be present and required to not be an identifier
4127/// list.
4128///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004129/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4130/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4131/// (C++0x) trailing-return-type[opt].
4132///
4133/// [C++0x] exception-specification:
4134/// dynamic-exception-specification
4135/// noexcept-specification
4136///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004137void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004138 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004139 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004140 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004141 assert(getCurScope()->isFunctionPrototypeScope() &&
4142 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004143 // lparen is already consumed!
4144 assert(D.isPastIdentifier() && "Should not call before identifier!");
4145
4146 // This should be true when the function has typed arguments.
4147 // Otherwise, it is treated as a K&R-style function.
4148 bool HasProto = false;
4149 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004150 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 // Remember where we see an ellipsis, if any.
4152 SourceLocation EllipsisLoc;
4153
4154 DeclSpec DS(AttrFactory);
4155 bool RefQualifierIsLValueRef = true;
4156 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004157 SourceLocation ConstQualifierLoc;
4158 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004159 ExceptionSpecificationType ESpecType = EST_None;
4160 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004161 SmallVector<ParsedType, 2> DynamicExceptions;
4162 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004163 ExprResult NoexceptExpr;
4164 ParsedType TrailingReturnType;
4165
4166 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004167 if (isFunctionDeclaratorIdentifierList()) {
4168 if (RequiresArg)
4169 Diag(Tok, diag::err_argument_required_after_attribute);
4170
4171 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4172
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004173 Tracker.consumeClose();
4174 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004175 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004176 if (Tok.isNot(tok::r_paren))
4177 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4178 else if (RequiresArg)
4179 Diag(Tok, diag::err_argument_required_after_attribute);
4180
4181 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4182
4183 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004184 Tracker.consumeClose();
4185 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004186
4187 if (getLang().CPlusPlus) {
4188 MaybeParseCXX0XAttributes(attrs);
4189
4190 // Parse cv-qualifier-seq[opt].
4191 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004192 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004193 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004194 ConstQualifierLoc = DS.getConstSpecLoc();
4195 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4196 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004197
4198 // Parse ref-qualifier[opt].
4199 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004200 Diag(Tok, getLang().CPlusPlus0x ?
4201 diag::warn_cxx98_compat_ref_qualifier :
4202 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203
4204 RefQualifierIsLValueRef = Tok.is(tok::amp);
4205 RefQualifierLoc = ConsumeToken();
4206 EndLoc = RefQualifierLoc;
4207 }
4208
4209 // Parse exception-specification[opt].
4210 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4211 DynamicExceptions,
4212 DynamicExceptionRanges,
4213 NoexceptExpr);
4214 if (ESpecType != EST_None)
4215 EndLoc = ESpecRange.getEnd();
4216
4217 // Parse trailing-return-type[opt].
4218 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004219 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004220 SourceRange Range;
4221 TrailingReturnType = ParseTrailingReturnType(Range).get();
4222 if (Range.getEnd().isValid())
4223 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004224 }
4225 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004226 }
4227
4228 // Remember that we parsed a function type, and remember the attributes.
4229 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4230 /*isVariadic=*/EllipsisLoc.isValid(),
4231 EllipsisLoc,
4232 ParamInfo.data(), ParamInfo.size(),
4233 DS.getTypeQualifiers(),
4234 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004235 RefQualifierLoc, ConstQualifierLoc,
4236 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004237 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004238 ESpecType, ESpecRange.getBegin(),
4239 DynamicExceptions.data(),
4240 DynamicExceptionRanges.data(),
4241 DynamicExceptions.size(),
4242 NoexceptExpr.isUsable() ?
4243 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004244 Tracker.getOpenLocation(),
4245 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004246 TrailingReturnType),
4247 attrs, EndLoc);
4248}
4249
4250/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4251/// identifier list form for a K&R-style function: void foo(a,b,c)
4252///
4253/// Note that identifier-lists are only allowed for normal declarators, not for
4254/// abstract-declarators.
4255bool Parser::isFunctionDeclaratorIdentifierList() {
4256 return !getLang().CPlusPlus
4257 && Tok.is(tok::identifier)
4258 && !TryAltiVecVectorToken()
4259 // K&R identifier lists can't have typedefs as identifiers, per C99
4260 // 6.7.5.3p11.
4261 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4262 // Identifier lists follow a really simple grammar: the identifiers can
4263 // be followed *only* by a ", identifier" or ")". However, K&R
4264 // identifier lists are really rare in the brave new modern world, and
4265 // it is very common for someone to typo a type in a non-K&R style
4266 // list. If we are presented with something like: "void foo(intptr x,
4267 // float y)", we don't want to start parsing the function declarator as
4268 // though it is a K&R style declarator just because intptr is an
4269 // invalid type.
4270 //
4271 // To handle this, we check to see if the token after the first
4272 // identifier is a "," or ")". Only then do we parse it as an
4273 // identifier list.
4274 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4275}
4276
4277/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4278/// we found a K&R-style identifier list instead of a typed parameter list.
4279///
4280/// After returning, ParamInfo will hold the parsed parameters.
4281///
4282/// identifier-list: [C99 6.7.5]
4283/// identifier
4284/// identifier-list ',' identifier
4285///
4286void Parser::ParseFunctionDeclaratorIdentifierList(
4287 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004288 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004289 // If there was no identifier specified for the declarator, either we are in
4290 // an abstract-declarator, or we are in a parameter declarator which was found
4291 // to be abstract. In abstract-declarators, identifier lists are not valid:
4292 // diagnose this.
4293 if (!D.getIdentifier())
4294 Diag(Tok, diag::ext_ident_list_in_param);
4295
4296 // Maintain an efficient lookup of params we have seen so far.
4297 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4298
4299 while (1) {
4300 // If this isn't an identifier, report the error and skip until ')'.
4301 if (Tok.isNot(tok::identifier)) {
4302 Diag(Tok, diag::err_expected_ident);
4303 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4304 // Forget we parsed anything.
4305 ParamInfo.clear();
4306 return;
4307 }
4308
4309 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4310
4311 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4312 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4313 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4314
4315 // Verify that the argument identifier has not already been mentioned.
4316 if (!ParamsSoFar.insert(ParmII)) {
4317 Diag(Tok, diag::err_param_redefinition) << ParmII;
4318 } else {
4319 // Remember this identifier in ParamInfo.
4320 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4321 Tok.getLocation(),
4322 0));
4323 }
4324
4325 // Eat the identifier.
4326 ConsumeToken();
4327
4328 // The list continues if we see a comma.
4329 if (Tok.isNot(tok::comma))
4330 break;
4331 ConsumeToken();
4332 }
4333}
4334
4335/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4336/// after the opening parenthesis. This function will not parse a K&R-style
4337/// identifier list.
4338///
4339/// D is the declarator being parsed. If attrs is non-null, then the caller
4340/// parsed those arguments immediately after the open paren - they should be
4341/// considered to be the first argument of a parameter.
4342///
4343/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4344/// be the location of the ellipsis, if any was parsed.
4345///
Reid Spencer5f016e22007-07-11 17:01:13 +00004346/// parameter-type-list: [C99 6.7.5]
4347/// parameter-list
4348/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004349/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004350///
4351/// parameter-list: [C99 6.7.5]
4352/// parameter-declaration
4353/// parameter-list ',' parameter-declaration
4354///
4355/// parameter-declaration: [C99 6.7.5]
4356/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004357/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004358/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004359/// declaration-specifiers abstract-declarator[opt]
4360/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004361/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004362/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4363///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004364void Parser::ParseParameterDeclarationClause(
4365 Declarator &D,
4366 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004367 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004368 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004369
Chris Lattnerf97409f2008-04-06 06:57:35 +00004370 while (1) {
4371 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004372 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004373 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004374 }
Mike Stump1eb44332009-09-09 15:08:12 +00004375
Chris Lattnerf97409f2008-04-06 06:57:35 +00004376 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004377 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004378 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004379
John McCall7f040a92010-12-24 02:08:15 +00004380 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004381 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004382 ParseMicrosoftAttributes(DS.getAttributes());
4383
4384 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004385
4386 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004387 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004388 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4389 // attributes lost? Should they even be allowed?
4390 // FIXME: If we can leave the attributes in the token stream somehow, we can
4391 // get rid of a parameter (attrs) and this statement. It might be too much
4392 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004393 DS.takeAttributesFrom(attrs);
4394
Chris Lattnere64c5492009-02-27 18:38:20 +00004395 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004396
Chris Lattnerf97409f2008-04-06 06:57:35 +00004397 // Parse the declarator. This is "PrototypeContext", because we must
4398 // accept either 'declarator' or 'abstract-declarator' here.
4399 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4400 ParseDeclarator(ParmDecl);
4401
4402 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004403 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004404
Chris Lattnerf97409f2008-04-06 06:57:35 +00004405 // Remember this parsed parameter in ParamInfo.
4406 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004407
Douglas Gregor72b505b2008-12-16 21:30:33 +00004408 // DefArgToks is used when the parsing of default arguments needs
4409 // to be delayed.
4410 CachedTokens *DefArgToks = 0;
4411
Chris Lattnerf97409f2008-04-06 06:57:35 +00004412 // If no parameter was specified, verify that *something* was specified,
4413 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004414 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4415 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004416 // Completely missing, emit error.
4417 Diag(DSStart, diag::err_missing_param);
4418 } else {
4419 // Otherwise, we have something. Add it and let semantic analysis try
4420 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004421
Chris Lattnerf97409f2008-04-06 06:57:35 +00004422 // Inform the actions module about the parameter declarator, so it gets
4423 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004424 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004425
4426 // Parse the default argument, if any. We parse the default
4427 // arguments in all dialects; the semantic analysis in
4428 // ActOnParamDefaultArgument will reject the default argument in
4429 // C.
4430 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004431 SourceLocation EqualLoc = Tok.getLocation();
4432
Chris Lattner04421082008-04-08 04:40:51 +00004433 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004434 if (D.getContext() == Declarator::MemberContext) {
4435 // If we're inside a class definition, cache the tokens
4436 // corresponding to the default argument. We'll actually parse
4437 // them when we see the end of the class definition.
4438 // FIXME: Templates will require something similar.
4439 // FIXME: Can we use a smart pointer for Toks?
4440 DefArgToks = new CachedTokens;
4441
Mike Stump1eb44332009-09-09 15:08:12 +00004442 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004443 /*StopAtSemi=*/true,
4444 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004445 delete DefArgToks;
4446 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004447 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004448 } else {
4449 // Mark the end of the default argument so that we know when to
4450 // stop when we parse it later on.
4451 Token DefArgEnd;
4452 DefArgEnd.startToken();
4453 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4454 DefArgEnd.setLocation(Tok.getLocation());
4455 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004456 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004457 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004458 }
Chris Lattner04421082008-04-08 04:40:51 +00004459 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004460 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004461 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004462
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004463 // The argument isn't actually potentially evaluated unless it is
4464 // used.
4465 EnterExpressionEvaluationContext Eval(Actions,
4466 Sema::PotentiallyEvaluatedIfUsed);
4467
John McCall60d7b3a2010-08-24 06:29:42 +00004468 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004469 if (DefArgResult.isInvalid()) {
4470 Actions.ActOnParamDefaultArgumentError(Param);
4471 SkipUntil(tok::comma, tok::r_paren, true, true);
4472 } else {
4473 // Inform the actions module about the default argument
4474 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004475 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004476 }
Chris Lattner04421082008-04-08 04:40:51 +00004477 }
4478 }
Mike Stump1eb44332009-09-09 15:08:12 +00004479
4480 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4481 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004482 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004483 }
4484
4485 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004486 if (Tok.isNot(tok::comma)) {
4487 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004488 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4489
4490 if (!getLang().CPlusPlus) {
4491 // We have ellipsis without a preceding ',', which is ill-formed
4492 // in C. Complain and provide the fix.
4493 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004494 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004495 }
4496 }
4497
4498 break;
4499 }
Mike Stump1eb44332009-09-09 15:08:12 +00004500
Chris Lattnerf97409f2008-04-06 06:57:35 +00004501 // Consume the comma.
4502 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004503 }
Mike Stump1eb44332009-09-09 15:08:12 +00004504
Chris Lattner66d28652008-04-06 06:34:08 +00004505}
Chris Lattneref4715c2008-04-06 05:45:57 +00004506
Reid Spencer5f016e22007-07-11 17:01:13 +00004507/// [C90] direct-declarator '[' constant-expression[opt] ']'
4508/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4509/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4510/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4511/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4512void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004513 BalancedDelimiterTracker T(*this, tok::l_square);
4514 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004515
Chris Lattner378c7e42008-12-18 07:27:21 +00004516 // C array syntax has many features, but by-far the most common is [] and [4].
4517 // This code does a fast path to handle some of the most obvious cases.
4518 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004519 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004520 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004521 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004522
Chris Lattner378c7e42008-12-18 07:27:21 +00004523 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004524 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004525 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004526 T.getOpenLocation(),
4527 T.getCloseLocation()),
4528 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004529 return;
4530 } else if (Tok.getKind() == tok::numeric_constant &&
4531 GetLookAheadToken(1).is(tok::r_square)) {
4532 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004533 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004534 ConsumeToken();
4535
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004536 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004537 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004538 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004539
Chris Lattner378c7e42008-12-18 07:27:21 +00004540 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004541 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004542 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004543 T.getOpenLocation(),
4544 T.getCloseLocation()),
4545 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004546 return;
4547 }
Mike Stump1eb44332009-09-09 15:08:12 +00004548
Reid Spencer5f016e22007-07-11 17:01:13 +00004549 // If valid, this location is the position where we read the 'static' keyword.
4550 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004551 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004552 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004553
Reid Spencer5f016e22007-07-11 17:01:13 +00004554 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004555 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004556 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004557 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004558
Reid Spencer5f016e22007-07-11 17:01:13 +00004559 // If we haven't already read 'static', check to see if there is one after the
4560 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004561 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004562 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004563
Reid Spencer5f016e22007-07-11 17:01:13 +00004564 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4565 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004566 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004567
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004568 // Handle the case where we have '[*]' as the array size. However, a leading
4569 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4570 // the the token after the star is a ']'. Since stars in arrays are
4571 // infrequent, use of lookahead is not costly here.
4572 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004573 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004574
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004575 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004576 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004577 StaticLoc = SourceLocation(); // Drop the static.
4578 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004579 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004580 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004581 // Note, in C89, this production uses the constant-expr production instead
4582 // of assignment-expr. The only difference is that assignment-expr allows
4583 // things like '=' and '*='. Sema rejects these in C89 mode because they
4584 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004585
Douglas Gregore0762c92009-06-19 23:52:42 +00004586 // Parse the constant-expression or assignment-expression now (depending
4587 // on dialect).
4588 if (getLang().CPlusPlus)
4589 NumElements = ParseConstantExpression();
4590 else
4591 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004592 }
Mike Stump1eb44332009-09-09 15:08:12 +00004593
Reid Spencer5f016e22007-07-11 17:01:13 +00004594 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004595 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004596 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004597 // If the expression was invalid, skip it.
4598 SkipUntil(tok::r_square);
4599 return;
4600 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004601
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004602 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004603
John McCall0b7e6782011-03-24 11:26:52 +00004604 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004605 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004606
Chris Lattner378c7e42008-12-18 07:27:21 +00004607 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004608 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004609 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004610 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004611 T.getOpenLocation(),
4612 T.getCloseLocation()),
4613 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004614}
4615
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004616/// [GNU] typeof-specifier:
4617/// typeof ( expressions )
4618/// typeof ( type-name )
4619/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004620///
4621void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004622 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004623 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004624 SourceLocation StartLoc = ConsumeToken();
4625
John McCallcfb708c2010-01-13 20:03:27 +00004626 const bool hasParens = Tok.is(tok::l_paren);
4627
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004628 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004629 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004630 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004631 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4632 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004633 if (hasParens)
4634 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004635
4636 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004637 // FIXME: Not accurate, the range gets one token more than it should.
4638 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004639 else
4640 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004641
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004642 if (isCastExpr) {
4643 if (!CastTy) {
4644 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004645 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004646 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004647
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004648 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004649 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004650 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4651 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004652 DiagID, CastTy))
4653 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004654 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004655 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004656
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004657 // If we get here, the operand to the typeof was an expresion.
4658 if (Operand.isInvalid()) {
4659 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004660 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004661 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004662
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004663 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004664 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004665 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4666 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004667 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004668 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004669}
Chris Lattner1b492422010-02-28 18:33:55 +00004670
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004671/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004672/// _Atomic ( type-name )
4673///
4674void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4675 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4676
4677 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004678 BalancedDelimiterTracker T(*this, tok::l_paren);
4679 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004680 SkipUntil(tok::r_paren);
4681 return;
4682 }
4683
4684 TypeResult Result = ParseTypeName();
4685 if (Result.isInvalid()) {
4686 SkipUntil(tok::r_paren);
4687 return;
4688 }
4689
4690 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004691 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004692
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004693 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004694 return;
4695
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004696 DS.setTypeofParensRange(T.getRange());
4697 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004698
4699 const char *PrevSpec = 0;
4700 unsigned DiagID;
4701 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4702 DiagID, Result.release()))
4703 Diag(StartLoc, DiagID) << PrevSpec;
4704}
4705
Chris Lattner1b492422010-02-28 18:33:55 +00004706
4707/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4708/// from TryAltiVecVectorToken.
4709bool Parser::TryAltiVecVectorTokenOutOfLine() {
4710 Token Next = NextToken();
4711 switch (Next.getKind()) {
4712 default: return false;
4713 case tok::kw_short:
4714 case tok::kw_long:
4715 case tok::kw_signed:
4716 case tok::kw_unsigned:
4717 case tok::kw_void:
4718 case tok::kw_char:
4719 case tok::kw_int:
4720 case tok::kw_float:
4721 case tok::kw_double:
4722 case tok::kw_bool:
4723 case tok::kw___pixel:
4724 Tok.setKind(tok::kw___vector);
4725 return true;
4726 case tok::identifier:
4727 if (Next.getIdentifierInfo() == Ident_pixel) {
4728 Tok.setKind(tok::kw___vector);
4729 return true;
4730 }
4731 return false;
4732 }
4733}
4734
4735bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4736 const char *&PrevSpec, unsigned &DiagID,
4737 bool &isInvalid) {
4738 if (Tok.getIdentifierInfo() == Ident_vector) {
4739 Token Next = NextToken();
4740 switch (Next.getKind()) {
4741 case tok::kw_short:
4742 case tok::kw_long:
4743 case tok::kw_signed:
4744 case tok::kw_unsigned:
4745 case tok::kw_void:
4746 case tok::kw_char:
4747 case tok::kw_int:
4748 case tok::kw_float:
4749 case tok::kw_double:
4750 case tok::kw_bool:
4751 case tok::kw___pixel:
4752 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4753 return true;
4754 case tok::identifier:
4755 if (Next.getIdentifierInfo() == Ident_pixel) {
4756 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4757 return true;
4758 }
4759 break;
4760 default:
4761 break;
4762 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004763 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004764 DS.isTypeAltiVecVector()) {
4765 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4766 return true;
4767 }
4768 return false;
4769}