blob: 8cf25b619d60a948007c5cef690fff8859ccb57b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnereaaebc72009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000056
57/// isAttributeLateParsed - Return true if the attribute has arguments that
58/// require late parsing.
59static bool isAttributeLateParsed(const IdentifierInfo &II) {
60 return llvm::StringSwitch<bool>(II.getName())
61#include "clang/Parse/AttrLateParsed.inc"
62 .Default(false);
63}
64
65
Sean Huntbbd37c62009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000067///
68/// [GNU] attributes:
69/// attribute
70/// attributes attribute
71///
72/// [GNU] attribute:
73/// '__attribute__' '(' '(' attribute-list ')' ')'
74///
75/// [GNU] attribute-list:
76/// attrib
77/// attribute_list ',' attrib
78///
79/// [GNU] attrib:
80/// empty
81/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
85///
86/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000091///
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000093/// token lookahead. Comment from gcc: "If they start with an identifier
94/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// start with that identifier; otherwise they are an expression list."
96///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000097/// GCC does not require the ',' between attribs in an attribute-list.
98///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
103
John McCall7f040a92010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000114 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall7f040a92010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000192 IdentifierInfo *ParmName = 0;
193 SourceLocation ParmLoc;
194 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 switch (Tok.getKind()) {
197 case tok::kw_char:
198 case tok::kw_wchar_t:
199 case tok::kw_char16_t:
200 case tok::kw_char32_t:
201 case tok::kw_bool:
202 case tok::kw_short:
203 case tok::kw_int:
204 case tok::kw_long:
205 case tok::kw___int64:
206 case tok::kw_signed:
207 case tok::kw_unsigned:
208 case tok::kw_float:
209 case tok::kw_double:
210 case tok::kw_void:
211 case tok::kw_typeof:
212 // __attribute__(( vec_type_hint(char) ))
213 // FIXME: Don't just discard the builtin type token.
214 ConsumeToken();
215 BuiltinType = true;
216 break;
217
218 case tok::identifier:
219 ParmName = Tok.getIdentifierInfo();
220 ParmLoc = ConsumeToken();
221 break;
222
223 default:
224 break;
225 }
226
227 ExprVector ArgExprs(Actions);
228
229 if (!BuiltinType &&
230 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
231 // Eat the comma.
232 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000233 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000235 // Parse the non-empty comma-separated list of expressions.
236 while (1) {
237 ExprResult ArgExpr(ParseAssignmentExpression());
238 if (ArgExpr.isInvalid()) {
239 SkipUntil(tok::r_paren);
240 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000242 ArgExprs.push_back(ArgExpr.release());
243 if (Tok.isNot(tok::comma))
244 break;
245 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000248 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
249 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
250 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000251 while (Tok.is(tok::identifier)) {
252 ConsumeToken();
253 if (Tok.is(tok::greater))
254 break;
255 if (Tok.is(tok::comma)) {
256 ConsumeToken();
257 continue;
258 }
259 }
260 if (Tok.isNot(tok::greater))
261 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000262 SkipUntil(tok::r_paren, false, true); // skip until ')'
263 }
264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265
266 SourceLocation RParen = Tok.getLocation();
267 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
268 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000269 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
271 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
272 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000273 }
274}
275
276
Eli Friedmana23b4852009-06-08 07:21:15 +0000277/// ParseMicrosoftDeclSpec - Parse an __declspec construct
278///
279/// [MS] decl-specifier:
280/// __declspec ( extended-decl-modifier-seq )
281///
282/// [MS] extended-decl-modifier-seq:
283/// extended-decl-modifier[opt]
284/// extended-decl-modifier extended-decl-modifier-seq
285
John McCall7f040a92010-12-24 02:08:15 +0000286void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000287 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000288
Steve Narofff59e17e2008-12-24 20:59:21 +0000289 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000290 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
291 "declspec")) {
292 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000293 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 }
Francois Pichet373197b2011-05-07 19:04:49 +0000295
Eli Friedman290eeb02009-06-08 23:27:34 +0000296 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000299
300 // FIXME: Remove this when we have proper __declspec(property()) support.
301 // Just skip everything inside property().
302 if (AttrName->getName() == "property") {
303 ConsumeParen();
304 SkipUntil(tok::r_paren);
305 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000306 if (Tok.is(tok::l_paren)) {
307 ConsumeParen();
308 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
309 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000310 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000312 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000313 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
314 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 }
316 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
317 SkipUntil(tok::r_paren, false);
318 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
320 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000321 }
322 }
323 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
324 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000325 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000326}
327
John McCall7f040a92010-12-24 02:08:15 +0000328void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000329 // Treat these like attributes
330 // FIXME: Allow Sema to distinguish between these and real attributes!
331 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000332 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000333 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000334 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000335 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000336 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
337 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000338 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
339 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000340 // FIXME: Support these properly!
341 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000345}
346
John McCall7f040a92010-12-24 02:08:15 +0000347void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000348 // Treat these like attributes
349 while (Tok.is(tok::kw___pascal)) {
350 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
351 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000352 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 }
John McCall7f040a92010-12-24 02:08:15 +0000355}
356
Peter Collingbournef315fa82011-02-14 01:42:53 +0000357void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
358 // Treat these like attributes
359 while (Tok.is(tok::kw___kernel)) {
360 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000361 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
362 AttrNameLoc, 0, AttrNameLoc, 0,
363 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000364 }
365}
366
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000367void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
368 SourceLocation Loc = Tok.getLocation();
369 switch(Tok.getKind()) {
370 // OpenCL qualifiers:
371 case tok::kw___private:
372 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413 default: break;
414 }
415}
416
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000417/// \brief Parse a version number.
418///
419/// version:
420/// simple-integer
421/// simple-integer ',' simple-integer
422/// simple-integer ',' simple-integer ',' simple-integer
423VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
424 Range = Tok.getLocation();
425
426 if (!Tok.is(tok::numeric_constant)) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 // Parse the major (and possibly minor and subminor) versions, which
433 // are stored in the numeric constant. We utilize a quirk of the
434 // lexer, which is that it handles something like 1.2.3 as a single
435 // numeric constant, rather than two separate tokens.
436 llvm::SmallString<512> Buffer;
437 Buffer.resize(Tok.getLength()+1);
438 const char *ThisTokBegin = &Buffer[0];
439
440 // Get the spelling of the token, which eliminates trigraphs, etc.
441 bool Invalid = false;
442 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
443 if (Invalid)
444 return VersionTuple();
445
446 // Parse the major version.
447 unsigned AfterMajor = 0;
448 unsigned Major = 0;
449 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
450 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
451 ++AfterMajor;
452 }
453
454 if (AfterMajor == 0) {
455 Diag(Tok, diag::err_expected_version);
456 SkipUntil(tok::comma, tok::r_paren, true, true, true);
457 return VersionTuple();
458 }
459
460 if (AfterMajor == ActualLength) {
461 ConsumeToken();
462
463 // We only had a single version component.
464 if (Major == 0) {
465 Diag(Tok, diag::err_zero_version);
466 return VersionTuple();
467 }
468
469 return VersionTuple(Major);
470 }
471
472 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 // Parse the minor version.
479 unsigned AfterMinor = AfterMajor + 1;
480 unsigned Minor = 0;
481 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
482 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
483 ++AfterMinor;
484 }
485
486 if (AfterMinor == ActualLength) {
487 ConsumeToken();
488
489 // We had major.minor.
490 if (Major == 0 && Minor == 0) {
491 Diag(Tok, diag::err_zero_version);
492 return VersionTuple();
493 }
494
495 return VersionTuple(Major, Minor);
496 }
497
498 // If what follows is not a '.', we have a problem.
499 if (ThisTokBegin[AfterMinor] != '.') {
500 Diag(Tok, diag::err_expected_version);
501 SkipUntil(tok::comma, tok::r_paren, true, true, true);
502 return VersionTuple();
503 }
504
505 // Parse the subminor version.
506 unsigned AfterSubminor = AfterMinor + 1;
507 unsigned Subminor = 0;
508 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
509 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
510 ++AfterSubminor;
511 }
512
513 if (AfterSubminor != ActualLength) {
514 Diag(Tok, diag::err_expected_version);
515 SkipUntil(tok::comma, tok::r_paren, true, true, true);
516 return VersionTuple();
517 }
518 ConsumeToken();
519 return VersionTuple(Major, Minor, Subminor);
520}
521
522/// \brief Parse the contents of the "availability" attribute.
523///
524/// availability-attribute:
525/// 'availability' '(' platform ',' version-arg-list ')'
526///
527/// platform:
528/// identifier
529///
530/// version-arg-list:
531/// version-arg
532/// version-arg ',' version-arg-list
533///
534/// version-arg:
535/// 'introduced' '=' version
536/// 'deprecated' '=' version
537/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000538/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
540 SourceLocation AvailabilityLoc,
541 ParsedAttributes &attrs,
542 SourceLocation *endLoc) {
543 SourceLocation PlatformLoc;
544 IdentifierInfo *Platform = 0;
545
546 enum { Introduced, Deprecated, Obsoleted, Unknown };
547 AvailabilityChange Changes[Unknown];
548
549 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
551 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000552 Diag(Tok, diag::err_expected_lparen);
553 return;
554 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555
556 // Parse the platform name,
557 if (Tok.isNot(tok::identifier)) {
558 Diag(Tok, diag::err_availability_expected_platform);
559 SkipUntil(tok::r_paren);
560 return;
561 }
562 Platform = Tok.getIdentifierInfo();
563 PlatformLoc = ConsumeToken();
564
565 // Parse the ',' following the platform name.
566 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
567 return;
568
569 // If we haven't grabbed the pointers for the identifiers
570 // "introduced", "deprecated", and "obsoleted", do so now.
571 if (!Ident_introduced) {
572 Ident_introduced = PP.getIdentifierInfo("introduced");
573 Ident_deprecated = PP.getIdentifierInfo("deprecated");
574 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000575 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000576 }
577
578 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000579 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000580 do {
581 if (Tok.isNot(tok::identifier)) {
582 Diag(Tok, diag::err_availability_expected_change);
583 SkipUntil(tok::r_paren);
584 return;
585 }
586 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
587 SourceLocation KeywordLoc = ConsumeToken();
588
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 if (Keyword == Ident_unavailable) {
590 if (UnavailableLoc.isValid()) {
591 Diag(KeywordLoc, diag::err_availability_redundant)
592 << Keyword << SourceRange(UnavailableLoc);
593 }
594 UnavailableLoc = KeywordLoc;
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 continue;
601 }
602
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000603 if (Tok.isNot(tok::equal)) {
604 Diag(Tok, diag::err_expected_equal_after)
605 << Keyword;
606 SkipUntil(tok::r_paren);
607 return;
608 }
609 ConsumeToken();
610
611 SourceRange VersionRange;
612 VersionTuple Version = ParseVersionTuple(VersionRange);
613
614 if (Version.empty()) {
615 SkipUntil(tok::r_paren);
616 return;
617 }
618
619 unsigned Index;
620 if (Keyword == Ident_introduced)
621 Index = Introduced;
622 else if (Keyword == Ident_deprecated)
623 Index = Deprecated;
624 else if (Keyword == Ident_obsoleted)
625 Index = Obsoleted;
626 else
627 Index = Unknown;
628
629 if (Index < Unknown) {
630 if (!Changes[Index].KeywordLoc.isInvalid()) {
631 Diag(KeywordLoc, diag::err_availability_redundant)
632 << Keyword
633 << SourceRange(Changes[Index].KeywordLoc,
634 Changes[Index].VersionRange.getEnd());
635 }
636
637 Changes[Index].KeywordLoc = KeywordLoc;
638 Changes[Index].Version = Version;
639 Changes[Index].VersionRange = VersionRange;
640 } else {
641 Diag(KeywordLoc, diag::err_availability_unknown_change)
642 << Keyword << VersionRange;
643 }
644
645 if (Tok.isNot(tok::comma))
646 break;
647
648 ConsumeToken();
649 } while (true);
650
651 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000653 return;
654
655 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000656 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000657
Douglas Gregorb53e4172011-03-26 03:35:55 +0000658 // The 'unavailable' availability cannot be combined with any other
659 // availability changes. Make sure that hasn't happened.
660 if (UnavailableLoc.isValid()) {
661 bool Complained = false;
662 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
663 if (Changes[Index].KeywordLoc.isValid()) {
664 if (!Complained) {
665 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
666 << SourceRange(Changes[Index].KeywordLoc,
667 Changes[Index].VersionRange.getEnd());
668 Complained = true;
669 }
670
671 // Clear out the availability.
672 Changes[Index] = AvailabilityChange();
673 }
674 }
675 }
676
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000678 attrs.addNew(&Availability,
679 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000680 0, SourceLocation(),
681 Platform, PlatformLoc,
682 Changes[Introduced],
683 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000684 Changes[Obsoleted],
685 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686}
687
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000688
689// Late Parsed Attributes:
690// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
691
692void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
693
694void Parser::LateParsedClass::ParseLexedAttributes() {
695 Self->ParseLexedAttributes(*Class);
696}
697
698void Parser::LateParsedAttribute::ParseLexedAttributes() {
699 Self->ParseLexedAttribute(*this);
700}
701
702/// Wrapper class which calls ParseLexedAttribute, after setting up the
703/// scope appropriately.
704void Parser::ParseLexedAttributes(ParsingClass &Class) {
705 // Deal with templates
706 // FIXME: Test cases to make sure this does the right thing for templates.
707 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
708 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
709 HasTemplateScope);
710 if (HasTemplateScope)
711 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
712
713 // Set or update the scope flags to include Scope::ThisScope.
714 bool AlreadyHasClassScope = Class.TopLevelClass;
715 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
716 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
717 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
718
719 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
720 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
721 }
722}
723
724/// \brief Finish parsing an attribute for which parsing was delayed.
725/// This will be called at the end of parsing a class declaration
726/// for each LateParsedAttribute. We consume the saved tokens and
727/// create an attribute with the arguments filled in. We add this
728/// to the Attribute list for the decl.
729void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
730 // Save the current token position.
731 SourceLocation OrigLoc = Tok.getLocation();
732
733 // Append the current token at the end of the new token stream so that it
734 // doesn't get lost.
735 LA.Toks.push_back(Tok);
736 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
737 // Consume the previously pushed token.
738 ConsumeAnyToken();
739
740 ParsedAttributes Attrs(AttrFactory);
741 SourceLocation endLoc;
742
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000743 // If the Decl is templatized, add template parameters to scope.
744 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
745 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
746 if (HasTemplateScope)
747 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
748
749 // If the Decl is on a function, add function parameters to the scope.
750 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
751 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
752 if (HasFunctionScope)
753 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
754
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
756
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000757 if (HasFunctionScope) {
758 Actions.ActOnExitFunctionContext();
759 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
760 }
761 if (HasTemplateScope) {
762 TempScope.Exit();
763 }
764
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000765 // Late parsed attributes must be attached to Decls by hand. If the
766 // LA.D is not set, then this was not done properly.
767 assert(LA.D && "No decl attached to late parsed attribute");
768 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
769
770 if (Tok.getLocation() != OrigLoc) {
771 // Due to a parsing error, we either went over the cached tokens or
772 // there are still cached tokens left, so we skip the leftover tokens.
773 // Since this is an uncommon situation that should be avoided, use the
774 // expensive isBeforeInTranslationUnit call.
775 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
776 OrigLoc))
777 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
778 ConsumeAnyToken();
779 }
780}
781
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000782/// \brief Wrapper around a case statement checking if AttrName is
783/// one of the thread safety attributes
784bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
785 return llvm::StringSwitch<bool>(AttrName)
786 .Case("guarded_by", true)
787 .Case("guarded_var", true)
788 .Case("pt_guarded_by", true)
789 .Case("pt_guarded_var", true)
790 .Case("lockable", true)
791 .Case("scoped_lockable", true)
792 .Case("no_thread_safety_analysis", true)
793 .Case("acquired_after", true)
794 .Case("acquired_before", true)
795 .Case("exclusive_lock_function", true)
796 .Case("shared_lock_function", true)
797 .Case("exclusive_trylock_function", true)
798 .Case("shared_trylock_function", true)
799 .Case("unlock_function", true)
800 .Case("lock_returned", true)
801 .Case("locks_excluded", true)
802 .Case("exclusive_locks_required", true)
803 .Case("shared_locks_required", true)
804 .Default(false);
805}
806
807/// \brief Parse the contents of thread safety attributes. These
808/// should always be parsed as an expression list.
809///
810/// We need to special case the parsing due to the fact that if the first token
811/// of the first argument is an identifier, the main parse loop will store
812/// that token as a "parameter" and the rest of
813/// the arguments will be added to a list of "arguments". However,
814/// subsequent tokens in the first argument are lost. We instead parse each
815/// argument as an expression and add all arguments to the list of "arguments".
816/// In future, we will take advantage of this special case to also
817/// deal with some argument scoping issues here (for example, referring to a
818/// function parameter in the attribute on that function).
819void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
820 SourceLocation AttrNameLoc,
821 ParsedAttributes &Attrs,
822 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000823 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000824
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000825 BalancedDelimiterTracker T(*this, tok::l_paren);
826 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827
828 ExprVector ArgExprs(Actions);
829 bool ArgExprsOk = true;
830
831 // now parse the list of expressions
832 while (1) {
833 ExprResult ArgExpr(ParseAssignmentExpression());
834 if (ArgExpr.isInvalid()) {
835 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000836 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 break;
838 } else {
839 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 if (Tok.isNot(tok::comma))
842 break;
843 ConsumeToken(); // Eat the comma, move to the next argument
844 }
845 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000846 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000847 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
848 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000850 if (EndLoc)
851 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000852}
853
John McCall7f040a92010-12-24 02:08:15 +0000854void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
855 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
856 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000857}
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859/// ParseDeclaration - Parse a full 'declaration', which consists of
860/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000861/// 'Context' should be a Declarator::TheContext value. This returns the
862/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000863///
864/// declaration: [C99 6.7]
865/// block-declaration ->
866/// simple-declaration
867/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000868/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000869/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000870/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000871/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000872/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000873/// others... [FIXME]
874///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000875Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
876 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000877 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000878 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000879 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000880 // Must temporarily exit the objective-c container scope for
881 // parsing c none objective-c decls.
882 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000883
John McCalld226f652010-08-21 09:40:31 +0000884 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000885 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000886 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000887 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000888 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000889 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000890 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000891 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000892 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000893 // Could be the start of an inline namespace. Allowed as an ext in C++03.
894 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000895 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000896 SourceLocation InlineLoc = ConsumeToken();
897 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
898 break;
899 }
John McCall7f040a92010-12-24 02:08:15 +0000900 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000901 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000902 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000904 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000905 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000906 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000907 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000908 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000909 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000910 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000911 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000912 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000913 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000914 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000915 default:
John McCall7f040a92010-12-24 02:08:15 +0000916 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000917 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000918
Chris Lattner682bf922009-03-29 16:50:03 +0000919 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000920 // single decl, convert it now. Alias declarations can also declare a type;
921 // include that too if it is present.
922 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000923}
924
925/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
926/// declaration-specifiers init-declarator-list[opt] ';'
927///[C90/C++]init-declarator-list ';' [TODO]
928/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000929///
Richard Smithad762fc2011-04-14 22:09:26 +0000930/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
931/// attribute-specifier-seq[opt] type-specifier-seq declarator
932///
Chris Lattnercd147752009-03-29 17:27:48 +0000933/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000934/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000935///
936/// If FRI is non-null, we might be parsing a for-range-declaration instead
937/// of a simple-declaration. If we find that we are, we also parse the
938/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000939Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
940 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000941 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000942 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000943 bool RequireSemi,
944 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000946 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000947 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000948
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000949 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000950 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000951 StmtResult R = Actions.ActOnVlaStmt(DS);
952 if (R.isUsable())
953 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
956 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000957 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000958 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000959 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000960 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000961 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000962 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000964
965 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000966}
Mike Stump1eb44332009-09-09 15:08:12 +0000967
John McCalld8ac0572009-11-03 19:26:08 +0000968/// ParseDeclGroup - Having concluded that this is either a function
969/// definition or a group of object declarations, actually parse the
970/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000971Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
972 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000973 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000974 SourceLocation *DeclEnd,
975 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000976 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000977 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000978 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000979
John McCalld8ac0572009-11-03 19:26:08 +0000980 // Bail out if the first declarator didn't seem well-formed.
981 if (!D.hasName() && !D.mayOmitIdentifier()) {
982 // Skip until ; or }.
983 SkipUntil(tok::r_brace, true, true);
984 if (Tok.is(tok::semi))
985 ConsumeToken();
986 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattnerc82daef2010-07-11 22:24:20 +0000989 // Check to see if we have a function *definition* which must have a body.
990 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
991 // Look at the next token to make sure that this isn't a function
992 // declaration. We have to check this because __attribute__ might be the
993 // start of a function definition in GCC-extended K&R C.
994 !isDeclarationAfterDeclarator()) {
995
Chris Lattner004659a2010-07-11 22:42:07 +0000996 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000997 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
998 Diag(Tok, diag::err_function_declared_typedef);
999
1000 // Recover by treating the 'typedef' as spurious.
1001 DS.ClearStorageClassSpecs();
1002 }
1003
John McCalld226f652010-08-21 09:40:31 +00001004 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001005 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001006 }
1007
1008 if (isDeclarationSpecifier()) {
1009 // If there is an invalid declaration specifier right after the function
1010 // prototype, then we must be in a missing semicolon case where this isn't
1011 // actually a body. Just fall through into the code that handles it as a
1012 // prototype, and let the top-level code handle the erroneous declspec
1013 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001014 } else {
1015 Diag(Tok, diag::err_expected_fn_body);
1016 SkipUntil(tok::semi);
1017 return DeclGroupPtrTy();
1018 }
1019 }
1020
Richard Smithad762fc2011-04-14 22:09:26 +00001021 if (ParseAttributesAfterDeclarator(D))
1022 return DeclGroupPtrTy();
1023
1024 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1025 // must parse and analyze the for-range-initializer before the declaration is
1026 // analyzed.
1027 if (FRI && Tok.is(tok::colon)) {
1028 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001029 if (Tok.is(tok::l_brace))
1030 FRI->RangeExpr = ParseBraceInitializer();
1031 else
1032 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001033 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1034 Actions.ActOnCXXForRangeDecl(ThisDecl);
1035 Actions.FinalizeDeclaration(ThisDecl);
1036 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1037 }
1038
Chris Lattner5f9e2722011-07-23 10:55:15 +00001039 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001040 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001041 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001042 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001043 DeclsInGroup.push_back(FirstDecl);
1044
1045 // If we don't have a comma, it is either the end of the list (a ';') or an
1046 // error, bail out.
1047 while (Tok.is(tok::comma)) {
1048 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +00001049 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001050
1051 // Parse the next declarator.
1052 D.clear();
1053
1054 // Accept attributes in an init-declarator. In the first declarator in a
1055 // declaration, these would be part of the declspec. In subsequent
1056 // declarators, they become part of the declarator itself, so that they
1057 // don't apply to declarators after *this* one. Examples:
1058 // short __attribute__((common)) var; -> declspec
1059 // short var __attribute__((common)); -> declarator
1060 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001061 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001062
1063 ParseDeclarator(D);
1064
John McCalld226f652010-08-21 09:40:31 +00001065 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001066 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001067 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001068 DeclsInGroup.push_back(ThisDecl);
1069 }
1070
1071 if (DeclEnd)
1072 *DeclEnd = Tok.getLocation();
1073
1074 if (Context != Declarator::ForContext &&
1075 ExpectAndConsume(tok::semi,
1076 Context == Declarator::FileContext
1077 ? diag::err_invalid_token_after_toplevel_declarator
1078 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001079 // Okay, there was no semicolon and one was expected. If we see a
1080 // declaration specifier, just assume it was missing and continue parsing.
1081 // Otherwise things are very confused and we skip to recover.
1082 if (!isDeclarationSpecifier()) {
1083 SkipUntil(tok::r_brace, true, true);
1084 if (Tok.is(tok::semi))
1085 ConsumeToken();
1086 }
John McCalld8ac0572009-11-03 19:26:08 +00001087 }
1088
Douglas Gregor23c94db2010-07-02 17:43:08 +00001089 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001090 DeclsInGroup.data(),
1091 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001092}
1093
Richard Smithad762fc2011-04-14 22:09:26 +00001094/// Parse an optional simple-asm-expr and attributes, and attach them to a
1095/// declarator. Returns true on an error.
1096bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1097 // If a simple-asm-expr is present, parse it.
1098 if (Tok.is(tok::kw_asm)) {
1099 SourceLocation Loc;
1100 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1101 if (AsmLabel.isInvalid()) {
1102 SkipUntil(tok::semi, true, true);
1103 return true;
1104 }
1105
1106 D.setAsmLabel(AsmLabel.release());
1107 D.SetRangeEnd(Loc);
1108 }
1109
1110 MaybeParseGNUAttributes(D);
1111 return false;
1112}
1113
Douglas Gregor1426e532009-05-12 21:31:51 +00001114/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1115/// declarator'. This method parses the remainder of the declaration
1116/// (including any attributes or initializer, among other things) and
1117/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001118///
Reid Spencer5f016e22007-07-11 17:01:13 +00001119/// init-declarator: [C99 6.7]
1120/// declarator
1121/// declarator '=' initializer
1122/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1123/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001124/// [C++] declarator initializer[opt]
1125///
1126/// [C++] initializer:
1127/// [C++] '=' initializer-clause
1128/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001129/// [C++0x] '=' 'default' [TODO]
1130/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001131/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001132///
1133/// According to the standard grammar, =default and =delete are function
1134/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001135///
John McCalld226f652010-08-21 09:40:31 +00001136Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001137 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001138 if (ParseAttributesAfterDeclarator(D))
1139 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Richard Smithad762fc2011-04-14 22:09:26 +00001141 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1142}
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Richard Smithad762fc2011-04-14 22:09:26 +00001144Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1145 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001146 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001147 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001148 switch (TemplateInfo.Kind) {
1149 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001150 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001151 break;
1152
1153 case ParsedTemplateInfo::Template:
1154 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001155 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001156 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001157 TemplateInfo.TemplateParams->data(),
1158 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001159 D);
1160 break;
1161
1162 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001163 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001164 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001165 TemplateInfo.ExternLoc,
1166 TemplateInfo.TemplateLoc,
1167 D);
1168 if (ThisRes.isInvalid()) {
1169 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001170 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001171 }
1172
1173 ThisDecl = ThisRes.get();
1174 break;
1175 }
1176 }
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Richard Smith34b41d92011-02-20 03:19:35 +00001178 bool TypeContainsAuto =
1179 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1180
Douglas Gregor1426e532009-05-12 21:31:51 +00001181 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001182 if (isTokenEqualOrMistypedEqualEqual(
1183 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001184 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001185 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001186 if (D.isFunctionDeclarator())
1187 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1188 << 1 /* delete */;
1189 else
1190 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001191 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001192 if (D.isFunctionDeclarator())
1193 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1194 << 1 /* delete */;
1195 else
1196 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001197 } else {
John McCall731ad842009-12-19 09:28:58 +00001198 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1199 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001200 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001201 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001202
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001203 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001204 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001205 cutOffParsing();
1206 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001207 }
1208
John McCall60d7b3a2010-08-24 06:29:42 +00001209 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001210
John McCall731ad842009-12-19 09:28:58 +00001211 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001212 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001213 ExitScope();
1214 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001215
Douglas Gregor1426e532009-05-12 21:31:51 +00001216 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001217 SkipUntil(tok::comma, true, true);
1218 Actions.ActOnInitializerError(ThisDecl);
1219 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001220 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1221 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001222 }
1223 } else if (Tok.is(tok::l_paren)) {
1224 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001225 BalancedDelimiterTracker T(*this, tok::l_paren);
1226 T.consumeOpen();
1227
Douglas Gregor1426e532009-05-12 21:31:51 +00001228 ExprVector Exprs(Actions);
1229 CommaLocsTy CommaLocs;
1230
Douglas Gregorb4debae2009-12-22 17:47:17 +00001231 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1232 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001233 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001234 }
1235
Douglas Gregor1426e532009-05-12 21:31:51 +00001236 if (ParseExpressionList(Exprs, CommaLocs)) {
1237 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001238
1239 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001240 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001241 ExitScope();
1242 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001243 } else {
1244 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001245 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001246
1247 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1248 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001249
1250 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001251 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001252 ExitScope();
1253 }
1254
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001255 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001256 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001257 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001258 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001259 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001260 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1261 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001262 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1263
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001264 if (D.getCXXScopeSpec().isSet()) {
1265 EnterScope(0);
1266 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1267 }
1268
1269 ExprResult Init(ParseBraceInitializer());
1270
1271 if (D.getCXXScopeSpec().isSet()) {
1272 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1273 ExitScope();
1274 }
1275
1276 if (Init.isInvalid()) {
1277 Actions.ActOnInitializerError(ThisDecl);
1278 } else
1279 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1280 /*DirectInit=*/true, TypeContainsAuto);
1281
Douglas Gregor1426e532009-05-12 21:31:51 +00001282 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001283 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001284 }
1285
Richard Smith483b9f32011-02-21 20:05:19 +00001286 Actions.FinalizeDeclaration(ThisDecl);
1287
Douglas Gregor1426e532009-05-12 21:31:51 +00001288 return ThisDecl;
1289}
1290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291/// ParseSpecifierQualifierList
1292/// specifier-qualifier-list:
1293/// type-specifier specifier-qualifier-list[opt]
1294/// type-qualifier specifier-qualifier-list[opt]
1295/// [GNU] attributes specifier-qualifier-list[opt]
1296///
Richard Smithc89edf52011-07-01 19:46:12 +00001297void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1299 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001300 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001301 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Validate declspec for type-name.
1304 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001305 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001306 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 // Issue diagnostic and remove storage class if present.
1310 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1311 if (DS.getStorageClassSpecLoc().isValid())
1312 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1313 else
1314 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1315 DS.ClearStorageClassSpecs();
1316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // Issue diagnostic and remove function specfier if present.
1319 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001320 if (DS.isInlineSpecified())
1321 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1322 if (DS.isVirtualSpecified())
1323 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1324 if (DS.isExplicitSpecified())
1325 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 DS.ClearFunctionSpecs();
1327 }
1328}
1329
Chris Lattnerc199ab32009-04-12 20:42:31 +00001330/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1331/// specified token is valid after the identifier in a declarator which
1332/// immediately follows the declspec. For example, these things are valid:
1333///
1334/// int x [ 4]; // direct-declarator
1335/// int x ( int y); // direct-declarator
1336/// int(int x ) // direct-declarator
1337/// int x ; // simple-declaration
1338/// int x = 17; // init-declarator-list
1339/// int x , y; // init-declarator-list
1340/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001341/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001342/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001343///
1344/// This is not, because 'x' does not immediately follow the declspec (though
1345/// ')' happens to be valid anyway).
1346/// int (x)
1347///
1348static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1349 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1350 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001351 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001352}
1353
Chris Lattnere40c2952009-04-14 21:34:55 +00001354
1355/// ParseImplicitInt - This method is called when we have an non-typename
1356/// identifier in a declspec (which normally terminates the decl spec) when
1357/// the declspec has no type specifier. In this case, the declspec is either
1358/// malformed or is "implicit int" (in K&R and C89).
1359///
1360/// This method handles diagnosing this prettily and returns false if the
1361/// declspec is done being processed. If it recovers and thinks there may be
1362/// other pieces of declspec after it, it returns true.
1363///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001364bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001365 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001366 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001367 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Chris Lattnere40c2952009-04-14 21:34:55 +00001369 SourceLocation Loc = Tok.getLocation();
1370 // If we see an identifier that is not a type name, we normally would
1371 // parse it as the identifer being declared. However, when a typename
1372 // is typo'd or the definition is not included, this will incorrectly
1373 // parse the typename as the identifier name and fall over misparsing
1374 // later parts of the diagnostic.
1375 //
1376 // As such, we try to do some look-ahead in cases where this would
1377 // otherwise be an "implicit-int" case to see if this is invalid. For
1378 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1379 // an identifier with implicit int, we'd get a parse error because the
1380 // next token is obviously invalid for a type. Parse these as a case
1381 // with an invalid type specifier.
1382 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattnere40c2952009-04-14 21:34:55 +00001384 // Since we know that this either implicit int (which is rare) or an
1385 // error, we'd do lookahead to try to do better recovery.
1386 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1387 // If this token is valid for implicit int, e.g. "static x = 4", then
1388 // we just avoid eating the identifier, so it will be parsed as the
1389 // identifier in the declarator.
1390 return false;
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Chris Lattnere40c2952009-04-14 21:34:55 +00001393 // Otherwise, if we don't consume this token, we are going to emit an
1394 // error anyway. Try to recover from various common problems. Check
1395 // to see if this was a reference to a tag name without a tag specified.
1396 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001397 //
1398 // C++ doesn't need this, and isTagName doesn't take SS.
1399 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001400 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001401 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Douglas Gregor23c94db2010-07-02 17:43:08 +00001403 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001404 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001405 case DeclSpec::TST_enum:
1406 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1407 case DeclSpec::TST_union:
1408 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1409 case DeclSpec::TST_struct:
1410 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1411 case DeclSpec::TST_class:
1412 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Chris Lattnerf4382f52009-04-14 22:17:06 +00001415 if (TagName) {
1416 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001417 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001418 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Chris Lattnerf4382f52009-04-14 22:17:06 +00001420 // Parse this as a tag as if the missing tag were present.
1421 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001422 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001423 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001424 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001425 return true;
1426 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregora786fdb2009-10-13 23:27:22 +00001429 // This is almost certainly an invalid type name. Let the action emit a
1430 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001431 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001432 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001433 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001434 // The action emitted a diagnostic, so we don't have to.
1435 if (T) {
1436 // The action has suggested that the type T could be used. Set that as
1437 // the type in the declaration specifiers, consume the would-be type
1438 // name token, and we're done.
1439 const char *PrevSpec;
1440 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001441 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001442 DS.SetRangeEnd(Tok.getLocation());
1443 ConsumeToken();
1444
1445 // There may be other declaration specifiers after this.
1446 return true;
1447 }
1448
1449 // Fall through; the action had no suggestion for us.
1450 } else {
1451 // The action did not emit a diagnostic, so emit one now.
1452 SourceRange R;
1453 if (SS) R = SS->getRange();
1454 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Douglas Gregora786fdb2009-10-13 23:27:22 +00001457 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001458 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001459 unsigned DiagID;
1460 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001461 DS.SetRangeEnd(Tok.getLocation());
1462 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Chris Lattnere40c2952009-04-14 21:34:55 +00001464 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1465 // avoid rippling error messages on subsequent uses of the same type,
1466 // could be useful if #include was forgotten.
1467 return false;
1468}
1469
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001470/// \brief Determine the declaration specifier context from the declarator
1471/// context.
1472///
1473/// \param Context the declarator context, which is one of the
1474/// Declarator::TheContext enumerator values.
1475Parser::DeclSpecContext
1476Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1477 if (Context == Declarator::MemberContext)
1478 return DSC_class;
1479 if (Context == Declarator::FileContext)
1480 return DSC_top_level;
1481 return DSC_normal;
1482}
1483
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001484/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1485///
1486/// FIXME: Simply returns an alignof() expression if the argument is a
1487/// type. Ideally, the type should be propagated directly into Sema.
1488///
1489/// [C1X/C++0x] type-id
1490/// [C1X] constant-expression
1491/// [C++0x] assignment-expression
1492ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1493 if (isTypeIdInParens()) {
1494 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1495 SourceLocation TypeLoc = Tok.getLocation();
1496 ParsedType Ty = ParseTypeName().get();
1497 SourceRange TypeRange(Start, Tok.getLocation());
1498 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1499 Ty.getAsOpaquePtr(), TypeRange);
1500 } else
1501 return ParseConstantExpression();
1502}
1503
1504/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1505/// attribute to Attrs.
1506///
1507/// alignment-specifier:
1508/// [C1X] '_Alignas' '(' type-id ')'
1509/// [C1X] '_Alignas' '(' constant-expression ')'
1510/// [C++0x] 'alignas' '(' type-id ')'
1511/// [C++0x] 'alignas' '(' assignment-expression ')'
1512void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1513 SourceLocation *endLoc) {
1514 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1515 "Not an alignment-specifier!");
1516
1517 SourceLocation KWLoc = Tok.getLocation();
1518 ConsumeToken();
1519
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001520 BalancedDelimiterTracker T(*this, tok::l_paren);
1521 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001522 return;
1523
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001524 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation());
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001525 if (ArgExpr.isInvalid()) {
1526 SkipUntil(tok::r_paren);
1527 return;
1528 }
1529
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001530 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001531 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001532 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001533
1534 ExprVector ArgExprs(Actions);
1535 ArgExprs.push_back(ArgExpr.release());
1536 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001537 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001538}
1539
Reid Spencer5f016e22007-07-11 17:01:13 +00001540/// ParseDeclarationSpecifiers
1541/// declaration-specifiers: [C99 6.7]
1542/// storage-class-specifier declaration-specifiers[opt]
1543/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001544/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001545/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001546/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001547/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001548///
1549/// storage-class-specifier: [C99 6.7.1]
1550/// 'typedef'
1551/// 'extern'
1552/// 'static'
1553/// 'auto'
1554/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001555/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001556/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001557/// function-specifier: [C99 6.7.4]
1558/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001559/// [C++] 'virtual'
1560/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001561/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001562/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001563/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001566void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001567 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001568 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001569 DeclSpecContext DSContext) {
1570 if (DS.getSourceRange().isInvalid()) {
1571 DS.SetRangeStart(Tok.getLocation());
1572 DS.SetRangeEnd(Tok.getLocation());
1573 }
1574
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001576 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001578 unsigned DiagID = 0;
1579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001583 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001584 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001585 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1586 MaybeParseCXX0XAttributes(DS.getAttributes());
1587
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 // If this is not a declaration specifier token, we're done reading decl
1589 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001590 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001593 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001594 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001595 if (DS.hasTypeSpecifier()) {
1596 bool AllowNonIdentifiers
1597 = (getCurScope()->getFlags() & (Scope::ControlScope |
1598 Scope::BlockScope |
1599 Scope::TemplateParamScope |
1600 Scope::FunctionPrototypeScope |
1601 Scope::AtCatchScope)) == 0;
1602 bool AllowNestedNameSpecifiers
1603 = DSContext == DSC_top_level ||
1604 (DSContext == DSC_class && DS.isFriendSpecified());
1605
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001606 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1607 AllowNonIdentifiers,
1608 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001609 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001610 }
1611
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001612 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1613 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1614 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001615 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1616 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001617 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001618 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001619 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001620 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001621
1622 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001623 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001624 }
1625
Chris Lattner5e02c472009-01-05 00:07:25 +00001626 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001627 // C++ scope specifier. Annotate and loop, or bail out on error.
1628 if (TryAnnotateCXXScopeToken(true)) {
1629 if (!DS.hasTypeSpecifier())
1630 DS.SetTypeSpecError();
1631 goto DoneWithDeclSpec;
1632 }
John McCall2e0a7152010-03-01 18:20:46 +00001633 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1634 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001635 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001636
1637 case tok::annot_cxxscope: {
1638 if (DS.hasTypeSpecifier())
1639 goto DoneWithDeclSpec;
1640
John McCallaa87d332009-12-12 11:40:51 +00001641 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001642 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1643 Tok.getAnnotationRange(),
1644 SS);
John McCallaa87d332009-12-12 11:40:51 +00001645
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001646 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001647 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001648 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001649 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001650 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001651 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001652
1653 // C++ [class.qual]p2:
1654 // In a lookup in which the constructor is an acceptable lookup
1655 // result and the nested-name-specifier nominates a class C:
1656 //
1657 // - if the name specified after the
1658 // nested-name-specifier, when looked up in C, is the
1659 // injected-class-name of C (Clause 9), or
1660 //
1661 // - if the name specified after the nested-name-specifier
1662 // is the same as the identifier or the
1663 // simple-template-id's template-name in the last
1664 // component of the nested-name-specifier,
1665 //
1666 // the name is instead considered to name the constructor of
1667 // class C.
1668 //
1669 // Thus, if the template-name is actually the constructor
1670 // name, then the code is ill-formed; this interpretation is
1671 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001672 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001673 if ((DSContext == DSC_top_level ||
1674 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1675 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001676 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001677 if (isConstructorDeclarator()) {
1678 // The user meant this to be an out-of-line constructor
1679 // definition, but template arguments are not allowed
1680 // there. Just allow this as a constructor; we'll
1681 // complain about it later.
1682 goto DoneWithDeclSpec;
1683 }
1684
1685 // The user meant this to name a type, but it actually names
1686 // a constructor with some extraneous template
1687 // arguments. Complain, then parse it as a type as the user
1688 // intended.
1689 Diag(TemplateId->TemplateNameLoc,
1690 diag::err_out_of_line_template_id_names_constructor)
1691 << TemplateId->Name;
1692 }
1693
John McCallaa87d332009-12-12 11:40:51 +00001694 DS.getTypeSpecScope() = SS;
1695 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001696 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001697 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001698 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001699 continue;
1700 }
1701
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001702 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001703 DS.getTypeSpecScope() = SS;
1704 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001705 if (Tok.getAnnotationValue()) {
1706 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1708 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001709 PrevSpec, DiagID, T);
1710 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001711 else
1712 DS.SetTypeSpecError();
1713 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1714 ConsumeToken(); // The typename
1715 }
1716
Douglas Gregor9135c722009-03-25 15:40:00 +00001717 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001718 goto DoneWithDeclSpec;
1719
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001720 // If we're in a context where the identifier could be a class name,
1721 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001722 if ((DSContext == DSC_top_level ||
1723 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001724 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001725 &SS)) {
1726 if (isConstructorDeclarator())
1727 goto DoneWithDeclSpec;
1728
1729 // As noted in C++ [class.qual]p2 (cited above), when the name
1730 // of the class is qualified in a context where it could name
1731 // a constructor, its a constructor name. However, we've
1732 // looked at the declarator, and the user probably meant this
1733 // to be a type. Complain that it isn't supposed to be treated
1734 // as a type, then proceed to parse it as a type.
1735 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1736 << Next.getIdentifierInfo();
1737 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001738
John McCallb3d87482010-08-24 05:47:05 +00001739 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1740 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001741 getCurScope(), &SS,
1742 false, false, ParsedType(),
1743 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001744
Chris Lattnerf4382f52009-04-14 22:17:06 +00001745 // If the referenced identifier is not a type, then this declspec is
1746 // erroneous: We already checked about that it has no type specifier, and
1747 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001748 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001749 if (TypeRep == 0) {
1750 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001751 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001752 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001753 }
Mike Stump1eb44332009-09-09 15:08:12 +00001754
John McCallaa87d332009-12-12 11:40:51 +00001755 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001756 ConsumeToken(); // The C++ scope.
1757
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001759 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001760 if (isInvalid)
1761 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001763 DS.SetRangeEnd(Tok.getLocation());
1764 ConsumeToken(); // The typename.
1765
1766 continue;
1767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Chris Lattner80d0c892009-01-21 19:48:37 +00001769 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001770 if (Tok.getAnnotationValue()) {
1771 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001773 DiagID, T);
1774 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001775 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001776
1777 if (isInvalid)
1778 break;
1779
Chris Lattner80d0c892009-01-21 19:48:37 +00001780 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1781 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Chris Lattner80d0c892009-01-21 19:48:37 +00001783 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1784 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001785 // Objective-C interface.
1786 if (Tok.is(tok::less) && getLang().ObjC1)
1787 ParseObjCProtocolQualifiers(DS);
1788
Chris Lattner80d0c892009-01-21 19:48:37 +00001789 continue;
1790 }
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregorbfad9152011-04-28 15:48:45 +00001792 case tok::kw___is_signed:
1793 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1794 // typically treats it as a trait. If we see __is_signed as it appears
1795 // in libstdc++, e.g.,
1796 //
1797 // static const bool __is_signed;
1798 //
1799 // then treat __is_signed as an identifier rather than as a keyword.
1800 if (DS.getTypeSpecType() == TST_bool &&
1801 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1802 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1803 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1804 Tok.setKind(tok::identifier);
1805 }
1806
1807 // We're done with the declaration-specifiers.
1808 goto DoneWithDeclSpec;
1809
Chris Lattner3bd934a2008-07-26 01:18:38 +00001810 // typedef-name
1811 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001812 // In C++, check to see if this is a scope specifier like foo::bar::, if
1813 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001814 if (getLang().CPlusPlus) {
1815 if (TryAnnotateCXXScopeToken(true)) {
1816 if (!DS.hasTypeSpecifier())
1817 DS.SetTypeSpecError();
1818 goto DoneWithDeclSpec;
1819 }
1820 if (!Tok.is(tok::identifier))
1821 continue;
1822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Chris Lattner3bd934a2008-07-26 01:18:38 +00001824 // This identifier can only be a typedef name if we haven't already seen
1825 // a type-specifier. Without this check we misparse:
1826 // typedef int X; struct Y { short X; }; as 'short int'.
1827 if (DS.hasTypeSpecifier())
1828 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001829
John Thompson82287d12010-02-05 00:12:22 +00001830 // Check for need to substitute AltiVec keyword tokens.
1831 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1832 break;
1833
Chris Lattner3bd934a2008-07-26 01:18:38 +00001834 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001835 ParsedType TypeRep =
1836 Actions.getTypeName(*Tok.getIdentifierInfo(),
1837 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001838
Chris Lattnerc199ab32009-04-12 20:42:31 +00001839 // If this is not a typedef name, don't parse it as part of the declspec,
1840 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001841 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001842 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001843 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001844 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001845
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001846 // If we're in a context where the identifier could be a class name,
1847 // check whether this is a constructor declaration.
1848 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001849 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001850 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001851 goto DoneWithDeclSpec;
1852
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001854 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001855 if (isInvalid)
1856 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Chris Lattner3bd934a2008-07-26 01:18:38 +00001858 DS.SetRangeEnd(Tok.getLocation());
1859 ConsumeToken(); // The identifier
1860
1861 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1862 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001863 // Objective-C interface.
1864 if (Tok.is(tok::less) && getLang().ObjC1)
1865 ParseObjCProtocolQualifiers(DS);
1866
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001867 // Need to support trailing type qualifiers (e.g. "id<p> const").
1868 // If a type specifier follows, it will be diagnosed elsewhere.
1869 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001870 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001871
1872 // type-name
1873 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001874 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001875 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001876 // This template-id does not refer to a type name, so we're
1877 // done with the type-specifiers.
1878 goto DoneWithDeclSpec;
1879 }
1880
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001881 // If we're in a context where the template-id could be a
1882 // constructor name or specialization, check whether this is a
1883 // constructor declaration.
1884 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001885 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001886 isConstructorDeclarator())
1887 goto DoneWithDeclSpec;
1888
Douglas Gregor39a8de12009-02-25 19:37:18 +00001889 // Turn the template-id annotation token into a type annotation
1890 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001891 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001892 continue;
1893 }
1894
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 // GNU attributes support.
1896 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001897 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001899
1900 // Microsoft declspec support.
1901 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001902 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001903 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Steve Naroff239f0732008-12-25 14:16:32 +00001905 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001906 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001907 // FIXME: Add handling here!
1908 break;
1909
1910 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001911 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001912 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001913 case tok::kw___cdecl:
1914 case tok::kw___stdcall:
1915 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001916 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001917 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001918 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001919 continue;
1920
Dawn Perchik52fc3142010-09-03 01:29:35 +00001921 // Borland single token adornments.
1922 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001923 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001924 continue;
1925
Peter Collingbournef315fa82011-02-14 01:42:53 +00001926 // OpenCL single token adornments.
1927 case tok::kw___kernel:
1928 ParseOpenCLAttributes(DS.getAttributes());
1929 continue;
1930
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 // storage-class-specifier
1932 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001933 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
1934 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 break;
1936 case tok::kw_extern:
1937 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001938 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001939 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
1940 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001942 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001943 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
1944 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001945 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 case tok::kw_static:
1947 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001948 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001949 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
1950 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001951 break;
1952 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001953 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001954 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001955 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1956 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001957 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001958 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001959 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001960 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1962 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001963 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001964 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1965 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001966 break;
1967 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001968 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
1969 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001971 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001972 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
1973 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001974 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001976 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 // function-specifier
1980 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001981 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001983 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001984 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001985 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001986 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001987 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001988 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001989
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001990 // alignment-specifier
1991 case tok::kw__Alignas:
1992 if (!getLang().C1X)
1993 Diag(Tok, diag::ext_c1x_alignas);
1994 ParseAlignmentSpecifier(DS.getAttributes());
1995 continue;
1996
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001997 // friend
1998 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001999 if (DSContext == DSC_class)
2000 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2001 else {
2002 PrevSpec = ""; // not actually used by the diagnostic
2003 DiagID = diag::err_friend_invalid_in_context;
2004 isInvalid = true;
2005 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002006 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Douglas Gregor8d267c52011-09-09 02:06:17 +00002008 // Modules
2009 case tok::kw___module_private__:
2010 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2011 break;
2012
Sebastian Redl2ac67232009-11-05 15:47:02 +00002013 // constexpr
2014 case tok::kw_constexpr:
2015 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2016 break;
2017
Chris Lattner80d0c892009-01-21 19:48:37 +00002018 // type-specifier
2019 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002020 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2021 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002022 break;
2023 case tok::kw_long:
2024 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002025 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2026 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002027 else
John McCallfec54012009-08-03 20:12:06 +00002028 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2029 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002030 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002031 case tok::kw___int64:
2032 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2033 DiagID);
2034 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002035 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002036 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2037 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002038 break;
2039 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002040 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2041 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002042 break;
2043 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002044 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2045 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002046 break;
2047 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2049 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002050 break;
2051 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2053 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002054 break;
2055 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2057 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002058 break;
2059 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2061 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002062 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002063 case tok::kw_half:
2064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2065 DiagID);
2066 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002067 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2069 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002070 break;
2071 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2073 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002074 break;
2075 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2077 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002078 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002079 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2081 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002082 break;
2083 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2085 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002086 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002087 case tok::kw_bool:
2088 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002089 if (Tok.is(tok::kw_bool) &&
2090 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2091 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2092 PrevSpec = ""; // Not used by the diagnostic.
2093 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002094 // For better error recovery.
2095 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002096 isInvalid = true;
2097 } else {
2098 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2099 DiagID);
2100 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002101 break;
2102 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2104 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002105 break;
2106 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2108 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002109 break;
2110 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2112 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002113 break;
John Thompson82287d12010-02-05 00:12:22 +00002114 case tok::kw___vector:
2115 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2116 break;
2117 case tok::kw___pixel:
2118 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2119 break;
John McCalla5fc4722011-04-09 22:50:59 +00002120 case tok::kw___unknown_anytype:
2121 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2122 PrevSpec, DiagID);
2123 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002124
2125 // class-specifier:
2126 case tok::kw_class:
2127 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002128 case tok::kw_union: {
2129 tok::TokenKind Kind = Tok.getKind();
2130 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002131 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002133 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002134
2135 // enum-specifier:
2136 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002137 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002138 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002139 continue;
2140
2141 // cv-qualifier:
2142 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002143 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2144 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002145 break;
2146 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002147 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2148 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002149 break;
2150 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002151 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2152 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002153 break;
2154
Douglas Gregord57959a2009-03-27 23:10:48 +00002155 // C++ typename-specifier:
2156 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002157 if (TryAnnotateTypeOrScopeToken()) {
2158 DS.SetTypeSpecError();
2159 goto DoneWithDeclSpec;
2160 }
2161 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002162 continue;
2163 break;
2164
Chris Lattner80d0c892009-01-21 19:48:37 +00002165 // GNU typeof support.
2166 case tok::kw_typeof:
2167 ParseTypeofSpecifier(DS);
2168 continue;
2169
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002170 case tok::kw_decltype:
2171 ParseDecltypeSpecifier(DS);
2172 continue;
2173
Sean Huntdb5d44b2011-05-19 05:37:45 +00002174 case tok::kw___underlying_type:
2175 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002176 continue;
2177
2178 case tok::kw__Atomic:
2179 ParseAtomicSpecifier(DS);
2180 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002181
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002182 // OpenCL qualifiers:
2183 case tok::kw_private:
2184 if (!getLang().OpenCL)
2185 goto DoneWithDeclSpec;
2186 case tok::kw___private:
2187 case tok::kw___global:
2188 case tok::kw___local:
2189 case tok::kw___constant:
2190 case tok::kw___read_only:
2191 case tok::kw___write_only:
2192 case tok::kw___read_write:
2193 ParseOpenCLQualifiers(DS);
2194 break;
2195
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002196 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002197 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002198 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2199 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002200 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002201 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Douglas Gregor46f936e2010-11-19 17:10:50 +00002203 if (!ParseObjCProtocolQualifiers(DS))
2204 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2205 << FixItHint::CreateInsertion(Loc, "id")
2206 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002207
2208 // Need to support trailing type qualifiers (e.g. "id<p> const").
2209 // If a type specifier follows, it will be diagnosed elsewhere.
2210 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 }
John McCallfec54012009-08-03 20:12:06 +00002212 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002213 if (isInvalid) {
2214 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002215 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002216
2217 if (DiagID == diag::ext_duplicate_declspec)
2218 Diag(Tok, DiagID)
2219 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2220 else
2221 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002223
Chris Lattner81c018d2008-03-13 06:29:04 +00002224 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002225 if (DiagID != diag::err_bool_redeclaration)
2226 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 }
2228}
Douglas Gregoradcac882008-12-01 23:54:00 +00002229
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002230/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002231/// primarily follow the C++ grammar with additions for C99 and GNU,
2232/// which together subsume the C grammar. Note that the C++
2233/// type-specifier also includes the C type-qualifier (for const,
2234/// volatile, and C99 restrict). Returns true if a type-specifier was
2235/// found (and parsed), false otherwise.
2236///
2237/// type-specifier: [C++ 7.1.5]
2238/// simple-type-specifier
2239/// class-specifier
2240/// enum-specifier
2241/// elaborated-type-specifier [TODO]
2242/// cv-qualifier
2243///
2244/// cv-qualifier: [C++ 7.1.5.1]
2245/// 'const'
2246/// 'volatile'
2247/// [C99] 'restrict'
2248///
2249/// simple-type-specifier: [ C++ 7.1.5.2]
2250/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2251/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2252/// 'char'
2253/// 'wchar_t'
2254/// 'bool'
2255/// 'short'
2256/// 'int'
2257/// 'long'
2258/// 'signed'
2259/// 'unsigned'
2260/// 'float'
2261/// 'double'
2262/// 'void'
2263/// [C99] '_Bool'
2264/// [C99] '_Complex'
2265/// [C99] '_Imaginary' // Removed in TC2?
2266/// [GNU] '_Decimal32'
2267/// [GNU] '_Decimal64'
2268/// [GNU] '_Decimal128'
2269/// [GNU] typeof-specifier
2270/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2271/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002272/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002273/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002274bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002275 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002276 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002277 const ParsedTemplateInfo &TemplateInfo,
2278 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002279 SourceLocation Loc = Tok.getLocation();
2280
2281 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002282 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002283 // If we already have a type specifier, this identifier is not a type.
2284 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2285 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2286 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2287 return false;
John Thompson82287d12010-02-05 00:12:22 +00002288 // Check for need to substitute AltiVec keyword tokens.
2289 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2290 break;
2291 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002292 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002293 // Annotate typenames and C++ scope specifiers. If we get one, just
2294 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002295 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2296 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002297 return true;
2298 if (Tok.is(tok::identifier))
2299 return false;
2300 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2301 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002302 case tok::coloncolon: // ::foo::bar
2303 if (NextToken().is(tok::kw_new) || // ::new
2304 NextToken().is(tok::kw_delete)) // ::delete
2305 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Chris Lattner166a8fc2009-01-04 23:41:41 +00002307 // Annotate typenames and C++ scope specifiers. If we get one, just
2308 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002309 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2310 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002311 return true;
2312 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2313 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor12e083c2008-11-07 15:42:26 +00002315 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002316 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002317 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2319 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002320 DiagID, T);
2321 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002322 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002323 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2324 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Douglas Gregor12e083c2008-11-07 15:42:26 +00002326 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2327 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2328 // Objective-C interface. If we don't have Objective-C or a '<', this is
2329 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002330 if (Tok.is(tok::less) && getLang().ObjC1)
2331 ParseObjCProtocolQualifiers(DS);
2332
Douglas Gregor12e083c2008-11-07 15:42:26 +00002333 return true;
2334 }
2335
2336 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002337 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002338 break;
2339 case tok::kw_long:
2340 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002341 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2342 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002343 else
John McCallfec54012009-08-03 20:12:06 +00002344 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2345 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002346 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002347 case tok::kw___int64:
2348 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2349 DiagID);
2350 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002351 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002352 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002353 break;
2354 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002355 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2356 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002357 break;
2358 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002359 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2360 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002361 break;
2362 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002363 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2364 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002365 break;
2366 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002368 break;
2369 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002371 break;
2372 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002374 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002375 case tok::kw_half:
2376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2377 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002378 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002379 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002380 break;
2381 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002383 break;
2384 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002386 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002387 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002389 break;
2390 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002391 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002392 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002393 case tok::kw_bool:
2394 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002395 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002396 break;
2397 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002398 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2399 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002400 break;
2401 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2403 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002404 break;
2405 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002406 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2407 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002408 break;
John Thompson82287d12010-02-05 00:12:22 +00002409 case tok::kw___vector:
2410 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2411 break;
2412 case tok::kw___pixel:
2413 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2414 break;
2415
Douglas Gregor12e083c2008-11-07 15:42:26 +00002416 // class-specifier:
2417 case tok::kw_class:
2418 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002419 case tok::kw_union: {
2420 tok::TokenKind Kind = Tok.getKind();
2421 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002422 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2423 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002424 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002425 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002426
2427 // enum-specifier:
2428 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002429 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002430 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002431 return true;
2432
2433 // cv-qualifier:
2434 case tok::kw_const:
2435 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002436 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002437 break;
2438 case tok::kw_volatile:
2439 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002440 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002441 break;
2442 case tok::kw_restrict:
2443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002444 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002445 break;
2446
2447 // GNU typeof support.
2448 case tok::kw_typeof:
2449 ParseTypeofSpecifier(DS);
2450 return true;
2451
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002452 // C++0x decltype support.
2453 case tok::kw_decltype:
2454 ParseDecltypeSpecifier(DS);
2455 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Sean Huntdb5d44b2011-05-19 05:37:45 +00002457 // C++0x type traits support.
2458 case tok::kw___underlying_type:
2459 ParseUnderlyingTypeSpecifier(DS);
2460 return true;
2461
Eli Friedmanb001de72011-10-06 23:00:33 +00002462 case tok::kw__Atomic:
2463 ParseAtomicSpecifier(DS);
2464 return true;
2465
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002466 // OpenCL qualifiers:
2467 case tok::kw_private:
2468 if (!getLang().OpenCL)
2469 return false;
2470 case tok::kw___private:
2471 case tok::kw___global:
2472 case tok::kw___local:
2473 case tok::kw___constant:
2474 case tok::kw___read_only:
2475 case tok::kw___write_only:
2476 case tok::kw___read_write:
2477 ParseOpenCLQualifiers(DS);
2478 break;
2479
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002480 // C++0x auto support.
2481 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002482 // This is only called in situations where a storage-class specifier is
2483 // illegal, so we can assume an auto type specifier was intended even in
2484 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2485 // extension diagnostic.
2486 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002487 return false;
2488
John McCallfec54012009-08-03 20:12:06 +00002489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002490 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002491
Eli Friedman290eeb02009-06-08 23:27:34 +00002492 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002493 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002494 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002495 case tok::kw___cdecl:
2496 case tok::kw___stdcall:
2497 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002498 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002499 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002500 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002501 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002502
Dawn Perchik52fc3142010-09-03 01:29:35 +00002503 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002504 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002505 return true;
2506
Douglas Gregor12e083c2008-11-07 15:42:26 +00002507 default:
2508 // Not a type-specifier; do nothing.
2509 return false;
2510 }
2511
2512 // If the specifier combination wasn't legal, issue a diagnostic.
2513 if (isInvalid) {
2514 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002515 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002516 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002517 }
2518 DS.SetRangeEnd(Tok.getLocation());
2519 ConsumeToken(); // whatever we parsed above.
2520 return true;
2521}
Reid Spencer5f016e22007-07-11 17:01:13 +00002522
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002523/// ParseStructDeclaration - Parse a struct declaration without the terminating
2524/// semicolon.
2525///
Reid Spencer5f016e22007-07-11 17:01:13 +00002526/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002527/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002528/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002529/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002530/// struct-declarator-list:
2531/// struct-declarator
2532/// struct-declarator-list ',' struct-declarator
2533/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2534/// struct-declarator:
2535/// declarator
2536/// [GNU] declarator attributes[opt]
2537/// declarator[opt] ':' constant-expression
2538/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2539///
Chris Lattnere1359422008-04-10 06:46:29 +00002540void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002541ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002542
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002543 if (Tok.is(tok::kw___extension__)) {
2544 // __extension__ silences extension warnings in the subexpression.
2545 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002546 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002547 return ParseStructDeclaration(DS, Fields);
2548 }
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Steve Naroff28a7ca82007-08-20 22:28:22 +00002550 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002551 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002552
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002553 // If there are no declarators, this is a free-standing declaration
2554 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002555 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002556 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002557 return;
2558 }
2559
2560 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002561 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002562 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002563 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002564 FieldDeclarator DeclaratorInfo(DS);
2565
2566 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002567 if (!FirstDeclarator)
2568 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002569
Steve Naroff28a7ca82007-08-20 22:28:22 +00002570 /// struct-declarator: declarator
2571 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002572 if (Tok.isNot(tok::colon)) {
2573 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2574 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002575 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002576 }
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Chris Lattner04d66662007-10-09 17:33:22 +00002578 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002579 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002580 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002581 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002582 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002583 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002584 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002585 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002586
Steve Naroff28a7ca82007-08-20 22:28:22 +00002587 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002588 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002589
John McCallbdd563e2009-11-03 02:38:08 +00002590 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002591 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002592 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002593
Steve Naroff28a7ca82007-08-20 22:28:22 +00002594 // If we don't have a comma, it is either the end of the list (a ';')
2595 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002596 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002597 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002598
Steve Naroff28a7ca82007-08-20 22:28:22 +00002599 // Consume the comma.
2600 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002601
John McCallbdd563e2009-11-03 02:38:08 +00002602 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002603 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002604}
2605
2606/// ParseStructUnionBody
2607/// struct-contents:
2608/// struct-declaration-list
2609/// [EXT] empty
2610/// [GNU] "struct-declaration-list" without terminatoring ';'
2611/// struct-declaration-list:
2612/// struct-declaration
2613/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002614/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002615///
Reid Spencer5f016e22007-07-11 17:01:13 +00002616void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002617 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002618 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2619 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002621 BalancedDelimiterTracker T(*this, tok::l_brace);
2622 if (T.consumeOpen())
2623 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002625 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002626 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002627
Reid Spencer5f016e22007-07-11 17:01:13 +00002628 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2629 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002630 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002631 Diag(Tok, diag::ext_empty_struct_union)
2632 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002633
Chris Lattner5f9e2722011-07-23 10:55:15 +00002634 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002635
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002637 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002638 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Reid Spencer5f016e22007-07-11 17:01:13 +00002640 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002641 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002642 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002643 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002644 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 ConsumeToken();
2646 continue;
2647 }
Chris Lattnere1359422008-04-10 06:46:29 +00002648
2649 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002650 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002651
John McCallbdd563e2009-11-03 02:38:08 +00002652 if (!Tok.is(tok::at)) {
2653 struct CFieldCallback : FieldCallback {
2654 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002655 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002656 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002657
John McCalld226f652010-08-21 09:40:31 +00002658 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002659 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002660 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2661
John McCalld226f652010-08-21 09:40:31 +00002662 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002663 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002664 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002665 FD.D.getDeclSpec().getSourceRange().getBegin(),
2666 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002667 FieldDecls.push_back(Field);
2668 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002669 }
John McCallbdd563e2009-11-03 02:38:08 +00002670 } Callback(*this, TagDecl, FieldDecls);
2671
2672 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002673 } else { // Handle @defs
2674 ConsumeToken();
2675 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2676 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002677 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002678 continue;
2679 }
2680 ConsumeToken();
2681 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2682 if (!Tok.is(tok::identifier)) {
2683 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002684 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002685 continue;
2686 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002687 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002688 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002689 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002690 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2691 ConsumeToken();
2692 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002693 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002694
Chris Lattner04d66662007-10-09 17:33:22 +00002695 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002697 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002698 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 break;
2700 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002701 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2702 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002704 // If we stopped at a ';', eat it.
2705 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 }
2707 }
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002709 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002710
John McCall0b7e6782011-03-24 11:26:52 +00002711 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002712 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002713 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002714
Douglas Gregor23c94db2010-07-02 17:43:08 +00002715 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002716 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002717 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002718 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002719 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002720 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2721 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002722}
2723
Reid Spencer5f016e22007-07-11 17:01:13 +00002724/// ParseEnumSpecifier
2725/// enum-specifier: [C99 6.7.2.2]
2726/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002727///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002728/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2729/// '}' attributes[opt]
2730/// 'enum' identifier
2731/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002732///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002733/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2734/// [C++0x] enum-head '{' enumerator-list ',' '}'
2735///
2736/// enum-head: [C++0x]
2737/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2738/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2739///
2740/// enum-key: [C++0x]
2741/// 'enum'
2742/// 'enum' 'class'
2743/// 'enum' 'struct'
2744///
2745/// enum-base: [C++0x]
2746/// ':' type-specifier-seq
2747///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002748/// [C++] elaborated-type-specifier:
2749/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2750///
Chris Lattner4c97d762009-04-12 21:49:30 +00002751void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002752 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002753 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002754 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002755 if (Tok.is(tok::code_completion)) {
2756 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002757 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002758 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002759 }
John McCall57c13002011-07-06 05:58:41 +00002760
2761 bool IsScopedEnum = false;
2762 bool IsScopedUsingClassTag = false;
2763
2764 if (getLang().CPlusPlus0x &&
2765 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002766 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002767 IsScopedEnum = true;
2768 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2769 ConsumeToken();
2770 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002771
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002772 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002773 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002774 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002775
Douglas Gregor5471bc82011-09-08 17:18:35 +00002776 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002777 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002778
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002779 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002780 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002781 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2782 // if a fixed underlying type is allowed.
2783 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2784
John McCallb3d87482010-08-24 05:47:05 +00002785 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002786 return;
2787
2788 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002789 Diag(Tok, diag::err_expected_ident);
2790 if (Tok.isNot(tok::l_brace)) {
2791 // Has no name and is not a definition.
2792 // Skip the rest of this declarator, up until the comma or semicolon.
2793 SkipUntil(tok::comma, true);
2794 return;
2795 }
2796 }
2797 }
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002799 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002800 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2801 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002802 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002804 // Skip the rest of this declarator, up until the comma or semicolon.
2805 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002807 }
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002809 // If an identifier is present, consume and remember it.
2810 IdentifierInfo *Name = 0;
2811 SourceLocation NameLoc;
2812 if (Tok.is(tok::identifier)) {
2813 Name = Tok.getIdentifierInfo();
2814 NameLoc = ConsumeToken();
2815 }
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002817 if (!Name && IsScopedEnum) {
2818 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2819 // declaration of a scoped enumeration.
2820 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2821 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002822 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002823 }
2824
2825 TypeResult BaseType;
2826
Douglas Gregora61b3e72010-12-01 17:42:47 +00002827 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002828 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002829 bool PossibleBitfield = false;
2830 if (getCurScope()->getFlags() & Scope::ClassScope) {
2831 // If we're in class scope, this can either be an enum declaration with
2832 // an underlying type, or a declaration of a bitfield member. We try to
2833 // use a simple disambiguation scheme first to catch the common cases
2834 // (integer literal, sizeof); if it's still ambiguous, we then consider
2835 // anything that's a simple-type-specifier followed by '(' as an
2836 // expression. This suffices because function types are not valid
2837 // underlying types anyway.
2838 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2839 // If the next token starts an expression, we know we're parsing a
2840 // bit-field. This is the common case.
2841 if (TPR == TPResult::True())
2842 PossibleBitfield = true;
2843 // If the next token starts a type-specifier-seq, it may be either a
2844 // a fixed underlying type or the start of a function-style cast in C++;
2845 // lookahead one more token to see if it's obvious that we have a
2846 // fixed underlying type.
2847 else if (TPR == TPResult::False() &&
2848 GetLookAheadToken(2).getKind() == tok::semi) {
2849 // Consume the ':'.
2850 ConsumeToken();
2851 } else {
2852 // We have the start of a type-specifier-seq, so we have to perform
2853 // tentative parsing to determine whether we have an expression or a
2854 // type.
2855 TentativeParsingAction TPA(*this);
2856
2857 // Consume the ':'.
2858 ConsumeToken();
2859
Douglas Gregor86f208c2011-02-22 20:32:04 +00002860 if ((getLang().CPlusPlus &&
2861 isCXXDeclarationSpecifier() != TPResult::True()) ||
2862 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002863 // We'll parse this as a bitfield later.
2864 PossibleBitfield = true;
2865 TPA.Revert();
2866 } else {
2867 // We have a type-specifier-seq.
2868 TPA.Commit();
2869 }
2870 }
2871 } else {
2872 // Consume the ':'.
2873 ConsumeToken();
2874 }
2875
2876 if (!PossibleBitfield) {
2877 SourceRange Range;
2878 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002879
Douglas Gregor5471bc82011-09-08 17:18:35 +00002880 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002881 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2882 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002883 if (getLang().CPlusPlus0x)
2884 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002885 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002886 }
2887
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002888 // There are three options here. If we have 'enum foo;', then this is a
2889 // forward declaration. If we have 'enum foo {...' then this is a
2890 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2891 //
2892 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2893 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2894 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2895 //
John McCallf312b1e2010-08-26 23:41:50 +00002896 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002897 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002898 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002899 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002900 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002901 else
John McCallf312b1e2010-08-26 23:41:50 +00002902 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002903
2904 // enums cannot be templates, although they can be referenced from a
2905 // template.
2906 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002907 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002908 Diag(Tok, diag::err_enum_template);
2909
2910 // Skip the rest of this declarator, up until the comma or semicolon.
2911 SkipUntil(tok::comma, true);
2912 return;
2913 }
2914
Douglas Gregorb9075602011-02-22 02:55:24 +00002915 if (!Name && TUK != Sema::TUK_Definition) {
2916 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2917
2918 // Skip the rest of this declarator, up until the comma or semicolon.
2919 SkipUntil(tok::comma, true);
2920 return;
2921 }
2922
Douglas Gregor402abb52009-05-28 23:31:59 +00002923 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002924 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002925 const char *PrevSpec = 0;
2926 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002927 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002928 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002929 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002930 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002931 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002932 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002933
Douglas Gregor48c89f42010-04-24 16:38:41 +00002934 if (IsDependent) {
2935 // This enum has a dependent nested-name-specifier. Handle it as a
2936 // dependent tag.
2937 if (!Name) {
2938 DS.SetTypeSpecError();
2939 Diag(Tok, diag::err_expected_type_name_after_typename);
2940 return;
2941 }
2942
Douglas Gregor23c94db2010-07-02 17:43:08 +00002943 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002944 TUK, SS, Name, StartLoc,
2945 NameLoc);
2946 if (Type.isInvalid()) {
2947 DS.SetTypeSpecError();
2948 return;
2949 }
2950
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002951 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2952 NameLoc.isValid() ? NameLoc : StartLoc,
2953 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002954 Diag(StartLoc, DiagID) << PrevSpec;
2955
2956 return;
2957 }
Mike Stump1eb44332009-09-09 15:08:12 +00002958
John McCalld226f652010-08-21 09:40:31 +00002959 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002960 // The action failed to produce an enumeration tag. If this is a
2961 // definition, consume the entire definition.
2962 if (Tok.is(tok::l_brace)) {
2963 ConsumeBrace();
2964 SkipUntil(tok::r_brace);
2965 }
2966
2967 DS.SetTypeSpecError();
2968 return;
2969 }
2970
Chris Lattner04d66662007-10-09 17:33:22 +00002971 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002972 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002974 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2975 NameLoc.isValid() ? NameLoc : StartLoc,
2976 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002977 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002978}
2979
2980/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2981/// enumerator-list:
2982/// enumerator
2983/// enumerator-list ',' enumerator
2984/// enumerator:
2985/// enumeration-constant
2986/// enumeration-constant '=' constant-expression
2987/// enumeration-constant:
2988/// identifier
2989///
John McCalld226f652010-08-21 09:40:31 +00002990void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002991 // Enter the scope of the enum body and start the definition.
2992 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002993 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002994
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002995 BalancedDelimiterTracker T(*this, tok::l_brace);
2996 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Chris Lattner7946dd32007-08-27 17:24:30 +00002998 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002999 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003000 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Chris Lattner5f9e2722011-07-23 10:55:15 +00003002 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003003
John McCalld226f652010-08-21 09:40:31 +00003004 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Reid Spencer5f016e22007-07-11 17:01:13 +00003006 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003007 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003008 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3009 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003010
John McCall5b629aa2010-10-22 23:36:17 +00003011 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003012 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003013 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003014
Reid Spencer5f016e22007-07-11 17:01:13 +00003015 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003016 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003017 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003018 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003019 AssignedVal = ParseConstantExpression();
3020 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003021 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003022 }
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Reid Spencer5f016e22007-07-11 17:01:13 +00003024 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003025 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3026 LastEnumConstDecl,
3027 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003028 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003029 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003030 EnumConstantDecls.push_back(EnumConstDecl);
3031 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Douglas Gregor751f6922010-09-07 14:51:08 +00003033 if (Tok.is(tok::identifier)) {
3034 // We're missing a comma between enumerators.
3035 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3036 Diag(Loc, diag::err_enumerator_list_missing_comma)
3037 << FixItHint::CreateInsertion(Loc, ", ");
3038 continue;
3039 }
3040
Chris Lattner04d66662007-10-09 17:33:22 +00003041 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 break;
3043 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Richard Smith7fe62082011-10-15 05:09:34 +00003045 if (Tok.isNot(tok::identifier)) {
3046 if (!getLang().C99 && !getLang().CPlusPlus0x)
3047 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3048 << getLang().CPlusPlus
3049 << FixItHint::CreateRemoval(CommaLoc);
3050 else if (getLang().CPlusPlus0x)
3051 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3052 << FixItHint::CreateRemoval(CommaLoc);
3053 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 }
Mike Stump1eb44332009-09-09 15:08:12 +00003055
Reid Spencer5f016e22007-07-11 17:01:13 +00003056 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003057 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003058
Reid Spencer5f016e22007-07-11 17:01:13 +00003059 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003060 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003061 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003062
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003063 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3064 EnumDecl, EnumConstantDecls.data(),
3065 EnumConstantDecls.size(), getCurScope(),
3066 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Douglas Gregor72de6672009-01-08 20:45:30 +00003068 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003069 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3070 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003071}
3072
3073/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003074/// start of a type-qualifier-list.
3075bool Parser::isTypeQualifier() const {
3076 switch (Tok.getKind()) {
3077 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003078
3079 // type-qualifier only in OpenCL
3080 case tok::kw_private:
3081 return getLang().OpenCL;
3082
Steve Naroff5f8aa692008-02-11 23:15:56 +00003083 // type-qualifier
3084 case tok::kw_const:
3085 case tok::kw_volatile:
3086 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003087 case tok::kw___private:
3088 case tok::kw___local:
3089 case tok::kw___global:
3090 case tok::kw___constant:
3091 case tok::kw___read_only:
3092 case tok::kw___read_write:
3093 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003094 return true;
3095 }
3096}
3097
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003098/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3099/// is definitely a type-specifier. Return false if it isn't part of a type
3100/// specifier or if we're not sure.
3101bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3102 switch (Tok.getKind()) {
3103 default: return false;
3104 // type-specifiers
3105 case tok::kw_short:
3106 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003107 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003108 case tok::kw_signed:
3109 case tok::kw_unsigned:
3110 case tok::kw__Complex:
3111 case tok::kw__Imaginary:
3112 case tok::kw_void:
3113 case tok::kw_char:
3114 case tok::kw_wchar_t:
3115 case tok::kw_char16_t:
3116 case tok::kw_char32_t:
3117 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003118 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003119 case tok::kw_float:
3120 case tok::kw_double:
3121 case tok::kw_bool:
3122 case tok::kw__Bool:
3123 case tok::kw__Decimal32:
3124 case tok::kw__Decimal64:
3125 case tok::kw__Decimal128:
3126 case tok::kw___vector:
3127
3128 // struct-or-union-specifier (C99) or class-specifier (C++)
3129 case tok::kw_class:
3130 case tok::kw_struct:
3131 case tok::kw_union:
3132 // enum-specifier
3133 case tok::kw_enum:
3134
3135 // typedef-name
3136 case tok::annot_typename:
3137 return true;
3138 }
3139}
3140
Steve Naroff5f8aa692008-02-11 23:15:56 +00003141/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003142/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003143bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 switch (Tok.getKind()) {
3145 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Chris Lattner166a8fc2009-01-04 23:41:41 +00003147 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003148 if (TryAltiVecVectorToken())
3149 return true;
3150 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003151 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003152 // Annotate typenames and C++ scope specifiers. If we get one, just
3153 // recurse to handle whatever we get.
3154 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003155 return true;
3156 if (Tok.is(tok::identifier))
3157 return false;
3158 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003159
Chris Lattner166a8fc2009-01-04 23:41:41 +00003160 case tok::coloncolon: // ::foo::bar
3161 if (NextToken().is(tok::kw_new) || // ::new
3162 NextToken().is(tok::kw_delete)) // ::delete
3163 return false;
3164
Chris Lattner166a8fc2009-01-04 23:41:41 +00003165 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003166 return true;
3167 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Reid Spencer5f016e22007-07-11 17:01:13 +00003169 // GNU attributes support.
3170 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003171 // GNU typeof support.
3172 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Reid Spencer5f016e22007-07-11 17:01:13 +00003174 // type-specifiers
3175 case tok::kw_short:
3176 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003177 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003178 case tok::kw_signed:
3179 case tok::kw_unsigned:
3180 case tok::kw__Complex:
3181 case tok::kw__Imaginary:
3182 case tok::kw_void:
3183 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003184 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003185 case tok::kw_char16_t:
3186 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003187 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003188 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003189 case tok::kw_float:
3190 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003191 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003192 case tok::kw__Bool:
3193 case tok::kw__Decimal32:
3194 case tok::kw__Decimal64:
3195 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003196 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Chris Lattner99dc9142008-04-13 18:59:07 +00003198 // struct-or-union-specifier (C99) or class-specifier (C++)
3199 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003200 case tok::kw_struct:
3201 case tok::kw_union:
3202 // enum-specifier
3203 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Reid Spencer5f016e22007-07-11 17:01:13 +00003205 // type-qualifier
3206 case tok::kw_const:
3207 case tok::kw_volatile:
3208 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003209
3210 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003211 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003212 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003213
Chris Lattner7c186be2008-10-20 00:25:30 +00003214 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3215 case tok::less:
3216 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003217
Steve Naroff239f0732008-12-25 14:16:32 +00003218 case tok::kw___cdecl:
3219 case tok::kw___stdcall:
3220 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003221 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003222 case tok::kw___w64:
3223 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003224 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003225 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003226 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003227
3228 case tok::kw___private:
3229 case tok::kw___local:
3230 case tok::kw___global:
3231 case tok::kw___constant:
3232 case tok::kw___read_only:
3233 case tok::kw___read_write:
3234 case tok::kw___write_only:
3235
Eli Friedman290eeb02009-06-08 23:27:34 +00003236 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003237
3238 case tok::kw_private:
3239 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003240
3241 // C1x _Atomic()
3242 case tok::kw__Atomic:
3243 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003244 }
3245}
3246
3247/// isDeclarationSpecifier() - Return true if the current token is part of a
3248/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003249///
3250/// \param DisambiguatingWithExpression True to indicate that the purpose of
3251/// this check is to disambiguate between an expression and a declaration.
3252bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003253 switch (Tok.getKind()) {
3254 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003256 case tok::kw_private:
3257 return getLang().OpenCL;
3258
Chris Lattner166a8fc2009-01-04 23:41:41 +00003259 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003260 // Unfortunate hack to support "Class.factoryMethod" notation.
3261 if (getLang().ObjC1 && NextToken().is(tok::period))
3262 return false;
John Thompson82287d12010-02-05 00:12:22 +00003263 if (TryAltiVecVectorToken())
3264 return true;
3265 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003266 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003267 // Annotate typenames and C++ scope specifiers. If we get one, just
3268 // recurse to handle whatever we get.
3269 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003270 return true;
3271 if (Tok.is(tok::identifier))
3272 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003273
3274 // If we're in Objective-C and we have an Objective-C class type followed
3275 // by an identifier and then either ':' or ']', in a place where an
3276 // expression is permitted, then this is probably a class message send
3277 // missing the initial '['. In this case, we won't consider this to be
3278 // the start of a declaration.
3279 if (DisambiguatingWithExpression &&
3280 isStartOfObjCClassMessageMissingOpenBracket())
3281 return false;
3282
John McCall9ba61662010-02-26 08:45:28 +00003283 return isDeclarationSpecifier();
3284
Chris Lattner166a8fc2009-01-04 23:41:41 +00003285 case tok::coloncolon: // ::foo::bar
3286 if (NextToken().is(tok::kw_new) || // ::new
3287 NextToken().is(tok::kw_delete)) // ::delete
3288 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Chris Lattner166a8fc2009-01-04 23:41:41 +00003290 // Annotate typenames and C++ scope specifiers. If we get one, just
3291 // recurse to handle whatever we get.
3292 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003293 return true;
3294 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 // storage-class-specifier
3297 case tok::kw_typedef:
3298 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003299 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 case tok::kw_static:
3301 case tok::kw_auto:
3302 case tok::kw_register:
3303 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003304
Douglas Gregor8d267c52011-09-09 02:06:17 +00003305 // Modules
3306 case tok::kw___module_private__:
3307
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 // type-specifiers
3309 case tok::kw_short:
3310 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003311 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003312 case tok::kw_signed:
3313 case tok::kw_unsigned:
3314 case tok::kw__Complex:
3315 case tok::kw__Imaginary:
3316 case tok::kw_void:
3317 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003318 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003319 case tok::kw_char16_t:
3320 case tok::kw_char32_t:
3321
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003323 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003324 case tok::kw_float:
3325 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003326 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 case tok::kw__Bool:
3328 case tok::kw__Decimal32:
3329 case tok::kw__Decimal64:
3330 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003331 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Chris Lattner99dc9142008-04-13 18:59:07 +00003333 // struct-or-union-specifier (C99) or class-specifier (C++)
3334 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 case tok::kw_struct:
3336 case tok::kw_union:
3337 // enum-specifier
3338 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 // type-qualifier
3341 case tok::kw_const:
3342 case tok::kw_volatile:
3343 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003344
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 // function-specifier
3346 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003347 case tok::kw_virtual:
3348 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003349
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003350 // static_assert-declaration
3351 case tok::kw__Static_assert:
3352
Chris Lattner1ef08762007-08-09 17:01:07 +00003353 // GNU typeof support.
3354 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003355
Chris Lattner1ef08762007-08-09 17:01:07 +00003356 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003357 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003358 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003359
Francois Pichete3d49b42011-06-19 08:02:06 +00003360 // C++0x decltype.
3361 case tok::kw_decltype:
3362 return true;
3363
Eli Friedmanb001de72011-10-06 23:00:33 +00003364 // C1x _Atomic()
3365 case tok::kw__Atomic:
3366 return true;
3367
Chris Lattnerf3948c42008-07-26 03:38:44 +00003368 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3369 case tok::less:
3370 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003371
Douglas Gregord9d75e52011-04-27 05:41:15 +00003372 // typedef-name
3373 case tok::annot_typename:
3374 return !DisambiguatingWithExpression ||
3375 !isStartOfObjCClassMessageMissingOpenBracket();
3376
Steve Naroff47f52092009-01-06 19:34:12 +00003377 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003378 case tok::kw___cdecl:
3379 case tok::kw___stdcall:
3380 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003381 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003382 case tok::kw___w64:
3383 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003384 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003385 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003386 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003387 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003388
3389 case tok::kw___private:
3390 case tok::kw___local:
3391 case tok::kw___global:
3392 case tok::kw___constant:
3393 case tok::kw___read_only:
3394 case tok::kw___read_write:
3395 case tok::kw___write_only:
3396
Eli Friedman290eeb02009-06-08 23:27:34 +00003397 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003398 }
3399}
3400
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003401bool Parser::isConstructorDeclarator() {
3402 TentativeParsingAction TPA(*this);
3403
3404 // Parse the C++ scope specifier.
3405 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003406 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003407 TPA.Revert();
3408 return false;
3409 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003410
3411 // Parse the constructor name.
3412 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3413 // We already know that we have a constructor name; just consume
3414 // the token.
3415 ConsumeToken();
3416 } else {
3417 TPA.Revert();
3418 return false;
3419 }
3420
3421 // Current class name must be followed by a left parentheses.
3422 if (Tok.isNot(tok::l_paren)) {
3423 TPA.Revert();
3424 return false;
3425 }
3426 ConsumeParen();
3427
3428 // A right parentheses or ellipsis signals that we have a constructor.
3429 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3430 TPA.Revert();
3431 return true;
3432 }
3433
3434 // If we need to, enter the specified scope.
3435 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003436 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003437 DeclScopeObj.EnterDeclaratorScope();
3438
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003439 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003440 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003441 MaybeParseMicrosoftAttributes(Attrs);
3442
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003443 // Check whether the next token(s) are part of a declaration
3444 // specifier, in which case we have the start of a parameter and,
3445 // therefore, we know that this is a constructor.
3446 bool IsConstructor = isDeclarationSpecifier();
3447 TPA.Revert();
3448 return IsConstructor;
3449}
Reid Spencer5f016e22007-07-11 17:01:13 +00003450
3451/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003452/// type-qualifier-list: [C99 6.7.5]
3453/// type-qualifier
3454/// [vendor] attributes
3455/// [ only if VendorAttributesAllowed=true ]
3456/// type-qualifier-list type-qualifier
3457/// [vendor] type-qualifier-list attributes
3458/// [ only if VendorAttributesAllowed=true ]
3459/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3460/// [ only if CXX0XAttributesAllowed=true ]
3461/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003462///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003463void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3464 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003465 bool CXX0XAttributesAllowed) {
3466 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3467 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003468 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003469 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003470 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003471 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003472 else
3473 Diag(Loc, diag::err_attributes_not_allowed);
3474 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003475
3476 SourceLocation EndLoc;
3477
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003479 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003481 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 SourceLocation Loc = Tok.getLocation();
3483
3484 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003485 case tok::code_completion:
3486 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003487 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003488
Reid Spencer5f016e22007-07-11 17:01:13 +00003489 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003490 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3491 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003492 break;
3493 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003494 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3495 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003496 break;
3497 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003498 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3499 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003501
3502 // OpenCL qualifiers:
3503 case tok::kw_private:
3504 if (!getLang().OpenCL)
3505 goto DoneWithTypeQuals;
3506 case tok::kw___private:
3507 case tok::kw___global:
3508 case tok::kw___local:
3509 case tok::kw___constant:
3510 case tok::kw___read_only:
3511 case tok::kw___write_only:
3512 case tok::kw___read_write:
3513 ParseOpenCLQualifiers(DS);
3514 break;
3515
Eli Friedman290eeb02009-06-08 23:27:34 +00003516 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003517 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003518 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003519 case tok::kw___cdecl:
3520 case tok::kw___stdcall:
3521 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003522 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003523 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003524 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003525 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003526 continue;
3527 }
3528 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003529 case tok::kw___pascal:
3530 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003531 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003532 continue;
3533 }
3534 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003536 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003537 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003538 continue; // do *not* consume the next token!
3539 }
3540 // otherwise, FALL THROUGH!
3541 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003542 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003543 // If this is not a type-qualifier token, we're done reading type
3544 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003545 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003546 if (EndLoc.isValid())
3547 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003548 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003549 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003550
Reid Spencer5f016e22007-07-11 17:01:13 +00003551 // If the specifier combination wasn't legal, issue a diagnostic.
3552 if (isInvalid) {
3553 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003554 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003555 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003556 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003557 }
3558}
3559
3560
3561/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3562///
3563void Parser::ParseDeclarator(Declarator &D) {
3564 /// This implements the 'declarator' production in the C grammar, then checks
3565 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003566 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003567}
3568
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003569/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3570/// is parsed by the function passed to it. Pass null, and the direct-declarator
3571/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003572/// ptr-operator production.
3573///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003574/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3575/// [C] pointer[opt] direct-declarator
3576/// [C++] direct-declarator
3577/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003578///
3579/// pointer: [C99 6.7.5]
3580/// '*' type-qualifier-list[opt]
3581/// '*' type-qualifier-list[opt] pointer
3582///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003583/// ptr-operator:
3584/// '*' cv-qualifier-seq[opt]
3585/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003586/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003587/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003588/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003589/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003590void Parser::ParseDeclaratorInternal(Declarator &D,
3591 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003592 if (Diags.hasAllExtensionsSilenced())
3593 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003594
Sebastian Redlf30208a2009-01-24 21:16:55 +00003595 // C++ member pointers start with a '::' or a nested-name.
3596 // Member pointers get special handling, since there's no place for the
3597 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003598 if (getLang().CPlusPlus &&
3599 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3600 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003601 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003602 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003603
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003604 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003605 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003606 // The scope spec really belongs to the direct-declarator.
3607 D.getCXXScopeSpec() = SS;
3608 if (DirectDeclParser)
3609 (this->*DirectDeclParser)(D);
3610 return;
3611 }
3612
3613 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003614 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003615 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003616 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003617 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003618
3619 // Recurse to parse whatever is left.
3620 ParseDeclaratorInternal(D, DirectDeclParser);
3621
3622 // Sema will have to catch (syntactically invalid) pointers into global
3623 // scope. It has to catch pointers into namespace scope anyway.
3624 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003625 Loc),
3626 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003627 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003628 return;
3629 }
3630 }
3631
3632 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003633 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003634 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003635 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003636 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003637 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003638 if (DirectDeclParser)
3639 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003640 return;
3641 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003642
Sebastian Redl05532f22009-03-15 22:02:01 +00003643 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3644 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003645 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003646 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003647
Chris Lattner9af55002009-03-27 04:18:06 +00003648 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003649 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003650 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003651
Reid Spencer5f016e22007-07-11 17:01:13 +00003652 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003653 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003654
Reid Spencer5f016e22007-07-11 17:01:13 +00003655 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003656 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003657 if (Kind == tok::star)
3658 // Remember that we parsed a pointer type, and remember the type-quals.
3659 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003660 DS.getConstSpecLoc(),
3661 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003662 DS.getRestrictSpecLoc()),
3663 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003664 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003665 else
3666 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003667 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003668 Loc),
3669 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003670 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003671 } else {
3672 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003673 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003674
Sebastian Redl743de1f2009-03-23 00:00:23 +00003675 // Complain about rvalue references in C++03, but then go on and build
3676 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003677 if (Kind == tok::ampamp)
3678 Diag(Loc, getLang().CPlusPlus0x ?
3679 diag::warn_cxx98_compat_rvalue_reference :
3680 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003681
Reid Spencer5f016e22007-07-11 17:01:13 +00003682 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3683 // cv-qualifiers are introduced through the use of a typedef or of a
3684 // template type argument, in which case the cv-qualifiers are ignored.
3685 //
3686 // [GNU] Retricted references are allowed.
3687 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003688 // [C++0x] Attributes on references are not allowed.
3689 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003690 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003691
3692 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3693 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3694 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003695 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003696 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3697 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003698 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 }
3700
3701 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003702 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003703
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003704 if (D.getNumTypeObjects() > 0) {
3705 // C++ [dcl.ref]p4: There shall be no references to references.
3706 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3707 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003708 if (const IdentifierInfo *II = D.getIdentifier())
3709 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3710 << II;
3711 else
3712 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3713 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003714
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003715 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003716 // can go ahead and build the (technically ill-formed)
3717 // declarator: reference collapsing will take care of it.
3718 }
3719 }
3720
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003722 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003723 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003724 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003725 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003726 }
3727}
3728
3729/// ParseDirectDeclarator
3730/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003731/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003732/// '(' declarator ')'
3733/// [GNU] '(' attributes declarator ')'
3734/// [C90] direct-declarator '[' constant-expression[opt] ']'
3735/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3736/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3737/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3738/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3739/// direct-declarator '(' parameter-type-list ')'
3740/// direct-declarator '(' identifier-list[opt] ')'
3741/// [GNU] direct-declarator '(' parameter-forward-declarations
3742/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003743/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3744/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003745/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003746///
3747/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003748/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003749/// '::'[opt] nested-name-specifier[opt] type-name
3750///
3751/// id-expression: [C++ 5.1]
3752/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003753/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003754///
3755/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003756/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003757/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003758/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003759/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003760/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003761///
Reid Spencer5f016e22007-07-11 17:01:13 +00003762void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003763 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003764
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003765 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3766 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003767 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003768 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003769 }
3770
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003771 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003772 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003773 // Change the declaration context for name lookup, until this function
3774 // is exited (and the declarator has been parsed).
3775 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003776 }
3777
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003778 // C++0x [dcl.fct]p14:
3779 // There is a syntactic ambiguity when an ellipsis occurs at the end
3780 // of a parameter-declaration-clause without a preceding comma. In
3781 // this case, the ellipsis is parsed as part of the
3782 // abstract-declarator if the type of the parameter names a template
3783 // parameter pack that has not been expanded; otherwise, it is parsed
3784 // as part of the parameter-declaration-clause.
3785 if (Tok.is(tok::ellipsis) &&
3786 !((D.getContext() == Declarator::PrototypeContext ||
3787 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003788 NextToken().is(tok::r_paren) &&
3789 !Actions.containsUnexpandedParameterPacks(D)))
3790 D.setEllipsisLoc(ConsumeToken());
3791
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003792 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3793 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3794 // We found something that indicates the start of an unqualified-id.
3795 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003796 bool AllowConstructorName;
3797 if (D.getDeclSpec().hasTypeSpecifier())
3798 AllowConstructorName = false;
3799 else if (D.getCXXScopeSpec().isSet())
3800 AllowConstructorName =
3801 (D.getContext() == Declarator::FileContext ||
3802 (D.getContext() == Declarator::MemberContext &&
3803 D.getDeclSpec().isFriendSpecified()));
3804 else
3805 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3806
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003807 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3808 /*EnteringContext=*/true,
3809 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003810 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003811 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003812 D.getName()) ||
3813 // Once we're past the identifier, if the scope was bad, mark the
3814 // whole declarator bad.
3815 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003816 D.SetIdentifier(0, Tok.getLocation());
3817 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003818 } else {
3819 // Parsed the unqualified-id; update range information and move along.
3820 if (D.getSourceRange().getBegin().isInvalid())
3821 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3822 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003823 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003824 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003825 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003826 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003827 assert(!getLang().CPlusPlus &&
3828 "There's a C++-specific check for tok::identifier above");
3829 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3830 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3831 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003832 goto PastIdentifier;
3833 }
3834
3835 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 // direct-declarator: '(' declarator ')'
3837 // direct-declarator: '(' attributes declarator ')'
3838 // Example: 'char (*X)' or 'int (*XX)(void)'
3839 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003840
3841 // If the declarator was parenthesized, we entered the declarator
3842 // scope when parsing the parenthesized declarator, then exited
3843 // the scope already. Re-enter the scope, if we need to.
3844 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003845 // If there was an error parsing parenthesized declarator, declarator
3846 // scope may have been enterred before. Don't do it again.
3847 if (!D.isInvalidType() &&
3848 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003849 // Change the declaration context for name lookup, until this function
3850 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003851 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003852 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003853 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003854 // This could be something simple like "int" (in which case the declarator
3855 // portion is empty), if an abstract-declarator is allowed.
3856 D.SetIdentifier(0, Tok.getLocation());
3857 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003858 if (D.getContext() == Declarator::MemberContext)
3859 Diag(Tok, diag::err_expected_member_name_or_semi)
3860 << D.getDeclSpec().getSourceRange();
3861 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003862 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003863 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003864 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003866 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 }
Mike Stump1eb44332009-09-09 15:08:12 +00003868
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003869 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003870 assert(D.isPastIdentifier() &&
3871 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003872
Sean Huntbbd37c62009-11-21 08:43:09 +00003873 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003874 if (D.getIdentifier())
3875 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003876
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003878 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003879 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3880 // In such a case, check if we actually have a function declarator; if it
3881 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003882 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3883 // When not in file scope, warn for ambiguous function declarators, just
3884 // in case the author intended it as a variable definition.
3885 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3886 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3887 break;
3888 }
John McCall0b7e6782011-03-24 11:26:52 +00003889 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003890 BalancedDelimiterTracker T(*this, tok::l_paren);
3891 T.consumeOpen();
3892 ParseFunctionDeclarator(D, attrs, T);
Chris Lattner04d66662007-10-09 17:33:22 +00003893 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 ParseBracketDeclarator(D);
3895 } else {
3896 break;
3897 }
3898 }
3899}
3900
Chris Lattneref4715c2008-04-06 05:45:57 +00003901/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3902/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003903/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003904/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3905///
3906/// direct-declarator:
3907/// '(' declarator ')'
3908/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003909/// direct-declarator '(' parameter-type-list ')'
3910/// direct-declarator '(' identifier-list[opt] ')'
3911/// [GNU] direct-declarator '(' parameter-forward-declarations
3912/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003913///
3914void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003915 BalancedDelimiterTracker T(*this, tok::l_paren);
3916 T.consumeOpen();
3917
Chris Lattneref4715c2008-04-06 05:45:57 +00003918 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003919
Chris Lattner7399ee02008-10-20 02:05:46 +00003920 // Eat any attributes before we look at whether this is a grouping or function
3921 // declarator paren. If this is a grouping paren, the attribute applies to
3922 // the type being built up, for example:
3923 // int (__attribute__(()) *x)(long y)
3924 // If this ends up not being a grouping paren, the attribute applies to the
3925 // first argument, for example:
3926 // int (__attribute__(()) int x)
3927 // In either case, we need to eat any attributes to be able to determine what
3928 // sort of paren this is.
3929 //
John McCall0b7e6782011-03-24 11:26:52 +00003930 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003931 bool RequiresArg = false;
3932 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003933 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003934
Chris Lattner7399ee02008-10-20 02:05:46 +00003935 // We require that the argument list (if this is a non-grouping paren) be
3936 // present even if the attribute list was empty.
3937 RequiresArg = true;
3938 }
Steve Naroff239f0732008-12-25 14:16:32 +00003939 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003940 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003941 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003942 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003943 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003944 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003945 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003946 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003947 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003948 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003949
Chris Lattneref4715c2008-04-06 05:45:57 +00003950 // If we haven't past the identifier yet (or where the identifier would be
3951 // stored, if this is an abstract declarator), then this is probably just
3952 // grouping parens. However, if this could be an abstract-declarator, then
3953 // this could also be the start of function arguments (consider 'void()').
3954 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003955
Chris Lattneref4715c2008-04-06 05:45:57 +00003956 if (!D.mayOmitIdentifier()) {
3957 // If this can't be an abstract-declarator, this *must* be a grouping
3958 // paren, because we haven't seen the identifier yet.
3959 isGrouping = true;
3960 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003961 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003962 isDeclarationSpecifier()) { // 'int(int)' is a function.
3963 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3964 // considered to be a type, not a K&R identifier-list.
3965 isGrouping = false;
3966 } else {
3967 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3968 isGrouping = true;
3969 }
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Chris Lattneref4715c2008-04-06 05:45:57 +00003971 // If this is a grouping paren, handle:
3972 // direct-declarator: '(' declarator ')'
3973 // direct-declarator: '(' attributes declarator ')'
3974 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003975 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003976 D.setGroupingParens(true);
3977
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003978 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003979 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003980 T.consumeClose();
3981 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3982 T.getCloseLocation()),
3983 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003984
3985 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003986 return;
3987 }
Mike Stump1eb44332009-09-09 15:08:12 +00003988
Chris Lattneref4715c2008-04-06 05:45:57 +00003989 // Okay, if this wasn't a grouping paren, it must be the start of a function
3990 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003991 // identifier (and remember where it would have been), then call into
3992 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003993 D.SetIdentifier(0, Tok.getLocation());
3994
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003995 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003996}
3997
3998/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3999/// declarator D up to a paren, which indicates that we are parsing function
4000/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004001///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004002/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004003/// after the open paren - they should be considered to be the first argument of
4004/// a parameter. If RequiresArg is true, then the first argument of the
4005/// function is required to be present and required to not be an identifier
4006/// list.
4007///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004008/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4009/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4010/// (C++0x) trailing-return-type[opt].
4011///
4012/// [C++0x] exception-specification:
4013/// dynamic-exception-specification
4014/// noexcept-specification
4015///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004016void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004017 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004018 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004019 bool RequiresArg) {
4020 // lparen is already consumed!
4021 assert(D.isPastIdentifier() && "Should not call before identifier!");
4022
4023 // This should be true when the function has typed arguments.
4024 // Otherwise, it is treated as a K&R-style function.
4025 bool HasProto = false;
4026 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004027 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004028 // Remember where we see an ellipsis, if any.
4029 SourceLocation EllipsisLoc;
4030
4031 DeclSpec DS(AttrFactory);
4032 bool RefQualifierIsLValueRef = true;
4033 SourceLocation RefQualifierLoc;
4034 ExceptionSpecificationType ESpecType = EST_None;
4035 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004036 SmallVector<ParsedType, 2> DynamicExceptions;
4037 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004038 ExprResult NoexceptExpr;
4039 ParsedType TrailingReturnType;
4040
4041 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004042 if (isFunctionDeclaratorIdentifierList()) {
4043 if (RequiresArg)
4044 Diag(Tok, diag::err_argument_required_after_attribute);
4045
4046 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4047
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004048 Tracker.consumeClose();
4049 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004050 } else {
4051 // Enter function-declaration scope, limiting any declarators to the
4052 // function prototype scope, including parameter declarators.
4053 ParseScope PrototypeScope(this,
4054 Scope::FunctionPrototypeScope|Scope::DeclScope);
4055
4056 if (Tok.isNot(tok::r_paren))
4057 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4058 else if (RequiresArg)
4059 Diag(Tok, diag::err_argument_required_after_attribute);
4060
4061 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4062
4063 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004064 Tracker.consumeClose();
4065 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004066
4067 if (getLang().CPlusPlus) {
4068 MaybeParseCXX0XAttributes(attrs);
4069
4070 // Parse cv-qualifier-seq[opt].
4071 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4072 if (!DS.getSourceRange().getEnd().isInvalid())
4073 EndLoc = DS.getSourceRange().getEnd();
4074
4075 // Parse ref-qualifier[opt].
4076 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004077 Diag(Tok, getLang().CPlusPlus0x ?
4078 diag::warn_cxx98_compat_ref_qualifier :
4079 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004080
4081 RefQualifierIsLValueRef = Tok.is(tok::amp);
4082 RefQualifierLoc = ConsumeToken();
4083 EndLoc = RefQualifierLoc;
4084 }
4085
4086 // Parse exception-specification[opt].
4087 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4088 DynamicExceptions,
4089 DynamicExceptionRanges,
4090 NoexceptExpr);
4091 if (ESpecType != EST_None)
4092 EndLoc = ESpecRange.getEnd();
4093
4094 // Parse trailing-return-type[opt].
4095 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004096 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004097 SourceRange Range;
4098 TrailingReturnType = ParseTrailingReturnType(Range).get();
4099 if (Range.getEnd().isValid())
4100 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004101 }
4102 }
4103
4104 // Leave prototype scope.
4105 PrototypeScope.Exit();
4106 }
4107
4108 // Remember that we parsed a function type, and remember the attributes.
4109 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4110 /*isVariadic=*/EllipsisLoc.isValid(),
4111 EllipsisLoc,
4112 ParamInfo.data(), ParamInfo.size(),
4113 DS.getTypeQualifiers(),
4114 RefQualifierIsLValueRef,
4115 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004116 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004117 ESpecType, ESpecRange.getBegin(),
4118 DynamicExceptions.data(),
4119 DynamicExceptionRanges.data(),
4120 DynamicExceptions.size(),
4121 NoexceptExpr.isUsable() ?
4122 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004123 Tracker.getOpenLocation(),
4124 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004125 TrailingReturnType),
4126 attrs, EndLoc);
4127}
4128
4129/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4130/// identifier list form for a K&R-style function: void foo(a,b,c)
4131///
4132/// Note that identifier-lists are only allowed for normal declarators, not for
4133/// abstract-declarators.
4134bool Parser::isFunctionDeclaratorIdentifierList() {
4135 return !getLang().CPlusPlus
4136 && Tok.is(tok::identifier)
4137 && !TryAltiVecVectorToken()
4138 // K&R identifier lists can't have typedefs as identifiers, per C99
4139 // 6.7.5.3p11.
4140 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4141 // Identifier lists follow a really simple grammar: the identifiers can
4142 // be followed *only* by a ", identifier" or ")". However, K&R
4143 // identifier lists are really rare in the brave new modern world, and
4144 // it is very common for someone to typo a type in a non-K&R style
4145 // list. If we are presented with something like: "void foo(intptr x,
4146 // float y)", we don't want to start parsing the function declarator as
4147 // though it is a K&R style declarator just because intptr is an
4148 // invalid type.
4149 //
4150 // To handle this, we check to see if the token after the first
4151 // identifier is a "," or ")". Only then do we parse it as an
4152 // identifier list.
4153 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4154}
4155
4156/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4157/// we found a K&R-style identifier list instead of a typed parameter list.
4158///
4159/// After returning, ParamInfo will hold the parsed parameters.
4160///
4161/// identifier-list: [C99 6.7.5]
4162/// identifier
4163/// identifier-list ',' identifier
4164///
4165void Parser::ParseFunctionDeclaratorIdentifierList(
4166 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004167 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004168 // If there was no identifier specified for the declarator, either we are in
4169 // an abstract-declarator, or we are in a parameter declarator which was found
4170 // to be abstract. In abstract-declarators, identifier lists are not valid:
4171 // diagnose this.
4172 if (!D.getIdentifier())
4173 Diag(Tok, diag::ext_ident_list_in_param);
4174
4175 // Maintain an efficient lookup of params we have seen so far.
4176 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4177
4178 while (1) {
4179 // If this isn't an identifier, report the error and skip until ')'.
4180 if (Tok.isNot(tok::identifier)) {
4181 Diag(Tok, diag::err_expected_ident);
4182 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4183 // Forget we parsed anything.
4184 ParamInfo.clear();
4185 return;
4186 }
4187
4188 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4189
4190 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4191 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4192 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4193
4194 // Verify that the argument identifier has not already been mentioned.
4195 if (!ParamsSoFar.insert(ParmII)) {
4196 Diag(Tok, diag::err_param_redefinition) << ParmII;
4197 } else {
4198 // Remember this identifier in ParamInfo.
4199 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4200 Tok.getLocation(),
4201 0));
4202 }
4203
4204 // Eat the identifier.
4205 ConsumeToken();
4206
4207 // The list continues if we see a comma.
4208 if (Tok.isNot(tok::comma))
4209 break;
4210 ConsumeToken();
4211 }
4212}
4213
4214/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4215/// after the opening parenthesis. This function will not parse a K&R-style
4216/// identifier list.
4217///
4218/// D is the declarator being parsed. If attrs is non-null, then the caller
4219/// parsed those arguments immediately after the open paren - they should be
4220/// considered to be the first argument of a parameter.
4221///
4222/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4223/// be the location of the ellipsis, if any was parsed.
4224///
Reid Spencer5f016e22007-07-11 17:01:13 +00004225/// parameter-type-list: [C99 6.7.5]
4226/// parameter-list
4227/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004228/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004229///
4230/// parameter-list: [C99 6.7.5]
4231/// parameter-declaration
4232/// parameter-list ',' parameter-declaration
4233///
4234/// parameter-declaration: [C99 6.7.5]
4235/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004236/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004237/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004238/// declaration-specifiers abstract-declarator[opt]
4239/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004240/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004241/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4242///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004243void Parser::ParseParameterDeclarationClause(
4244 Declarator &D,
4245 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004246 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004247 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004248
Chris Lattnerf97409f2008-04-06 06:57:35 +00004249 while (1) {
4250 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004251 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004252 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004253 }
Mike Stump1eb44332009-09-09 15:08:12 +00004254
Chris Lattnerf97409f2008-04-06 06:57:35 +00004255 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004256 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004257 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004258
John McCall7f040a92010-12-24 02:08:15 +00004259 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004260 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004261 ParseMicrosoftAttributes(DS.getAttributes());
4262
4263 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004264
4265 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004266 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004267 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4268 // attributes lost? Should they even be allowed?
4269 // FIXME: If we can leave the attributes in the token stream somehow, we can
4270 // get rid of a parameter (attrs) and this statement. It might be too much
4271 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004272 DS.takeAttributesFrom(attrs);
4273
Chris Lattnere64c5492009-02-27 18:38:20 +00004274 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004275
Chris Lattnerf97409f2008-04-06 06:57:35 +00004276 // Parse the declarator. This is "PrototypeContext", because we must
4277 // accept either 'declarator' or 'abstract-declarator' here.
4278 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4279 ParseDeclarator(ParmDecl);
4280
4281 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004282 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004283
Chris Lattnerf97409f2008-04-06 06:57:35 +00004284 // Remember this parsed parameter in ParamInfo.
4285 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004286
Douglas Gregor72b505b2008-12-16 21:30:33 +00004287 // DefArgToks is used when the parsing of default arguments needs
4288 // to be delayed.
4289 CachedTokens *DefArgToks = 0;
4290
Chris Lattnerf97409f2008-04-06 06:57:35 +00004291 // If no parameter was specified, verify that *something* was specified,
4292 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004293 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4294 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004295 // Completely missing, emit error.
4296 Diag(DSStart, diag::err_missing_param);
4297 } else {
4298 // Otherwise, we have something. Add it and let semantic analysis try
4299 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004300
Chris Lattnerf97409f2008-04-06 06:57:35 +00004301 // Inform the actions module about the parameter declarator, so it gets
4302 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004303 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004304
4305 // Parse the default argument, if any. We parse the default
4306 // arguments in all dialects; the semantic analysis in
4307 // ActOnParamDefaultArgument will reject the default argument in
4308 // C.
4309 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004310 SourceLocation EqualLoc = Tok.getLocation();
4311
Chris Lattner04421082008-04-08 04:40:51 +00004312 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004313 if (D.getContext() == Declarator::MemberContext) {
4314 // If we're inside a class definition, cache the tokens
4315 // corresponding to the default argument. We'll actually parse
4316 // them when we see the end of the class definition.
4317 // FIXME: Templates will require something similar.
4318 // FIXME: Can we use a smart pointer for Toks?
4319 DefArgToks = new CachedTokens;
4320
Mike Stump1eb44332009-09-09 15:08:12 +00004321 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004322 /*StopAtSemi=*/true,
4323 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004324 delete DefArgToks;
4325 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004326 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004327 } else {
4328 // Mark the end of the default argument so that we know when to
4329 // stop when we parse it later on.
4330 Token DefArgEnd;
4331 DefArgEnd.startToken();
4332 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4333 DefArgEnd.setLocation(Tok.getLocation());
4334 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004335 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004336 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004337 }
Chris Lattner04421082008-04-08 04:40:51 +00004338 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004339 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004340 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004341
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004342 // The argument isn't actually potentially evaluated unless it is
4343 // used.
4344 EnterExpressionEvaluationContext Eval(Actions,
4345 Sema::PotentiallyEvaluatedIfUsed);
4346
John McCall60d7b3a2010-08-24 06:29:42 +00004347 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004348 if (DefArgResult.isInvalid()) {
4349 Actions.ActOnParamDefaultArgumentError(Param);
4350 SkipUntil(tok::comma, tok::r_paren, true, true);
4351 } else {
4352 // Inform the actions module about the default argument
4353 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004354 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004355 }
Chris Lattner04421082008-04-08 04:40:51 +00004356 }
4357 }
Mike Stump1eb44332009-09-09 15:08:12 +00004358
4359 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4360 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004361 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004362 }
4363
4364 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004365 if (Tok.isNot(tok::comma)) {
4366 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004367 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4368
4369 if (!getLang().CPlusPlus) {
4370 // We have ellipsis without a preceding ',', which is ill-formed
4371 // in C. Complain and provide the fix.
4372 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004373 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004374 }
4375 }
4376
4377 break;
4378 }
Mike Stump1eb44332009-09-09 15:08:12 +00004379
Chris Lattnerf97409f2008-04-06 06:57:35 +00004380 // Consume the comma.
4381 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004382 }
Mike Stump1eb44332009-09-09 15:08:12 +00004383
Chris Lattner66d28652008-04-06 06:34:08 +00004384}
Chris Lattneref4715c2008-04-06 05:45:57 +00004385
Reid Spencer5f016e22007-07-11 17:01:13 +00004386/// [C90] direct-declarator '[' constant-expression[opt] ']'
4387/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4388/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4389/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4390/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4391void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004392 BalancedDelimiterTracker T(*this, tok::l_square);
4393 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004394
Chris Lattner378c7e42008-12-18 07:27:21 +00004395 // C array syntax has many features, but by-far the most common is [] and [4].
4396 // This code does a fast path to handle some of the most obvious cases.
4397 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004398 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004399 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004400 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004401
Chris Lattner378c7e42008-12-18 07:27:21 +00004402 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004403 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004404 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004405 T.getOpenLocation(),
4406 T.getCloseLocation()),
4407 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004408 return;
4409 } else if (Tok.getKind() == tok::numeric_constant &&
4410 GetLookAheadToken(1).is(tok::r_square)) {
4411 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004412 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004413 ConsumeToken();
4414
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004415 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004416 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004417 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004418
Chris Lattner378c7e42008-12-18 07:27:21 +00004419 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004420 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004421 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004422 T.getOpenLocation(),
4423 T.getCloseLocation()),
4424 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004425 return;
4426 }
Mike Stump1eb44332009-09-09 15:08:12 +00004427
Reid Spencer5f016e22007-07-11 17:01:13 +00004428 // If valid, this location is the position where we read the 'static' keyword.
4429 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004430 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004431 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004432
Reid Spencer5f016e22007-07-11 17:01:13 +00004433 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004434 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004435 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004436 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004437
Reid Spencer5f016e22007-07-11 17:01:13 +00004438 // If we haven't already read 'static', check to see if there is one after the
4439 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004440 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004441 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004442
Reid Spencer5f016e22007-07-11 17:01:13 +00004443 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4444 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004445 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004446
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004447 // Handle the case where we have '[*]' as the array size. However, a leading
4448 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4449 // the the token after the star is a ']'. Since stars in arrays are
4450 // infrequent, use of lookahead is not costly here.
4451 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004452 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004453
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004454 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004455 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004456 StaticLoc = SourceLocation(); // Drop the static.
4457 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004458 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004459 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004460 // Note, in C89, this production uses the constant-expr production instead
4461 // of assignment-expr. The only difference is that assignment-expr allows
4462 // things like '=' and '*='. Sema rejects these in C89 mode because they
4463 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004464
Douglas Gregore0762c92009-06-19 23:52:42 +00004465 // Parse the constant-expression or assignment-expression now (depending
4466 // on dialect).
4467 if (getLang().CPlusPlus)
4468 NumElements = ParseConstantExpression();
4469 else
4470 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004471 }
Mike Stump1eb44332009-09-09 15:08:12 +00004472
Reid Spencer5f016e22007-07-11 17:01:13 +00004473 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004474 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004475 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004476 // If the expression was invalid, skip it.
4477 SkipUntil(tok::r_square);
4478 return;
4479 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004480
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004481 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004482
John McCall0b7e6782011-03-24 11:26:52 +00004483 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004484 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004485
Chris Lattner378c7e42008-12-18 07:27:21 +00004486 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004487 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004488 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004489 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004490 T.getOpenLocation(),
4491 T.getCloseLocation()),
4492 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004493}
4494
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004495/// [GNU] typeof-specifier:
4496/// typeof ( expressions )
4497/// typeof ( type-name )
4498/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004499///
4500void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004501 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004502 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004503 SourceLocation StartLoc = ConsumeToken();
4504
John McCallcfb708c2010-01-13 20:03:27 +00004505 const bool hasParens = Tok.is(tok::l_paren);
4506
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004507 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004508 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004509 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004510 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4511 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004512 if (hasParens)
4513 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004514
4515 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004516 // FIXME: Not accurate, the range gets one token more than it should.
4517 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004518 else
4519 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004520
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004521 if (isCastExpr) {
4522 if (!CastTy) {
4523 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004524 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004525 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004526
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004527 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004528 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004529 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4530 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004531 DiagID, CastTy))
4532 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004533 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004534 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004535
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004536 // If we get here, the operand to the typeof was an expresion.
4537 if (Operand.isInvalid()) {
4538 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004539 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004540 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004541
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004542 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004543 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004544 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4545 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004546 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004547 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004548}
Chris Lattner1b492422010-02-28 18:33:55 +00004549
Eli Friedmanb001de72011-10-06 23:00:33 +00004550/// [C1X] atomic-specifier:
4551/// _Atomic ( type-name )
4552///
4553void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4554 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4555
4556 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004557 BalancedDelimiterTracker T(*this, tok::l_paren);
4558 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004559 SkipUntil(tok::r_paren);
4560 return;
4561 }
4562
4563 TypeResult Result = ParseTypeName();
4564 if (Result.isInvalid()) {
4565 SkipUntil(tok::r_paren);
4566 return;
4567 }
4568
4569 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004570 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004571
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004572 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004573 return;
4574
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004575 DS.setTypeofParensRange(T.getRange());
4576 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004577
4578 const char *PrevSpec = 0;
4579 unsigned DiagID;
4580 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4581 DiagID, Result.release()))
4582 Diag(StartLoc, DiagID) << PrevSpec;
4583}
4584
Chris Lattner1b492422010-02-28 18:33:55 +00004585
4586/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4587/// from TryAltiVecVectorToken.
4588bool Parser::TryAltiVecVectorTokenOutOfLine() {
4589 Token Next = NextToken();
4590 switch (Next.getKind()) {
4591 default: return false;
4592 case tok::kw_short:
4593 case tok::kw_long:
4594 case tok::kw_signed:
4595 case tok::kw_unsigned:
4596 case tok::kw_void:
4597 case tok::kw_char:
4598 case tok::kw_int:
4599 case tok::kw_float:
4600 case tok::kw_double:
4601 case tok::kw_bool:
4602 case tok::kw___pixel:
4603 Tok.setKind(tok::kw___vector);
4604 return true;
4605 case tok::identifier:
4606 if (Next.getIdentifierInfo() == Ident_pixel) {
4607 Tok.setKind(tok::kw___vector);
4608 return true;
4609 }
4610 return false;
4611 }
4612}
4613
4614bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4615 const char *&PrevSpec, unsigned &DiagID,
4616 bool &isInvalid) {
4617 if (Tok.getIdentifierInfo() == Ident_vector) {
4618 Token Next = NextToken();
4619 switch (Next.getKind()) {
4620 case tok::kw_short:
4621 case tok::kw_long:
4622 case tok::kw_signed:
4623 case tok::kw_unsigned:
4624 case tok::kw_void:
4625 case tok::kw_char:
4626 case tok::kw_int:
4627 case tok::kw_float:
4628 case tok::kw_double:
4629 case tok::kw_bool:
4630 case tok::kw___pixel:
4631 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4632 return true;
4633 case tok::identifier:
4634 if (Next.getIdentifierInfo() == Ident_pixel) {
4635 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4636 return true;
4637 }
4638 break;
4639 default:
4640 break;
4641 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004642 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004643 DS.isTypeAltiVecVector()) {
4644 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4645 return true;
4646 }
4647 return false;
4648}