blob: 0c60e287414eaf964456e82977e8250eabe194ac [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)) {
251 Diag(Tok, diag::err_iboutletcollection_with_protocol);
252 SkipUntil(tok::r_paren, false, true); // skip until ')'
253 }
254 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000255
256 SourceLocation RParen = Tok.getLocation();
257 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
258 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000259 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000260 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
261 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
262 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000263 }
264}
265
266
Eli Friedmana23b4852009-06-08 07:21:15 +0000267/// ParseMicrosoftDeclSpec - Parse an __declspec construct
268///
269/// [MS] decl-specifier:
270/// __declspec ( extended-decl-modifier-seq )
271///
272/// [MS] extended-decl-modifier-seq:
273/// extended-decl-modifier[opt]
274/// extended-decl-modifier extended-decl-modifier-seq
275
John McCall7f040a92010-12-24 02:08:15 +0000276void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000277 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000278
Steve Narofff59e17e2008-12-24 20:59:21 +0000279 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000280 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
281 "declspec")) {
282 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000283 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000284 }
Francois Pichet373197b2011-05-07 19:04:49 +0000285
Eli Friedman290eeb02009-06-08 23:27:34 +0000286 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000287 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
288 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000289
290 // FIXME: Remove this when we have proper __declspec(property()) support.
291 // Just skip everything inside property().
292 if (AttrName->getName() == "property") {
293 ConsumeParen();
294 SkipUntil(tok::r_paren);
295 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000296 if (Tok.is(tok::l_paren)) {
297 ConsumeParen();
298 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
299 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000300 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000301 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000302 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000303 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
304 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000305 }
306 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
307 SkipUntil(tok::r_paren, false);
308 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000309 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
310 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 }
312 }
313 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
314 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000315 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000316}
317
John McCall7f040a92010-12-24 02:08:15 +0000318void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000319 // Treat these like attributes
320 // FIXME: Allow Sema to distinguish between these and real attributes!
321 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000322 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000323 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000324 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000325 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000326 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
327 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000328 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
329 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000330 // FIXME: Support these properly!
331 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000332 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
333 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000334 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000335}
336
John McCall7f040a92010-12-24 02:08:15 +0000337void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000338 // Treat these like attributes
339 while (Tok.is(tok::kw___pascal)) {
340 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
341 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000344 }
John McCall7f040a92010-12-24 02:08:15 +0000345}
346
Peter Collingbournef315fa82011-02-14 01:42:53 +0000347void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
348 // Treat these like attributes
349 while (Tok.is(tok::kw___kernel)) {
350 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000351 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
352 AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000354 }
355}
356
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000357void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
358 SourceLocation Loc = Tok.getLocation();
359 switch(Tok.getKind()) {
360 // OpenCL qualifiers:
361 case tok::kw___private:
362 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000363 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000365 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000366 break;
367
368 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000369 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000371 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372 break;
373
374 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000375 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000377 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 break;
379
380 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 break;
385
386 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000387 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000389 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000390 break;
391
392 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000393 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000395 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000396 break;
397
398 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000399 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000401 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000402 break;
403 default: break;
404 }
405}
406
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000407/// \brief Parse a version number.
408///
409/// version:
410/// simple-integer
411/// simple-integer ',' simple-integer
412/// simple-integer ',' simple-integer ',' simple-integer
413VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
414 Range = Tok.getLocation();
415
416 if (!Tok.is(tok::numeric_constant)) {
417 Diag(Tok, diag::err_expected_version);
418 SkipUntil(tok::comma, tok::r_paren, true, true, true);
419 return VersionTuple();
420 }
421
422 // Parse the major (and possibly minor and subminor) versions, which
423 // are stored in the numeric constant. We utilize a quirk of the
424 // lexer, which is that it handles something like 1.2.3 as a single
425 // numeric constant, rather than two separate tokens.
426 llvm::SmallString<512> Buffer;
427 Buffer.resize(Tok.getLength()+1);
428 const char *ThisTokBegin = &Buffer[0];
429
430 // Get the spelling of the token, which eliminates trigraphs, etc.
431 bool Invalid = false;
432 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
433 if (Invalid)
434 return VersionTuple();
435
436 // Parse the major version.
437 unsigned AfterMajor = 0;
438 unsigned Major = 0;
439 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
440 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
441 ++AfterMajor;
442 }
443
444 if (AfterMajor == 0) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 if (AfterMajor == ActualLength) {
451 ConsumeToken();
452
453 // We only had a single version component.
454 if (Major == 0) {
455 Diag(Tok, diag::err_zero_version);
456 return VersionTuple();
457 }
458
459 return VersionTuple(Major);
460 }
461
462 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
463 Diag(Tok, diag::err_expected_version);
464 SkipUntil(tok::comma, tok::r_paren, true, true, true);
465 return VersionTuple();
466 }
467
468 // Parse the minor version.
469 unsigned AfterMinor = AfterMajor + 1;
470 unsigned Minor = 0;
471 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
472 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
473 ++AfterMinor;
474 }
475
476 if (AfterMinor == ActualLength) {
477 ConsumeToken();
478
479 // We had major.minor.
480 if (Major == 0 && Minor == 0) {
481 Diag(Tok, diag::err_zero_version);
482 return VersionTuple();
483 }
484
485 return VersionTuple(Major, Minor);
486 }
487
488 // If what follows is not a '.', we have a problem.
489 if (ThisTokBegin[AfterMinor] != '.') {
490 Diag(Tok, diag::err_expected_version);
491 SkipUntil(tok::comma, tok::r_paren, true, true, true);
492 return VersionTuple();
493 }
494
495 // Parse the subminor version.
496 unsigned AfterSubminor = AfterMinor + 1;
497 unsigned Subminor = 0;
498 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
499 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
500 ++AfterSubminor;
501 }
502
503 if (AfterSubminor != ActualLength) {
504 Diag(Tok, diag::err_expected_version);
505 SkipUntil(tok::comma, tok::r_paren, true, true, true);
506 return VersionTuple();
507 }
508 ConsumeToken();
509 return VersionTuple(Major, Minor, Subminor);
510}
511
512/// \brief Parse the contents of the "availability" attribute.
513///
514/// availability-attribute:
515/// 'availability' '(' platform ',' version-arg-list ')'
516///
517/// platform:
518/// identifier
519///
520/// version-arg-list:
521/// version-arg
522/// version-arg ',' version-arg-list
523///
524/// version-arg:
525/// 'introduced' '=' version
526/// 'deprecated' '=' version
527/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000528/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000529void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
530 SourceLocation AvailabilityLoc,
531 ParsedAttributes &attrs,
532 SourceLocation *endLoc) {
533 SourceLocation PlatformLoc;
534 IdentifierInfo *Platform = 0;
535
536 enum { Introduced, Deprecated, Obsoleted, Unknown };
537 AvailabilityChange Changes[Unknown];
538
539 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000540 BalancedDelimiterTracker T(*this, tok::l_paren);
541 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000542 Diag(Tok, diag::err_expected_lparen);
543 return;
544 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545
546 // Parse the platform name,
547 if (Tok.isNot(tok::identifier)) {
548 Diag(Tok, diag::err_availability_expected_platform);
549 SkipUntil(tok::r_paren);
550 return;
551 }
552 Platform = Tok.getIdentifierInfo();
553 PlatformLoc = ConsumeToken();
554
555 // Parse the ',' following the platform name.
556 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
557 return;
558
559 // If we haven't grabbed the pointers for the identifiers
560 // "introduced", "deprecated", and "obsoleted", do so now.
561 if (!Ident_introduced) {
562 Ident_introduced = PP.getIdentifierInfo("introduced");
563 Ident_deprecated = PP.getIdentifierInfo("deprecated");
564 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000565 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000566 }
567
568 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000569 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000570 do {
571 if (Tok.isNot(tok::identifier)) {
572 Diag(Tok, diag::err_availability_expected_change);
573 SkipUntil(tok::r_paren);
574 return;
575 }
576 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
577 SourceLocation KeywordLoc = ConsumeToken();
578
Douglas Gregorb53e4172011-03-26 03:35:55 +0000579 if (Keyword == Ident_unavailable) {
580 if (UnavailableLoc.isValid()) {
581 Diag(KeywordLoc, diag::err_availability_redundant)
582 << Keyword << SourceRange(UnavailableLoc);
583 }
584 UnavailableLoc = KeywordLoc;
585
586 if (Tok.isNot(tok::comma))
587 break;
588
589 ConsumeToken();
590 continue;
591 }
592
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000593 if (Tok.isNot(tok::equal)) {
594 Diag(Tok, diag::err_expected_equal_after)
595 << Keyword;
596 SkipUntil(tok::r_paren);
597 return;
598 }
599 ConsumeToken();
600
601 SourceRange VersionRange;
602 VersionTuple Version = ParseVersionTuple(VersionRange);
603
604 if (Version.empty()) {
605 SkipUntil(tok::r_paren);
606 return;
607 }
608
609 unsigned Index;
610 if (Keyword == Ident_introduced)
611 Index = Introduced;
612 else if (Keyword == Ident_deprecated)
613 Index = Deprecated;
614 else if (Keyword == Ident_obsoleted)
615 Index = Obsoleted;
616 else
617 Index = Unknown;
618
619 if (Index < Unknown) {
620 if (!Changes[Index].KeywordLoc.isInvalid()) {
621 Diag(KeywordLoc, diag::err_availability_redundant)
622 << Keyword
623 << SourceRange(Changes[Index].KeywordLoc,
624 Changes[Index].VersionRange.getEnd());
625 }
626
627 Changes[Index].KeywordLoc = KeywordLoc;
628 Changes[Index].Version = Version;
629 Changes[Index].VersionRange = VersionRange;
630 } else {
631 Diag(KeywordLoc, diag::err_availability_unknown_change)
632 << Keyword << VersionRange;
633 }
634
635 if (Tok.isNot(tok::comma))
636 break;
637
638 ConsumeToken();
639 } while (true);
640
641 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000642 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000643 return;
644
645 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000646 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000647
Douglas Gregorb53e4172011-03-26 03:35:55 +0000648 // The 'unavailable' availability cannot be combined with any other
649 // availability changes. Make sure that hasn't happened.
650 if (UnavailableLoc.isValid()) {
651 bool Complained = false;
652 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
653 if (Changes[Index].KeywordLoc.isValid()) {
654 if (!Complained) {
655 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
656 << SourceRange(Changes[Index].KeywordLoc,
657 Changes[Index].VersionRange.getEnd());
658 Complained = true;
659 }
660
661 // Clear out the availability.
662 Changes[Index] = AvailabilityChange();
663 }
664 }
665 }
666
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000667 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000668 attrs.addNew(&Availability,
669 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000670 0, SourceLocation(),
671 Platform, PlatformLoc,
672 Changes[Introduced],
673 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000674 Changes[Obsoleted],
675 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676}
677
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000678
679// Late Parsed Attributes:
680// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
681
682void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
683
684void Parser::LateParsedClass::ParseLexedAttributes() {
685 Self->ParseLexedAttributes(*Class);
686}
687
688void Parser::LateParsedAttribute::ParseLexedAttributes() {
689 Self->ParseLexedAttribute(*this);
690}
691
692/// Wrapper class which calls ParseLexedAttribute, after setting up the
693/// scope appropriately.
694void Parser::ParseLexedAttributes(ParsingClass &Class) {
695 // Deal with templates
696 // FIXME: Test cases to make sure this does the right thing for templates.
697 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
698 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
699 HasTemplateScope);
700 if (HasTemplateScope)
701 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
702
703 // Set or update the scope flags to include Scope::ThisScope.
704 bool AlreadyHasClassScope = Class.TopLevelClass;
705 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
706 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
707 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
708
709 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
710 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
711 }
712}
713
714/// \brief Finish parsing an attribute for which parsing was delayed.
715/// This will be called at the end of parsing a class declaration
716/// for each LateParsedAttribute. We consume the saved tokens and
717/// create an attribute with the arguments filled in. We add this
718/// to the Attribute list for the decl.
719void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
720 // Save the current token position.
721 SourceLocation OrigLoc = Tok.getLocation();
722
723 // Append the current token at the end of the new token stream so that it
724 // doesn't get lost.
725 LA.Toks.push_back(Tok);
726 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
727 // Consume the previously pushed token.
728 ConsumeAnyToken();
729
730 ParsedAttributes Attrs(AttrFactory);
731 SourceLocation endLoc;
732
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000733 // If the Decl is templatized, add template parameters to scope.
734 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
735 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
736 if (HasTemplateScope)
737 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
738
739 // If the Decl is on a function, add function parameters to the scope.
740 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
741 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
742 if (HasFunctionScope)
743 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
744
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000745 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
746
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000747 if (HasFunctionScope) {
748 Actions.ActOnExitFunctionContext();
749 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
750 }
751 if (HasTemplateScope) {
752 TempScope.Exit();
753 }
754
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755 // Late parsed attributes must be attached to Decls by hand. If the
756 // LA.D is not set, then this was not done properly.
757 assert(LA.D && "No decl attached to late parsed attribute");
758 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
759
760 if (Tok.getLocation() != OrigLoc) {
761 // Due to a parsing error, we either went over the cached tokens or
762 // there are still cached tokens left, so we skip the leftover tokens.
763 // Since this is an uncommon situation that should be avoided, use the
764 // expensive isBeforeInTranslationUnit call.
765 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
766 OrigLoc))
767 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
768 ConsumeAnyToken();
769 }
770}
771
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000772/// \brief Wrapper around a case statement checking if AttrName is
773/// one of the thread safety attributes
774bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
775 return llvm::StringSwitch<bool>(AttrName)
776 .Case("guarded_by", true)
777 .Case("guarded_var", true)
778 .Case("pt_guarded_by", true)
779 .Case("pt_guarded_var", true)
780 .Case("lockable", true)
781 .Case("scoped_lockable", true)
782 .Case("no_thread_safety_analysis", true)
783 .Case("acquired_after", true)
784 .Case("acquired_before", true)
785 .Case("exclusive_lock_function", true)
786 .Case("shared_lock_function", true)
787 .Case("exclusive_trylock_function", true)
788 .Case("shared_trylock_function", true)
789 .Case("unlock_function", true)
790 .Case("lock_returned", true)
791 .Case("locks_excluded", true)
792 .Case("exclusive_locks_required", true)
793 .Case("shared_locks_required", true)
794 .Default(false);
795}
796
797/// \brief Parse the contents of thread safety attributes. These
798/// should always be parsed as an expression list.
799///
800/// We need to special case the parsing due to the fact that if the first token
801/// of the first argument is an identifier, the main parse loop will store
802/// that token as a "parameter" and the rest of
803/// the arguments will be added to a list of "arguments". However,
804/// subsequent tokens in the first argument are lost. We instead parse each
805/// argument as an expression and add all arguments to the list of "arguments".
806/// In future, we will take advantage of this special case to also
807/// deal with some argument scoping issues here (for example, referring to a
808/// function parameter in the attribute on that function).
809void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
810 SourceLocation AttrNameLoc,
811 ParsedAttributes &Attrs,
812 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000813 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000815 BalancedDelimiterTracker T(*this, tok::l_paren);
816 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000817
818 ExprVector ArgExprs(Actions);
819 bool ArgExprsOk = true;
820
821 // now parse the list of expressions
822 while (1) {
823 ExprResult ArgExpr(ParseAssignmentExpression());
824 if (ArgExpr.isInvalid()) {
825 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000826 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827 break;
828 } else {
829 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000830 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831 if (Tok.isNot(tok::comma))
832 break;
833 ConsumeToken(); // Eat the comma, move to the next argument
834 }
835 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000836 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
838 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000840 if (EndLoc)
841 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842}
843
John McCall7f040a92010-12-24 02:08:15 +0000844void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
845 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
846 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000847}
848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849/// ParseDeclaration - Parse a full 'declaration', which consists of
850/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000851/// 'Context' should be a Declarator::TheContext value. This returns the
852/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000853///
854/// declaration: [C99 6.7]
855/// block-declaration ->
856/// simple-declaration
857/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000858/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000859/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000860/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000861/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000862/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000863/// others... [FIXME]
864///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000865Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
866 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000867 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000868 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000869 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000870 // Must temporarily exit the objective-c container scope for
871 // parsing c none objective-c decls.
872 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000873
John McCalld226f652010-08-21 09:40:31 +0000874 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000875 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000876 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000877 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000878 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000879 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000880 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000881 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000882 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000883 // Could be the start of an inline namespace. Allowed as an ext in C++03.
884 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000885 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000886 SourceLocation InlineLoc = ConsumeToken();
887 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
888 break;
889 }
John McCall7f040a92010-12-24 02:08:15 +0000890 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000891 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000892 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000893 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000894 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000895 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000896 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000897 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000898 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000899 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000900 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000901 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000902 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000903 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000904 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000905 default:
John McCall7f040a92010-12-24 02:08:15 +0000906 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000907 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000908
Chris Lattner682bf922009-03-29 16:50:03 +0000909 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000910 // single decl, convert it now. Alias declarations can also declare a type;
911 // include that too if it is present.
912 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000913}
914
915/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
916/// declaration-specifiers init-declarator-list[opt] ';'
917///[C90/C++]init-declarator-list ';' [TODO]
918/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000919///
Richard Smithad762fc2011-04-14 22:09:26 +0000920/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
921/// attribute-specifier-seq[opt] type-specifier-seq declarator
922///
Chris Lattnercd147752009-03-29 17:27:48 +0000923/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000924/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000925///
926/// If FRI is non-null, we might be parsing a for-range-declaration instead
927/// of a simple-declaration. If we find that we are, we also parse the
928/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000929Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
930 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000931 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000932 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000933 bool RequireSemi,
934 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000936 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000937 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000938
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000939 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000940 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000941 StmtResult R = Actions.ActOnVlaStmt(DS);
942 if (R.isUsable())
943 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000944
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
946 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000947 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000948 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000949 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000950 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000951 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000952 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000954
955 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000956}
Mike Stump1eb44332009-09-09 15:08:12 +0000957
John McCalld8ac0572009-11-03 19:26:08 +0000958/// ParseDeclGroup - Having concluded that this is either a function
959/// definition or a group of object declarations, actually parse the
960/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000961Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
962 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000963 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000964 SourceLocation *DeclEnd,
965 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000966 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000967 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000968 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000969
John McCalld8ac0572009-11-03 19:26:08 +0000970 // Bail out if the first declarator didn't seem well-formed.
971 if (!D.hasName() && !D.mayOmitIdentifier()) {
972 // Skip until ; or }.
973 SkipUntil(tok::r_brace, true, true);
974 if (Tok.is(tok::semi))
975 ConsumeToken();
976 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattnerc82daef2010-07-11 22:24:20 +0000979 // Check to see if we have a function *definition* which must have a body.
980 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
981 // Look at the next token to make sure that this isn't a function
982 // declaration. We have to check this because __attribute__ might be the
983 // start of a function definition in GCC-extended K&R C.
984 !isDeclarationAfterDeclarator()) {
985
Chris Lattner004659a2010-07-11 22:42:07 +0000986 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000987 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
988 Diag(Tok, diag::err_function_declared_typedef);
989
990 // Recover by treating the 'typedef' as spurious.
991 DS.ClearStorageClassSpecs();
992 }
993
John McCalld226f652010-08-21 09:40:31 +0000994 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000995 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000996 }
997
998 if (isDeclarationSpecifier()) {
999 // If there is an invalid declaration specifier right after the function
1000 // prototype, then we must be in a missing semicolon case where this isn't
1001 // actually a body. Just fall through into the code that handles it as a
1002 // prototype, and let the top-level code handle the erroneous declspec
1003 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001004 } else {
1005 Diag(Tok, diag::err_expected_fn_body);
1006 SkipUntil(tok::semi);
1007 return DeclGroupPtrTy();
1008 }
1009 }
1010
Richard Smithad762fc2011-04-14 22:09:26 +00001011 if (ParseAttributesAfterDeclarator(D))
1012 return DeclGroupPtrTy();
1013
1014 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1015 // must parse and analyze the for-range-initializer before the declaration is
1016 // analyzed.
1017 if (FRI && Tok.is(tok::colon)) {
1018 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001019 if (Tok.is(tok::l_brace))
1020 FRI->RangeExpr = ParseBraceInitializer();
1021 else
1022 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001023 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1024 Actions.ActOnCXXForRangeDecl(ThisDecl);
1025 Actions.FinalizeDeclaration(ThisDecl);
1026 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1027 }
1028
Chris Lattner5f9e2722011-07-23 10:55:15 +00001029 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001030 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001031 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001032 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001033 DeclsInGroup.push_back(FirstDecl);
1034
1035 // If we don't have a comma, it is either the end of the list (a ';') or an
1036 // error, bail out.
1037 while (Tok.is(tok::comma)) {
1038 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +00001039 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001040
1041 // Parse the next declarator.
1042 D.clear();
1043
1044 // Accept attributes in an init-declarator. In the first declarator in a
1045 // declaration, these would be part of the declspec. In subsequent
1046 // declarators, they become part of the declarator itself, so that they
1047 // don't apply to declarators after *this* one. Examples:
1048 // short __attribute__((common)) var; -> declspec
1049 // short var __attribute__((common)); -> declarator
1050 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001051 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001052
1053 ParseDeclarator(D);
1054
John McCalld226f652010-08-21 09:40:31 +00001055 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001056 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001057 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001058 DeclsInGroup.push_back(ThisDecl);
1059 }
1060
1061 if (DeclEnd)
1062 *DeclEnd = Tok.getLocation();
1063
1064 if (Context != Declarator::ForContext &&
1065 ExpectAndConsume(tok::semi,
1066 Context == Declarator::FileContext
1067 ? diag::err_invalid_token_after_toplevel_declarator
1068 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001069 // Okay, there was no semicolon and one was expected. If we see a
1070 // declaration specifier, just assume it was missing and continue parsing.
1071 // Otherwise things are very confused and we skip to recover.
1072 if (!isDeclarationSpecifier()) {
1073 SkipUntil(tok::r_brace, true, true);
1074 if (Tok.is(tok::semi))
1075 ConsumeToken();
1076 }
John McCalld8ac0572009-11-03 19:26:08 +00001077 }
1078
Douglas Gregor23c94db2010-07-02 17:43:08 +00001079 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001080 DeclsInGroup.data(),
1081 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001082}
1083
Richard Smithad762fc2011-04-14 22:09:26 +00001084/// Parse an optional simple-asm-expr and attributes, and attach them to a
1085/// declarator. Returns true on an error.
1086bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1087 // If a simple-asm-expr is present, parse it.
1088 if (Tok.is(tok::kw_asm)) {
1089 SourceLocation Loc;
1090 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1091 if (AsmLabel.isInvalid()) {
1092 SkipUntil(tok::semi, true, true);
1093 return true;
1094 }
1095
1096 D.setAsmLabel(AsmLabel.release());
1097 D.SetRangeEnd(Loc);
1098 }
1099
1100 MaybeParseGNUAttributes(D);
1101 return false;
1102}
1103
Douglas Gregor1426e532009-05-12 21:31:51 +00001104/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1105/// declarator'. This method parses the remainder of the declaration
1106/// (including any attributes or initializer, among other things) and
1107/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001108///
Reid Spencer5f016e22007-07-11 17:01:13 +00001109/// init-declarator: [C99 6.7]
1110/// declarator
1111/// declarator '=' initializer
1112/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1113/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001114/// [C++] declarator initializer[opt]
1115///
1116/// [C++] initializer:
1117/// [C++] '=' initializer-clause
1118/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001119/// [C++0x] '=' 'default' [TODO]
1120/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001121/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001122///
1123/// According to the standard grammar, =default and =delete are function
1124/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001125///
John McCalld226f652010-08-21 09:40:31 +00001126Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001127 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001128 if (ParseAttributesAfterDeclarator(D))
1129 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Richard Smithad762fc2011-04-14 22:09:26 +00001131 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1132}
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Richard Smithad762fc2011-04-14 22:09:26 +00001134Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1135 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001136 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001137 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001138 switch (TemplateInfo.Kind) {
1139 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001140 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001141 break;
1142
1143 case ParsedTemplateInfo::Template:
1144 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001145 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001146 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001147 TemplateInfo.TemplateParams->data(),
1148 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001149 D);
1150 break;
1151
1152 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001153 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001154 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001155 TemplateInfo.ExternLoc,
1156 TemplateInfo.TemplateLoc,
1157 D);
1158 if (ThisRes.isInvalid()) {
1159 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001160 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001161 }
1162
1163 ThisDecl = ThisRes.get();
1164 break;
1165 }
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Richard Smith34b41d92011-02-20 03:19:35 +00001168 bool TypeContainsAuto =
1169 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1170
Douglas Gregor1426e532009-05-12 21:31:51 +00001171 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001172 if (isTokenEqualOrMistypedEqualEqual(
1173 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001174 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001175 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001176 if (D.isFunctionDeclarator())
1177 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1178 << 1 /* delete */;
1179 else
1180 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001181 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001182 if (D.isFunctionDeclarator())
1183 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1184 << 1 /* delete */;
1185 else
1186 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001187 } else {
John McCall731ad842009-12-19 09:28:58 +00001188 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1189 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001190 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001191 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001192
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001193 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001194 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001195 cutOffParsing();
1196 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001197 }
1198
John McCall60d7b3a2010-08-24 06:29:42 +00001199 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001200
John McCall731ad842009-12-19 09:28:58 +00001201 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001202 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001203 ExitScope();
1204 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001205
Douglas Gregor1426e532009-05-12 21:31:51 +00001206 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001207 SkipUntil(tok::comma, true, true);
1208 Actions.ActOnInitializerError(ThisDecl);
1209 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001210 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1211 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001212 }
1213 } else if (Tok.is(tok::l_paren)) {
1214 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001215 BalancedDelimiterTracker T(*this, tok::l_paren);
1216 T.consumeOpen();
1217
Douglas Gregor1426e532009-05-12 21:31:51 +00001218 ExprVector Exprs(Actions);
1219 CommaLocsTy CommaLocs;
1220
Douglas Gregorb4debae2009-12-22 17:47:17 +00001221 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1222 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001223 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001224 }
1225
Douglas Gregor1426e532009-05-12 21:31:51 +00001226 if (ParseExpressionList(Exprs, CommaLocs)) {
1227 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001228
1229 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001231 ExitScope();
1232 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001233 } else {
1234 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001235 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001236
1237 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1238 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001239
1240 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001241 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001242 ExitScope();
1243 }
1244
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001245 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001246 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001247 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001248 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001249 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001250 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1251 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001252 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1253
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001254 if (D.getCXXScopeSpec().isSet()) {
1255 EnterScope(0);
1256 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1257 }
1258
1259 ExprResult Init(ParseBraceInitializer());
1260
1261 if (D.getCXXScopeSpec().isSet()) {
1262 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1263 ExitScope();
1264 }
1265
1266 if (Init.isInvalid()) {
1267 Actions.ActOnInitializerError(ThisDecl);
1268 } else
1269 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1270 /*DirectInit=*/true, TypeContainsAuto);
1271
Douglas Gregor1426e532009-05-12 21:31:51 +00001272 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001273 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001274 }
1275
Richard Smith483b9f32011-02-21 20:05:19 +00001276 Actions.FinalizeDeclaration(ThisDecl);
1277
Douglas Gregor1426e532009-05-12 21:31:51 +00001278 return ThisDecl;
1279}
1280
Reid Spencer5f016e22007-07-11 17:01:13 +00001281/// ParseSpecifierQualifierList
1282/// specifier-qualifier-list:
1283/// type-specifier specifier-qualifier-list[opt]
1284/// type-qualifier specifier-qualifier-list[opt]
1285/// [GNU] attributes specifier-qualifier-list[opt]
1286///
Richard Smithc89edf52011-07-01 19:46:12 +00001287void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1289 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001290 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001291 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // Validate declspec for type-name.
1294 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001295 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001296 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // Issue diagnostic and remove storage class if present.
1300 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1301 if (DS.getStorageClassSpecLoc().isValid())
1302 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1303 else
1304 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1305 DS.ClearStorageClassSpecs();
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // Issue diagnostic and remove function specfier if present.
1309 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001310 if (DS.isInlineSpecified())
1311 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1312 if (DS.isVirtualSpecified())
1313 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1314 if (DS.isExplicitSpecified())
1315 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 DS.ClearFunctionSpecs();
1317 }
1318}
1319
Chris Lattnerc199ab32009-04-12 20:42:31 +00001320/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1321/// specified token is valid after the identifier in a declarator which
1322/// immediately follows the declspec. For example, these things are valid:
1323///
1324/// int x [ 4]; // direct-declarator
1325/// int x ( int y); // direct-declarator
1326/// int(int x ) // direct-declarator
1327/// int x ; // simple-declaration
1328/// int x = 17; // init-declarator-list
1329/// int x , y; // init-declarator-list
1330/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001331/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001332/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001333///
1334/// This is not, because 'x' does not immediately follow the declspec (though
1335/// ')' happens to be valid anyway).
1336/// int (x)
1337///
1338static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1339 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1340 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001341 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001342}
1343
Chris Lattnere40c2952009-04-14 21:34:55 +00001344
1345/// ParseImplicitInt - This method is called when we have an non-typename
1346/// identifier in a declspec (which normally terminates the decl spec) when
1347/// the declspec has no type specifier. In this case, the declspec is either
1348/// malformed or is "implicit int" (in K&R and C89).
1349///
1350/// This method handles diagnosing this prettily and returns false if the
1351/// declspec is done being processed. If it recovers and thinks there may be
1352/// other pieces of declspec after it, it returns true.
1353///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001354bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001355 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001356 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001357 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattnere40c2952009-04-14 21:34:55 +00001359 SourceLocation Loc = Tok.getLocation();
1360 // If we see an identifier that is not a type name, we normally would
1361 // parse it as the identifer being declared. However, when a typename
1362 // is typo'd or the definition is not included, this will incorrectly
1363 // parse the typename as the identifier name and fall over misparsing
1364 // later parts of the diagnostic.
1365 //
1366 // As such, we try to do some look-ahead in cases where this would
1367 // otherwise be an "implicit-int" case to see if this is invalid. For
1368 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1369 // an identifier with implicit int, we'd get a parse error because the
1370 // next token is obviously invalid for a type. Parse these as a case
1371 // with an invalid type specifier.
1372 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattnere40c2952009-04-14 21:34:55 +00001374 // Since we know that this either implicit int (which is rare) or an
1375 // error, we'd do lookahead to try to do better recovery.
1376 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1377 // If this token is valid for implicit int, e.g. "static x = 4", then
1378 // we just avoid eating the identifier, so it will be parsed as the
1379 // identifier in the declarator.
1380 return false;
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Chris Lattnere40c2952009-04-14 21:34:55 +00001383 // Otherwise, if we don't consume this token, we are going to emit an
1384 // error anyway. Try to recover from various common problems. Check
1385 // to see if this was a reference to a tag name without a tag specified.
1386 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001387 //
1388 // C++ doesn't need this, and isTagName doesn't take SS.
1389 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001390 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001391 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregor23c94db2010-07-02 17:43:08 +00001393 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001394 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001395 case DeclSpec::TST_enum:
1396 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1397 case DeclSpec::TST_union:
1398 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1399 case DeclSpec::TST_struct:
1400 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1401 case DeclSpec::TST_class:
1402 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001403 }
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Chris Lattnerf4382f52009-04-14 22:17:06 +00001405 if (TagName) {
1406 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001407 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001408 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Chris Lattnerf4382f52009-04-14 22:17:06 +00001410 // Parse this as a tag as if the missing tag were present.
1411 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001412 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001413 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001414 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001415 return true;
1416 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregora786fdb2009-10-13 23:27:22 +00001419 // This is almost certainly an invalid type name. Let the action emit a
1420 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001421 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001422 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001423 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001424 // The action emitted a diagnostic, so we don't have to.
1425 if (T) {
1426 // The action has suggested that the type T could be used. Set that as
1427 // the type in the declaration specifiers, consume the would-be type
1428 // name token, and we're done.
1429 const char *PrevSpec;
1430 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001431 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001432 DS.SetRangeEnd(Tok.getLocation());
1433 ConsumeToken();
1434
1435 // There may be other declaration specifiers after this.
1436 return true;
1437 }
1438
1439 // Fall through; the action had no suggestion for us.
1440 } else {
1441 // The action did not emit a diagnostic, so emit one now.
1442 SourceRange R;
1443 if (SS) R = SS->getRange();
1444 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Douglas Gregora786fdb2009-10-13 23:27:22 +00001447 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001448 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001449 unsigned DiagID;
1450 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001451 DS.SetRangeEnd(Tok.getLocation());
1452 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Chris Lattnere40c2952009-04-14 21:34:55 +00001454 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1455 // avoid rippling error messages on subsequent uses of the same type,
1456 // could be useful if #include was forgotten.
1457 return false;
1458}
1459
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001460/// \brief Determine the declaration specifier context from the declarator
1461/// context.
1462///
1463/// \param Context the declarator context, which is one of the
1464/// Declarator::TheContext enumerator values.
1465Parser::DeclSpecContext
1466Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1467 if (Context == Declarator::MemberContext)
1468 return DSC_class;
1469 if (Context == Declarator::FileContext)
1470 return DSC_top_level;
1471 return DSC_normal;
1472}
1473
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001474/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1475///
1476/// FIXME: Simply returns an alignof() expression if the argument is a
1477/// type. Ideally, the type should be propagated directly into Sema.
1478///
1479/// [C1X/C++0x] type-id
1480/// [C1X] constant-expression
1481/// [C++0x] assignment-expression
1482ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1483 if (isTypeIdInParens()) {
1484 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1485 SourceLocation TypeLoc = Tok.getLocation();
1486 ParsedType Ty = ParseTypeName().get();
1487 SourceRange TypeRange(Start, Tok.getLocation());
1488 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1489 Ty.getAsOpaquePtr(), TypeRange);
1490 } else
1491 return ParseConstantExpression();
1492}
1493
1494/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1495/// attribute to Attrs.
1496///
1497/// alignment-specifier:
1498/// [C1X] '_Alignas' '(' type-id ')'
1499/// [C1X] '_Alignas' '(' constant-expression ')'
1500/// [C++0x] 'alignas' '(' type-id ')'
1501/// [C++0x] 'alignas' '(' assignment-expression ')'
1502void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1503 SourceLocation *endLoc) {
1504 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1505 "Not an alignment-specifier!");
1506
1507 SourceLocation KWLoc = Tok.getLocation();
1508 ConsumeToken();
1509
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001510 BalancedDelimiterTracker T(*this, tok::l_paren);
1511 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001512 return;
1513
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001514 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation());
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001515 if (ArgExpr.isInvalid()) {
1516 SkipUntil(tok::r_paren);
1517 return;
1518 }
1519
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001520 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001521 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001522 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001523
1524 ExprVector ArgExprs(Actions);
1525 ArgExprs.push_back(ArgExpr.release());
1526 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001527 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001528}
1529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530/// ParseDeclarationSpecifiers
1531/// declaration-specifiers: [C99 6.7]
1532/// storage-class-specifier declaration-specifiers[opt]
1533/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001534/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001535/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001536/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001537/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001538///
1539/// storage-class-specifier: [C99 6.7.1]
1540/// 'typedef'
1541/// 'extern'
1542/// 'static'
1543/// 'auto'
1544/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001545/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001546/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001547/// function-specifier: [C99 6.7.4]
1548/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001549/// [C++] 'virtual'
1550/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001551/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001552/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001553/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001554
Reid Spencer5f016e22007-07-11 17:01:13 +00001555///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001556void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001557 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001558 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001559 DeclSpecContext DSContext) {
1560 if (DS.getSourceRange().isInvalid()) {
1561 DS.SetRangeStart(Tok.getLocation());
1562 DS.SetRangeEnd(Tok.getLocation());
1563 }
1564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001566 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001568 unsigned DiagID = 0;
1569
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001571
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001574 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001575 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1576 MaybeParseCXX0XAttributes(DS.getAttributes());
1577
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 // If this is not a declaration specifier token, we're done reading decl
1579 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001580 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001583 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001584 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001585 if (DS.hasTypeSpecifier()) {
1586 bool AllowNonIdentifiers
1587 = (getCurScope()->getFlags() & (Scope::ControlScope |
1588 Scope::BlockScope |
1589 Scope::TemplateParamScope |
1590 Scope::FunctionPrototypeScope |
1591 Scope::AtCatchScope)) == 0;
1592 bool AllowNestedNameSpecifiers
1593 = DSContext == DSC_top_level ||
1594 (DSContext == DSC_class && DS.isFriendSpecified());
1595
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001596 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1597 AllowNonIdentifiers,
1598 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001599 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001600 }
1601
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001602 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1603 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1604 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001605 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1606 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001607 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001608 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001609 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001610 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001611
1612 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001613 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001614 }
1615
Chris Lattner5e02c472009-01-05 00:07:25 +00001616 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001617 // C++ scope specifier. Annotate and loop, or bail out on error.
1618 if (TryAnnotateCXXScopeToken(true)) {
1619 if (!DS.hasTypeSpecifier())
1620 DS.SetTypeSpecError();
1621 goto DoneWithDeclSpec;
1622 }
John McCall2e0a7152010-03-01 18:20:46 +00001623 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1624 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001625 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001626
1627 case tok::annot_cxxscope: {
1628 if (DS.hasTypeSpecifier())
1629 goto DoneWithDeclSpec;
1630
John McCallaa87d332009-12-12 11:40:51 +00001631 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001632 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1633 Tok.getAnnotationRange(),
1634 SS);
John McCallaa87d332009-12-12 11:40:51 +00001635
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001636 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001637 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001638 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001639 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001640 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001641 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001642
1643 // C++ [class.qual]p2:
1644 // In a lookup in which the constructor is an acceptable lookup
1645 // result and the nested-name-specifier nominates a class C:
1646 //
1647 // - if the name specified after the
1648 // nested-name-specifier, when looked up in C, is the
1649 // injected-class-name of C (Clause 9), or
1650 //
1651 // - if the name specified after the nested-name-specifier
1652 // is the same as the identifier or the
1653 // simple-template-id's template-name in the last
1654 // component of the nested-name-specifier,
1655 //
1656 // the name is instead considered to name the constructor of
1657 // class C.
1658 //
1659 // Thus, if the template-name is actually the constructor
1660 // name, then the code is ill-formed; this interpretation is
1661 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001662 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001663 if ((DSContext == DSC_top_level ||
1664 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1665 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001666 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001667 if (isConstructorDeclarator()) {
1668 // The user meant this to be an out-of-line constructor
1669 // definition, but template arguments are not allowed
1670 // there. Just allow this as a constructor; we'll
1671 // complain about it later.
1672 goto DoneWithDeclSpec;
1673 }
1674
1675 // The user meant this to name a type, but it actually names
1676 // a constructor with some extraneous template
1677 // arguments. Complain, then parse it as a type as the user
1678 // intended.
1679 Diag(TemplateId->TemplateNameLoc,
1680 diag::err_out_of_line_template_id_names_constructor)
1681 << TemplateId->Name;
1682 }
1683
John McCallaa87d332009-12-12 11:40:51 +00001684 DS.getTypeSpecScope() = SS;
1685 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001686 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001687 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001688 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001689 continue;
1690 }
1691
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001692 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001693 DS.getTypeSpecScope() = SS;
1694 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001695 if (Tok.getAnnotationValue()) {
1696 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1698 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001699 PrevSpec, DiagID, T);
1700 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001701 else
1702 DS.SetTypeSpecError();
1703 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1704 ConsumeToken(); // The typename
1705 }
1706
Douglas Gregor9135c722009-03-25 15:40:00 +00001707 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001708 goto DoneWithDeclSpec;
1709
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001710 // If we're in a context where the identifier could be a class name,
1711 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001712 if ((DSContext == DSC_top_level ||
1713 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001714 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001715 &SS)) {
1716 if (isConstructorDeclarator())
1717 goto DoneWithDeclSpec;
1718
1719 // As noted in C++ [class.qual]p2 (cited above), when the name
1720 // of the class is qualified in a context where it could name
1721 // a constructor, its a constructor name. However, we've
1722 // looked at the declarator, and the user probably meant this
1723 // to be a type. Complain that it isn't supposed to be treated
1724 // as a type, then proceed to parse it as a type.
1725 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1726 << Next.getIdentifierInfo();
1727 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001728
John McCallb3d87482010-08-24 05:47:05 +00001729 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1730 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001731 getCurScope(), &SS,
1732 false, false, ParsedType(),
1733 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001734
Chris Lattnerf4382f52009-04-14 22:17:06 +00001735 // If the referenced identifier is not a type, then this declspec is
1736 // erroneous: We already checked about that it has no type specifier, and
1737 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001738 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001739 if (TypeRep == 0) {
1740 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001741 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001742 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
John McCallaa87d332009-12-12 11:40:51 +00001745 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001746 ConsumeToken(); // The C++ scope.
1747
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001749 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001750 if (isInvalid)
1751 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001753 DS.SetRangeEnd(Tok.getLocation());
1754 ConsumeToken(); // The typename.
1755
1756 continue;
1757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Chris Lattner80d0c892009-01-21 19:48:37 +00001759 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001760 if (Tok.getAnnotationValue()) {
1761 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001763 DiagID, T);
1764 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001765 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001766
1767 if (isInvalid)
1768 break;
1769
Chris Lattner80d0c892009-01-21 19:48:37 +00001770 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1771 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Chris Lattner80d0c892009-01-21 19:48:37 +00001773 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1774 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001775 // Objective-C interface.
1776 if (Tok.is(tok::less) && getLang().ObjC1)
1777 ParseObjCProtocolQualifiers(DS);
1778
Chris Lattner80d0c892009-01-21 19:48:37 +00001779 continue;
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregorbfad9152011-04-28 15:48:45 +00001782 case tok::kw___is_signed:
1783 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1784 // typically treats it as a trait. If we see __is_signed as it appears
1785 // in libstdc++, e.g.,
1786 //
1787 // static const bool __is_signed;
1788 //
1789 // then treat __is_signed as an identifier rather than as a keyword.
1790 if (DS.getTypeSpecType() == TST_bool &&
1791 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1792 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1793 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1794 Tok.setKind(tok::identifier);
1795 }
1796
1797 // We're done with the declaration-specifiers.
1798 goto DoneWithDeclSpec;
1799
Chris Lattner3bd934a2008-07-26 01:18:38 +00001800 // typedef-name
1801 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001802 // In C++, check to see if this is a scope specifier like foo::bar::, if
1803 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001804 if (getLang().CPlusPlus) {
1805 if (TryAnnotateCXXScopeToken(true)) {
1806 if (!DS.hasTypeSpecifier())
1807 DS.SetTypeSpecError();
1808 goto DoneWithDeclSpec;
1809 }
1810 if (!Tok.is(tok::identifier))
1811 continue;
1812 }
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Chris Lattner3bd934a2008-07-26 01:18:38 +00001814 // This identifier can only be a typedef name if we haven't already seen
1815 // a type-specifier. Without this check we misparse:
1816 // typedef int X; struct Y { short X; }; as 'short int'.
1817 if (DS.hasTypeSpecifier())
1818 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
John Thompson82287d12010-02-05 00:12:22 +00001820 // Check for need to substitute AltiVec keyword tokens.
1821 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1822 break;
1823
Chris Lattner3bd934a2008-07-26 01:18:38 +00001824 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001825 ParsedType TypeRep =
1826 Actions.getTypeName(*Tok.getIdentifierInfo(),
1827 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001828
Chris Lattnerc199ab32009-04-12 20:42:31 +00001829 // If this is not a typedef name, don't parse it as part of the declspec,
1830 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001831 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001832 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001833 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001834 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001835
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001836 // If we're in a context where the identifier could be a class name,
1837 // check whether this is a constructor declaration.
1838 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001839 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001840 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001841 goto DoneWithDeclSpec;
1842
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001843 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001844 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001845 if (isInvalid)
1846 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Chris Lattner3bd934a2008-07-26 01:18:38 +00001848 DS.SetRangeEnd(Tok.getLocation());
1849 ConsumeToken(); // The identifier
1850
1851 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1852 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001853 // Objective-C interface.
1854 if (Tok.is(tok::less) && getLang().ObjC1)
1855 ParseObjCProtocolQualifiers(DS);
1856
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001857 // Need to support trailing type qualifiers (e.g. "id<p> const").
1858 // If a type specifier follows, it will be diagnosed elsewhere.
1859 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001860 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001861
1862 // type-name
1863 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001864 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001865 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001866 // This template-id does not refer to a type name, so we're
1867 // done with the type-specifiers.
1868 goto DoneWithDeclSpec;
1869 }
1870
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001871 // If we're in a context where the template-id could be a
1872 // constructor name or specialization, check whether this is a
1873 // constructor declaration.
1874 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001875 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001876 isConstructorDeclarator())
1877 goto DoneWithDeclSpec;
1878
Douglas Gregor39a8de12009-02-25 19:37:18 +00001879 // Turn the template-id annotation token into a type annotation
1880 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001881 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001882 continue;
1883 }
1884
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 // GNU attributes support.
1886 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001887 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001889
1890 // Microsoft declspec support.
1891 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001892 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001893 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Steve Naroff239f0732008-12-25 14:16:32 +00001895 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001896 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001897 // FIXME: Add handling here!
1898 break;
1899
1900 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001901 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001902 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001903 case tok::kw___cdecl:
1904 case tok::kw___stdcall:
1905 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001906 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001907 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001908 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001909 continue;
1910
Dawn Perchik52fc3142010-09-03 01:29:35 +00001911 // Borland single token adornments.
1912 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001913 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001914 continue;
1915
Peter Collingbournef315fa82011-02-14 01:42:53 +00001916 // OpenCL single token adornments.
1917 case tok::kw___kernel:
1918 ParseOpenCLAttributes(DS.getAttributes());
1919 continue;
1920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 // storage-class-specifier
1922 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001923 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
1924 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 break;
1926 case tok::kw_extern:
1927 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001928 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001929 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
1930 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001932 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001933 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
1934 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001935 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 case tok::kw_static:
1937 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001938 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001939 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
1940 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 break;
1942 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001943 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001944 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001945 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1946 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001947 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001948 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001949 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001950 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001951 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1952 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001953 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001954 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1955 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 break;
1957 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001958 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
1959 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001961 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001962 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
1963 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001964 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001966 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Reid Spencer5f016e22007-07-11 17:01:13 +00001969 // function-specifier
1970 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001971 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001973 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001974 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001975 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001976 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001977 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001978 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001979
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001980 // alignment-specifier
1981 case tok::kw__Alignas:
1982 if (!getLang().C1X)
1983 Diag(Tok, diag::ext_c1x_alignas);
1984 ParseAlignmentSpecifier(DS.getAttributes());
1985 continue;
1986
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001987 // friend
1988 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001989 if (DSContext == DSC_class)
1990 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1991 else {
1992 PrevSpec = ""; // not actually used by the diagnostic
1993 DiagID = diag::err_friend_invalid_in_context;
1994 isInvalid = true;
1995 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001996 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregor8d267c52011-09-09 02:06:17 +00001998 // Modules
1999 case tok::kw___module_private__:
2000 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2001 break;
2002
Sebastian Redl2ac67232009-11-05 15:47:02 +00002003 // constexpr
2004 case tok::kw_constexpr:
2005 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2006 break;
2007
Chris Lattner80d0c892009-01-21 19:48:37 +00002008 // type-specifier
2009 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002010 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2011 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002012 break;
2013 case tok::kw_long:
2014 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002015 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2016 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002017 else
John McCallfec54012009-08-03 20:12:06 +00002018 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2019 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002020 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002021 case tok::kw___int64:
2022 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2023 DiagID);
2024 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002025 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002026 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2027 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002028 break;
2029 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002030 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2031 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002032 break;
2033 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002034 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2035 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002036 break;
2037 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002038 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2039 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002040 break;
2041 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2043 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002044 break;
2045 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002046 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2047 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002048 break;
2049 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2051 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002052 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002053 case tok::kw_half:
2054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2055 DiagID);
2056 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002057 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002058 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2059 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002060 break;
2061 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2063 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002064 break;
2065 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2067 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002068 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002069 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2071 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002072 break;
2073 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2075 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002076 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002077 case tok::kw_bool:
2078 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002079 if (Tok.is(tok::kw_bool) &&
2080 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2081 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2082 PrevSpec = ""; // Not used by the diagnostic.
2083 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002084 // For better error recovery.
2085 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002086 isInvalid = true;
2087 } else {
2088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2089 DiagID);
2090 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002091 break;
2092 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002093 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2094 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002095 break;
2096 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2098 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002099 break;
2100 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2102 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002103 break;
John Thompson82287d12010-02-05 00:12:22 +00002104 case tok::kw___vector:
2105 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2106 break;
2107 case tok::kw___pixel:
2108 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2109 break;
John McCalla5fc4722011-04-09 22:50:59 +00002110 case tok::kw___unknown_anytype:
2111 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2112 PrevSpec, DiagID);
2113 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002114
2115 // class-specifier:
2116 case tok::kw_class:
2117 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002118 case tok::kw_union: {
2119 tok::TokenKind Kind = Tok.getKind();
2120 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002121 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002123 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002124
2125 // enum-specifier:
2126 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002127 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002128 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002129 continue;
2130
2131 // cv-qualifier:
2132 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002133 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2134 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002135 break;
2136 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002137 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2138 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002139 break;
2140 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002141 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2142 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002143 break;
2144
Douglas Gregord57959a2009-03-27 23:10:48 +00002145 // C++ typename-specifier:
2146 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002147 if (TryAnnotateTypeOrScopeToken()) {
2148 DS.SetTypeSpecError();
2149 goto DoneWithDeclSpec;
2150 }
2151 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002152 continue;
2153 break;
2154
Chris Lattner80d0c892009-01-21 19:48:37 +00002155 // GNU typeof support.
2156 case tok::kw_typeof:
2157 ParseTypeofSpecifier(DS);
2158 continue;
2159
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002160 case tok::kw_decltype:
2161 ParseDecltypeSpecifier(DS);
2162 continue;
2163
Sean Huntdb5d44b2011-05-19 05:37:45 +00002164 case tok::kw___underlying_type:
2165 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002166 continue;
2167
2168 case tok::kw__Atomic:
2169 ParseAtomicSpecifier(DS);
2170 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002171
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002172 // OpenCL qualifiers:
2173 case tok::kw_private:
2174 if (!getLang().OpenCL)
2175 goto DoneWithDeclSpec;
2176 case tok::kw___private:
2177 case tok::kw___global:
2178 case tok::kw___local:
2179 case tok::kw___constant:
2180 case tok::kw___read_only:
2181 case tok::kw___write_only:
2182 case tok::kw___read_write:
2183 ParseOpenCLQualifiers(DS);
2184 break;
2185
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002186 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002187 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002188 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2189 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002190 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002191 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Douglas Gregor46f936e2010-11-19 17:10:50 +00002193 if (!ParseObjCProtocolQualifiers(DS))
2194 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2195 << FixItHint::CreateInsertion(Loc, "id")
2196 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002197
2198 // Need to support trailing type qualifiers (e.g. "id<p> const").
2199 // If a type specifier follows, it will be diagnosed elsewhere.
2200 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 }
John McCallfec54012009-08-03 20:12:06 +00002202 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002203 if (isInvalid) {
2204 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002205 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002206
2207 if (DiagID == diag::ext_duplicate_declspec)
2208 Diag(Tok, DiagID)
2209 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2210 else
2211 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002213
Chris Lattner81c018d2008-03-13 06:29:04 +00002214 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002215 if (DiagID != diag::err_bool_redeclaration)
2216 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 }
2218}
Douglas Gregoradcac882008-12-01 23:54:00 +00002219
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002220/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002221/// primarily follow the C++ grammar with additions for C99 and GNU,
2222/// which together subsume the C grammar. Note that the C++
2223/// type-specifier also includes the C type-qualifier (for const,
2224/// volatile, and C99 restrict). Returns true if a type-specifier was
2225/// found (and parsed), false otherwise.
2226///
2227/// type-specifier: [C++ 7.1.5]
2228/// simple-type-specifier
2229/// class-specifier
2230/// enum-specifier
2231/// elaborated-type-specifier [TODO]
2232/// cv-qualifier
2233///
2234/// cv-qualifier: [C++ 7.1.5.1]
2235/// 'const'
2236/// 'volatile'
2237/// [C99] 'restrict'
2238///
2239/// simple-type-specifier: [ C++ 7.1.5.2]
2240/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2241/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2242/// 'char'
2243/// 'wchar_t'
2244/// 'bool'
2245/// 'short'
2246/// 'int'
2247/// 'long'
2248/// 'signed'
2249/// 'unsigned'
2250/// 'float'
2251/// 'double'
2252/// 'void'
2253/// [C99] '_Bool'
2254/// [C99] '_Complex'
2255/// [C99] '_Imaginary' // Removed in TC2?
2256/// [GNU] '_Decimal32'
2257/// [GNU] '_Decimal64'
2258/// [GNU] '_Decimal128'
2259/// [GNU] typeof-specifier
2260/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2261/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002262/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002263/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002264bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002265 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002266 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002267 const ParsedTemplateInfo &TemplateInfo,
2268 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002269 SourceLocation Loc = Tok.getLocation();
2270
2271 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002272 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002273 // If we already have a type specifier, this identifier is not a type.
2274 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2275 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2276 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2277 return false;
John Thompson82287d12010-02-05 00:12:22 +00002278 // Check for need to substitute AltiVec keyword tokens.
2279 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2280 break;
2281 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002282 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002283 // Annotate typenames and C++ scope specifiers. If we get one, just
2284 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002285 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2286 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002287 return true;
2288 if (Tok.is(tok::identifier))
2289 return false;
2290 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2291 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002292 case tok::coloncolon: // ::foo::bar
2293 if (NextToken().is(tok::kw_new) || // ::new
2294 NextToken().is(tok::kw_delete)) // ::delete
2295 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002296
Chris Lattner166a8fc2009-01-04 23:41:41 +00002297 // Annotate typenames and C++ scope specifiers. If we get one, just
2298 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002299 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2300 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002301 return true;
2302 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2303 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Douglas Gregor12e083c2008-11-07 15:42:26 +00002305 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002306 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002307 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2309 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002310 DiagID, T);
2311 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002312 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002313 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2314 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Douglas Gregor12e083c2008-11-07 15:42:26 +00002316 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2317 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2318 // Objective-C interface. If we don't have Objective-C or a '<', this is
2319 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002320 if (Tok.is(tok::less) && getLang().ObjC1)
2321 ParseObjCProtocolQualifiers(DS);
2322
Douglas Gregor12e083c2008-11-07 15:42:26 +00002323 return true;
2324 }
2325
2326 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002327 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002328 break;
2329 case tok::kw_long:
2330 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002331 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2332 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002333 else
John McCallfec54012009-08-03 20:12:06 +00002334 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2335 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002336 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002337 case tok::kw___int64:
2338 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2339 DiagID);
2340 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002341 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002342 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002343 break;
2344 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002345 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2346 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002347 break;
2348 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002349 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2350 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002351 break;
2352 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002353 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2354 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002355 break;
2356 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002358 break;
2359 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002361 break;
2362 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002364 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002365 case tok::kw_half:
2366 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2367 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002368 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002369 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002370 break;
2371 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002373 break;
2374 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002375 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002376 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002377 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002379 break;
2380 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002382 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002383 case tok::kw_bool:
2384 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002386 break;
2387 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2389 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002390 break;
2391 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002392 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2393 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002394 break;
2395 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2397 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002398 break;
John Thompson82287d12010-02-05 00:12:22 +00002399 case tok::kw___vector:
2400 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2401 break;
2402 case tok::kw___pixel:
2403 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2404 break;
2405
Douglas Gregor12e083c2008-11-07 15:42:26 +00002406 // class-specifier:
2407 case tok::kw_class:
2408 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002409 case tok::kw_union: {
2410 tok::TokenKind Kind = Tok.getKind();
2411 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002412 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2413 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002414 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002415 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002416
2417 // enum-specifier:
2418 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002419 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002420 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002421 return true;
2422
2423 // cv-qualifier:
2424 case tok::kw_const:
2425 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002426 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002427 break;
2428 case tok::kw_volatile:
2429 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002430 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002431 break;
2432 case tok::kw_restrict:
2433 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002434 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002435 break;
2436
2437 // GNU typeof support.
2438 case tok::kw_typeof:
2439 ParseTypeofSpecifier(DS);
2440 return true;
2441
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002442 // C++0x decltype support.
2443 case tok::kw_decltype:
2444 ParseDecltypeSpecifier(DS);
2445 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Sean Huntdb5d44b2011-05-19 05:37:45 +00002447 // C++0x type traits support.
2448 case tok::kw___underlying_type:
2449 ParseUnderlyingTypeSpecifier(DS);
2450 return true;
2451
Eli Friedmanb001de72011-10-06 23:00:33 +00002452 case tok::kw__Atomic:
2453 ParseAtomicSpecifier(DS);
2454 return true;
2455
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002456 // OpenCL qualifiers:
2457 case tok::kw_private:
2458 if (!getLang().OpenCL)
2459 return false;
2460 case tok::kw___private:
2461 case tok::kw___global:
2462 case tok::kw___local:
2463 case tok::kw___constant:
2464 case tok::kw___read_only:
2465 case tok::kw___write_only:
2466 case tok::kw___read_write:
2467 ParseOpenCLQualifiers(DS);
2468 break;
2469
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002470 // C++0x auto support.
2471 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002472 // This is only called in situations where a storage-class specifier is
2473 // illegal, so we can assume an auto type specifier was intended even in
2474 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2475 // extension diagnostic.
2476 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002477 return false;
2478
John McCallfec54012009-08-03 20:12:06 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002480 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002481
Eli Friedman290eeb02009-06-08 23:27:34 +00002482 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002483 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002484 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002485 case tok::kw___cdecl:
2486 case tok::kw___stdcall:
2487 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002488 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002489 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002490 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002491 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002492
Dawn Perchik52fc3142010-09-03 01:29:35 +00002493 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002494 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002495 return true;
2496
Douglas Gregor12e083c2008-11-07 15:42:26 +00002497 default:
2498 // Not a type-specifier; do nothing.
2499 return false;
2500 }
2501
2502 // If the specifier combination wasn't legal, issue a diagnostic.
2503 if (isInvalid) {
2504 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002505 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002506 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002507 }
2508 DS.SetRangeEnd(Tok.getLocation());
2509 ConsumeToken(); // whatever we parsed above.
2510 return true;
2511}
Reid Spencer5f016e22007-07-11 17:01:13 +00002512
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002513/// ParseStructDeclaration - Parse a struct declaration without the terminating
2514/// semicolon.
2515///
Reid Spencer5f016e22007-07-11 17:01:13 +00002516/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002517/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002518/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002519/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002520/// struct-declarator-list:
2521/// struct-declarator
2522/// struct-declarator-list ',' struct-declarator
2523/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2524/// struct-declarator:
2525/// declarator
2526/// [GNU] declarator attributes[opt]
2527/// declarator[opt] ':' constant-expression
2528/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2529///
Chris Lattnere1359422008-04-10 06:46:29 +00002530void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002531ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002532
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002533 if (Tok.is(tok::kw___extension__)) {
2534 // __extension__ silences extension warnings in the subexpression.
2535 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002536 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002537 return ParseStructDeclaration(DS, Fields);
2538 }
Mike Stump1eb44332009-09-09 15:08:12 +00002539
Steve Naroff28a7ca82007-08-20 22:28:22 +00002540 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002541 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002542
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002543 // If there are no declarators, this is a free-standing declaration
2544 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002545 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002546 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002547 return;
2548 }
2549
2550 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002551 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002552 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002553 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002554 FieldDeclarator DeclaratorInfo(DS);
2555
2556 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002557 if (!FirstDeclarator)
2558 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Steve Naroff28a7ca82007-08-20 22:28:22 +00002560 /// struct-declarator: declarator
2561 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002562 if (Tok.isNot(tok::colon)) {
2563 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2564 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002565 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002566 }
Mike Stump1eb44332009-09-09 15:08:12 +00002567
Chris Lattner04d66662007-10-09 17:33:22 +00002568 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002569 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002570 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002571 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002572 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002573 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002574 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002575 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002576
Steve Naroff28a7ca82007-08-20 22:28:22 +00002577 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002578 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002579
John McCallbdd563e2009-11-03 02:38:08 +00002580 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002581 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002582 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002583
Steve Naroff28a7ca82007-08-20 22:28:22 +00002584 // If we don't have a comma, it is either the end of the list (a ';')
2585 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002586 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002587 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002588
Steve Naroff28a7ca82007-08-20 22:28:22 +00002589 // Consume the comma.
2590 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002591
John McCallbdd563e2009-11-03 02:38:08 +00002592 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002593 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002594}
2595
2596/// ParseStructUnionBody
2597/// struct-contents:
2598/// struct-declaration-list
2599/// [EXT] empty
2600/// [GNU] "struct-declaration-list" without terminatoring ';'
2601/// struct-declaration-list:
2602/// struct-declaration
2603/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002604/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002605///
Reid Spencer5f016e22007-07-11 17:01:13 +00002606void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002607 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002608 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2609 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002611 BalancedDelimiterTracker T(*this, tok::l_brace);
2612 if (T.consumeOpen())
2613 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002615 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002616 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002617
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2619 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002620 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002621 Diag(Tok, diag::ext_empty_struct_union)
2622 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002623
Chris Lattner5f9e2722011-07-23 10:55:15 +00002624 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002625
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002627 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002628 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002629
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002631 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002632 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002633 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002634 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002635 ConsumeToken();
2636 continue;
2637 }
Chris Lattnere1359422008-04-10 06:46:29 +00002638
2639 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002640 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002641
John McCallbdd563e2009-11-03 02:38:08 +00002642 if (!Tok.is(tok::at)) {
2643 struct CFieldCallback : FieldCallback {
2644 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002645 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002646 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002647
John McCalld226f652010-08-21 09:40:31 +00002648 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002649 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002650 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2651
John McCalld226f652010-08-21 09:40:31 +00002652 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002653 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002654 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002655 FD.D.getDeclSpec().getSourceRange().getBegin(),
2656 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002657 FieldDecls.push_back(Field);
2658 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002659 }
John McCallbdd563e2009-11-03 02:38:08 +00002660 } Callback(*this, TagDecl, FieldDecls);
2661
2662 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002663 } else { // Handle @defs
2664 ConsumeToken();
2665 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2666 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002667 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002668 continue;
2669 }
2670 ConsumeToken();
2671 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2672 if (!Tok.is(tok::identifier)) {
2673 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002674 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002675 continue;
2676 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002677 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002678 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002679 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002680 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2681 ConsumeToken();
2682 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002683 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002684
Chris Lattner04d66662007-10-09 17:33:22 +00002685 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002686 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002687 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002688 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 break;
2690 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002691 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2692 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002693 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002694 // If we stopped at a ';', eat it.
2695 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 }
2697 }
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002699 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002700
John McCall0b7e6782011-03-24 11:26:52 +00002701 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002702 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002703 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002704
Douglas Gregor23c94db2010-07-02 17:43:08 +00002705 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002706 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002707 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002708 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002709 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002710 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2711 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002712}
2713
Reid Spencer5f016e22007-07-11 17:01:13 +00002714/// ParseEnumSpecifier
2715/// enum-specifier: [C99 6.7.2.2]
2716/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002717///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002718/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2719/// '}' attributes[opt]
2720/// 'enum' identifier
2721/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002722///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002723/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2724/// [C++0x] enum-head '{' enumerator-list ',' '}'
2725///
2726/// enum-head: [C++0x]
2727/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2728/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2729///
2730/// enum-key: [C++0x]
2731/// 'enum'
2732/// 'enum' 'class'
2733/// 'enum' 'struct'
2734///
2735/// enum-base: [C++0x]
2736/// ':' type-specifier-seq
2737///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002738/// [C++] elaborated-type-specifier:
2739/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2740///
Chris Lattner4c97d762009-04-12 21:49:30 +00002741void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002742 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002743 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002744 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002745 if (Tok.is(tok::code_completion)) {
2746 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002747 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002748 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002749 }
John McCall57c13002011-07-06 05:58:41 +00002750
2751 bool IsScopedEnum = false;
2752 bool IsScopedUsingClassTag = false;
2753
2754 if (getLang().CPlusPlus0x &&
2755 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002756 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002757 IsScopedEnum = true;
2758 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2759 ConsumeToken();
2760 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002761
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002762 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002763 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002764 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002765
Douglas Gregor5471bc82011-09-08 17:18:35 +00002766 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002767 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002768
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002769 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002770 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002771 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2772 // if a fixed underlying type is allowed.
2773 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2774
John McCallb3d87482010-08-24 05:47:05 +00002775 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002776 return;
2777
2778 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002779 Diag(Tok, diag::err_expected_ident);
2780 if (Tok.isNot(tok::l_brace)) {
2781 // Has no name and is not a definition.
2782 // Skip the rest of this declarator, up until the comma or semicolon.
2783 SkipUntil(tok::comma, true);
2784 return;
2785 }
2786 }
2787 }
Mike Stump1eb44332009-09-09 15:08:12 +00002788
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002789 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002790 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2791 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002792 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002794 // Skip the rest of this declarator, up until the comma or semicolon.
2795 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002796 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002797 }
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002799 // If an identifier is present, consume and remember it.
2800 IdentifierInfo *Name = 0;
2801 SourceLocation NameLoc;
2802 if (Tok.is(tok::identifier)) {
2803 Name = Tok.getIdentifierInfo();
2804 NameLoc = ConsumeToken();
2805 }
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002807 if (!Name && IsScopedEnum) {
2808 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2809 // declaration of a scoped enumeration.
2810 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2811 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002812 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002813 }
2814
2815 TypeResult BaseType;
2816
Douglas Gregora61b3e72010-12-01 17:42:47 +00002817 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002818 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002819 bool PossibleBitfield = false;
2820 if (getCurScope()->getFlags() & Scope::ClassScope) {
2821 // If we're in class scope, this can either be an enum declaration with
2822 // an underlying type, or a declaration of a bitfield member. We try to
2823 // use a simple disambiguation scheme first to catch the common cases
2824 // (integer literal, sizeof); if it's still ambiguous, we then consider
2825 // anything that's a simple-type-specifier followed by '(' as an
2826 // expression. This suffices because function types are not valid
2827 // underlying types anyway.
2828 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2829 // If the next token starts an expression, we know we're parsing a
2830 // bit-field. This is the common case.
2831 if (TPR == TPResult::True())
2832 PossibleBitfield = true;
2833 // If the next token starts a type-specifier-seq, it may be either a
2834 // a fixed underlying type or the start of a function-style cast in C++;
2835 // lookahead one more token to see if it's obvious that we have a
2836 // fixed underlying type.
2837 else if (TPR == TPResult::False() &&
2838 GetLookAheadToken(2).getKind() == tok::semi) {
2839 // Consume the ':'.
2840 ConsumeToken();
2841 } else {
2842 // We have the start of a type-specifier-seq, so we have to perform
2843 // tentative parsing to determine whether we have an expression or a
2844 // type.
2845 TentativeParsingAction TPA(*this);
2846
2847 // Consume the ':'.
2848 ConsumeToken();
2849
Douglas Gregor86f208c2011-02-22 20:32:04 +00002850 if ((getLang().CPlusPlus &&
2851 isCXXDeclarationSpecifier() != TPResult::True()) ||
2852 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002853 // We'll parse this as a bitfield later.
2854 PossibleBitfield = true;
2855 TPA.Revert();
2856 } else {
2857 // We have a type-specifier-seq.
2858 TPA.Commit();
2859 }
2860 }
2861 } else {
2862 // Consume the ':'.
2863 ConsumeToken();
2864 }
2865
2866 if (!PossibleBitfield) {
2867 SourceRange Range;
2868 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002869
Douglas Gregor5471bc82011-09-08 17:18:35 +00002870 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002871 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2872 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002873 if (getLang().CPlusPlus0x)
2874 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002875 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002876 }
2877
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002878 // There are three options here. If we have 'enum foo;', then this is a
2879 // forward declaration. If we have 'enum foo {...' then this is a
2880 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2881 //
2882 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2883 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2884 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2885 //
John McCallf312b1e2010-08-26 23:41:50 +00002886 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002887 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002888 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002889 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002890 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002891 else
John McCallf312b1e2010-08-26 23:41:50 +00002892 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002893
2894 // enums cannot be templates, although they can be referenced from a
2895 // template.
2896 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002897 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002898 Diag(Tok, diag::err_enum_template);
2899
2900 // Skip the rest of this declarator, up until the comma or semicolon.
2901 SkipUntil(tok::comma, true);
2902 return;
2903 }
2904
Douglas Gregorb9075602011-02-22 02:55:24 +00002905 if (!Name && TUK != Sema::TUK_Definition) {
2906 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2907
2908 // Skip the rest of this declarator, up until the comma or semicolon.
2909 SkipUntil(tok::comma, true);
2910 return;
2911 }
2912
Douglas Gregor402abb52009-05-28 23:31:59 +00002913 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002914 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002915 const char *PrevSpec = 0;
2916 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002917 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002918 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002919 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002920 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002921 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002922 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002923
Douglas Gregor48c89f42010-04-24 16:38:41 +00002924 if (IsDependent) {
2925 // This enum has a dependent nested-name-specifier. Handle it as a
2926 // dependent tag.
2927 if (!Name) {
2928 DS.SetTypeSpecError();
2929 Diag(Tok, diag::err_expected_type_name_after_typename);
2930 return;
2931 }
2932
Douglas Gregor23c94db2010-07-02 17:43:08 +00002933 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002934 TUK, SS, Name, StartLoc,
2935 NameLoc);
2936 if (Type.isInvalid()) {
2937 DS.SetTypeSpecError();
2938 return;
2939 }
2940
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002941 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2942 NameLoc.isValid() ? NameLoc : StartLoc,
2943 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002944 Diag(StartLoc, DiagID) << PrevSpec;
2945
2946 return;
2947 }
Mike Stump1eb44332009-09-09 15:08:12 +00002948
John McCalld226f652010-08-21 09:40:31 +00002949 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002950 // The action failed to produce an enumeration tag. If this is a
2951 // definition, consume the entire definition.
2952 if (Tok.is(tok::l_brace)) {
2953 ConsumeBrace();
2954 SkipUntil(tok::r_brace);
2955 }
2956
2957 DS.SetTypeSpecError();
2958 return;
2959 }
2960
Chris Lattner04d66662007-10-09 17:33:22 +00002961 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002962 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002964 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2965 NameLoc.isValid() ? NameLoc : StartLoc,
2966 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002967 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002968}
2969
2970/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2971/// enumerator-list:
2972/// enumerator
2973/// enumerator-list ',' enumerator
2974/// enumerator:
2975/// enumeration-constant
2976/// enumeration-constant '=' constant-expression
2977/// enumeration-constant:
2978/// identifier
2979///
John McCalld226f652010-08-21 09:40:31 +00002980void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002981 // Enter the scope of the enum body and start the definition.
2982 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002983 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002984
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002985 BalancedDelimiterTracker T(*this, tok::l_brace);
2986 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Chris Lattner7946dd32007-08-27 17:24:30 +00002988 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002989 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002990 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Chris Lattner5f9e2722011-07-23 10:55:15 +00002992 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002993
John McCalld226f652010-08-21 09:40:31 +00002994 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Reid Spencer5f016e22007-07-11 17:01:13 +00002996 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002997 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2999 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003000
John McCall5b629aa2010-10-22 23:36:17 +00003001 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003002 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003003 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003004
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003006 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003007 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003008 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003009 AssignedVal = ParseConstantExpression();
3010 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003011 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003012 }
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Reid Spencer5f016e22007-07-11 17:01:13 +00003014 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003015 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3016 LastEnumConstDecl,
3017 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003018 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003019 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003020 EnumConstantDecls.push_back(EnumConstDecl);
3021 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Douglas Gregor751f6922010-09-07 14:51:08 +00003023 if (Tok.is(tok::identifier)) {
3024 // We're missing a comma between enumerators.
3025 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3026 Diag(Loc, diag::err_enumerator_list_missing_comma)
3027 << FixItHint::CreateInsertion(Loc, ", ");
3028 continue;
3029 }
3030
Chris Lattner04d66662007-10-09 17:33:22 +00003031 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003032 break;
3033 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Richard Smith7fe62082011-10-15 05:09:34 +00003035 if (Tok.isNot(tok::identifier)) {
3036 if (!getLang().C99 && !getLang().CPlusPlus0x)
3037 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3038 << getLang().CPlusPlus
3039 << FixItHint::CreateRemoval(CommaLoc);
3040 else if (getLang().CPlusPlus0x)
3041 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3042 << FixItHint::CreateRemoval(CommaLoc);
3043 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003044 }
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Reid Spencer5f016e22007-07-11 17:01:13 +00003046 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003047 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003048
Reid Spencer5f016e22007-07-11 17:01:13 +00003049 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003050 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003051 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003052
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003053 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3054 EnumDecl, EnumConstantDecls.data(),
3055 EnumConstantDecls.size(), getCurScope(),
3056 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003057
Douglas Gregor72de6672009-01-08 20:45:30 +00003058 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003059 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3060 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003061}
3062
3063/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003064/// start of a type-qualifier-list.
3065bool Parser::isTypeQualifier() const {
3066 switch (Tok.getKind()) {
3067 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003068
3069 // type-qualifier only in OpenCL
3070 case tok::kw_private:
3071 return getLang().OpenCL;
3072
Steve Naroff5f8aa692008-02-11 23:15:56 +00003073 // type-qualifier
3074 case tok::kw_const:
3075 case tok::kw_volatile:
3076 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003077 case tok::kw___private:
3078 case tok::kw___local:
3079 case tok::kw___global:
3080 case tok::kw___constant:
3081 case tok::kw___read_only:
3082 case tok::kw___read_write:
3083 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003084 return true;
3085 }
3086}
3087
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003088/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3089/// is definitely a type-specifier. Return false if it isn't part of a type
3090/// specifier or if we're not sure.
3091bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3092 switch (Tok.getKind()) {
3093 default: return false;
3094 // type-specifiers
3095 case tok::kw_short:
3096 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003097 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003098 case tok::kw_signed:
3099 case tok::kw_unsigned:
3100 case tok::kw__Complex:
3101 case tok::kw__Imaginary:
3102 case tok::kw_void:
3103 case tok::kw_char:
3104 case tok::kw_wchar_t:
3105 case tok::kw_char16_t:
3106 case tok::kw_char32_t:
3107 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003108 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003109 case tok::kw_float:
3110 case tok::kw_double:
3111 case tok::kw_bool:
3112 case tok::kw__Bool:
3113 case tok::kw__Decimal32:
3114 case tok::kw__Decimal64:
3115 case tok::kw__Decimal128:
3116 case tok::kw___vector:
3117
3118 // struct-or-union-specifier (C99) or class-specifier (C++)
3119 case tok::kw_class:
3120 case tok::kw_struct:
3121 case tok::kw_union:
3122 // enum-specifier
3123 case tok::kw_enum:
3124
3125 // typedef-name
3126 case tok::annot_typename:
3127 return true;
3128 }
3129}
3130
Steve Naroff5f8aa692008-02-11 23:15:56 +00003131/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003132/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003133bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003134 switch (Tok.getKind()) {
3135 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Chris Lattner166a8fc2009-01-04 23:41:41 +00003137 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003138 if (TryAltiVecVectorToken())
3139 return true;
3140 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003141 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003142 // Annotate typenames and C++ scope specifiers. If we get one, just
3143 // recurse to handle whatever we get.
3144 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003145 return true;
3146 if (Tok.is(tok::identifier))
3147 return false;
3148 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003149
Chris Lattner166a8fc2009-01-04 23:41:41 +00003150 case tok::coloncolon: // ::foo::bar
3151 if (NextToken().is(tok::kw_new) || // ::new
3152 NextToken().is(tok::kw_delete)) // ::delete
3153 return false;
3154
Chris Lattner166a8fc2009-01-04 23:41:41 +00003155 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003156 return true;
3157 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 // GNU attributes support.
3160 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003161 // GNU typeof support.
3162 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Reid Spencer5f016e22007-07-11 17:01:13 +00003164 // type-specifiers
3165 case tok::kw_short:
3166 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003167 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 case tok::kw_signed:
3169 case tok::kw_unsigned:
3170 case tok::kw__Complex:
3171 case tok::kw__Imaginary:
3172 case tok::kw_void:
3173 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003174 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003175 case tok::kw_char16_t:
3176 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003177 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003178 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003179 case tok::kw_float:
3180 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003181 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003182 case tok::kw__Bool:
3183 case tok::kw__Decimal32:
3184 case tok::kw__Decimal64:
3185 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003186 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Chris Lattner99dc9142008-04-13 18:59:07 +00003188 // struct-or-union-specifier (C99) or class-specifier (C++)
3189 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003190 case tok::kw_struct:
3191 case tok::kw_union:
3192 // enum-specifier
3193 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 // type-qualifier
3196 case tok::kw_const:
3197 case tok::kw_volatile:
3198 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003199
3200 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003201 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003202 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003203
Chris Lattner7c186be2008-10-20 00:25:30 +00003204 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3205 case tok::less:
3206 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003207
Steve Naroff239f0732008-12-25 14:16:32 +00003208 case tok::kw___cdecl:
3209 case tok::kw___stdcall:
3210 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003211 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003212 case tok::kw___w64:
3213 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003214 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003215 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003216 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003217
3218 case tok::kw___private:
3219 case tok::kw___local:
3220 case tok::kw___global:
3221 case tok::kw___constant:
3222 case tok::kw___read_only:
3223 case tok::kw___read_write:
3224 case tok::kw___write_only:
3225
Eli Friedman290eeb02009-06-08 23:27:34 +00003226 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003227
3228 case tok::kw_private:
3229 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003230
3231 // C1x _Atomic()
3232 case tok::kw__Atomic:
3233 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 }
3235}
3236
3237/// isDeclarationSpecifier() - Return true if the current token is part of a
3238/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003239///
3240/// \param DisambiguatingWithExpression True to indicate that the purpose of
3241/// this check is to disambiguate between an expression and a declaration.
3242bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003243 switch (Tok.getKind()) {
3244 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003246 case tok::kw_private:
3247 return getLang().OpenCL;
3248
Chris Lattner166a8fc2009-01-04 23:41:41 +00003249 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003250 // Unfortunate hack to support "Class.factoryMethod" notation.
3251 if (getLang().ObjC1 && NextToken().is(tok::period))
3252 return false;
John Thompson82287d12010-02-05 00:12:22 +00003253 if (TryAltiVecVectorToken())
3254 return true;
3255 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003256 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003257 // Annotate typenames and C++ scope specifiers. If we get one, just
3258 // recurse to handle whatever we get.
3259 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003260 return true;
3261 if (Tok.is(tok::identifier))
3262 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003263
3264 // If we're in Objective-C and we have an Objective-C class type followed
3265 // by an identifier and then either ':' or ']', in a place where an
3266 // expression is permitted, then this is probably a class message send
3267 // missing the initial '['. In this case, we won't consider this to be
3268 // the start of a declaration.
3269 if (DisambiguatingWithExpression &&
3270 isStartOfObjCClassMessageMissingOpenBracket())
3271 return false;
3272
John McCall9ba61662010-02-26 08:45:28 +00003273 return isDeclarationSpecifier();
3274
Chris Lattner166a8fc2009-01-04 23:41:41 +00003275 case tok::coloncolon: // ::foo::bar
3276 if (NextToken().is(tok::kw_new) || // ::new
3277 NextToken().is(tok::kw_delete)) // ::delete
3278 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Chris Lattner166a8fc2009-01-04 23:41:41 +00003280 // Annotate typenames and C++ scope specifiers. If we get one, just
3281 // recurse to handle whatever we get.
3282 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003283 return true;
3284 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Reid Spencer5f016e22007-07-11 17:01:13 +00003286 // storage-class-specifier
3287 case tok::kw_typedef:
3288 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003289 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 case tok::kw_static:
3291 case tok::kw_auto:
3292 case tok::kw_register:
3293 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Douglas Gregor8d267c52011-09-09 02:06:17 +00003295 // Modules
3296 case tok::kw___module_private__:
3297
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 // type-specifiers
3299 case tok::kw_short:
3300 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003301 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003302 case tok::kw_signed:
3303 case tok::kw_unsigned:
3304 case tok::kw__Complex:
3305 case tok::kw__Imaginary:
3306 case tok::kw_void:
3307 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003308 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003309 case tok::kw_char16_t:
3310 case tok::kw_char32_t:
3311
Reid Spencer5f016e22007-07-11 17:01:13 +00003312 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003313 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 case tok::kw_float:
3315 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003316 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 case tok::kw__Bool:
3318 case tok::kw__Decimal32:
3319 case tok::kw__Decimal64:
3320 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003321 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003322
Chris Lattner99dc9142008-04-13 18:59:07 +00003323 // struct-or-union-specifier (C99) or class-specifier (C++)
3324 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 case tok::kw_struct:
3326 case tok::kw_union:
3327 // enum-specifier
3328 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 // type-qualifier
3331 case tok::kw_const:
3332 case tok::kw_volatile:
3333 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003334
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 // function-specifier
3336 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003337 case tok::kw_virtual:
3338 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003339
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003340 // static_assert-declaration
3341 case tok::kw__Static_assert:
3342
Chris Lattner1ef08762007-08-09 17:01:07 +00003343 // GNU typeof support.
3344 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Chris Lattner1ef08762007-08-09 17:01:07 +00003346 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003347 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003348 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Francois Pichete3d49b42011-06-19 08:02:06 +00003350 // C++0x decltype.
3351 case tok::kw_decltype:
3352 return true;
3353
Eli Friedmanb001de72011-10-06 23:00:33 +00003354 // C1x _Atomic()
3355 case tok::kw__Atomic:
3356 return true;
3357
Chris Lattnerf3948c42008-07-26 03:38:44 +00003358 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3359 case tok::less:
3360 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003361
Douglas Gregord9d75e52011-04-27 05:41:15 +00003362 // typedef-name
3363 case tok::annot_typename:
3364 return !DisambiguatingWithExpression ||
3365 !isStartOfObjCClassMessageMissingOpenBracket();
3366
Steve Naroff47f52092009-01-06 19:34:12 +00003367 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003368 case tok::kw___cdecl:
3369 case tok::kw___stdcall:
3370 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003371 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003372 case tok::kw___w64:
3373 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003374 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003375 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003376 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003377 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003378
3379 case tok::kw___private:
3380 case tok::kw___local:
3381 case tok::kw___global:
3382 case tok::kw___constant:
3383 case tok::kw___read_only:
3384 case tok::kw___read_write:
3385 case tok::kw___write_only:
3386
Eli Friedman290eeb02009-06-08 23:27:34 +00003387 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003388 }
3389}
3390
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003391bool Parser::isConstructorDeclarator() {
3392 TentativeParsingAction TPA(*this);
3393
3394 // Parse the C++ scope specifier.
3395 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003396 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003397 TPA.Revert();
3398 return false;
3399 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003400
3401 // Parse the constructor name.
3402 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3403 // We already know that we have a constructor name; just consume
3404 // the token.
3405 ConsumeToken();
3406 } else {
3407 TPA.Revert();
3408 return false;
3409 }
3410
3411 // Current class name must be followed by a left parentheses.
3412 if (Tok.isNot(tok::l_paren)) {
3413 TPA.Revert();
3414 return false;
3415 }
3416 ConsumeParen();
3417
3418 // A right parentheses or ellipsis signals that we have a constructor.
3419 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3420 TPA.Revert();
3421 return true;
3422 }
3423
3424 // If we need to, enter the specified scope.
3425 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003426 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003427 DeclScopeObj.EnterDeclaratorScope();
3428
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003429 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003430 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003431 MaybeParseMicrosoftAttributes(Attrs);
3432
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003433 // Check whether the next token(s) are part of a declaration
3434 // specifier, in which case we have the start of a parameter and,
3435 // therefore, we know that this is a constructor.
3436 bool IsConstructor = isDeclarationSpecifier();
3437 TPA.Revert();
3438 return IsConstructor;
3439}
Reid Spencer5f016e22007-07-11 17:01:13 +00003440
3441/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003442/// type-qualifier-list: [C99 6.7.5]
3443/// type-qualifier
3444/// [vendor] attributes
3445/// [ only if VendorAttributesAllowed=true ]
3446/// type-qualifier-list type-qualifier
3447/// [vendor] type-qualifier-list attributes
3448/// [ only if VendorAttributesAllowed=true ]
3449/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3450/// [ only if CXX0XAttributesAllowed=true ]
3451/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003452///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003453void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3454 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003455 bool CXX0XAttributesAllowed) {
3456 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3457 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003458 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003459 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003460 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003461 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003462 else
3463 Diag(Loc, diag::err_attributes_not_allowed);
3464 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003465
3466 SourceLocation EndLoc;
3467
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003469 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003471 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 SourceLocation Loc = Tok.getLocation();
3473
3474 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003475 case tok::code_completion:
3476 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003477 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003478
Reid Spencer5f016e22007-07-11 17:01:13 +00003479 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003480 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3481 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 break;
3483 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003484 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3485 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003486 break;
3487 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003488 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3489 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003491
3492 // OpenCL qualifiers:
3493 case tok::kw_private:
3494 if (!getLang().OpenCL)
3495 goto DoneWithTypeQuals;
3496 case tok::kw___private:
3497 case tok::kw___global:
3498 case tok::kw___local:
3499 case tok::kw___constant:
3500 case tok::kw___read_only:
3501 case tok::kw___write_only:
3502 case tok::kw___read_write:
3503 ParseOpenCLQualifiers(DS);
3504 break;
3505
Eli Friedman290eeb02009-06-08 23:27:34 +00003506 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003507 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003508 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003509 case tok::kw___cdecl:
3510 case tok::kw___stdcall:
3511 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003512 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003513 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003514 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003515 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003516 continue;
3517 }
3518 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003519 case tok::kw___pascal:
3520 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003521 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003522 continue;
3523 }
3524 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003526 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003527 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003528 continue; // do *not* consume the next token!
3529 }
3530 // otherwise, FALL THROUGH!
3531 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003532 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003533 // If this is not a type-qualifier token, we're done reading type
3534 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003535 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003536 if (EndLoc.isValid())
3537 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003538 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003539 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003540
Reid Spencer5f016e22007-07-11 17:01:13 +00003541 // If the specifier combination wasn't legal, issue a diagnostic.
3542 if (isInvalid) {
3543 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003544 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003545 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003546 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003547 }
3548}
3549
3550
3551/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3552///
3553void Parser::ParseDeclarator(Declarator &D) {
3554 /// This implements the 'declarator' production in the C grammar, then checks
3555 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003556 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003557}
3558
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003559/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3560/// is parsed by the function passed to it. Pass null, and the direct-declarator
3561/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003562/// ptr-operator production.
3563///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003564/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3565/// [C] pointer[opt] direct-declarator
3566/// [C++] direct-declarator
3567/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003568///
3569/// pointer: [C99 6.7.5]
3570/// '*' type-qualifier-list[opt]
3571/// '*' type-qualifier-list[opt] pointer
3572///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003573/// ptr-operator:
3574/// '*' cv-qualifier-seq[opt]
3575/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003576/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003577/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003578/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003579/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003580void Parser::ParseDeclaratorInternal(Declarator &D,
3581 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003582 if (Diags.hasAllExtensionsSilenced())
3583 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003584
Sebastian Redlf30208a2009-01-24 21:16:55 +00003585 // C++ member pointers start with a '::' or a nested-name.
3586 // Member pointers get special handling, since there's no place for the
3587 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003588 if (getLang().CPlusPlus &&
3589 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3590 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003591 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003592 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003593
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003594 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003595 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003596 // The scope spec really belongs to the direct-declarator.
3597 D.getCXXScopeSpec() = SS;
3598 if (DirectDeclParser)
3599 (this->*DirectDeclParser)(D);
3600 return;
3601 }
3602
3603 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003604 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003605 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003606 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003607 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003608
3609 // Recurse to parse whatever is left.
3610 ParseDeclaratorInternal(D, DirectDeclParser);
3611
3612 // Sema will have to catch (syntactically invalid) pointers into global
3613 // scope. It has to catch pointers into namespace scope anyway.
3614 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003615 Loc),
3616 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003617 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003618 return;
3619 }
3620 }
3621
3622 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003623 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003624 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003625 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003626 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003627 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003628 if (DirectDeclParser)
3629 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003630 return;
3631 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003632
Sebastian Redl05532f22009-03-15 22:02:01 +00003633 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3634 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003635 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003636 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003637
Chris Lattner9af55002009-03-27 04:18:06 +00003638 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003639 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003640 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003641
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003643 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003644
Reid Spencer5f016e22007-07-11 17:01:13 +00003645 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003646 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003647 if (Kind == tok::star)
3648 // Remember that we parsed a pointer type, and remember the type-quals.
3649 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003650 DS.getConstSpecLoc(),
3651 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003652 DS.getRestrictSpecLoc()),
3653 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003654 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003655 else
3656 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003657 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003658 Loc),
3659 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003660 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003661 } else {
3662 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003663 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003664
Sebastian Redl743de1f2009-03-23 00:00:23 +00003665 // Complain about rvalue references in C++03, but then go on and build
3666 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003667 if (Kind == tok::ampamp)
3668 Diag(Loc, getLang().CPlusPlus0x ?
3669 diag::warn_cxx98_compat_rvalue_reference :
3670 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003671
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3673 // cv-qualifiers are introduced through the use of a typedef or of a
3674 // template type argument, in which case the cv-qualifiers are ignored.
3675 //
3676 // [GNU] Retricted references are allowed.
3677 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003678 // [C++0x] Attributes on references are not allowed.
3679 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003680 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003681
3682 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3683 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3684 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003685 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003686 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3687 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003688 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 }
3690
3691 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003692 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003693
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003694 if (D.getNumTypeObjects() > 0) {
3695 // C++ [dcl.ref]p4: There shall be no references to references.
3696 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3697 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003698 if (const IdentifierInfo *II = D.getIdentifier())
3699 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3700 << II;
3701 else
3702 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3703 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003704
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003705 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003706 // can go ahead and build the (technically ill-formed)
3707 // declarator: reference collapsing will take care of it.
3708 }
3709 }
3710
Reid Spencer5f016e22007-07-11 17:01:13 +00003711 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003712 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003713 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003714 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003715 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 }
3717}
3718
3719/// ParseDirectDeclarator
3720/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003721/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003722/// '(' declarator ')'
3723/// [GNU] '(' attributes declarator ')'
3724/// [C90] direct-declarator '[' constant-expression[opt] ']'
3725/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3726/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3727/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3728/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3729/// direct-declarator '(' parameter-type-list ')'
3730/// direct-declarator '(' identifier-list[opt] ')'
3731/// [GNU] direct-declarator '(' parameter-forward-declarations
3732/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003733/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3734/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003735/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003736///
3737/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003738/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003739/// '::'[opt] nested-name-specifier[opt] type-name
3740///
3741/// id-expression: [C++ 5.1]
3742/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003743/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003744///
3745/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003746/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003747/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003748/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003749/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003750/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003751///
Reid Spencer5f016e22007-07-11 17:01:13 +00003752void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003753 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003754
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003755 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3756 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003757 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003758 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003759 }
3760
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003761 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003762 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003763 // Change the declaration context for name lookup, until this function
3764 // is exited (and the declarator has been parsed).
3765 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003766 }
3767
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003768 // C++0x [dcl.fct]p14:
3769 // There is a syntactic ambiguity when an ellipsis occurs at the end
3770 // of a parameter-declaration-clause without a preceding comma. In
3771 // this case, the ellipsis is parsed as part of the
3772 // abstract-declarator if the type of the parameter names a template
3773 // parameter pack that has not been expanded; otherwise, it is parsed
3774 // as part of the parameter-declaration-clause.
3775 if (Tok.is(tok::ellipsis) &&
3776 !((D.getContext() == Declarator::PrototypeContext ||
3777 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003778 NextToken().is(tok::r_paren) &&
3779 !Actions.containsUnexpandedParameterPacks(D)))
3780 D.setEllipsisLoc(ConsumeToken());
3781
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003782 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3783 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3784 // We found something that indicates the start of an unqualified-id.
3785 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003786 bool AllowConstructorName;
3787 if (D.getDeclSpec().hasTypeSpecifier())
3788 AllowConstructorName = false;
3789 else if (D.getCXXScopeSpec().isSet())
3790 AllowConstructorName =
3791 (D.getContext() == Declarator::FileContext ||
3792 (D.getContext() == Declarator::MemberContext &&
3793 D.getDeclSpec().isFriendSpecified()));
3794 else
3795 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3796
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003797 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3798 /*EnteringContext=*/true,
3799 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003800 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003801 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003802 D.getName()) ||
3803 // Once we're past the identifier, if the scope was bad, mark the
3804 // whole declarator bad.
3805 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003806 D.SetIdentifier(0, Tok.getLocation());
3807 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003808 } else {
3809 // Parsed the unqualified-id; update range information and move along.
3810 if (D.getSourceRange().getBegin().isInvalid())
3811 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3812 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003813 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003814 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003815 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003816 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003817 assert(!getLang().CPlusPlus &&
3818 "There's a C++-specific check for tok::identifier above");
3819 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3820 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3821 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003822 goto PastIdentifier;
3823 }
3824
3825 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003826 // direct-declarator: '(' declarator ')'
3827 // direct-declarator: '(' attributes declarator ')'
3828 // Example: 'char (*X)' or 'int (*XX)(void)'
3829 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003830
3831 // If the declarator was parenthesized, we entered the declarator
3832 // scope when parsing the parenthesized declarator, then exited
3833 // the scope already. Re-enter the scope, if we need to.
3834 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003835 // If there was an error parsing parenthesized declarator, declarator
3836 // scope may have been enterred before. Don't do it again.
3837 if (!D.isInvalidType() &&
3838 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003839 // Change the declaration context for name lookup, until this function
3840 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003841 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003842 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003843 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003844 // This could be something simple like "int" (in which case the declarator
3845 // portion is empty), if an abstract-declarator is allowed.
3846 D.SetIdentifier(0, Tok.getLocation());
3847 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003848 if (D.getContext() == Declarator::MemberContext)
3849 Diag(Tok, diag::err_expected_member_name_or_semi)
3850 << D.getDeclSpec().getSourceRange();
3851 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003852 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003853 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003854 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003856 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003857 }
Mike Stump1eb44332009-09-09 15:08:12 +00003858
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003859 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003860 assert(D.isPastIdentifier() &&
3861 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003862
Sean Huntbbd37c62009-11-21 08:43:09 +00003863 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003864 if (D.getIdentifier())
3865 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003866
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003868 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003869 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3870 // In such a case, check if we actually have a function declarator; if it
3871 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003872 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3873 // When not in file scope, warn for ambiguous function declarators, just
3874 // in case the author intended it as a variable definition.
3875 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3876 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3877 break;
3878 }
John McCall0b7e6782011-03-24 11:26:52 +00003879 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003880 BalancedDelimiterTracker T(*this, tok::l_paren);
3881 T.consumeOpen();
3882 ParseFunctionDeclarator(D, attrs, T);
Chris Lattner04d66662007-10-09 17:33:22 +00003883 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 ParseBracketDeclarator(D);
3885 } else {
3886 break;
3887 }
3888 }
3889}
3890
Chris Lattneref4715c2008-04-06 05:45:57 +00003891/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3892/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003893/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003894/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3895///
3896/// direct-declarator:
3897/// '(' declarator ')'
3898/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003899/// direct-declarator '(' parameter-type-list ')'
3900/// direct-declarator '(' identifier-list[opt] ')'
3901/// [GNU] direct-declarator '(' parameter-forward-declarations
3902/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003903///
3904void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003905 BalancedDelimiterTracker T(*this, tok::l_paren);
3906 T.consumeOpen();
3907
Chris Lattneref4715c2008-04-06 05:45:57 +00003908 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003909
Chris Lattner7399ee02008-10-20 02:05:46 +00003910 // Eat any attributes before we look at whether this is a grouping or function
3911 // declarator paren. If this is a grouping paren, the attribute applies to
3912 // the type being built up, for example:
3913 // int (__attribute__(()) *x)(long y)
3914 // If this ends up not being a grouping paren, the attribute applies to the
3915 // first argument, for example:
3916 // int (__attribute__(()) int x)
3917 // In either case, we need to eat any attributes to be able to determine what
3918 // sort of paren this is.
3919 //
John McCall0b7e6782011-03-24 11:26:52 +00003920 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003921 bool RequiresArg = false;
3922 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003923 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Chris Lattner7399ee02008-10-20 02:05:46 +00003925 // We require that the argument list (if this is a non-grouping paren) be
3926 // present even if the attribute list was empty.
3927 RequiresArg = true;
3928 }
Steve Naroff239f0732008-12-25 14:16:32 +00003929 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003930 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003931 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003932 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003933 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003934 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003935 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003936 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003937 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003938 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003939
Chris Lattneref4715c2008-04-06 05:45:57 +00003940 // If we haven't past the identifier yet (or where the identifier would be
3941 // stored, if this is an abstract declarator), then this is probably just
3942 // grouping parens. However, if this could be an abstract-declarator, then
3943 // this could also be the start of function arguments (consider 'void()').
3944 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003945
Chris Lattneref4715c2008-04-06 05:45:57 +00003946 if (!D.mayOmitIdentifier()) {
3947 // If this can't be an abstract-declarator, this *must* be a grouping
3948 // paren, because we haven't seen the identifier yet.
3949 isGrouping = true;
3950 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003951 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003952 isDeclarationSpecifier()) { // 'int(int)' is a function.
3953 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3954 // considered to be a type, not a K&R identifier-list.
3955 isGrouping = false;
3956 } else {
3957 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3958 isGrouping = true;
3959 }
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Chris Lattneref4715c2008-04-06 05:45:57 +00003961 // If this is a grouping paren, handle:
3962 // direct-declarator: '(' declarator ')'
3963 // direct-declarator: '(' attributes declarator ')'
3964 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003965 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003966 D.setGroupingParens(true);
3967
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003968 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003969 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003970 T.consumeClose();
3971 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3972 T.getCloseLocation()),
3973 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003974
3975 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003976 return;
3977 }
Mike Stump1eb44332009-09-09 15:08:12 +00003978
Chris Lattneref4715c2008-04-06 05:45:57 +00003979 // Okay, if this wasn't a grouping paren, it must be the start of a function
3980 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003981 // identifier (and remember where it would have been), then call into
3982 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003983 D.SetIdentifier(0, Tok.getLocation());
3984
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003985 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003986}
3987
3988/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3989/// declarator D up to a paren, which indicates that we are parsing function
3990/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003991///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003992/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003993/// after the open paren - they should be considered to be the first argument of
3994/// a parameter. If RequiresArg is true, then the first argument of the
3995/// function is required to be present and required to not be an identifier
3996/// list.
3997///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003998/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3999/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4000/// (C++0x) trailing-return-type[opt].
4001///
4002/// [C++0x] exception-specification:
4003/// dynamic-exception-specification
4004/// noexcept-specification
4005///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004006void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004007 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004008 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004009 bool RequiresArg) {
4010 // lparen is already consumed!
4011 assert(D.isPastIdentifier() && "Should not call before identifier!");
4012
4013 // This should be true when the function has typed arguments.
4014 // Otherwise, it is treated as a K&R-style function.
4015 bool HasProto = false;
4016 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004017 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004018 // Remember where we see an ellipsis, if any.
4019 SourceLocation EllipsisLoc;
4020
4021 DeclSpec DS(AttrFactory);
4022 bool RefQualifierIsLValueRef = true;
4023 SourceLocation RefQualifierLoc;
4024 ExceptionSpecificationType ESpecType = EST_None;
4025 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004026 SmallVector<ParsedType, 2> DynamicExceptions;
4027 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004028 ExprResult NoexceptExpr;
4029 ParsedType TrailingReturnType;
4030
4031 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004032 if (isFunctionDeclaratorIdentifierList()) {
4033 if (RequiresArg)
4034 Diag(Tok, diag::err_argument_required_after_attribute);
4035
4036 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4037
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004038 Tracker.consumeClose();
4039 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004040 } else {
4041 // Enter function-declaration scope, limiting any declarators to the
4042 // function prototype scope, including parameter declarators.
4043 ParseScope PrototypeScope(this,
4044 Scope::FunctionPrototypeScope|Scope::DeclScope);
4045
4046 if (Tok.isNot(tok::r_paren))
4047 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4048 else if (RequiresArg)
4049 Diag(Tok, diag::err_argument_required_after_attribute);
4050
4051 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4052
4053 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004054 Tracker.consumeClose();
4055 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004056
4057 if (getLang().CPlusPlus) {
4058 MaybeParseCXX0XAttributes(attrs);
4059
4060 // Parse cv-qualifier-seq[opt].
4061 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4062 if (!DS.getSourceRange().getEnd().isInvalid())
4063 EndLoc = DS.getSourceRange().getEnd();
4064
4065 // Parse ref-qualifier[opt].
4066 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004067 Diag(Tok, getLang().CPlusPlus0x ?
4068 diag::warn_cxx98_compat_ref_qualifier :
4069 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004070
4071 RefQualifierIsLValueRef = Tok.is(tok::amp);
4072 RefQualifierLoc = ConsumeToken();
4073 EndLoc = RefQualifierLoc;
4074 }
4075
4076 // Parse exception-specification[opt].
4077 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4078 DynamicExceptions,
4079 DynamicExceptionRanges,
4080 NoexceptExpr);
4081 if (ESpecType != EST_None)
4082 EndLoc = ESpecRange.getEnd();
4083
4084 // Parse trailing-return-type[opt].
4085 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004086 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004087 SourceRange Range;
4088 TrailingReturnType = ParseTrailingReturnType(Range).get();
4089 if (Range.getEnd().isValid())
4090 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004091 }
4092 }
4093
4094 // Leave prototype scope.
4095 PrototypeScope.Exit();
4096 }
4097
4098 // Remember that we parsed a function type, and remember the attributes.
4099 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4100 /*isVariadic=*/EllipsisLoc.isValid(),
4101 EllipsisLoc,
4102 ParamInfo.data(), ParamInfo.size(),
4103 DS.getTypeQualifiers(),
4104 RefQualifierIsLValueRef,
4105 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004106 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004107 ESpecType, ESpecRange.getBegin(),
4108 DynamicExceptions.data(),
4109 DynamicExceptionRanges.data(),
4110 DynamicExceptions.size(),
4111 NoexceptExpr.isUsable() ?
4112 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004113 Tracker.getOpenLocation(),
4114 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004115 TrailingReturnType),
4116 attrs, EndLoc);
4117}
4118
4119/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4120/// identifier list form for a K&R-style function: void foo(a,b,c)
4121///
4122/// Note that identifier-lists are only allowed for normal declarators, not for
4123/// abstract-declarators.
4124bool Parser::isFunctionDeclaratorIdentifierList() {
4125 return !getLang().CPlusPlus
4126 && Tok.is(tok::identifier)
4127 && !TryAltiVecVectorToken()
4128 // K&R identifier lists can't have typedefs as identifiers, per C99
4129 // 6.7.5.3p11.
4130 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4131 // Identifier lists follow a really simple grammar: the identifiers can
4132 // be followed *only* by a ", identifier" or ")". However, K&R
4133 // identifier lists are really rare in the brave new modern world, and
4134 // it is very common for someone to typo a type in a non-K&R style
4135 // list. If we are presented with something like: "void foo(intptr x,
4136 // float y)", we don't want to start parsing the function declarator as
4137 // though it is a K&R style declarator just because intptr is an
4138 // invalid type.
4139 //
4140 // To handle this, we check to see if the token after the first
4141 // identifier is a "," or ")". Only then do we parse it as an
4142 // identifier list.
4143 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4144}
4145
4146/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4147/// we found a K&R-style identifier list instead of a typed parameter list.
4148///
4149/// After returning, ParamInfo will hold the parsed parameters.
4150///
4151/// identifier-list: [C99 6.7.5]
4152/// identifier
4153/// identifier-list ',' identifier
4154///
4155void Parser::ParseFunctionDeclaratorIdentifierList(
4156 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004157 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004158 // If there was no identifier specified for the declarator, either we are in
4159 // an abstract-declarator, or we are in a parameter declarator which was found
4160 // to be abstract. In abstract-declarators, identifier lists are not valid:
4161 // diagnose this.
4162 if (!D.getIdentifier())
4163 Diag(Tok, diag::ext_ident_list_in_param);
4164
4165 // Maintain an efficient lookup of params we have seen so far.
4166 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4167
4168 while (1) {
4169 // If this isn't an identifier, report the error and skip until ')'.
4170 if (Tok.isNot(tok::identifier)) {
4171 Diag(Tok, diag::err_expected_ident);
4172 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4173 // Forget we parsed anything.
4174 ParamInfo.clear();
4175 return;
4176 }
4177
4178 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4179
4180 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4181 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4182 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4183
4184 // Verify that the argument identifier has not already been mentioned.
4185 if (!ParamsSoFar.insert(ParmII)) {
4186 Diag(Tok, diag::err_param_redefinition) << ParmII;
4187 } else {
4188 // Remember this identifier in ParamInfo.
4189 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4190 Tok.getLocation(),
4191 0));
4192 }
4193
4194 // Eat the identifier.
4195 ConsumeToken();
4196
4197 // The list continues if we see a comma.
4198 if (Tok.isNot(tok::comma))
4199 break;
4200 ConsumeToken();
4201 }
4202}
4203
4204/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4205/// after the opening parenthesis. This function will not parse a K&R-style
4206/// identifier list.
4207///
4208/// D is the declarator being parsed. If attrs is non-null, then the caller
4209/// parsed those arguments immediately after the open paren - they should be
4210/// considered to be the first argument of a parameter.
4211///
4212/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4213/// be the location of the ellipsis, if any was parsed.
4214///
Reid Spencer5f016e22007-07-11 17:01:13 +00004215/// parameter-type-list: [C99 6.7.5]
4216/// parameter-list
4217/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004218/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004219///
4220/// parameter-list: [C99 6.7.5]
4221/// parameter-declaration
4222/// parameter-list ',' parameter-declaration
4223///
4224/// parameter-declaration: [C99 6.7.5]
4225/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004226/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004227/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004228/// declaration-specifiers abstract-declarator[opt]
4229/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004230/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004231/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4232///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004233void Parser::ParseParameterDeclarationClause(
4234 Declarator &D,
4235 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004236 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004237 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004238
Chris Lattnerf97409f2008-04-06 06:57:35 +00004239 while (1) {
4240 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004241 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004242 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004243 }
Mike Stump1eb44332009-09-09 15:08:12 +00004244
Chris Lattnerf97409f2008-04-06 06:57:35 +00004245 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004246 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004247 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004248
John McCall7f040a92010-12-24 02:08:15 +00004249 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004250 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004251 ParseMicrosoftAttributes(DS.getAttributes());
4252
4253 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004254
4255 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004256 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004257 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4258 // attributes lost? Should they even be allowed?
4259 // FIXME: If we can leave the attributes in the token stream somehow, we can
4260 // get rid of a parameter (attrs) and this statement. It might be too much
4261 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004262 DS.takeAttributesFrom(attrs);
4263
Chris Lattnere64c5492009-02-27 18:38:20 +00004264 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004265
Chris Lattnerf97409f2008-04-06 06:57:35 +00004266 // Parse the declarator. This is "PrototypeContext", because we must
4267 // accept either 'declarator' or 'abstract-declarator' here.
4268 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4269 ParseDeclarator(ParmDecl);
4270
4271 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004272 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004273
Chris Lattnerf97409f2008-04-06 06:57:35 +00004274 // Remember this parsed parameter in ParamInfo.
4275 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004276
Douglas Gregor72b505b2008-12-16 21:30:33 +00004277 // DefArgToks is used when the parsing of default arguments needs
4278 // to be delayed.
4279 CachedTokens *DefArgToks = 0;
4280
Chris Lattnerf97409f2008-04-06 06:57:35 +00004281 // If no parameter was specified, verify that *something* was specified,
4282 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004283 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4284 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004285 // Completely missing, emit error.
4286 Diag(DSStart, diag::err_missing_param);
4287 } else {
4288 // Otherwise, we have something. Add it and let semantic analysis try
4289 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004290
Chris Lattnerf97409f2008-04-06 06:57:35 +00004291 // Inform the actions module about the parameter declarator, so it gets
4292 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004293 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004294
4295 // Parse the default argument, if any. We parse the default
4296 // arguments in all dialects; the semantic analysis in
4297 // ActOnParamDefaultArgument will reject the default argument in
4298 // C.
4299 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004300 SourceLocation EqualLoc = Tok.getLocation();
4301
Chris Lattner04421082008-04-08 04:40:51 +00004302 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004303 if (D.getContext() == Declarator::MemberContext) {
4304 // If we're inside a class definition, cache the tokens
4305 // corresponding to the default argument. We'll actually parse
4306 // them when we see the end of the class definition.
4307 // FIXME: Templates will require something similar.
4308 // FIXME: Can we use a smart pointer for Toks?
4309 DefArgToks = new CachedTokens;
4310
Mike Stump1eb44332009-09-09 15:08:12 +00004311 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004312 /*StopAtSemi=*/true,
4313 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004314 delete DefArgToks;
4315 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004316 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004317 } else {
4318 // Mark the end of the default argument so that we know when to
4319 // stop when we parse it later on.
4320 Token DefArgEnd;
4321 DefArgEnd.startToken();
4322 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4323 DefArgEnd.setLocation(Tok.getLocation());
4324 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004325 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004326 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004327 }
Chris Lattner04421082008-04-08 04:40:51 +00004328 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004329 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004330 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004331
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004332 // The argument isn't actually potentially evaluated unless it is
4333 // used.
4334 EnterExpressionEvaluationContext Eval(Actions,
4335 Sema::PotentiallyEvaluatedIfUsed);
4336
John McCall60d7b3a2010-08-24 06:29:42 +00004337 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004338 if (DefArgResult.isInvalid()) {
4339 Actions.ActOnParamDefaultArgumentError(Param);
4340 SkipUntil(tok::comma, tok::r_paren, true, true);
4341 } else {
4342 // Inform the actions module about the default argument
4343 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004344 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004345 }
Chris Lattner04421082008-04-08 04:40:51 +00004346 }
4347 }
Mike Stump1eb44332009-09-09 15:08:12 +00004348
4349 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4350 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004351 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004352 }
4353
4354 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004355 if (Tok.isNot(tok::comma)) {
4356 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004357 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4358
4359 if (!getLang().CPlusPlus) {
4360 // We have ellipsis without a preceding ',', which is ill-formed
4361 // in C. Complain and provide the fix.
4362 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004363 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004364 }
4365 }
4366
4367 break;
4368 }
Mike Stump1eb44332009-09-09 15:08:12 +00004369
Chris Lattnerf97409f2008-04-06 06:57:35 +00004370 // Consume the comma.
4371 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004372 }
Mike Stump1eb44332009-09-09 15:08:12 +00004373
Chris Lattner66d28652008-04-06 06:34:08 +00004374}
Chris Lattneref4715c2008-04-06 05:45:57 +00004375
Reid Spencer5f016e22007-07-11 17:01:13 +00004376/// [C90] direct-declarator '[' constant-expression[opt] ']'
4377/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4378/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4379/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4380/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4381void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004382 BalancedDelimiterTracker T(*this, tok::l_square);
4383 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004384
Chris Lattner378c7e42008-12-18 07:27:21 +00004385 // C array syntax has many features, but by-far the most common is [] and [4].
4386 // This code does a fast path to handle some of the most obvious cases.
4387 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004388 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004389 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004390 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004391
Chris Lattner378c7e42008-12-18 07:27:21 +00004392 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004393 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004394 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004395 T.getOpenLocation(),
4396 T.getCloseLocation()),
4397 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004398 return;
4399 } else if (Tok.getKind() == tok::numeric_constant &&
4400 GetLookAheadToken(1).is(tok::r_square)) {
4401 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004402 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004403 ConsumeToken();
4404
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004405 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004406 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004407 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004408
Chris Lattner378c7e42008-12-18 07:27:21 +00004409 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004410 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004411 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004412 T.getOpenLocation(),
4413 T.getCloseLocation()),
4414 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004415 return;
4416 }
Mike Stump1eb44332009-09-09 15:08:12 +00004417
Reid Spencer5f016e22007-07-11 17:01:13 +00004418 // If valid, this location is the position where we read the 'static' keyword.
4419 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004420 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004421 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004422
Reid Spencer5f016e22007-07-11 17:01:13 +00004423 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004424 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004425 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004426 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004427
Reid Spencer5f016e22007-07-11 17:01:13 +00004428 // If we haven't already read 'static', check to see if there is one after the
4429 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004430 if (!StaticLoc.isValid() && 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 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4434 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004435 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004436
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004437 // Handle the case where we have '[*]' as the array size. However, a leading
4438 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4439 // the the token after the star is a ']'. Since stars in arrays are
4440 // infrequent, use of lookahead is not costly here.
4441 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004442 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004443
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004444 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004445 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004446 StaticLoc = SourceLocation(); // Drop the static.
4447 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004448 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004449 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004450 // Note, in C89, this production uses the constant-expr production instead
4451 // of assignment-expr. The only difference is that assignment-expr allows
4452 // things like '=' and '*='. Sema rejects these in C89 mode because they
4453 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004454
Douglas Gregore0762c92009-06-19 23:52:42 +00004455 // Parse the constant-expression or assignment-expression now (depending
4456 // on dialect).
4457 if (getLang().CPlusPlus)
4458 NumElements = ParseConstantExpression();
4459 else
4460 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004461 }
Mike Stump1eb44332009-09-09 15:08:12 +00004462
Reid Spencer5f016e22007-07-11 17:01:13 +00004463 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004464 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004465 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004466 // If the expression was invalid, skip it.
4467 SkipUntil(tok::r_square);
4468 return;
4469 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004470
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004471 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004472
John McCall0b7e6782011-03-24 11:26:52 +00004473 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004474 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004475
Chris Lattner378c7e42008-12-18 07:27:21 +00004476 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004477 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004478 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004479 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004480 T.getOpenLocation(),
4481 T.getCloseLocation()),
4482 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004483}
4484
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004485/// [GNU] typeof-specifier:
4486/// typeof ( expressions )
4487/// typeof ( type-name )
4488/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004489///
4490void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004491 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004492 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004493 SourceLocation StartLoc = ConsumeToken();
4494
John McCallcfb708c2010-01-13 20:03:27 +00004495 const bool hasParens = Tok.is(tok::l_paren);
4496
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004497 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004498 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004499 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004500 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4501 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004502 if (hasParens)
4503 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004504
4505 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004506 // FIXME: Not accurate, the range gets one token more than it should.
4507 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004508 else
4509 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004510
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004511 if (isCastExpr) {
4512 if (!CastTy) {
4513 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004514 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004515 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004516
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004517 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004518 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004519 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4520 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004521 DiagID, CastTy))
4522 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004523 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004524 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004525
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004526 // If we get here, the operand to the typeof was an expresion.
4527 if (Operand.isInvalid()) {
4528 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004529 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004530 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004531
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004532 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004533 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004534 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4535 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004536 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004537 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004538}
Chris Lattner1b492422010-02-28 18:33:55 +00004539
Eli Friedmanb001de72011-10-06 23:00:33 +00004540/// [C1X] atomic-specifier:
4541/// _Atomic ( type-name )
4542///
4543void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4544 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4545
4546 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004547 BalancedDelimiterTracker T(*this, tok::l_paren);
4548 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004549 SkipUntil(tok::r_paren);
4550 return;
4551 }
4552
4553 TypeResult Result = ParseTypeName();
4554 if (Result.isInvalid()) {
4555 SkipUntil(tok::r_paren);
4556 return;
4557 }
4558
4559 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004560 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004561
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004562 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004563 return;
4564
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004565 DS.setTypeofParensRange(T.getRange());
4566 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004567
4568 const char *PrevSpec = 0;
4569 unsigned DiagID;
4570 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4571 DiagID, Result.release()))
4572 Diag(StartLoc, DiagID) << PrevSpec;
4573}
4574
Chris Lattner1b492422010-02-28 18:33:55 +00004575
4576/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4577/// from TryAltiVecVectorToken.
4578bool Parser::TryAltiVecVectorTokenOutOfLine() {
4579 Token Next = NextToken();
4580 switch (Next.getKind()) {
4581 default: return false;
4582 case tok::kw_short:
4583 case tok::kw_long:
4584 case tok::kw_signed:
4585 case tok::kw_unsigned:
4586 case tok::kw_void:
4587 case tok::kw_char:
4588 case tok::kw_int:
4589 case tok::kw_float:
4590 case tok::kw_double:
4591 case tok::kw_bool:
4592 case tok::kw___pixel:
4593 Tok.setKind(tok::kw___vector);
4594 return true;
4595 case tok::identifier:
4596 if (Next.getIdentifierInfo() == Ident_pixel) {
4597 Tok.setKind(tok::kw___vector);
4598 return true;
4599 }
4600 return false;
4601 }
4602}
4603
4604bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4605 const char *&PrevSpec, unsigned &DiagID,
4606 bool &isInvalid) {
4607 if (Tok.getIdentifierInfo() == Ident_vector) {
4608 Token Next = NextToken();
4609 switch (Next.getKind()) {
4610 case tok::kw_short:
4611 case tok::kw_long:
4612 case tok::kw_signed:
4613 case tok::kw_unsigned:
4614 case tok::kw_void:
4615 case tok::kw_char:
4616 case tok::kw_int:
4617 case tok::kw_float:
4618 case tok::kw_double:
4619 case tok::kw_bool:
4620 case tok::kw___pixel:
4621 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4622 return true;
4623 case tok::identifier:
4624 if (Next.getIdentifierInfo() == Ident_pixel) {
4625 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4626 return true;
4627 }
4628 break;
4629 default:
4630 break;
4631 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004632 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004633 DS.isTypeAltiVecVector()) {
4634 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4635 return true;
4636 }
4637 return false;
4638}