blob: 3f4f4f8375584ef71ed3300a8763d892a2d23dd4 [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 }
248
249 SourceLocation RParen = Tok.getLocation();
250 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
251 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000252 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000253 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
254 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
255 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000256 }
257}
258
259
Eli Friedmana23b4852009-06-08 07:21:15 +0000260/// ParseMicrosoftDeclSpec - Parse an __declspec construct
261///
262/// [MS] decl-specifier:
263/// __declspec ( extended-decl-modifier-seq )
264///
265/// [MS] extended-decl-modifier-seq:
266/// extended-decl-modifier[opt]
267/// extended-decl-modifier extended-decl-modifier-seq
268
John McCall7f040a92010-12-24 02:08:15 +0000269void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000270 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000271
Steve Narofff59e17e2008-12-24 20:59:21 +0000272 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000273 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
274 "declspec")) {
275 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000276 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000277 }
Francois Pichet373197b2011-05-07 19:04:49 +0000278
Eli Friedman290eeb02009-06-08 23:27:34 +0000279 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000280 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
281 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000282
283 // FIXME: Remove this when we have proper __declspec(property()) support.
284 // Just skip everything inside property().
285 if (AttrName->getName() == "property") {
286 ConsumeParen();
287 SkipUntil(tok::r_paren);
288 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000289 if (Tok.is(tok::l_paren)) {
290 ConsumeParen();
291 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
292 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000293 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000295 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000296 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
297 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000298 }
299 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
300 SkipUntil(tok::r_paren, false);
301 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000302 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
303 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000304 }
305 }
306 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
307 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000308 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000309}
310
John McCall7f040a92010-12-24 02:08:15 +0000311void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000312 // Treat these like attributes
313 // FIXME: Allow Sema to distinguish between these and real attributes!
314 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000315 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000316 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000317 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000318 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000319 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
320 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000321 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
322 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000323 // FIXME: Support these properly!
324 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000325 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
326 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000327 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000328}
329
John McCall7f040a92010-12-24 02:08:15 +0000330void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000331 // Treat these like attributes
332 while (Tok.is(tok::kw___pascal)) {
333 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
334 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000335 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
336 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000337 }
John McCall7f040a92010-12-24 02:08:15 +0000338}
339
Peter Collingbournef315fa82011-02-14 01:42:53 +0000340void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
341 // Treat these like attributes
342 while (Tok.is(tok::kw___kernel)) {
343 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000344 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
345 AttrNameLoc, 0, AttrNameLoc, 0,
346 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000347 }
348}
349
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000350void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
351 SourceLocation Loc = Tok.getLocation();
352 switch(Tok.getKind()) {
353 // OpenCL qualifiers:
354 case tok::kw___private:
355 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000356 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000357 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000358 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000359 break;
360
361 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000362 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000363 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000364 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000365 break;
366
367 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000368 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000369 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000370 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000371 break;
372
373 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000374 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000375 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000376 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000377 break;
378
379 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000380 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000382 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000383 break;
384
385 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000386 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000387 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000388 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000389 break;
390
391 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000392 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000394 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000395 break;
396 default: break;
397 }
398}
399
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000400/// \brief Parse a version number.
401///
402/// version:
403/// simple-integer
404/// simple-integer ',' simple-integer
405/// simple-integer ',' simple-integer ',' simple-integer
406VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
407 Range = Tok.getLocation();
408
409 if (!Tok.is(tok::numeric_constant)) {
410 Diag(Tok, diag::err_expected_version);
411 SkipUntil(tok::comma, tok::r_paren, true, true, true);
412 return VersionTuple();
413 }
414
415 // Parse the major (and possibly minor and subminor) versions, which
416 // are stored in the numeric constant. We utilize a quirk of the
417 // lexer, which is that it handles something like 1.2.3 as a single
418 // numeric constant, rather than two separate tokens.
419 llvm::SmallString<512> Buffer;
420 Buffer.resize(Tok.getLength()+1);
421 const char *ThisTokBegin = &Buffer[0];
422
423 // Get the spelling of the token, which eliminates trigraphs, etc.
424 bool Invalid = false;
425 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
426 if (Invalid)
427 return VersionTuple();
428
429 // Parse the major version.
430 unsigned AfterMajor = 0;
431 unsigned Major = 0;
432 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
433 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
434 ++AfterMajor;
435 }
436
437 if (AfterMajor == 0) {
438 Diag(Tok, diag::err_expected_version);
439 SkipUntil(tok::comma, tok::r_paren, true, true, true);
440 return VersionTuple();
441 }
442
443 if (AfterMajor == ActualLength) {
444 ConsumeToken();
445
446 // We only had a single version component.
447 if (Major == 0) {
448 Diag(Tok, diag::err_zero_version);
449 return VersionTuple();
450 }
451
452 return VersionTuple(Major);
453 }
454
455 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
456 Diag(Tok, diag::err_expected_version);
457 SkipUntil(tok::comma, tok::r_paren, true, true, true);
458 return VersionTuple();
459 }
460
461 // Parse the minor version.
462 unsigned AfterMinor = AfterMajor + 1;
463 unsigned Minor = 0;
464 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
465 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
466 ++AfterMinor;
467 }
468
469 if (AfterMinor == ActualLength) {
470 ConsumeToken();
471
472 // We had major.minor.
473 if (Major == 0 && Minor == 0) {
474 Diag(Tok, diag::err_zero_version);
475 return VersionTuple();
476 }
477
478 return VersionTuple(Major, Minor);
479 }
480
481 // If what follows is not a '.', we have a problem.
482 if (ThisTokBegin[AfterMinor] != '.') {
483 Diag(Tok, diag::err_expected_version);
484 SkipUntil(tok::comma, tok::r_paren, true, true, true);
485 return VersionTuple();
486 }
487
488 // Parse the subminor version.
489 unsigned AfterSubminor = AfterMinor + 1;
490 unsigned Subminor = 0;
491 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
492 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
493 ++AfterSubminor;
494 }
495
496 if (AfterSubminor != ActualLength) {
497 Diag(Tok, diag::err_expected_version);
498 SkipUntil(tok::comma, tok::r_paren, true, true, true);
499 return VersionTuple();
500 }
501 ConsumeToken();
502 return VersionTuple(Major, Minor, Subminor);
503}
504
505/// \brief Parse the contents of the "availability" attribute.
506///
507/// availability-attribute:
508/// 'availability' '(' platform ',' version-arg-list ')'
509///
510/// platform:
511/// identifier
512///
513/// version-arg-list:
514/// version-arg
515/// version-arg ',' version-arg-list
516///
517/// version-arg:
518/// 'introduced' '=' version
519/// 'deprecated' '=' version
520/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000521/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000522void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
523 SourceLocation AvailabilityLoc,
524 ParsedAttributes &attrs,
525 SourceLocation *endLoc) {
526 SourceLocation PlatformLoc;
527 IdentifierInfo *Platform = 0;
528
529 enum { Introduced, Deprecated, Obsoleted, Unknown };
530 AvailabilityChange Changes[Unknown];
531
532 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000533 BalancedDelimiterTracker T(*this, tok::l_paren);
534 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000535 Diag(Tok, diag::err_expected_lparen);
536 return;
537 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000538
539 // Parse the platform name,
540 if (Tok.isNot(tok::identifier)) {
541 Diag(Tok, diag::err_availability_expected_platform);
542 SkipUntil(tok::r_paren);
543 return;
544 }
545 Platform = Tok.getIdentifierInfo();
546 PlatformLoc = ConsumeToken();
547
548 // Parse the ',' following the platform name.
549 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
550 return;
551
552 // If we haven't grabbed the pointers for the identifiers
553 // "introduced", "deprecated", and "obsoleted", do so now.
554 if (!Ident_introduced) {
555 Ident_introduced = PP.getIdentifierInfo("introduced");
556 Ident_deprecated = PP.getIdentifierInfo("deprecated");
557 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000558 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559 }
560
561 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000562 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563 do {
564 if (Tok.isNot(tok::identifier)) {
565 Diag(Tok, diag::err_availability_expected_change);
566 SkipUntil(tok::r_paren);
567 return;
568 }
569 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
570 SourceLocation KeywordLoc = ConsumeToken();
571
Douglas Gregorb53e4172011-03-26 03:35:55 +0000572 if (Keyword == Ident_unavailable) {
573 if (UnavailableLoc.isValid()) {
574 Diag(KeywordLoc, diag::err_availability_redundant)
575 << Keyword << SourceRange(UnavailableLoc);
576 }
577 UnavailableLoc = KeywordLoc;
578
579 if (Tok.isNot(tok::comma))
580 break;
581
582 ConsumeToken();
583 continue;
584 }
585
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 if (Tok.isNot(tok::equal)) {
587 Diag(Tok, diag::err_expected_equal_after)
588 << Keyword;
589 SkipUntil(tok::r_paren);
590 return;
591 }
592 ConsumeToken();
593
594 SourceRange VersionRange;
595 VersionTuple Version = ParseVersionTuple(VersionRange);
596
597 if (Version.empty()) {
598 SkipUntil(tok::r_paren);
599 return;
600 }
601
602 unsigned Index;
603 if (Keyword == Ident_introduced)
604 Index = Introduced;
605 else if (Keyword == Ident_deprecated)
606 Index = Deprecated;
607 else if (Keyword == Ident_obsoleted)
608 Index = Obsoleted;
609 else
610 Index = Unknown;
611
612 if (Index < Unknown) {
613 if (!Changes[Index].KeywordLoc.isInvalid()) {
614 Diag(KeywordLoc, diag::err_availability_redundant)
615 << Keyword
616 << SourceRange(Changes[Index].KeywordLoc,
617 Changes[Index].VersionRange.getEnd());
618 }
619
620 Changes[Index].KeywordLoc = KeywordLoc;
621 Changes[Index].Version = Version;
622 Changes[Index].VersionRange = VersionRange;
623 } else {
624 Diag(KeywordLoc, diag::err_availability_unknown_change)
625 << Keyword << VersionRange;
626 }
627
628 if (Tok.isNot(tok::comma))
629 break;
630
631 ConsumeToken();
632 } while (true);
633
634 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000635 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000636 return;
637
638 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000639 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000640
Douglas Gregorb53e4172011-03-26 03:35:55 +0000641 // The 'unavailable' availability cannot be combined with any other
642 // availability changes. Make sure that hasn't happened.
643 if (UnavailableLoc.isValid()) {
644 bool Complained = false;
645 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
646 if (Changes[Index].KeywordLoc.isValid()) {
647 if (!Complained) {
648 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
649 << SourceRange(Changes[Index].KeywordLoc,
650 Changes[Index].VersionRange.getEnd());
651 Complained = true;
652 }
653
654 // Clear out the availability.
655 Changes[Index] = AvailabilityChange();
656 }
657 }
658 }
659
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000660 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000661 attrs.addNew(&Availability,
662 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000663 0, SourceLocation(),
664 Platform, PlatformLoc,
665 Changes[Introduced],
666 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000667 Changes[Obsoleted],
668 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000669}
670
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000671
672// Late Parsed Attributes:
673// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
674
675void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
676
677void Parser::LateParsedClass::ParseLexedAttributes() {
678 Self->ParseLexedAttributes(*Class);
679}
680
681void Parser::LateParsedAttribute::ParseLexedAttributes() {
682 Self->ParseLexedAttribute(*this);
683}
684
685/// Wrapper class which calls ParseLexedAttribute, after setting up the
686/// scope appropriately.
687void Parser::ParseLexedAttributes(ParsingClass &Class) {
688 // Deal with templates
689 // FIXME: Test cases to make sure this does the right thing for templates.
690 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
691 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
692 HasTemplateScope);
693 if (HasTemplateScope)
694 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
695
696 // Set or update the scope flags to include Scope::ThisScope.
697 bool AlreadyHasClassScope = Class.TopLevelClass;
698 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
699 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
700 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
701
702 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
703 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
704 }
705}
706
707/// \brief Finish parsing an attribute for which parsing was delayed.
708/// This will be called at the end of parsing a class declaration
709/// for each LateParsedAttribute. We consume the saved tokens and
710/// create an attribute with the arguments filled in. We add this
711/// to the Attribute list for the decl.
712void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
713 // Save the current token position.
714 SourceLocation OrigLoc = Tok.getLocation();
715
716 // Append the current token at the end of the new token stream so that it
717 // doesn't get lost.
718 LA.Toks.push_back(Tok);
719 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
720 // Consume the previously pushed token.
721 ConsumeAnyToken();
722
723 ParsedAttributes Attrs(AttrFactory);
724 SourceLocation endLoc;
725
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000726 // If the Decl is templatized, add template parameters to scope.
727 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
728 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
729 if (HasTemplateScope)
730 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
731
732 // If the Decl is on a function, add function parameters to the scope.
733 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
734 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
735 if (HasFunctionScope)
736 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
737
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000738 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
739
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000740 if (HasFunctionScope) {
741 Actions.ActOnExitFunctionContext();
742 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
743 }
744 if (HasTemplateScope) {
745 TempScope.Exit();
746 }
747
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000748 // Late parsed attributes must be attached to Decls by hand. If the
749 // LA.D is not set, then this was not done properly.
750 assert(LA.D && "No decl attached to late parsed attribute");
751 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
752
753 if (Tok.getLocation() != OrigLoc) {
754 // Due to a parsing error, we either went over the cached tokens or
755 // there are still cached tokens left, so we skip the leftover tokens.
756 // Since this is an uncommon situation that should be avoided, use the
757 // expensive isBeforeInTranslationUnit call.
758 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
759 OrigLoc))
760 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
761 ConsumeAnyToken();
762 }
763}
764
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000765/// \brief Wrapper around a case statement checking if AttrName is
766/// one of the thread safety attributes
767bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
768 return llvm::StringSwitch<bool>(AttrName)
769 .Case("guarded_by", true)
770 .Case("guarded_var", true)
771 .Case("pt_guarded_by", true)
772 .Case("pt_guarded_var", true)
773 .Case("lockable", true)
774 .Case("scoped_lockable", true)
775 .Case("no_thread_safety_analysis", true)
776 .Case("acquired_after", true)
777 .Case("acquired_before", true)
778 .Case("exclusive_lock_function", true)
779 .Case("shared_lock_function", true)
780 .Case("exclusive_trylock_function", true)
781 .Case("shared_trylock_function", true)
782 .Case("unlock_function", true)
783 .Case("lock_returned", true)
784 .Case("locks_excluded", true)
785 .Case("exclusive_locks_required", true)
786 .Case("shared_locks_required", true)
787 .Default(false);
788}
789
790/// \brief Parse the contents of thread safety attributes. These
791/// should always be parsed as an expression list.
792///
793/// We need to special case the parsing due to the fact that if the first token
794/// of the first argument is an identifier, the main parse loop will store
795/// that token as a "parameter" and the rest of
796/// the arguments will be added to a list of "arguments". However,
797/// subsequent tokens in the first argument are lost. We instead parse each
798/// argument as an expression and add all arguments to the list of "arguments".
799/// In future, we will take advantage of this special case to also
800/// deal with some argument scoping issues here (for example, referring to a
801/// function parameter in the attribute on that function).
802void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
803 SourceLocation AttrNameLoc,
804 ParsedAttributes &Attrs,
805 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000806 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000807
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000808 BalancedDelimiterTracker T(*this, tok::l_paren);
809 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000810
811 ExprVector ArgExprs(Actions);
812 bool ArgExprsOk = true;
813
814 // now parse the list of expressions
815 while (1) {
816 ExprResult ArgExpr(ParseAssignmentExpression());
817 if (ArgExpr.isInvalid()) {
818 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000819 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000820 break;
821 } else {
822 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000823 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000824 if (Tok.isNot(tok::comma))
825 break;
826 ConsumeToken(); // Eat the comma, move to the next argument
827 }
828 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000829 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000830 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
831 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000832 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000833 if (EndLoc)
834 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000835}
836
John McCall7f040a92010-12-24 02:08:15 +0000837void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
838 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
839 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000840}
841
Reid Spencer5f016e22007-07-11 17:01:13 +0000842/// ParseDeclaration - Parse a full 'declaration', which consists of
843/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000844/// 'Context' should be a Declarator::TheContext value. This returns the
845/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000846///
847/// declaration: [C99 6.7]
848/// block-declaration ->
849/// simple-declaration
850/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000851/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000852/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000853/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000854/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000855/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000856/// others... [FIXME]
857///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000858Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
859 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000860 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000861 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000862 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000863 // Must temporarily exit the objective-c container scope for
864 // parsing c none objective-c decls.
865 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000866
John McCalld226f652010-08-21 09:40:31 +0000867 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000868 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000869 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000870 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000871 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000872 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000873 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000874 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000875 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000876 // Could be the start of an inline namespace. Allowed as an ext in C++03.
877 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000878 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000879 SourceLocation InlineLoc = ConsumeToken();
880 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
881 break;
882 }
John McCall7f040a92010-12-24 02:08:15 +0000883 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000884 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000885 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000886 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000887 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000888 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000889 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000890 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000891 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000892 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000893 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000894 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000895 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000896 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000897 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000898 default:
John McCall7f040a92010-12-24 02:08:15 +0000899 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000900 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000901
Chris Lattner682bf922009-03-29 16:50:03 +0000902 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000903 // single decl, convert it now. Alias declarations can also declare a type;
904 // include that too if it is present.
905 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000906}
907
908/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
909/// declaration-specifiers init-declarator-list[opt] ';'
910///[C90/C++]init-declarator-list ';' [TODO]
911/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000912///
Richard Smithad762fc2011-04-14 22:09:26 +0000913/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
914/// attribute-specifier-seq[opt] type-specifier-seq declarator
915///
Chris Lattnercd147752009-03-29 17:27:48 +0000916/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000917/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000918///
919/// If FRI is non-null, we might be parsing a for-range-declaration instead
920/// of a simple-declaration. If we find that we are, we also parse the
921/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000922Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
923 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000924 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000925 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000926 bool RequireSemi,
927 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000929 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000930 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000931
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000932 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000933 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000934 StmtResult R = Actions.ActOnVlaStmt(DS);
935 if (R.isUsable())
936 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000937
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
939 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000940 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000941 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000942 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000943 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000944 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000945 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000947
948 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000949}
Mike Stump1eb44332009-09-09 15:08:12 +0000950
John McCalld8ac0572009-11-03 19:26:08 +0000951/// ParseDeclGroup - Having concluded that this is either a function
952/// definition or a group of object declarations, actually parse the
953/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000954Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
955 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000956 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000957 SourceLocation *DeclEnd,
958 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000959 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000960 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000961 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000962
John McCalld8ac0572009-11-03 19:26:08 +0000963 // Bail out if the first declarator didn't seem well-formed.
964 if (!D.hasName() && !D.mayOmitIdentifier()) {
965 // Skip until ; or }.
966 SkipUntil(tok::r_brace, true, true);
967 if (Tok.is(tok::semi))
968 ConsumeToken();
969 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattnerc82daef2010-07-11 22:24:20 +0000972 // Check to see if we have a function *definition* which must have a body.
973 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
974 // Look at the next token to make sure that this isn't a function
975 // declaration. We have to check this because __attribute__ might be the
976 // start of a function definition in GCC-extended K&R C.
977 !isDeclarationAfterDeclarator()) {
978
Chris Lattner004659a2010-07-11 22:42:07 +0000979 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000980 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
981 Diag(Tok, diag::err_function_declared_typedef);
982
983 // Recover by treating the 'typedef' as spurious.
984 DS.ClearStorageClassSpecs();
985 }
986
John McCalld226f652010-08-21 09:40:31 +0000987 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000988 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000989 }
990
991 if (isDeclarationSpecifier()) {
992 // If there is an invalid declaration specifier right after the function
993 // prototype, then we must be in a missing semicolon case where this isn't
994 // actually a body. Just fall through into the code that handles it as a
995 // prototype, and let the top-level code handle the erroneous declspec
996 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000997 } else {
998 Diag(Tok, diag::err_expected_fn_body);
999 SkipUntil(tok::semi);
1000 return DeclGroupPtrTy();
1001 }
1002 }
1003
Richard Smithad762fc2011-04-14 22:09:26 +00001004 if (ParseAttributesAfterDeclarator(D))
1005 return DeclGroupPtrTy();
1006
1007 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1008 // must parse and analyze the for-range-initializer before the declaration is
1009 // analyzed.
1010 if (FRI && Tok.is(tok::colon)) {
1011 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001012 if (Tok.is(tok::l_brace))
1013 FRI->RangeExpr = ParseBraceInitializer();
1014 else
1015 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001016 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1017 Actions.ActOnCXXForRangeDecl(ThisDecl);
1018 Actions.FinalizeDeclaration(ThisDecl);
1019 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1020 }
1021
Chris Lattner5f9e2722011-07-23 10:55:15 +00001022 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001023 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001024 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001025 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001026 DeclsInGroup.push_back(FirstDecl);
1027
1028 // If we don't have a comma, it is either the end of the list (a ';') or an
1029 // error, bail out.
1030 while (Tok.is(tok::comma)) {
1031 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +00001032 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001033
1034 // Parse the next declarator.
1035 D.clear();
1036
1037 // Accept attributes in an init-declarator. In the first declarator in a
1038 // declaration, these would be part of the declspec. In subsequent
1039 // declarators, they become part of the declarator itself, so that they
1040 // don't apply to declarators after *this* one. Examples:
1041 // short __attribute__((common)) var; -> declspec
1042 // short var __attribute__((common)); -> declarator
1043 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001044 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001045
1046 ParseDeclarator(D);
1047
John McCalld226f652010-08-21 09:40:31 +00001048 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001049 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001050 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001051 DeclsInGroup.push_back(ThisDecl);
1052 }
1053
1054 if (DeclEnd)
1055 *DeclEnd = Tok.getLocation();
1056
1057 if (Context != Declarator::ForContext &&
1058 ExpectAndConsume(tok::semi,
1059 Context == Declarator::FileContext
1060 ? diag::err_invalid_token_after_toplevel_declarator
1061 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001062 // Okay, there was no semicolon and one was expected. If we see a
1063 // declaration specifier, just assume it was missing and continue parsing.
1064 // Otherwise things are very confused and we skip to recover.
1065 if (!isDeclarationSpecifier()) {
1066 SkipUntil(tok::r_brace, true, true);
1067 if (Tok.is(tok::semi))
1068 ConsumeToken();
1069 }
John McCalld8ac0572009-11-03 19:26:08 +00001070 }
1071
Douglas Gregor23c94db2010-07-02 17:43:08 +00001072 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001073 DeclsInGroup.data(),
1074 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001075}
1076
Richard Smithad762fc2011-04-14 22:09:26 +00001077/// Parse an optional simple-asm-expr and attributes, and attach them to a
1078/// declarator. Returns true on an error.
1079bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1080 // If a simple-asm-expr is present, parse it.
1081 if (Tok.is(tok::kw_asm)) {
1082 SourceLocation Loc;
1083 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1084 if (AsmLabel.isInvalid()) {
1085 SkipUntil(tok::semi, true, true);
1086 return true;
1087 }
1088
1089 D.setAsmLabel(AsmLabel.release());
1090 D.SetRangeEnd(Loc);
1091 }
1092
1093 MaybeParseGNUAttributes(D);
1094 return false;
1095}
1096
Douglas Gregor1426e532009-05-12 21:31:51 +00001097/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1098/// declarator'. This method parses the remainder of the declaration
1099/// (including any attributes or initializer, among other things) and
1100/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001101///
Reid Spencer5f016e22007-07-11 17:01:13 +00001102/// init-declarator: [C99 6.7]
1103/// declarator
1104/// declarator '=' initializer
1105/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1106/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001107/// [C++] declarator initializer[opt]
1108///
1109/// [C++] initializer:
1110/// [C++] '=' initializer-clause
1111/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001112/// [C++0x] '=' 'default' [TODO]
1113/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001114/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001115///
1116/// According to the standard grammar, =default and =delete are function
1117/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001118///
John McCalld226f652010-08-21 09:40:31 +00001119Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001120 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001121 if (ParseAttributesAfterDeclarator(D))
1122 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Richard Smithad762fc2011-04-14 22:09:26 +00001124 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1125}
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Richard Smithad762fc2011-04-14 22:09:26 +00001127Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1128 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001129 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001130 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001131 switch (TemplateInfo.Kind) {
1132 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001133 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001134 break;
1135
1136 case ParsedTemplateInfo::Template:
1137 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001138 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001139 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001140 TemplateInfo.TemplateParams->data(),
1141 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001142 D);
1143 break;
1144
1145 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001146 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001147 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001148 TemplateInfo.ExternLoc,
1149 TemplateInfo.TemplateLoc,
1150 D);
1151 if (ThisRes.isInvalid()) {
1152 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001153 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001154 }
1155
1156 ThisDecl = ThisRes.get();
1157 break;
1158 }
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Richard Smith34b41d92011-02-20 03:19:35 +00001161 bool TypeContainsAuto =
1162 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1163
Douglas Gregor1426e532009-05-12 21:31:51 +00001164 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001165 if (isTokenEqualOrMistypedEqualEqual(
1166 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001167 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001168 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001169 if (D.isFunctionDeclarator())
1170 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1171 << 1 /* delete */;
1172 else
1173 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001174 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001175 if (D.isFunctionDeclarator())
1176 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1177 << 1 /* delete */;
1178 else
1179 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001180 } else {
John McCall731ad842009-12-19 09:28:58 +00001181 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1182 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001183 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001184 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001185
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001186 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001187 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001188 cutOffParsing();
1189 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001190 }
1191
John McCall60d7b3a2010-08-24 06:29:42 +00001192 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001193
John McCall731ad842009-12-19 09:28:58 +00001194 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001195 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001196 ExitScope();
1197 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001198
Douglas Gregor1426e532009-05-12 21:31:51 +00001199 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001200 SkipUntil(tok::comma, true, true);
1201 Actions.ActOnInitializerError(ThisDecl);
1202 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001203 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1204 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001205 }
1206 } else if (Tok.is(tok::l_paren)) {
1207 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001208 BalancedDelimiterTracker T(*this, tok::l_paren);
1209 T.consumeOpen();
1210
Douglas Gregor1426e532009-05-12 21:31:51 +00001211 ExprVector Exprs(Actions);
1212 CommaLocsTy CommaLocs;
1213
Douglas Gregorb4debae2009-12-22 17:47:17 +00001214 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1215 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001216 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001217 }
1218
Douglas Gregor1426e532009-05-12 21:31:51 +00001219 if (ParseExpressionList(Exprs, CommaLocs)) {
1220 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001221
1222 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001223 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001224 ExitScope();
1225 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001226 } else {
1227 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001228 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001229
1230 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1231 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001232
1233 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001234 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001235 ExitScope();
1236 }
1237
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001238 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001239 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001240 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001241 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001242 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001243 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1244 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001245 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1246
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001247 if (D.getCXXScopeSpec().isSet()) {
1248 EnterScope(0);
1249 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1250 }
1251
1252 ExprResult Init(ParseBraceInitializer());
1253
1254 if (D.getCXXScopeSpec().isSet()) {
1255 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1256 ExitScope();
1257 }
1258
1259 if (Init.isInvalid()) {
1260 Actions.ActOnInitializerError(ThisDecl);
1261 } else
1262 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1263 /*DirectInit=*/true, TypeContainsAuto);
1264
Douglas Gregor1426e532009-05-12 21:31:51 +00001265 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001266 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001267 }
1268
Richard Smith483b9f32011-02-21 20:05:19 +00001269 Actions.FinalizeDeclaration(ThisDecl);
1270
Douglas Gregor1426e532009-05-12 21:31:51 +00001271 return ThisDecl;
1272}
1273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274/// ParseSpecifierQualifierList
1275/// specifier-qualifier-list:
1276/// type-specifier specifier-qualifier-list[opt]
1277/// type-qualifier specifier-qualifier-list[opt]
1278/// [GNU] attributes specifier-qualifier-list[opt]
1279///
Richard Smithc89edf52011-07-01 19:46:12 +00001280void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1282 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001283 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001284 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 // Validate declspec for type-name.
1287 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001288 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001289 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 // Issue diagnostic and remove storage class if present.
1293 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1294 if (DS.getStorageClassSpecLoc().isValid())
1295 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1296 else
1297 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1298 DS.ClearStorageClassSpecs();
1299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 // Issue diagnostic and remove function specfier if present.
1302 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001303 if (DS.isInlineSpecified())
1304 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1305 if (DS.isVirtualSpecified())
1306 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1307 if (DS.isExplicitSpecified())
1308 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 DS.ClearFunctionSpecs();
1310 }
1311}
1312
Chris Lattnerc199ab32009-04-12 20:42:31 +00001313/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1314/// specified token is valid after the identifier in a declarator which
1315/// immediately follows the declspec. For example, these things are valid:
1316///
1317/// int x [ 4]; // direct-declarator
1318/// int x ( int y); // direct-declarator
1319/// int(int x ) // direct-declarator
1320/// int x ; // simple-declaration
1321/// int x = 17; // init-declarator-list
1322/// int x , y; // init-declarator-list
1323/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001324/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001325/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001326///
1327/// This is not, because 'x' does not immediately follow the declspec (though
1328/// ')' happens to be valid anyway).
1329/// int (x)
1330///
1331static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1332 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1333 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001334 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001335}
1336
Chris Lattnere40c2952009-04-14 21:34:55 +00001337
1338/// ParseImplicitInt - This method is called when we have an non-typename
1339/// identifier in a declspec (which normally terminates the decl spec) when
1340/// the declspec has no type specifier. In this case, the declspec is either
1341/// malformed or is "implicit int" (in K&R and C89).
1342///
1343/// This method handles diagnosing this prettily and returns false if the
1344/// declspec is done being processed. If it recovers and thinks there may be
1345/// other pieces of declspec after it, it returns true.
1346///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001347bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001348 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001349 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001350 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Chris Lattnere40c2952009-04-14 21:34:55 +00001352 SourceLocation Loc = Tok.getLocation();
1353 // If we see an identifier that is not a type name, we normally would
1354 // parse it as the identifer being declared. However, when a typename
1355 // is typo'd or the definition is not included, this will incorrectly
1356 // parse the typename as the identifier name and fall over misparsing
1357 // later parts of the diagnostic.
1358 //
1359 // As such, we try to do some look-ahead in cases where this would
1360 // otherwise be an "implicit-int" case to see if this is invalid. For
1361 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1362 // an identifier with implicit int, we'd get a parse error because the
1363 // next token is obviously invalid for a type. Parse these as a case
1364 // with an invalid type specifier.
1365 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Chris Lattnere40c2952009-04-14 21:34:55 +00001367 // Since we know that this either implicit int (which is rare) or an
1368 // error, we'd do lookahead to try to do better recovery.
1369 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1370 // If this token is valid for implicit int, e.g. "static x = 4", then
1371 // we just avoid eating the identifier, so it will be parsed as the
1372 // identifier in the declarator.
1373 return false;
1374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattnere40c2952009-04-14 21:34:55 +00001376 // Otherwise, if we don't consume this token, we are going to emit an
1377 // error anyway. Try to recover from various common problems. Check
1378 // to see if this was a reference to a tag name without a tag specified.
1379 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001380 //
1381 // C++ doesn't need this, and isTagName doesn't take SS.
1382 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001383 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001384 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregor23c94db2010-07-02 17:43:08 +00001386 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001387 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001388 case DeclSpec::TST_enum:
1389 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1390 case DeclSpec::TST_union:
1391 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1392 case DeclSpec::TST_struct:
1393 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1394 case DeclSpec::TST_class:
1395 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Chris Lattnerf4382f52009-04-14 22:17:06 +00001398 if (TagName) {
1399 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001400 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001401 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Chris Lattnerf4382f52009-04-14 22:17:06 +00001403 // Parse this as a tag as if the missing tag were present.
1404 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001405 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001406 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001407 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001408 return true;
1409 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Douglas Gregora786fdb2009-10-13 23:27:22 +00001412 // This is almost certainly an invalid type name. Let the action emit a
1413 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001414 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001415 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001416 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001417 // The action emitted a diagnostic, so we don't have to.
1418 if (T) {
1419 // The action has suggested that the type T could be used. Set that as
1420 // the type in the declaration specifiers, consume the would-be type
1421 // name token, and we're done.
1422 const char *PrevSpec;
1423 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001424 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001425 DS.SetRangeEnd(Tok.getLocation());
1426 ConsumeToken();
1427
1428 // There may be other declaration specifiers after this.
1429 return true;
1430 }
1431
1432 // Fall through; the action had no suggestion for us.
1433 } else {
1434 // The action did not emit a diagnostic, so emit one now.
1435 SourceRange R;
1436 if (SS) R = SS->getRange();
1437 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregora786fdb2009-10-13 23:27:22 +00001440 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001441 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001442 unsigned DiagID;
1443 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001444 DS.SetRangeEnd(Tok.getLocation());
1445 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Chris Lattnere40c2952009-04-14 21:34:55 +00001447 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1448 // avoid rippling error messages on subsequent uses of the same type,
1449 // could be useful if #include was forgotten.
1450 return false;
1451}
1452
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001453/// \brief Determine the declaration specifier context from the declarator
1454/// context.
1455///
1456/// \param Context the declarator context, which is one of the
1457/// Declarator::TheContext enumerator values.
1458Parser::DeclSpecContext
1459Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1460 if (Context == Declarator::MemberContext)
1461 return DSC_class;
1462 if (Context == Declarator::FileContext)
1463 return DSC_top_level;
1464 return DSC_normal;
1465}
1466
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001467/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1468///
1469/// FIXME: Simply returns an alignof() expression if the argument is a
1470/// type. Ideally, the type should be propagated directly into Sema.
1471///
1472/// [C1X/C++0x] type-id
1473/// [C1X] constant-expression
1474/// [C++0x] assignment-expression
1475ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1476 if (isTypeIdInParens()) {
1477 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1478 SourceLocation TypeLoc = Tok.getLocation();
1479 ParsedType Ty = ParseTypeName().get();
1480 SourceRange TypeRange(Start, Tok.getLocation());
1481 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1482 Ty.getAsOpaquePtr(), TypeRange);
1483 } else
1484 return ParseConstantExpression();
1485}
1486
1487/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1488/// attribute to Attrs.
1489///
1490/// alignment-specifier:
1491/// [C1X] '_Alignas' '(' type-id ')'
1492/// [C1X] '_Alignas' '(' constant-expression ')'
1493/// [C++0x] 'alignas' '(' type-id ')'
1494/// [C++0x] 'alignas' '(' assignment-expression ')'
1495void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1496 SourceLocation *endLoc) {
1497 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1498 "Not an alignment-specifier!");
1499
1500 SourceLocation KWLoc = Tok.getLocation();
1501 ConsumeToken();
1502
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001503 BalancedDelimiterTracker T(*this, tok::l_paren);
1504 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001505 return;
1506
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001507 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation());
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001508 if (ArgExpr.isInvalid()) {
1509 SkipUntil(tok::r_paren);
1510 return;
1511 }
1512
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001513 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001514 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001515 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001516
1517 ExprVector ArgExprs(Actions);
1518 ArgExprs.push_back(ArgExpr.release());
1519 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001520 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001521}
1522
Reid Spencer5f016e22007-07-11 17:01:13 +00001523/// ParseDeclarationSpecifiers
1524/// declaration-specifiers: [C99 6.7]
1525/// storage-class-specifier declaration-specifiers[opt]
1526/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001527/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001528/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001529/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001530/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001531///
1532/// storage-class-specifier: [C99 6.7.1]
1533/// 'typedef'
1534/// 'extern'
1535/// 'static'
1536/// 'auto'
1537/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001538/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001539/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001540/// function-specifier: [C99 6.7.4]
1541/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001542/// [C++] 'virtual'
1543/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001544/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001545/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001546/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001547
Reid Spencer5f016e22007-07-11 17:01:13 +00001548///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001549void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001550 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001551 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001552 DeclSpecContext DSContext) {
1553 if (DS.getSourceRange().isInvalid()) {
1554 DS.SetRangeStart(Tok.getLocation());
1555 DS.SetRangeEnd(Tok.getLocation());
1556 }
1557
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001559 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001561 unsigned DiagID = 0;
1562
Reid Spencer5f016e22007-07-11 17:01:13 +00001563 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001566 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001567 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001568 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1569 MaybeParseCXX0XAttributes(DS.getAttributes());
1570
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 // If this is not a declaration specifier token, we're done reading decl
1572 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001573 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001576 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001577 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001578 if (DS.hasTypeSpecifier()) {
1579 bool AllowNonIdentifiers
1580 = (getCurScope()->getFlags() & (Scope::ControlScope |
1581 Scope::BlockScope |
1582 Scope::TemplateParamScope |
1583 Scope::FunctionPrototypeScope |
1584 Scope::AtCatchScope)) == 0;
1585 bool AllowNestedNameSpecifiers
1586 = DSContext == DSC_top_level ||
1587 (DSContext == DSC_class && DS.isFriendSpecified());
1588
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001589 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1590 AllowNonIdentifiers,
1591 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001592 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001593 }
1594
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001595 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1596 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1597 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001598 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1599 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001600 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001601 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001602 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001603 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001604
1605 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001606 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001607 }
1608
Chris Lattner5e02c472009-01-05 00:07:25 +00001609 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001610 // C++ scope specifier. Annotate and loop, or bail out on error.
1611 if (TryAnnotateCXXScopeToken(true)) {
1612 if (!DS.hasTypeSpecifier())
1613 DS.SetTypeSpecError();
1614 goto DoneWithDeclSpec;
1615 }
John McCall2e0a7152010-03-01 18:20:46 +00001616 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1617 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001618 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001619
1620 case tok::annot_cxxscope: {
1621 if (DS.hasTypeSpecifier())
1622 goto DoneWithDeclSpec;
1623
John McCallaa87d332009-12-12 11:40:51 +00001624 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001625 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1626 Tok.getAnnotationRange(),
1627 SS);
John McCallaa87d332009-12-12 11:40:51 +00001628
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001629 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001630 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001631 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001632 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001633 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001634 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001635
1636 // C++ [class.qual]p2:
1637 // In a lookup in which the constructor is an acceptable lookup
1638 // result and the nested-name-specifier nominates a class C:
1639 //
1640 // - if the name specified after the
1641 // nested-name-specifier, when looked up in C, is the
1642 // injected-class-name of C (Clause 9), or
1643 //
1644 // - if the name specified after the nested-name-specifier
1645 // is the same as the identifier or the
1646 // simple-template-id's template-name in the last
1647 // component of the nested-name-specifier,
1648 //
1649 // the name is instead considered to name the constructor of
1650 // class C.
1651 //
1652 // Thus, if the template-name is actually the constructor
1653 // name, then the code is ill-formed; this interpretation is
1654 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001655 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001656 if ((DSContext == DSC_top_level ||
1657 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1658 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001659 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001660 if (isConstructorDeclarator()) {
1661 // The user meant this to be an out-of-line constructor
1662 // definition, but template arguments are not allowed
1663 // there. Just allow this as a constructor; we'll
1664 // complain about it later.
1665 goto DoneWithDeclSpec;
1666 }
1667
1668 // The user meant this to name a type, but it actually names
1669 // a constructor with some extraneous template
1670 // arguments. Complain, then parse it as a type as the user
1671 // intended.
1672 Diag(TemplateId->TemplateNameLoc,
1673 diag::err_out_of_line_template_id_names_constructor)
1674 << TemplateId->Name;
1675 }
1676
John McCallaa87d332009-12-12 11:40:51 +00001677 DS.getTypeSpecScope() = SS;
1678 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001679 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001680 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001681 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001682 continue;
1683 }
1684
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001685 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001686 DS.getTypeSpecScope() = SS;
1687 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001688 if (Tok.getAnnotationValue()) {
1689 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1691 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001692 PrevSpec, DiagID, T);
1693 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001694 else
1695 DS.SetTypeSpecError();
1696 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1697 ConsumeToken(); // The typename
1698 }
1699
Douglas Gregor9135c722009-03-25 15:40:00 +00001700 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001701 goto DoneWithDeclSpec;
1702
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001703 // If we're in a context where the identifier could be a class name,
1704 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001705 if ((DSContext == DSC_top_level ||
1706 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001707 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001708 &SS)) {
1709 if (isConstructorDeclarator())
1710 goto DoneWithDeclSpec;
1711
1712 // As noted in C++ [class.qual]p2 (cited above), when the name
1713 // of the class is qualified in a context where it could name
1714 // a constructor, its a constructor name. However, we've
1715 // looked at the declarator, and the user probably meant this
1716 // to be a type. Complain that it isn't supposed to be treated
1717 // as a type, then proceed to parse it as a type.
1718 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1719 << Next.getIdentifierInfo();
1720 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001721
John McCallb3d87482010-08-24 05:47:05 +00001722 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1723 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001724 getCurScope(), &SS,
1725 false, false, ParsedType(),
1726 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001727
Chris Lattnerf4382f52009-04-14 22:17:06 +00001728 // If the referenced identifier is not a type, then this declspec is
1729 // erroneous: We already checked about that it has no type specifier, and
1730 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001731 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001732 if (TypeRep == 0) {
1733 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001734 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001735 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
John McCallaa87d332009-12-12 11:40:51 +00001738 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001739 ConsumeToken(); // The C++ scope.
1740
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001742 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001743 if (isInvalid)
1744 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001746 DS.SetRangeEnd(Tok.getLocation());
1747 ConsumeToken(); // The typename.
1748
1749 continue;
1750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Chris Lattner80d0c892009-01-21 19:48:37 +00001752 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001753 if (Tok.getAnnotationValue()) {
1754 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001756 DiagID, T);
1757 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001758 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001759
1760 if (isInvalid)
1761 break;
1762
Chris Lattner80d0c892009-01-21 19:48:37 +00001763 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1764 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Chris Lattner80d0c892009-01-21 19:48:37 +00001766 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1767 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001768 // Objective-C interface.
1769 if (Tok.is(tok::less) && getLang().ObjC1)
1770 ParseObjCProtocolQualifiers(DS);
1771
Chris Lattner80d0c892009-01-21 19:48:37 +00001772 continue;
1773 }
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Douglas Gregorbfad9152011-04-28 15:48:45 +00001775 case tok::kw___is_signed:
1776 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1777 // typically treats it as a trait. If we see __is_signed as it appears
1778 // in libstdc++, e.g.,
1779 //
1780 // static const bool __is_signed;
1781 //
1782 // then treat __is_signed as an identifier rather than as a keyword.
1783 if (DS.getTypeSpecType() == TST_bool &&
1784 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1785 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1786 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1787 Tok.setKind(tok::identifier);
1788 }
1789
1790 // We're done with the declaration-specifiers.
1791 goto DoneWithDeclSpec;
1792
Chris Lattner3bd934a2008-07-26 01:18:38 +00001793 // typedef-name
1794 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001795 // In C++, check to see if this is a scope specifier like foo::bar::, if
1796 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001797 if (getLang().CPlusPlus) {
1798 if (TryAnnotateCXXScopeToken(true)) {
1799 if (!DS.hasTypeSpecifier())
1800 DS.SetTypeSpecError();
1801 goto DoneWithDeclSpec;
1802 }
1803 if (!Tok.is(tok::identifier))
1804 continue;
1805 }
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattner3bd934a2008-07-26 01:18:38 +00001807 // This identifier can only be a typedef name if we haven't already seen
1808 // a type-specifier. Without this check we misparse:
1809 // typedef int X; struct Y { short X; }; as 'short int'.
1810 if (DS.hasTypeSpecifier())
1811 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001812
John Thompson82287d12010-02-05 00:12:22 +00001813 // Check for need to substitute AltiVec keyword tokens.
1814 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1815 break;
1816
Chris Lattner3bd934a2008-07-26 01:18:38 +00001817 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001818 ParsedType TypeRep =
1819 Actions.getTypeName(*Tok.getIdentifierInfo(),
1820 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001821
Chris Lattnerc199ab32009-04-12 20:42:31 +00001822 // If this is not a typedef name, don't parse it as part of the declspec,
1823 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001824 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001825 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001826 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001827 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001828
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001829 // If we're in a context where the identifier could be a class name,
1830 // check whether this is a constructor declaration.
1831 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001832 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001833 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001834 goto DoneWithDeclSpec;
1835
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001836 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001837 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001838 if (isInvalid)
1839 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Chris Lattner3bd934a2008-07-26 01:18:38 +00001841 DS.SetRangeEnd(Tok.getLocation());
1842 ConsumeToken(); // The identifier
1843
1844 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1845 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001846 // Objective-C interface.
1847 if (Tok.is(tok::less) && getLang().ObjC1)
1848 ParseObjCProtocolQualifiers(DS);
1849
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001850 // Need to support trailing type qualifiers (e.g. "id<p> const").
1851 // If a type specifier follows, it will be diagnosed elsewhere.
1852 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001853 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001854
1855 // type-name
1856 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001857 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001858 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001859 // This template-id does not refer to a type name, so we're
1860 // done with the type-specifiers.
1861 goto DoneWithDeclSpec;
1862 }
1863
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001864 // If we're in a context where the template-id could be a
1865 // constructor name or specialization, check whether this is a
1866 // constructor declaration.
1867 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001868 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001869 isConstructorDeclarator())
1870 goto DoneWithDeclSpec;
1871
Douglas Gregor39a8de12009-02-25 19:37:18 +00001872 // Turn the template-id annotation token into a type annotation
1873 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001874 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001875 continue;
1876 }
1877
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 // GNU attributes support.
1879 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001880 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001882
1883 // Microsoft declspec support.
1884 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001885 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001886 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Steve Naroff239f0732008-12-25 14:16:32 +00001888 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001889 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001890 // FIXME: Add handling here!
1891 break;
1892
1893 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001894 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001895 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001896 case tok::kw___cdecl:
1897 case tok::kw___stdcall:
1898 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001899 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001900 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001901 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001902 continue;
1903
Dawn Perchik52fc3142010-09-03 01:29:35 +00001904 // Borland single token adornments.
1905 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001906 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001907 continue;
1908
Peter Collingbournef315fa82011-02-14 01:42:53 +00001909 // OpenCL single token adornments.
1910 case tok::kw___kernel:
1911 ParseOpenCLAttributes(DS.getAttributes());
1912 continue;
1913
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 // storage-class-specifier
1915 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001916 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
1917 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 break;
1919 case tok::kw_extern:
1920 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001921 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001922 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
1923 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001925 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001926 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
1927 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001928 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 case tok::kw_static:
1930 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001931 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001932 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
1933 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 break;
1935 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001936 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001937 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001938 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1939 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001940 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001941 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001942 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001943 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1945 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001946 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001947 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1948 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 break;
1950 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001951 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
1952 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001953 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001954 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001955 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
1956 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001957 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001958 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001959 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 // function-specifier
1963 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001964 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001966 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001967 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001968 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001969 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001970 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001971 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001972
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001973 // alignment-specifier
1974 case tok::kw__Alignas:
1975 if (!getLang().C1X)
1976 Diag(Tok, diag::ext_c1x_alignas);
1977 ParseAlignmentSpecifier(DS.getAttributes());
1978 continue;
1979
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001980 // friend
1981 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001982 if (DSContext == DSC_class)
1983 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1984 else {
1985 PrevSpec = ""; // not actually used by the diagnostic
1986 DiagID = diag::err_friend_invalid_in_context;
1987 isInvalid = true;
1988 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001989 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Douglas Gregor8d267c52011-09-09 02:06:17 +00001991 // Modules
1992 case tok::kw___module_private__:
1993 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
1994 break;
1995
Sebastian Redl2ac67232009-11-05 15:47:02 +00001996 // constexpr
1997 case tok::kw_constexpr:
1998 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1999 break;
2000
Chris Lattner80d0c892009-01-21 19:48:37 +00002001 // type-specifier
2002 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002003 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2004 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002005 break;
2006 case tok::kw_long:
2007 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002008 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2009 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002010 else
John McCallfec54012009-08-03 20:12:06 +00002011 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2012 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002013 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002014 case tok::kw___int64:
2015 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2016 DiagID);
2017 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002018 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2020 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002021 break;
2022 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002023 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2024 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002025 break;
2026 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002027 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2028 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002029 break;
2030 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002031 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2032 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002033 break;
2034 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2036 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002037 break;
2038 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2040 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002041 break;
2042 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2044 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002045 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002046 case tok::kw_half:
2047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2048 DiagID);
2049 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002050 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2052 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002053 break;
2054 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2056 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002057 break;
2058 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2060 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002061 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002062 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2064 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002065 break;
2066 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002067 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2068 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002069 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002070 case tok::kw_bool:
2071 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002072 if (Tok.is(tok::kw_bool) &&
2073 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2074 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2075 PrevSpec = ""; // Not used by the diagnostic.
2076 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002077 // For better error recovery.
2078 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002079 isInvalid = true;
2080 } else {
2081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2082 DiagID);
2083 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002084 break;
2085 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2087 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002088 break;
2089 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002090 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2091 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002092 break;
2093 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002094 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2095 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002096 break;
John Thompson82287d12010-02-05 00:12:22 +00002097 case tok::kw___vector:
2098 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2099 break;
2100 case tok::kw___pixel:
2101 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2102 break;
John McCalla5fc4722011-04-09 22:50:59 +00002103 case tok::kw___unknown_anytype:
2104 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2105 PrevSpec, DiagID);
2106 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002107
2108 // class-specifier:
2109 case tok::kw_class:
2110 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002111 case tok::kw_union: {
2112 tok::TokenKind Kind = Tok.getKind();
2113 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002114 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002115 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002116 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002117
2118 // enum-specifier:
2119 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002120 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002121 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 continue;
2123
2124 // cv-qualifier:
2125 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002126 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2127 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 break;
2129 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002130 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2131 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 break;
2133 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002134 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2135 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002136 break;
2137
Douglas Gregord57959a2009-03-27 23:10:48 +00002138 // C++ typename-specifier:
2139 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002140 if (TryAnnotateTypeOrScopeToken()) {
2141 DS.SetTypeSpecError();
2142 goto DoneWithDeclSpec;
2143 }
2144 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002145 continue;
2146 break;
2147
Chris Lattner80d0c892009-01-21 19:48:37 +00002148 // GNU typeof support.
2149 case tok::kw_typeof:
2150 ParseTypeofSpecifier(DS);
2151 continue;
2152
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002153 case tok::kw_decltype:
2154 ParseDecltypeSpecifier(DS);
2155 continue;
2156
Sean Huntdb5d44b2011-05-19 05:37:45 +00002157 case tok::kw___underlying_type:
2158 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002159 continue;
2160
2161 case tok::kw__Atomic:
2162 ParseAtomicSpecifier(DS);
2163 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002164
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002165 // OpenCL qualifiers:
2166 case tok::kw_private:
2167 if (!getLang().OpenCL)
2168 goto DoneWithDeclSpec;
2169 case tok::kw___private:
2170 case tok::kw___global:
2171 case tok::kw___local:
2172 case tok::kw___constant:
2173 case tok::kw___read_only:
2174 case tok::kw___write_only:
2175 case tok::kw___read_write:
2176 ParseOpenCLQualifiers(DS);
2177 break;
2178
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002179 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002180 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002181 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2182 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002183 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002184 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor46f936e2010-11-19 17:10:50 +00002186 if (!ParseObjCProtocolQualifiers(DS))
2187 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2188 << FixItHint::CreateInsertion(Loc, "id")
2189 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002190
2191 // Need to support trailing type qualifiers (e.g. "id<p> const").
2192 // If a type specifier follows, it will be diagnosed elsewhere.
2193 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002194 }
John McCallfec54012009-08-03 20:12:06 +00002195 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 if (isInvalid) {
2197 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002198 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002199
2200 if (DiagID == diag::ext_duplicate_declspec)
2201 Diag(Tok, DiagID)
2202 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2203 else
2204 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002205 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002206
Chris Lattner81c018d2008-03-13 06:29:04 +00002207 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002208 if (DiagID != diag::err_bool_redeclaration)
2209 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002210 }
2211}
Douglas Gregoradcac882008-12-01 23:54:00 +00002212
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002213/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002214/// primarily follow the C++ grammar with additions for C99 and GNU,
2215/// which together subsume the C grammar. Note that the C++
2216/// type-specifier also includes the C type-qualifier (for const,
2217/// volatile, and C99 restrict). Returns true if a type-specifier was
2218/// found (and parsed), false otherwise.
2219///
2220/// type-specifier: [C++ 7.1.5]
2221/// simple-type-specifier
2222/// class-specifier
2223/// enum-specifier
2224/// elaborated-type-specifier [TODO]
2225/// cv-qualifier
2226///
2227/// cv-qualifier: [C++ 7.1.5.1]
2228/// 'const'
2229/// 'volatile'
2230/// [C99] 'restrict'
2231///
2232/// simple-type-specifier: [ C++ 7.1.5.2]
2233/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2234/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2235/// 'char'
2236/// 'wchar_t'
2237/// 'bool'
2238/// 'short'
2239/// 'int'
2240/// 'long'
2241/// 'signed'
2242/// 'unsigned'
2243/// 'float'
2244/// 'double'
2245/// 'void'
2246/// [C99] '_Bool'
2247/// [C99] '_Complex'
2248/// [C99] '_Imaginary' // Removed in TC2?
2249/// [GNU] '_Decimal32'
2250/// [GNU] '_Decimal64'
2251/// [GNU] '_Decimal128'
2252/// [GNU] typeof-specifier
2253/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2254/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002255/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002256/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002257bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002258 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002259 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002260 const ParsedTemplateInfo &TemplateInfo,
2261 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002262 SourceLocation Loc = Tok.getLocation();
2263
2264 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002265 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002266 // If we already have a type specifier, this identifier is not a type.
2267 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2268 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2269 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2270 return false;
John Thompson82287d12010-02-05 00:12:22 +00002271 // Check for need to substitute AltiVec keyword tokens.
2272 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2273 break;
2274 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002275 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002276 // Annotate typenames and C++ scope specifiers. If we get one, just
2277 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002278 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2279 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002280 return true;
2281 if (Tok.is(tok::identifier))
2282 return false;
2283 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2284 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002285 case tok::coloncolon: // ::foo::bar
2286 if (NextToken().is(tok::kw_new) || // ::new
2287 NextToken().is(tok::kw_delete)) // ::delete
2288 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Chris Lattner166a8fc2009-01-04 23:41:41 +00002290 // Annotate typenames and C++ scope specifiers. If we get one, just
2291 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002292 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2293 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002294 return true;
2295 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2296 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Douglas Gregor12e083c2008-11-07 15:42:26 +00002298 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002299 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002300 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2302 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002303 DiagID, T);
2304 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002305 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002306 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2307 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Douglas Gregor12e083c2008-11-07 15:42:26 +00002309 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2310 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2311 // Objective-C interface. If we don't have Objective-C or a '<', this is
2312 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002313 if (Tok.is(tok::less) && getLang().ObjC1)
2314 ParseObjCProtocolQualifiers(DS);
2315
Douglas Gregor12e083c2008-11-07 15:42:26 +00002316 return true;
2317 }
2318
2319 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002321 break;
2322 case tok::kw_long:
2323 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002324 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2325 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002326 else
John McCallfec54012009-08-03 20:12:06 +00002327 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2328 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002329 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002330 case tok::kw___int64:
2331 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2332 DiagID);
2333 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002334 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002335 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002336 break;
2337 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002338 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2339 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002340 break;
2341 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002342 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2343 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002344 break;
2345 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002346 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2347 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002348 break;
2349 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002351 break;
2352 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002354 break;
2355 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002357 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002358 case tok::kw_half:
2359 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2360 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002361 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002363 break;
2364 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002366 break;
2367 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002368 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002369 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002370 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002372 break;
2373 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002375 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002376 case tok::kw_bool:
2377 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002379 break;
2380 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2382 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002383 break;
2384 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2386 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002387 break;
2388 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2390 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002391 break;
John Thompson82287d12010-02-05 00:12:22 +00002392 case tok::kw___vector:
2393 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2394 break;
2395 case tok::kw___pixel:
2396 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2397 break;
2398
Douglas Gregor12e083c2008-11-07 15:42:26 +00002399 // class-specifier:
2400 case tok::kw_class:
2401 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002402 case tok::kw_union: {
2403 tok::TokenKind Kind = Tok.getKind();
2404 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002405 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2406 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002407 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002408 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002409
2410 // enum-specifier:
2411 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002412 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002413 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002414 return true;
2415
2416 // cv-qualifier:
2417 case tok::kw_const:
2418 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002419 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002420 break;
2421 case tok::kw_volatile:
2422 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002423 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002424 break;
2425 case tok::kw_restrict:
2426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002427 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002428 break;
2429
2430 // GNU typeof support.
2431 case tok::kw_typeof:
2432 ParseTypeofSpecifier(DS);
2433 return true;
2434
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002435 // C++0x decltype support.
2436 case tok::kw_decltype:
2437 ParseDecltypeSpecifier(DS);
2438 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Sean Huntdb5d44b2011-05-19 05:37:45 +00002440 // C++0x type traits support.
2441 case tok::kw___underlying_type:
2442 ParseUnderlyingTypeSpecifier(DS);
2443 return true;
2444
Eli Friedmanb001de72011-10-06 23:00:33 +00002445 case tok::kw__Atomic:
2446 ParseAtomicSpecifier(DS);
2447 return true;
2448
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002449 // OpenCL qualifiers:
2450 case tok::kw_private:
2451 if (!getLang().OpenCL)
2452 return false;
2453 case tok::kw___private:
2454 case tok::kw___global:
2455 case tok::kw___local:
2456 case tok::kw___constant:
2457 case tok::kw___read_only:
2458 case tok::kw___write_only:
2459 case tok::kw___read_write:
2460 ParseOpenCLQualifiers(DS);
2461 break;
2462
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002463 // C++0x auto support.
2464 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002465 // This is only called in situations where a storage-class specifier is
2466 // illegal, so we can assume an auto type specifier was intended even in
2467 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2468 // extension diagnostic.
2469 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002470 return false;
2471
John McCallfec54012009-08-03 20:12:06 +00002472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002473 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002474
Eli Friedman290eeb02009-06-08 23:27:34 +00002475 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002476 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002477 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002478 case tok::kw___cdecl:
2479 case tok::kw___stdcall:
2480 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002481 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002482 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002483 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002484 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002485
Dawn Perchik52fc3142010-09-03 01:29:35 +00002486 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002487 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002488 return true;
2489
Douglas Gregor12e083c2008-11-07 15:42:26 +00002490 default:
2491 // Not a type-specifier; do nothing.
2492 return false;
2493 }
2494
2495 // If the specifier combination wasn't legal, issue a diagnostic.
2496 if (isInvalid) {
2497 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002498 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002499 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002500 }
2501 DS.SetRangeEnd(Tok.getLocation());
2502 ConsumeToken(); // whatever we parsed above.
2503 return true;
2504}
Reid Spencer5f016e22007-07-11 17:01:13 +00002505
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002506/// ParseStructDeclaration - Parse a struct declaration without the terminating
2507/// semicolon.
2508///
Reid Spencer5f016e22007-07-11 17:01:13 +00002509/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002510/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002511/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002512/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002513/// struct-declarator-list:
2514/// struct-declarator
2515/// struct-declarator-list ',' struct-declarator
2516/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2517/// struct-declarator:
2518/// declarator
2519/// [GNU] declarator attributes[opt]
2520/// declarator[opt] ':' constant-expression
2521/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2522///
Chris Lattnere1359422008-04-10 06:46:29 +00002523void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002524ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002525
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002526 if (Tok.is(tok::kw___extension__)) {
2527 // __extension__ silences extension warnings in the subexpression.
2528 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002529 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002530 return ParseStructDeclaration(DS, Fields);
2531 }
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Steve Naroff28a7ca82007-08-20 22:28:22 +00002533 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002534 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002536 // If there are no declarators, this is a free-standing declaration
2537 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002538 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002539 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002540 return;
2541 }
2542
2543 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002544 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002545 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002546 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002547 FieldDeclarator DeclaratorInfo(DS);
2548
2549 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002550 if (!FirstDeclarator)
2551 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002552
Steve Naroff28a7ca82007-08-20 22:28:22 +00002553 /// struct-declarator: declarator
2554 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002555 if (Tok.isNot(tok::colon)) {
2556 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2557 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002558 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002559 }
Mike Stump1eb44332009-09-09 15:08:12 +00002560
Chris Lattner04d66662007-10-09 17:33:22 +00002561 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002562 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002563 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002564 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002565 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002566 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002567 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002568 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002569
Steve Naroff28a7ca82007-08-20 22:28:22 +00002570 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002571 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002572
John McCallbdd563e2009-11-03 02:38:08 +00002573 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002574 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002575 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002576
Steve Naroff28a7ca82007-08-20 22:28:22 +00002577 // If we don't have a comma, it is either the end of the list (a ';')
2578 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002579 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002580 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002581
Steve Naroff28a7ca82007-08-20 22:28:22 +00002582 // Consume the comma.
2583 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002584
John McCallbdd563e2009-11-03 02:38:08 +00002585 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002586 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002587}
2588
2589/// ParseStructUnionBody
2590/// struct-contents:
2591/// struct-declaration-list
2592/// [EXT] empty
2593/// [GNU] "struct-declaration-list" without terminatoring ';'
2594/// struct-declaration-list:
2595/// struct-declaration
2596/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002597/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002598///
Reid Spencer5f016e22007-07-11 17:01:13 +00002599void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002600 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002601 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2602 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002604 BalancedDelimiterTracker T(*this, tok::l_brace);
2605 if (T.consumeOpen())
2606 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002607
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002608 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002609 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002610
Reid Spencer5f016e22007-07-11 17:01:13 +00002611 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2612 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002613 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002614 Diag(Tok, diag::ext_empty_struct_union)
2615 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002616
Chris Lattner5f9e2722011-07-23 10:55:15 +00002617 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002618
Reid Spencer5f016e22007-07-11 17:01:13 +00002619 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002620 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002621 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002624 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002625 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002626 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002627 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002628 ConsumeToken();
2629 continue;
2630 }
Chris Lattnere1359422008-04-10 06:46:29 +00002631
2632 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002633 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002634
John McCallbdd563e2009-11-03 02:38:08 +00002635 if (!Tok.is(tok::at)) {
2636 struct CFieldCallback : FieldCallback {
2637 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002638 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002639 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002640
John McCalld226f652010-08-21 09:40:31 +00002641 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002642 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002643 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2644
John McCalld226f652010-08-21 09:40:31 +00002645 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002646 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002647 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002648 FD.D.getDeclSpec().getSourceRange().getBegin(),
2649 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002650 FieldDecls.push_back(Field);
2651 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002652 }
John McCallbdd563e2009-11-03 02:38:08 +00002653 } Callback(*this, TagDecl, FieldDecls);
2654
2655 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002656 } else { // Handle @defs
2657 ConsumeToken();
2658 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2659 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002660 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002661 continue;
2662 }
2663 ConsumeToken();
2664 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2665 if (!Tok.is(tok::identifier)) {
2666 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002667 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002668 continue;
2669 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002670 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002671 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002672 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002673 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2674 ConsumeToken();
2675 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002676 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002677
Chris Lattner04d66662007-10-09 17:33:22 +00002678 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002679 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002680 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002681 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002682 break;
2683 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002684 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2685 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002686 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002687 // If we stopped at a ';', eat it.
2688 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 }
2690 }
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002692 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002693
John McCall0b7e6782011-03-24 11:26:52 +00002694 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002696 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002697
Douglas Gregor23c94db2010-07-02 17:43:08 +00002698 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002699 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002700 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002701 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002702 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002703 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2704 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002705}
2706
Reid Spencer5f016e22007-07-11 17:01:13 +00002707/// ParseEnumSpecifier
2708/// enum-specifier: [C99 6.7.2.2]
2709/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002710///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002711/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2712/// '}' attributes[opt]
2713/// 'enum' identifier
2714/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002715///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002716/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2717/// [C++0x] enum-head '{' enumerator-list ',' '}'
2718///
2719/// enum-head: [C++0x]
2720/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2721/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2722///
2723/// enum-key: [C++0x]
2724/// 'enum'
2725/// 'enum' 'class'
2726/// 'enum' 'struct'
2727///
2728/// enum-base: [C++0x]
2729/// ':' type-specifier-seq
2730///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002731/// [C++] elaborated-type-specifier:
2732/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2733///
Chris Lattner4c97d762009-04-12 21:49:30 +00002734void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002735 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002736 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002737 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002738 if (Tok.is(tok::code_completion)) {
2739 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002740 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002741 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002742 }
John McCall57c13002011-07-06 05:58:41 +00002743
2744 bool IsScopedEnum = false;
2745 bool IsScopedUsingClassTag = false;
2746
2747 if (getLang().CPlusPlus0x &&
2748 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002749 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002750 IsScopedEnum = true;
2751 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2752 ConsumeToken();
2753 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002754
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002755 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002756 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002757 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002758
Douglas Gregor5471bc82011-09-08 17:18:35 +00002759 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002760 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002761
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002762 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002763 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002764 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2765 // if a fixed underlying type is allowed.
2766 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2767
John McCallb3d87482010-08-24 05:47:05 +00002768 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002769 return;
2770
2771 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002772 Diag(Tok, diag::err_expected_ident);
2773 if (Tok.isNot(tok::l_brace)) {
2774 // Has no name and is not a definition.
2775 // Skip the rest of this declarator, up until the comma or semicolon.
2776 SkipUntil(tok::comma, true);
2777 return;
2778 }
2779 }
2780 }
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002782 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002783 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2784 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002785 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002786
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002787 // Skip the rest of this declarator, up until the comma or semicolon.
2788 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002790 }
Mike Stump1eb44332009-09-09 15:08:12 +00002791
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002792 // If an identifier is present, consume and remember it.
2793 IdentifierInfo *Name = 0;
2794 SourceLocation NameLoc;
2795 if (Tok.is(tok::identifier)) {
2796 Name = Tok.getIdentifierInfo();
2797 NameLoc = ConsumeToken();
2798 }
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002800 if (!Name && IsScopedEnum) {
2801 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2802 // declaration of a scoped enumeration.
2803 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2804 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002805 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002806 }
2807
2808 TypeResult BaseType;
2809
Douglas Gregora61b3e72010-12-01 17:42:47 +00002810 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002811 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002812 bool PossibleBitfield = false;
2813 if (getCurScope()->getFlags() & Scope::ClassScope) {
2814 // If we're in class scope, this can either be an enum declaration with
2815 // an underlying type, or a declaration of a bitfield member. We try to
2816 // use a simple disambiguation scheme first to catch the common cases
2817 // (integer literal, sizeof); if it's still ambiguous, we then consider
2818 // anything that's a simple-type-specifier followed by '(' as an
2819 // expression. This suffices because function types are not valid
2820 // underlying types anyway.
2821 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2822 // If the next token starts an expression, we know we're parsing a
2823 // bit-field. This is the common case.
2824 if (TPR == TPResult::True())
2825 PossibleBitfield = true;
2826 // If the next token starts a type-specifier-seq, it may be either a
2827 // a fixed underlying type or the start of a function-style cast in C++;
2828 // lookahead one more token to see if it's obvious that we have a
2829 // fixed underlying type.
2830 else if (TPR == TPResult::False() &&
2831 GetLookAheadToken(2).getKind() == tok::semi) {
2832 // Consume the ':'.
2833 ConsumeToken();
2834 } else {
2835 // We have the start of a type-specifier-seq, so we have to perform
2836 // tentative parsing to determine whether we have an expression or a
2837 // type.
2838 TentativeParsingAction TPA(*this);
2839
2840 // Consume the ':'.
2841 ConsumeToken();
2842
Douglas Gregor86f208c2011-02-22 20:32:04 +00002843 if ((getLang().CPlusPlus &&
2844 isCXXDeclarationSpecifier() != TPResult::True()) ||
2845 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002846 // We'll parse this as a bitfield later.
2847 PossibleBitfield = true;
2848 TPA.Revert();
2849 } else {
2850 // We have a type-specifier-seq.
2851 TPA.Commit();
2852 }
2853 }
2854 } else {
2855 // Consume the ':'.
2856 ConsumeToken();
2857 }
2858
2859 if (!PossibleBitfield) {
2860 SourceRange Range;
2861 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002862
Douglas Gregor5471bc82011-09-08 17:18:35 +00002863 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002864 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2865 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002866 if (getLang().CPlusPlus0x)
2867 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002868 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002869 }
2870
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002871 // There are three options here. If we have 'enum foo;', then this is a
2872 // forward declaration. If we have 'enum foo {...' then this is a
2873 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2874 //
2875 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2876 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2877 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2878 //
John McCallf312b1e2010-08-26 23:41:50 +00002879 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002880 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002881 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002882 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002883 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002884 else
John McCallf312b1e2010-08-26 23:41:50 +00002885 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002886
2887 // enums cannot be templates, although they can be referenced from a
2888 // template.
2889 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002890 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002891 Diag(Tok, diag::err_enum_template);
2892
2893 // Skip the rest of this declarator, up until the comma or semicolon.
2894 SkipUntil(tok::comma, true);
2895 return;
2896 }
2897
Douglas Gregorb9075602011-02-22 02:55:24 +00002898 if (!Name && TUK != Sema::TUK_Definition) {
2899 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2900
2901 // Skip the rest of this declarator, up until the comma or semicolon.
2902 SkipUntil(tok::comma, true);
2903 return;
2904 }
2905
Douglas Gregor402abb52009-05-28 23:31:59 +00002906 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002907 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002908 const char *PrevSpec = 0;
2909 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002910 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002911 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002912 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002913 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002914 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002915 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002916
Douglas Gregor48c89f42010-04-24 16:38:41 +00002917 if (IsDependent) {
2918 // This enum has a dependent nested-name-specifier. Handle it as a
2919 // dependent tag.
2920 if (!Name) {
2921 DS.SetTypeSpecError();
2922 Diag(Tok, diag::err_expected_type_name_after_typename);
2923 return;
2924 }
2925
Douglas Gregor23c94db2010-07-02 17:43:08 +00002926 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002927 TUK, SS, Name, StartLoc,
2928 NameLoc);
2929 if (Type.isInvalid()) {
2930 DS.SetTypeSpecError();
2931 return;
2932 }
2933
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002934 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2935 NameLoc.isValid() ? NameLoc : StartLoc,
2936 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002937 Diag(StartLoc, DiagID) << PrevSpec;
2938
2939 return;
2940 }
Mike Stump1eb44332009-09-09 15:08:12 +00002941
John McCalld226f652010-08-21 09:40:31 +00002942 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002943 // The action failed to produce an enumeration tag. If this is a
2944 // definition, consume the entire definition.
2945 if (Tok.is(tok::l_brace)) {
2946 ConsumeBrace();
2947 SkipUntil(tok::r_brace);
2948 }
2949
2950 DS.SetTypeSpecError();
2951 return;
2952 }
2953
Chris Lattner04d66662007-10-09 17:33:22 +00002954 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002955 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002957 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2958 NameLoc.isValid() ? NameLoc : StartLoc,
2959 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002960 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002961}
2962
2963/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2964/// enumerator-list:
2965/// enumerator
2966/// enumerator-list ',' enumerator
2967/// enumerator:
2968/// enumeration-constant
2969/// enumeration-constant '=' constant-expression
2970/// enumeration-constant:
2971/// identifier
2972///
John McCalld226f652010-08-21 09:40:31 +00002973void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002974 // Enter the scope of the enum body and start the definition.
2975 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002976 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002977
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002978 BalancedDelimiterTracker T(*this, tok::l_brace);
2979 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Chris Lattner7946dd32007-08-27 17:24:30 +00002981 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002982 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002983 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Chris Lattner5f9e2722011-07-23 10:55:15 +00002985 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002986
John McCalld226f652010-08-21 09:40:31 +00002987 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002988
Reid Spencer5f016e22007-07-11 17:01:13 +00002989 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002990 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002991 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2992 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002993
John McCall5b629aa2010-10-22 23:36:17 +00002994 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002995 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002996 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002997
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002999 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003000 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003001 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003002 AssignedVal = ParseConstantExpression();
3003 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003004 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 }
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003008 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3009 LastEnumConstDecl,
3010 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003011 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003012 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003013 EnumConstantDecls.push_back(EnumConstDecl);
3014 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Douglas Gregor751f6922010-09-07 14:51:08 +00003016 if (Tok.is(tok::identifier)) {
3017 // We're missing a comma between enumerators.
3018 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3019 Diag(Loc, diag::err_enumerator_list_missing_comma)
3020 << FixItHint::CreateInsertion(Loc, ", ");
3021 continue;
3022 }
3023
Chris Lattner04d66662007-10-09 17:33:22 +00003024 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 break;
3026 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003027
Richard Smith7fe62082011-10-15 05:09:34 +00003028 if (Tok.isNot(tok::identifier)) {
3029 if (!getLang().C99 && !getLang().CPlusPlus0x)
3030 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3031 << getLang().CPlusPlus
3032 << FixItHint::CreateRemoval(CommaLoc);
3033 else if (getLang().CPlusPlus0x)
3034 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3035 << FixItHint::CreateRemoval(CommaLoc);
3036 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003037 }
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Reid Spencer5f016e22007-07-11 17:01:13 +00003039 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003040 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003041
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003043 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003044 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003045
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003046 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3047 EnumDecl, EnumConstantDecls.data(),
3048 EnumConstantDecls.size(), getCurScope(),
3049 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003050
Douglas Gregor72de6672009-01-08 20:45:30 +00003051 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003052 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3053 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003054}
3055
3056/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003057/// start of a type-qualifier-list.
3058bool Parser::isTypeQualifier() const {
3059 switch (Tok.getKind()) {
3060 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003061
3062 // type-qualifier only in OpenCL
3063 case tok::kw_private:
3064 return getLang().OpenCL;
3065
Steve Naroff5f8aa692008-02-11 23:15:56 +00003066 // type-qualifier
3067 case tok::kw_const:
3068 case tok::kw_volatile:
3069 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003070 case tok::kw___private:
3071 case tok::kw___local:
3072 case tok::kw___global:
3073 case tok::kw___constant:
3074 case tok::kw___read_only:
3075 case tok::kw___read_write:
3076 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003077 return true;
3078 }
3079}
3080
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003081/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3082/// is definitely a type-specifier. Return false if it isn't part of a type
3083/// specifier or if we're not sure.
3084bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3085 switch (Tok.getKind()) {
3086 default: return false;
3087 // type-specifiers
3088 case tok::kw_short:
3089 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003090 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003091 case tok::kw_signed:
3092 case tok::kw_unsigned:
3093 case tok::kw__Complex:
3094 case tok::kw__Imaginary:
3095 case tok::kw_void:
3096 case tok::kw_char:
3097 case tok::kw_wchar_t:
3098 case tok::kw_char16_t:
3099 case tok::kw_char32_t:
3100 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003101 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003102 case tok::kw_float:
3103 case tok::kw_double:
3104 case tok::kw_bool:
3105 case tok::kw__Bool:
3106 case tok::kw__Decimal32:
3107 case tok::kw__Decimal64:
3108 case tok::kw__Decimal128:
3109 case tok::kw___vector:
3110
3111 // struct-or-union-specifier (C99) or class-specifier (C++)
3112 case tok::kw_class:
3113 case tok::kw_struct:
3114 case tok::kw_union:
3115 // enum-specifier
3116 case tok::kw_enum:
3117
3118 // typedef-name
3119 case tok::annot_typename:
3120 return true;
3121 }
3122}
3123
Steve Naroff5f8aa692008-02-11 23:15:56 +00003124/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003125/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003126bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 switch (Tok.getKind()) {
3128 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Chris Lattner166a8fc2009-01-04 23:41:41 +00003130 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003131 if (TryAltiVecVectorToken())
3132 return true;
3133 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003134 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003135 // Annotate typenames and C++ scope specifiers. If we get one, just
3136 // recurse to handle whatever we get.
3137 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003138 return true;
3139 if (Tok.is(tok::identifier))
3140 return false;
3141 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003142
Chris Lattner166a8fc2009-01-04 23:41:41 +00003143 case tok::coloncolon: // ::foo::bar
3144 if (NextToken().is(tok::kw_new) || // ::new
3145 NextToken().is(tok::kw_delete)) // ::delete
3146 return false;
3147
Chris Lattner166a8fc2009-01-04 23:41:41 +00003148 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003149 return true;
3150 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 // GNU attributes support.
3153 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003154 // GNU typeof support.
3155 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 // type-specifiers
3158 case tok::kw_short:
3159 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003160 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 case tok::kw_signed:
3162 case tok::kw_unsigned:
3163 case tok::kw__Complex:
3164 case tok::kw__Imaginary:
3165 case tok::kw_void:
3166 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003167 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003168 case tok::kw_char16_t:
3169 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003171 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003172 case tok::kw_float:
3173 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003174 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003175 case tok::kw__Bool:
3176 case tok::kw__Decimal32:
3177 case tok::kw__Decimal64:
3178 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003179 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Chris Lattner99dc9142008-04-13 18:59:07 +00003181 // struct-or-union-specifier (C99) or class-specifier (C++)
3182 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 case tok::kw_struct:
3184 case tok::kw_union:
3185 // enum-specifier
3186 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Reid Spencer5f016e22007-07-11 17:01:13 +00003188 // type-qualifier
3189 case tok::kw_const:
3190 case tok::kw_volatile:
3191 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003192
3193 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003194 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Chris Lattner7c186be2008-10-20 00:25:30 +00003197 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3198 case tok::less:
3199 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Steve Naroff239f0732008-12-25 14:16:32 +00003201 case tok::kw___cdecl:
3202 case tok::kw___stdcall:
3203 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003204 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003205 case tok::kw___w64:
3206 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003207 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003208 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003209 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003210
3211 case tok::kw___private:
3212 case tok::kw___local:
3213 case tok::kw___global:
3214 case tok::kw___constant:
3215 case tok::kw___read_only:
3216 case tok::kw___read_write:
3217 case tok::kw___write_only:
3218
Eli Friedman290eeb02009-06-08 23:27:34 +00003219 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003220
3221 case tok::kw_private:
3222 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003223
3224 // C1x _Atomic()
3225 case tok::kw__Atomic:
3226 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003227 }
3228}
3229
3230/// isDeclarationSpecifier() - Return true if the current token is part of a
3231/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003232///
3233/// \param DisambiguatingWithExpression True to indicate that the purpose of
3234/// this check is to disambiguate between an expression and a declaration.
3235bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003236 switch (Tok.getKind()) {
3237 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003238
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003239 case tok::kw_private:
3240 return getLang().OpenCL;
3241
Chris Lattner166a8fc2009-01-04 23:41:41 +00003242 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003243 // Unfortunate hack to support "Class.factoryMethod" notation.
3244 if (getLang().ObjC1 && NextToken().is(tok::period))
3245 return false;
John Thompson82287d12010-02-05 00:12:22 +00003246 if (TryAltiVecVectorToken())
3247 return true;
3248 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003249 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003250 // Annotate typenames and C++ scope specifiers. If we get one, just
3251 // recurse to handle whatever we get.
3252 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003253 return true;
3254 if (Tok.is(tok::identifier))
3255 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003256
3257 // If we're in Objective-C and we have an Objective-C class type followed
3258 // by an identifier and then either ':' or ']', in a place where an
3259 // expression is permitted, then this is probably a class message send
3260 // missing the initial '['. In this case, we won't consider this to be
3261 // the start of a declaration.
3262 if (DisambiguatingWithExpression &&
3263 isStartOfObjCClassMessageMissingOpenBracket())
3264 return false;
3265
John McCall9ba61662010-02-26 08:45:28 +00003266 return isDeclarationSpecifier();
3267
Chris Lattner166a8fc2009-01-04 23:41:41 +00003268 case tok::coloncolon: // ::foo::bar
3269 if (NextToken().is(tok::kw_new) || // ::new
3270 NextToken().is(tok::kw_delete)) // ::delete
3271 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003272
Chris Lattner166a8fc2009-01-04 23:41:41 +00003273 // Annotate typenames and C++ scope specifiers. If we get one, just
3274 // recurse to handle whatever we get.
3275 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003276 return true;
3277 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003278
Reid Spencer5f016e22007-07-11 17:01:13 +00003279 // storage-class-specifier
3280 case tok::kw_typedef:
3281 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003282 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003283 case tok::kw_static:
3284 case tok::kw_auto:
3285 case tok::kw_register:
3286 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Douglas Gregor8d267c52011-09-09 02:06:17 +00003288 // Modules
3289 case tok::kw___module_private__:
3290
Reid Spencer5f016e22007-07-11 17:01:13 +00003291 // type-specifiers
3292 case tok::kw_short:
3293 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003294 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003295 case tok::kw_signed:
3296 case tok::kw_unsigned:
3297 case tok::kw__Complex:
3298 case tok::kw__Imaginary:
3299 case tok::kw_void:
3300 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003301 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003302 case tok::kw_char16_t:
3303 case tok::kw_char32_t:
3304
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003306 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 case tok::kw_float:
3308 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003309 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 case tok::kw__Bool:
3311 case tok::kw__Decimal32:
3312 case tok::kw__Decimal64:
3313 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003314 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Chris Lattner99dc9142008-04-13 18:59:07 +00003316 // struct-or-union-specifier (C99) or class-specifier (C++)
3317 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 case tok::kw_struct:
3319 case tok::kw_union:
3320 // enum-specifier
3321 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003322
Reid Spencer5f016e22007-07-11 17:01:13 +00003323 // type-qualifier
3324 case tok::kw_const:
3325 case tok::kw_volatile:
3326 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003327
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 // function-specifier
3329 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003330 case tok::kw_virtual:
3331 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003332
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003333 // static_assert-declaration
3334 case tok::kw__Static_assert:
3335
Chris Lattner1ef08762007-08-09 17:01:07 +00003336 // GNU typeof support.
3337 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Chris Lattner1ef08762007-08-09 17:01:07 +00003339 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003340 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003341 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003342
Francois Pichete3d49b42011-06-19 08:02:06 +00003343 // C++0x decltype.
3344 case tok::kw_decltype:
3345 return true;
3346
Eli Friedmanb001de72011-10-06 23:00:33 +00003347 // C1x _Atomic()
3348 case tok::kw__Atomic:
3349 return true;
3350
Chris Lattnerf3948c42008-07-26 03:38:44 +00003351 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3352 case tok::less:
3353 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003354
Douglas Gregord9d75e52011-04-27 05:41:15 +00003355 // typedef-name
3356 case tok::annot_typename:
3357 return !DisambiguatingWithExpression ||
3358 !isStartOfObjCClassMessageMissingOpenBracket();
3359
Steve Naroff47f52092009-01-06 19:34:12 +00003360 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003361 case tok::kw___cdecl:
3362 case tok::kw___stdcall:
3363 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003364 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003365 case tok::kw___w64:
3366 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003367 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003368 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003369 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003370 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003371
3372 case tok::kw___private:
3373 case tok::kw___local:
3374 case tok::kw___global:
3375 case tok::kw___constant:
3376 case tok::kw___read_only:
3377 case tok::kw___read_write:
3378 case tok::kw___write_only:
3379
Eli Friedman290eeb02009-06-08 23:27:34 +00003380 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003381 }
3382}
3383
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003384bool Parser::isConstructorDeclarator() {
3385 TentativeParsingAction TPA(*this);
3386
3387 // Parse the C++ scope specifier.
3388 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003389 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003390 TPA.Revert();
3391 return false;
3392 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003393
3394 // Parse the constructor name.
3395 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3396 // We already know that we have a constructor name; just consume
3397 // the token.
3398 ConsumeToken();
3399 } else {
3400 TPA.Revert();
3401 return false;
3402 }
3403
3404 // Current class name must be followed by a left parentheses.
3405 if (Tok.isNot(tok::l_paren)) {
3406 TPA.Revert();
3407 return false;
3408 }
3409 ConsumeParen();
3410
3411 // A right parentheses or ellipsis signals that we have a constructor.
3412 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3413 TPA.Revert();
3414 return true;
3415 }
3416
3417 // If we need to, enter the specified scope.
3418 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003419 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003420 DeclScopeObj.EnterDeclaratorScope();
3421
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003422 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003423 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003424 MaybeParseMicrosoftAttributes(Attrs);
3425
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003426 // Check whether the next token(s) are part of a declaration
3427 // specifier, in which case we have the start of a parameter and,
3428 // therefore, we know that this is a constructor.
3429 bool IsConstructor = isDeclarationSpecifier();
3430 TPA.Revert();
3431 return IsConstructor;
3432}
Reid Spencer5f016e22007-07-11 17:01:13 +00003433
3434/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003435/// type-qualifier-list: [C99 6.7.5]
3436/// type-qualifier
3437/// [vendor] attributes
3438/// [ only if VendorAttributesAllowed=true ]
3439/// type-qualifier-list type-qualifier
3440/// [vendor] type-qualifier-list attributes
3441/// [ only if VendorAttributesAllowed=true ]
3442/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3443/// [ only if CXX0XAttributesAllowed=true ]
3444/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003445///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003446void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3447 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003448 bool CXX0XAttributesAllowed) {
3449 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3450 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003451 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003452 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003453 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003454 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003455 else
3456 Diag(Loc, diag::err_attributes_not_allowed);
3457 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003458
3459 SourceLocation EndLoc;
3460
Reid Spencer5f016e22007-07-11 17:01:13 +00003461 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003462 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003464 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003465 SourceLocation Loc = Tok.getLocation();
3466
3467 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003468 case tok::code_completion:
3469 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003470 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003471
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003473 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3474 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 break;
3476 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003477 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3478 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003479 break;
3480 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003481 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3482 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003483 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003484
3485 // OpenCL qualifiers:
3486 case tok::kw_private:
3487 if (!getLang().OpenCL)
3488 goto DoneWithTypeQuals;
3489 case tok::kw___private:
3490 case tok::kw___global:
3491 case tok::kw___local:
3492 case tok::kw___constant:
3493 case tok::kw___read_only:
3494 case tok::kw___write_only:
3495 case tok::kw___read_write:
3496 ParseOpenCLQualifiers(DS);
3497 break;
3498
Eli Friedman290eeb02009-06-08 23:27:34 +00003499 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003500 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003501 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003502 case tok::kw___cdecl:
3503 case tok::kw___stdcall:
3504 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003505 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003506 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003507 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003508 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003509 continue;
3510 }
3511 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003512 case tok::kw___pascal:
3513 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003514 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003515 continue;
3516 }
3517 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003518 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003519 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003520 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003521 continue; // do *not* consume the next token!
3522 }
3523 // otherwise, FALL THROUGH!
3524 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003525 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003526 // If this is not a type-qualifier token, we're done reading type
3527 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003528 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003529 if (EndLoc.isValid())
3530 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003531 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003532 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003533
Reid Spencer5f016e22007-07-11 17:01:13 +00003534 // If the specifier combination wasn't legal, issue a diagnostic.
3535 if (isInvalid) {
3536 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003537 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003538 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003539 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 }
3541}
3542
3543
3544/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3545///
3546void Parser::ParseDeclarator(Declarator &D) {
3547 /// This implements the 'declarator' production in the C grammar, then checks
3548 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003549 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003550}
3551
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003552/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3553/// is parsed by the function passed to it. Pass null, and the direct-declarator
3554/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003555/// ptr-operator production.
3556///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003557/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3558/// [C] pointer[opt] direct-declarator
3559/// [C++] direct-declarator
3560/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003561///
3562/// pointer: [C99 6.7.5]
3563/// '*' type-qualifier-list[opt]
3564/// '*' type-qualifier-list[opt] pointer
3565///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003566/// ptr-operator:
3567/// '*' cv-qualifier-seq[opt]
3568/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003569/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003570/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003571/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003572/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003573void Parser::ParseDeclaratorInternal(Declarator &D,
3574 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003575 if (Diags.hasAllExtensionsSilenced())
3576 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003577
Sebastian Redlf30208a2009-01-24 21:16:55 +00003578 // C++ member pointers start with a '::' or a nested-name.
3579 // Member pointers get special handling, since there's no place for the
3580 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003581 if (getLang().CPlusPlus &&
3582 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3583 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003584 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003585 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003586
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003587 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003588 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003589 // The scope spec really belongs to the direct-declarator.
3590 D.getCXXScopeSpec() = SS;
3591 if (DirectDeclParser)
3592 (this->*DirectDeclParser)(D);
3593 return;
3594 }
3595
3596 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003597 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003598 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003599 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003600 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003601
3602 // Recurse to parse whatever is left.
3603 ParseDeclaratorInternal(D, DirectDeclParser);
3604
3605 // Sema will have to catch (syntactically invalid) pointers into global
3606 // scope. It has to catch pointers into namespace scope anyway.
3607 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003608 Loc),
3609 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003610 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003611 return;
3612 }
3613 }
3614
3615 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003616 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003617 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003618 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003619 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003620 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003621 if (DirectDeclParser)
3622 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003623 return;
3624 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003625
Sebastian Redl05532f22009-03-15 22:02:01 +00003626 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3627 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003628 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003629 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003630
Chris Lattner9af55002009-03-27 04:18:06 +00003631 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003632 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003633 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003634
Reid Spencer5f016e22007-07-11 17:01:13 +00003635 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003636 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003637
Reid Spencer5f016e22007-07-11 17:01:13 +00003638 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003639 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003640 if (Kind == tok::star)
3641 // Remember that we parsed a pointer type, and remember the type-quals.
3642 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003643 DS.getConstSpecLoc(),
3644 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003645 DS.getRestrictSpecLoc()),
3646 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003647 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003648 else
3649 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003650 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003651 Loc),
3652 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003653 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003654 } else {
3655 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003656 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003657
Sebastian Redl743de1f2009-03-23 00:00:23 +00003658 // Complain about rvalue references in C++03, but then go on and build
3659 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003660 if (Kind == tok::ampamp)
3661 Diag(Loc, getLang().CPlusPlus0x ?
3662 diag::warn_cxx98_compat_rvalue_reference :
3663 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003664
Reid Spencer5f016e22007-07-11 17:01:13 +00003665 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3666 // cv-qualifiers are introduced through the use of a typedef or of a
3667 // template type argument, in which case the cv-qualifiers are ignored.
3668 //
3669 // [GNU] Retricted references are allowed.
3670 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003671 // [C++0x] Attributes on references are not allowed.
3672 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003673 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003674
3675 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3676 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3677 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003678 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003679 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3680 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003681 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003682 }
3683
3684 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003685 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003686
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003687 if (D.getNumTypeObjects() > 0) {
3688 // C++ [dcl.ref]p4: There shall be no references to references.
3689 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3690 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003691 if (const IdentifierInfo *II = D.getIdentifier())
3692 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3693 << II;
3694 else
3695 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3696 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003697
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003698 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003699 // can go ahead and build the (technically ill-formed)
3700 // declarator: reference collapsing will take care of it.
3701 }
3702 }
3703
Reid Spencer5f016e22007-07-11 17:01:13 +00003704 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003705 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003706 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003707 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003708 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003709 }
3710}
3711
3712/// ParseDirectDeclarator
3713/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003714/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003715/// '(' declarator ')'
3716/// [GNU] '(' attributes declarator ')'
3717/// [C90] direct-declarator '[' constant-expression[opt] ']'
3718/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3719/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3720/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3721/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3722/// direct-declarator '(' parameter-type-list ')'
3723/// direct-declarator '(' identifier-list[opt] ')'
3724/// [GNU] direct-declarator '(' parameter-forward-declarations
3725/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003726/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3727/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003728/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003729///
3730/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003731/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003732/// '::'[opt] nested-name-specifier[opt] type-name
3733///
3734/// id-expression: [C++ 5.1]
3735/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003736/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003737///
3738/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003739/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003740/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003741/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003742/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003743/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003744///
Reid Spencer5f016e22007-07-11 17:01:13 +00003745void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003746 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003747
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003748 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3749 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003750 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003751 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003752 }
3753
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003754 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003755 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003756 // Change the declaration context for name lookup, until this function
3757 // is exited (and the declarator has been parsed).
3758 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003759 }
3760
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003761 // C++0x [dcl.fct]p14:
3762 // There is a syntactic ambiguity when an ellipsis occurs at the end
3763 // of a parameter-declaration-clause without a preceding comma. In
3764 // this case, the ellipsis is parsed as part of the
3765 // abstract-declarator if the type of the parameter names a template
3766 // parameter pack that has not been expanded; otherwise, it is parsed
3767 // as part of the parameter-declaration-clause.
3768 if (Tok.is(tok::ellipsis) &&
3769 !((D.getContext() == Declarator::PrototypeContext ||
3770 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003771 NextToken().is(tok::r_paren) &&
3772 !Actions.containsUnexpandedParameterPacks(D)))
3773 D.setEllipsisLoc(ConsumeToken());
3774
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003775 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3776 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3777 // We found something that indicates the start of an unqualified-id.
3778 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003779 bool AllowConstructorName;
3780 if (D.getDeclSpec().hasTypeSpecifier())
3781 AllowConstructorName = false;
3782 else if (D.getCXXScopeSpec().isSet())
3783 AllowConstructorName =
3784 (D.getContext() == Declarator::FileContext ||
3785 (D.getContext() == Declarator::MemberContext &&
3786 D.getDeclSpec().isFriendSpecified()));
3787 else
3788 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3789
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003790 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3791 /*EnteringContext=*/true,
3792 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003793 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003794 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003795 D.getName()) ||
3796 // Once we're past the identifier, if the scope was bad, mark the
3797 // whole declarator bad.
3798 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003799 D.SetIdentifier(0, Tok.getLocation());
3800 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003801 } else {
3802 // Parsed the unqualified-id; update range information and move along.
3803 if (D.getSourceRange().getBegin().isInvalid())
3804 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3805 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003806 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003807 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003808 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003809 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003810 assert(!getLang().CPlusPlus &&
3811 "There's a C++-specific check for tok::identifier above");
3812 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3813 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3814 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003815 goto PastIdentifier;
3816 }
3817
3818 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003819 // direct-declarator: '(' declarator ')'
3820 // direct-declarator: '(' attributes declarator ')'
3821 // Example: 'char (*X)' or 'int (*XX)(void)'
3822 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003823
3824 // If the declarator was parenthesized, we entered the declarator
3825 // scope when parsing the parenthesized declarator, then exited
3826 // the scope already. Re-enter the scope, if we need to.
3827 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003828 // If there was an error parsing parenthesized declarator, declarator
3829 // scope may have been enterred before. Don't do it again.
3830 if (!D.isInvalidType() &&
3831 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003832 // Change the declaration context for name lookup, until this function
3833 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003834 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003835 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003836 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003837 // This could be something simple like "int" (in which case the declarator
3838 // portion is empty), if an abstract-declarator is allowed.
3839 D.SetIdentifier(0, Tok.getLocation());
3840 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003841 if (D.getContext() == Declarator::MemberContext)
3842 Diag(Tok, diag::err_expected_member_name_or_semi)
3843 << D.getDeclSpec().getSourceRange();
3844 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003845 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003846 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003847 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003849 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003850 }
Mike Stump1eb44332009-09-09 15:08:12 +00003851
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003852 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003853 assert(D.isPastIdentifier() &&
3854 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003855
Sean Huntbbd37c62009-11-21 08:43:09 +00003856 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003857 if (D.getIdentifier())
3858 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003859
Reid Spencer5f016e22007-07-11 17:01:13 +00003860 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003861 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003862 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3863 // In such a case, check if we actually have a function declarator; if it
3864 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003865 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3866 // When not in file scope, warn for ambiguous function declarators, just
3867 // in case the author intended it as a variable definition.
3868 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3869 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3870 break;
3871 }
John McCall0b7e6782011-03-24 11:26:52 +00003872 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003873 BalancedDelimiterTracker T(*this, tok::l_paren);
3874 T.consumeOpen();
3875 ParseFunctionDeclarator(D, attrs, T);
Chris Lattner04d66662007-10-09 17:33:22 +00003876 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 ParseBracketDeclarator(D);
3878 } else {
3879 break;
3880 }
3881 }
3882}
3883
Chris Lattneref4715c2008-04-06 05:45:57 +00003884/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3885/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003886/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003887/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3888///
3889/// direct-declarator:
3890/// '(' declarator ')'
3891/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003892/// direct-declarator '(' parameter-type-list ')'
3893/// direct-declarator '(' identifier-list[opt] ')'
3894/// [GNU] direct-declarator '(' parameter-forward-declarations
3895/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003896///
3897void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003898 BalancedDelimiterTracker T(*this, tok::l_paren);
3899 T.consumeOpen();
3900
Chris Lattneref4715c2008-04-06 05:45:57 +00003901 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003902
Chris Lattner7399ee02008-10-20 02:05:46 +00003903 // Eat any attributes before we look at whether this is a grouping or function
3904 // declarator paren. If this is a grouping paren, the attribute applies to
3905 // the type being built up, for example:
3906 // int (__attribute__(()) *x)(long y)
3907 // If this ends up not being a grouping paren, the attribute applies to the
3908 // first argument, for example:
3909 // int (__attribute__(()) int x)
3910 // In either case, we need to eat any attributes to be able to determine what
3911 // sort of paren this is.
3912 //
John McCall0b7e6782011-03-24 11:26:52 +00003913 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003914 bool RequiresArg = false;
3915 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003916 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003917
Chris Lattner7399ee02008-10-20 02:05:46 +00003918 // We require that the argument list (if this is a non-grouping paren) be
3919 // present even if the attribute list was empty.
3920 RequiresArg = true;
3921 }
Steve Naroff239f0732008-12-25 14:16:32 +00003922 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003923 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003924 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003925 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003926 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003927 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003928 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003929 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003930 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003931 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003932
Chris Lattneref4715c2008-04-06 05:45:57 +00003933 // If we haven't past the identifier yet (or where the identifier would be
3934 // stored, if this is an abstract declarator), then this is probably just
3935 // grouping parens. However, if this could be an abstract-declarator, then
3936 // this could also be the start of function arguments (consider 'void()').
3937 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003938
Chris Lattneref4715c2008-04-06 05:45:57 +00003939 if (!D.mayOmitIdentifier()) {
3940 // If this can't be an abstract-declarator, this *must* be a grouping
3941 // paren, because we haven't seen the identifier yet.
3942 isGrouping = true;
3943 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003944 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003945 isDeclarationSpecifier()) { // 'int(int)' is a function.
3946 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3947 // considered to be a type, not a K&R identifier-list.
3948 isGrouping = false;
3949 } else {
3950 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3951 isGrouping = true;
3952 }
Mike Stump1eb44332009-09-09 15:08:12 +00003953
Chris Lattneref4715c2008-04-06 05:45:57 +00003954 // If this is a grouping paren, handle:
3955 // direct-declarator: '(' declarator ')'
3956 // direct-declarator: '(' attributes declarator ')'
3957 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003958 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003959 D.setGroupingParens(true);
3960
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003961 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003962 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003963 T.consumeClose();
3964 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3965 T.getCloseLocation()),
3966 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003967
3968 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003969 return;
3970 }
Mike Stump1eb44332009-09-09 15:08:12 +00003971
Chris Lattneref4715c2008-04-06 05:45:57 +00003972 // Okay, if this wasn't a grouping paren, it must be the start of a function
3973 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003974 // identifier (and remember where it would have been), then call into
3975 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003976 D.SetIdentifier(0, Tok.getLocation());
3977
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003978 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003979}
3980
3981/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3982/// declarator D up to a paren, which indicates that we are parsing function
3983/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003984///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003985/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003986/// after the open paren - they should be considered to be the first argument of
3987/// a parameter. If RequiresArg is true, then the first argument of the
3988/// function is required to be present and required to not be an identifier
3989/// list.
3990///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003991/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3992/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3993/// (C++0x) trailing-return-type[opt].
3994///
3995/// [C++0x] exception-specification:
3996/// dynamic-exception-specification
3997/// noexcept-specification
3998///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003999void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004000 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004001 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004002 bool RequiresArg) {
4003 // lparen is already consumed!
4004 assert(D.isPastIdentifier() && "Should not call before identifier!");
4005
4006 // This should be true when the function has typed arguments.
4007 // Otherwise, it is treated as a K&R-style function.
4008 bool HasProto = false;
4009 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004010 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004011 // Remember where we see an ellipsis, if any.
4012 SourceLocation EllipsisLoc;
4013
4014 DeclSpec DS(AttrFactory);
4015 bool RefQualifierIsLValueRef = true;
4016 SourceLocation RefQualifierLoc;
4017 ExceptionSpecificationType ESpecType = EST_None;
4018 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004019 SmallVector<ParsedType, 2> DynamicExceptions;
4020 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004021 ExprResult NoexceptExpr;
4022 ParsedType TrailingReturnType;
4023
4024 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004025 if (isFunctionDeclaratorIdentifierList()) {
4026 if (RequiresArg)
4027 Diag(Tok, diag::err_argument_required_after_attribute);
4028
4029 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4030
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004031 Tracker.consumeClose();
4032 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004033 } else {
4034 // Enter function-declaration scope, limiting any declarators to the
4035 // function prototype scope, including parameter declarators.
4036 ParseScope PrototypeScope(this,
4037 Scope::FunctionPrototypeScope|Scope::DeclScope);
4038
4039 if (Tok.isNot(tok::r_paren))
4040 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4041 else if (RequiresArg)
4042 Diag(Tok, diag::err_argument_required_after_attribute);
4043
4044 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4045
4046 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004047 Tracker.consumeClose();
4048 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004049
4050 if (getLang().CPlusPlus) {
4051 MaybeParseCXX0XAttributes(attrs);
4052
4053 // Parse cv-qualifier-seq[opt].
4054 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4055 if (!DS.getSourceRange().getEnd().isInvalid())
4056 EndLoc = DS.getSourceRange().getEnd();
4057
4058 // Parse ref-qualifier[opt].
4059 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004060 Diag(Tok, getLang().CPlusPlus0x ?
4061 diag::warn_cxx98_compat_ref_qualifier :
4062 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004063
4064 RefQualifierIsLValueRef = Tok.is(tok::amp);
4065 RefQualifierLoc = ConsumeToken();
4066 EndLoc = RefQualifierLoc;
4067 }
4068
4069 // Parse exception-specification[opt].
4070 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4071 DynamicExceptions,
4072 DynamicExceptionRanges,
4073 NoexceptExpr);
4074 if (ESpecType != EST_None)
4075 EndLoc = ESpecRange.getEnd();
4076
4077 // Parse trailing-return-type[opt].
4078 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004079 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004080 SourceRange Range;
4081 TrailingReturnType = ParseTrailingReturnType(Range).get();
4082 if (Range.getEnd().isValid())
4083 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004084 }
4085 }
4086
4087 // Leave prototype scope.
4088 PrototypeScope.Exit();
4089 }
4090
4091 // Remember that we parsed a function type, and remember the attributes.
4092 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4093 /*isVariadic=*/EllipsisLoc.isValid(),
4094 EllipsisLoc,
4095 ParamInfo.data(), ParamInfo.size(),
4096 DS.getTypeQualifiers(),
4097 RefQualifierIsLValueRef,
4098 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004099 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004100 ESpecType, ESpecRange.getBegin(),
4101 DynamicExceptions.data(),
4102 DynamicExceptionRanges.data(),
4103 DynamicExceptions.size(),
4104 NoexceptExpr.isUsable() ?
4105 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004106 Tracker.getOpenLocation(),
4107 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004108 TrailingReturnType),
4109 attrs, EndLoc);
4110}
4111
4112/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4113/// identifier list form for a K&R-style function: void foo(a,b,c)
4114///
4115/// Note that identifier-lists are only allowed for normal declarators, not for
4116/// abstract-declarators.
4117bool Parser::isFunctionDeclaratorIdentifierList() {
4118 return !getLang().CPlusPlus
4119 && Tok.is(tok::identifier)
4120 && !TryAltiVecVectorToken()
4121 // K&R identifier lists can't have typedefs as identifiers, per C99
4122 // 6.7.5.3p11.
4123 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4124 // Identifier lists follow a really simple grammar: the identifiers can
4125 // be followed *only* by a ", identifier" or ")". However, K&R
4126 // identifier lists are really rare in the brave new modern world, and
4127 // it is very common for someone to typo a type in a non-K&R style
4128 // list. If we are presented with something like: "void foo(intptr x,
4129 // float y)", we don't want to start parsing the function declarator as
4130 // though it is a K&R style declarator just because intptr is an
4131 // invalid type.
4132 //
4133 // To handle this, we check to see if the token after the first
4134 // identifier is a "," or ")". Only then do we parse it as an
4135 // identifier list.
4136 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4137}
4138
4139/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4140/// we found a K&R-style identifier list instead of a typed parameter list.
4141///
4142/// After returning, ParamInfo will hold the parsed parameters.
4143///
4144/// identifier-list: [C99 6.7.5]
4145/// identifier
4146/// identifier-list ',' identifier
4147///
4148void Parser::ParseFunctionDeclaratorIdentifierList(
4149 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004150 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 // If there was no identifier specified for the declarator, either we are in
4152 // an abstract-declarator, or we are in a parameter declarator which was found
4153 // to be abstract. In abstract-declarators, identifier lists are not valid:
4154 // diagnose this.
4155 if (!D.getIdentifier())
4156 Diag(Tok, diag::ext_ident_list_in_param);
4157
4158 // Maintain an efficient lookup of params we have seen so far.
4159 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4160
4161 while (1) {
4162 // If this isn't an identifier, report the error and skip until ')'.
4163 if (Tok.isNot(tok::identifier)) {
4164 Diag(Tok, diag::err_expected_ident);
4165 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4166 // Forget we parsed anything.
4167 ParamInfo.clear();
4168 return;
4169 }
4170
4171 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4172
4173 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4174 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4175 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4176
4177 // Verify that the argument identifier has not already been mentioned.
4178 if (!ParamsSoFar.insert(ParmII)) {
4179 Diag(Tok, diag::err_param_redefinition) << ParmII;
4180 } else {
4181 // Remember this identifier in ParamInfo.
4182 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4183 Tok.getLocation(),
4184 0));
4185 }
4186
4187 // Eat the identifier.
4188 ConsumeToken();
4189
4190 // The list continues if we see a comma.
4191 if (Tok.isNot(tok::comma))
4192 break;
4193 ConsumeToken();
4194 }
4195}
4196
4197/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4198/// after the opening parenthesis. This function will not parse a K&R-style
4199/// identifier list.
4200///
4201/// D is the declarator being parsed. If attrs is non-null, then the caller
4202/// parsed those arguments immediately after the open paren - they should be
4203/// considered to be the first argument of a parameter.
4204///
4205/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4206/// be the location of the ellipsis, if any was parsed.
4207///
Reid Spencer5f016e22007-07-11 17:01:13 +00004208/// parameter-type-list: [C99 6.7.5]
4209/// parameter-list
4210/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004211/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004212///
4213/// parameter-list: [C99 6.7.5]
4214/// parameter-declaration
4215/// parameter-list ',' parameter-declaration
4216///
4217/// parameter-declaration: [C99 6.7.5]
4218/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004219/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004220/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004221/// declaration-specifiers abstract-declarator[opt]
4222/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004223/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004224/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4225///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004226void Parser::ParseParameterDeclarationClause(
4227 Declarator &D,
4228 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004229 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004230 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004231
Chris Lattnerf97409f2008-04-06 06:57:35 +00004232 while (1) {
4233 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004234 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004235 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004236 }
Mike Stump1eb44332009-09-09 15:08:12 +00004237
Chris Lattnerf97409f2008-04-06 06:57:35 +00004238 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004239 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004240 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004241
John McCall7f040a92010-12-24 02:08:15 +00004242 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004243 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004244 ParseMicrosoftAttributes(DS.getAttributes());
4245
4246 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004247
4248 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004249 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004250 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4251 // attributes lost? Should they even be allowed?
4252 // FIXME: If we can leave the attributes in the token stream somehow, we can
4253 // get rid of a parameter (attrs) and this statement. It might be too much
4254 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004255 DS.takeAttributesFrom(attrs);
4256
Chris Lattnere64c5492009-02-27 18:38:20 +00004257 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004258
Chris Lattnerf97409f2008-04-06 06:57:35 +00004259 // Parse the declarator. This is "PrototypeContext", because we must
4260 // accept either 'declarator' or 'abstract-declarator' here.
4261 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4262 ParseDeclarator(ParmDecl);
4263
4264 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004265 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004266
Chris Lattnerf97409f2008-04-06 06:57:35 +00004267 // Remember this parsed parameter in ParamInfo.
4268 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004269
Douglas Gregor72b505b2008-12-16 21:30:33 +00004270 // DefArgToks is used when the parsing of default arguments needs
4271 // to be delayed.
4272 CachedTokens *DefArgToks = 0;
4273
Chris Lattnerf97409f2008-04-06 06:57:35 +00004274 // If no parameter was specified, verify that *something* was specified,
4275 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004276 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4277 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004278 // Completely missing, emit error.
4279 Diag(DSStart, diag::err_missing_param);
4280 } else {
4281 // Otherwise, we have something. Add it and let semantic analysis try
4282 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004283
Chris Lattnerf97409f2008-04-06 06:57:35 +00004284 // Inform the actions module about the parameter declarator, so it gets
4285 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004286 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004287
4288 // Parse the default argument, if any. We parse the default
4289 // arguments in all dialects; the semantic analysis in
4290 // ActOnParamDefaultArgument will reject the default argument in
4291 // C.
4292 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004293 SourceLocation EqualLoc = Tok.getLocation();
4294
Chris Lattner04421082008-04-08 04:40:51 +00004295 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004296 if (D.getContext() == Declarator::MemberContext) {
4297 // If we're inside a class definition, cache the tokens
4298 // corresponding to the default argument. We'll actually parse
4299 // them when we see the end of the class definition.
4300 // FIXME: Templates will require something similar.
4301 // FIXME: Can we use a smart pointer for Toks?
4302 DefArgToks = new CachedTokens;
4303
Mike Stump1eb44332009-09-09 15:08:12 +00004304 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004305 /*StopAtSemi=*/true,
4306 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004307 delete DefArgToks;
4308 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004309 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004310 } else {
4311 // Mark the end of the default argument so that we know when to
4312 // stop when we parse it later on.
4313 Token DefArgEnd;
4314 DefArgEnd.startToken();
4315 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4316 DefArgEnd.setLocation(Tok.getLocation());
4317 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004318 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004319 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004320 }
Chris Lattner04421082008-04-08 04:40:51 +00004321 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004322 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004323 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004324
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004325 // The argument isn't actually potentially evaluated unless it is
4326 // used.
4327 EnterExpressionEvaluationContext Eval(Actions,
4328 Sema::PotentiallyEvaluatedIfUsed);
4329
John McCall60d7b3a2010-08-24 06:29:42 +00004330 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004331 if (DefArgResult.isInvalid()) {
4332 Actions.ActOnParamDefaultArgumentError(Param);
4333 SkipUntil(tok::comma, tok::r_paren, true, true);
4334 } else {
4335 // Inform the actions module about the default argument
4336 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004337 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004338 }
Chris Lattner04421082008-04-08 04:40:51 +00004339 }
4340 }
Mike Stump1eb44332009-09-09 15:08:12 +00004341
4342 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4343 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004344 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004345 }
4346
4347 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004348 if (Tok.isNot(tok::comma)) {
4349 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004350 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4351
4352 if (!getLang().CPlusPlus) {
4353 // We have ellipsis without a preceding ',', which is ill-formed
4354 // in C. Complain and provide the fix.
4355 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004356 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004357 }
4358 }
4359
4360 break;
4361 }
Mike Stump1eb44332009-09-09 15:08:12 +00004362
Chris Lattnerf97409f2008-04-06 06:57:35 +00004363 // Consume the comma.
4364 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004365 }
Mike Stump1eb44332009-09-09 15:08:12 +00004366
Chris Lattner66d28652008-04-06 06:34:08 +00004367}
Chris Lattneref4715c2008-04-06 05:45:57 +00004368
Reid Spencer5f016e22007-07-11 17:01:13 +00004369/// [C90] direct-declarator '[' constant-expression[opt] ']'
4370/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4371/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4372/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4373/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4374void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004375 BalancedDelimiterTracker T(*this, tok::l_square);
4376 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004377
Chris Lattner378c7e42008-12-18 07:27:21 +00004378 // C array syntax has many features, but by-far the most common is [] and [4].
4379 // This code does a fast path to handle some of the most obvious cases.
4380 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004381 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004382 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004383 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004384
Chris Lattner378c7e42008-12-18 07:27:21 +00004385 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004386 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004387 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004388 T.getOpenLocation(),
4389 T.getCloseLocation()),
4390 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004391 return;
4392 } else if (Tok.getKind() == tok::numeric_constant &&
4393 GetLookAheadToken(1).is(tok::r_square)) {
4394 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004395 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004396 ConsumeToken();
4397
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004398 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004399 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004400 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004401
Chris Lattner378c7e42008-12-18 07:27:21 +00004402 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004403 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004404 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004405 T.getOpenLocation(),
4406 T.getCloseLocation()),
4407 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004408 return;
4409 }
Mike Stump1eb44332009-09-09 15:08:12 +00004410
Reid Spencer5f016e22007-07-11 17:01:13 +00004411 // If valid, this location is the position where we read the 'static' keyword.
4412 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004413 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004414 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004415
Reid Spencer5f016e22007-07-11 17:01:13 +00004416 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004417 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004418 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004419 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004420
Reid Spencer5f016e22007-07-11 17:01:13 +00004421 // If we haven't already read 'static', check to see if there is one after the
4422 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004423 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004424 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004425
Reid Spencer5f016e22007-07-11 17:01:13 +00004426 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4427 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004428 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004429
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004430 // Handle the case where we have '[*]' as the array size. However, a leading
4431 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4432 // the the token after the star is a ']'. Since stars in arrays are
4433 // infrequent, use of lookahead is not costly here.
4434 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004435 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004436
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004437 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004438 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004439 StaticLoc = SourceLocation(); // Drop the static.
4440 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004441 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004442 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004443 // Note, in C89, this production uses the constant-expr production instead
4444 // of assignment-expr. The only difference is that assignment-expr allows
4445 // things like '=' and '*='. Sema rejects these in C89 mode because they
4446 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004447
Douglas Gregore0762c92009-06-19 23:52:42 +00004448 // Parse the constant-expression or assignment-expression now (depending
4449 // on dialect).
4450 if (getLang().CPlusPlus)
4451 NumElements = ParseConstantExpression();
4452 else
4453 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004454 }
Mike Stump1eb44332009-09-09 15:08:12 +00004455
Reid Spencer5f016e22007-07-11 17:01:13 +00004456 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004457 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004458 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004459 // If the expression was invalid, skip it.
4460 SkipUntil(tok::r_square);
4461 return;
4462 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004463
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004464 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004465
John McCall0b7e6782011-03-24 11:26:52 +00004466 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004467 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004468
Chris Lattner378c7e42008-12-18 07:27:21 +00004469 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004470 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004471 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004472 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004473 T.getOpenLocation(),
4474 T.getCloseLocation()),
4475 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004476}
4477
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004478/// [GNU] typeof-specifier:
4479/// typeof ( expressions )
4480/// typeof ( type-name )
4481/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004482///
4483void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004484 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004485 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004486 SourceLocation StartLoc = ConsumeToken();
4487
John McCallcfb708c2010-01-13 20:03:27 +00004488 const bool hasParens = Tok.is(tok::l_paren);
4489
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004490 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004491 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004492 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004493 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4494 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004495 if (hasParens)
4496 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004497
4498 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004499 // FIXME: Not accurate, the range gets one token more than it should.
4500 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004501 else
4502 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004503
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004504 if (isCastExpr) {
4505 if (!CastTy) {
4506 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004507 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004508 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004509
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004510 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004511 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004512 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4513 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004514 DiagID, CastTy))
4515 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004516 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004517 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004518
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004519 // If we get here, the operand to the typeof was an expresion.
4520 if (Operand.isInvalid()) {
4521 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004522 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004523 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004524
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004525 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004526 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004527 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4528 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004529 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004530 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004531}
Chris Lattner1b492422010-02-28 18:33:55 +00004532
Eli Friedmanb001de72011-10-06 23:00:33 +00004533/// [C1X] atomic-specifier:
4534/// _Atomic ( type-name )
4535///
4536void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4537 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4538
4539 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004540 BalancedDelimiterTracker T(*this, tok::l_paren);
4541 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004542 SkipUntil(tok::r_paren);
4543 return;
4544 }
4545
4546 TypeResult Result = ParseTypeName();
4547 if (Result.isInvalid()) {
4548 SkipUntil(tok::r_paren);
4549 return;
4550 }
4551
4552 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004553 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004554
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004555 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004556 return;
4557
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004558 DS.setTypeofParensRange(T.getRange());
4559 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004560
4561 const char *PrevSpec = 0;
4562 unsigned DiagID;
4563 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4564 DiagID, Result.release()))
4565 Diag(StartLoc, DiagID) << PrevSpec;
4566}
4567
Chris Lattner1b492422010-02-28 18:33:55 +00004568
4569/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4570/// from TryAltiVecVectorToken.
4571bool Parser::TryAltiVecVectorTokenOutOfLine() {
4572 Token Next = NextToken();
4573 switch (Next.getKind()) {
4574 default: return false;
4575 case tok::kw_short:
4576 case tok::kw_long:
4577 case tok::kw_signed:
4578 case tok::kw_unsigned:
4579 case tok::kw_void:
4580 case tok::kw_char:
4581 case tok::kw_int:
4582 case tok::kw_float:
4583 case tok::kw_double:
4584 case tok::kw_bool:
4585 case tok::kw___pixel:
4586 Tok.setKind(tok::kw___vector);
4587 return true;
4588 case tok::identifier:
4589 if (Next.getIdentifierInfo() == Ident_pixel) {
4590 Tok.setKind(tok::kw___vector);
4591 return true;
4592 }
4593 return false;
4594 }
4595}
4596
4597bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4598 const char *&PrevSpec, unsigned &DiagID,
4599 bool &isInvalid) {
4600 if (Tok.getIdentifierInfo() == Ident_vector) {
4601 Token Next = NextToken();
4602 switch (Next.getKind()) {
4603 case tok::kw_short:
4604 case tok::kw_long:
4605 case tok::kw_signed:
4606 case tok::kw_unsigned:
4607 case tok::kw_void:
4608 case tok::kw_char:
4609 case tok::kw_int:
4610 case tok::kw_float:
4611 case tok::kw_double:
4612 case tok::kw_bool:
4613 case tok::kw___pixel:
4614 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4615 return true;
4616 case tok::identifier:
4617 if (Next.getIdentifierInfo() == Ident_pixel) {
4618 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4619 return true;
4620 }
4621 break;
4622 default:
4623 break;
4624 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004625 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004626 DS.isTypeAltiVecVector()) {
4627 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4628 return true;
4629 }
4630 return false;
4631}