blob: 0c5a4879aedbe577446d4562a96fa08a3ff8fd85 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnereaaebc72009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000056
57/// isAttributeLateParsed - Return true if the attribute has arguments that
58/// require late parsing.
59static bool isAttributeLateParsed(const IdentifierInfo &II) {
60 return llvm::StringSwitch<bool>(II.getName())
61#include "clang/Parse/AttrLateParsed.inc"
62 .Default(false);
63}
64
65
Sean Huntbbd37c62009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000067///
68/// [GNU] attributes:
69/// attribute
70/// attributes attribute
71///
72/// [GNU] attribute:
73/// '__attribute__' '(' '(' attribute-list ')' ')'
74///
75/// [GNU] attribute-list:
76/// attrib
77/// attribute_list ',' attrib
78///
79/// [GNU] attrib:
80/// empty
81/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
85///
86/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000091///
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000093/// token lookahead. Comment from gcc: "If they start with an identifier
94/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// start with that identifier; otherwise they are an expression list."
96///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000097/// GCC does not require the ',' between attribs in an attribute-list.
98///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
103
John McCall7f040a92010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000114 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall7f040a92010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000192 IdentifierInfo *ParmName = 0;
193 SourceLocation ParmLoc;
194 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 switch (Tok.getKind()) {
197 case tok::kw_char:
198 case tok::kw_wchar_t:
199 case tok::kw_char16_t:
200 case tok::kw_char32_t:
201 case tok::kw_bool:
202 case tok::kw_short:
203 case tok::kw_int:
204 case tok::kw_long:
205 case tok::kw___int64:
206 case tok::kw_signed:
207 case tok::kw_unsigned:
208 case tok::kw_float:
209 case tok::kw_double:
210 case tok::kw_void:
211 case tok::kw_typeof:
212 // __attribute__(( vec_type_hint(char) ))
213 // FIXME: Don't just discard the builtin type token.
214 ConsumeToken();
215 BuiltinType = true;
216 break;
217
218 case tok::identifier:
219 ParmName = Tok.getIdentifierInfo();
220 ParmLoc = ConsumeToken();
221 break;
222
223 default:
224 break;
225 }
226
227 ExprVector ArgExprs(Actions);
228
229 if (!BuiltinType &&
230 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
231 // Eat the comma.
232 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000233 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000235 // Parse the non-empty comma-separated list of expressions.
236 while (1) {
237 ExprResult ArgExpr(ParseAssignmentExpression());
238 if (ArgExpr.isInvalid()) {
239 SkipUntil(tok::r_paren);
240 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000242 ArgExprs.push_back(ArgExpr.release());
243 if (Tok.isNot(tok::comma))
244 break;
245 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000248 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
249 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
250 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000251 while (Tok.is(tok::identifier)) {
252 ConsumeToken();
253 if (Tok.is(tok::greater))
254 break;
255 if (Tok.is(tok::comma)) {
256 ConsumeToken();
257 continue;
258 }
259 }
260 if (Tok.isNot(tok::greater))
261 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000262 SkipUntil(tok::r_paren, false, true); // skip until ')'
263 }
264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265
266 SourceLocation RParen = Tok.getLocation();
267 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
268 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000269 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
271 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
272 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000273 }
274}
275
276
Eli Friedmana23b4852009-06-08 07:21:15 +0000277/// ParseMicrosoftDeclSpec - Parse an __declspec construct
278///
279/// [MS] decl-specifier:
280/// __declspec ( extended-decl-modifier-seq )
281///
282/// [MS] extended-decl-modifier-seq:
283/// extended-decl-modifier[opt]
284/// extended-decl-modifier extended-decl-modifier-seq
285
John McCall7f040a92010-12-24 02:08:15 +0000286void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000287 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000288
Steve Narofff59e17e2008-12-24 20:59:21 +0000289 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000290 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
291 "declspec")) {
292 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000293 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 }
Francois Pichet373197b2011-05-07 19:04:49 +0000295
Eli Friedman290eeb02009-06-08 23:27:34 +0000296 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000299
300 // FIXME: Remove this when we have proper __declspec(property()) support.
301 // Just skip everything inside property().
302 if (AttrName->getName() == "property") {
303 ConsumeParen();
304 SkipUntil(tok::r_paren);
305 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000306 if (Tok.is(tok::l_paren)) {
307 ConsumeParen();
308 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
309 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000310 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000312 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000313 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
314 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 }
316 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
317 SkipUntil(tok::r_paren, false);
318 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
320 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000321 }
322 }
323 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
324 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000325 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000326}
327
John McCall7f040a92010-12-24 02:08:15 +0000328void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000329 // Treat these like attributes
330 // FIXME: Allow Sema to distinguish between these and real attributes!
331 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000332 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000333 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000334 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000335 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000336 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
337 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000338 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
339 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000340 // FIXME: Support these properly!
341 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000345}
346
John McCall7f040a92010-12-24 02:08:15 +0000347void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000348 // Treat these like attributes
349 while (Tok.is(tok::kw___pascal)) {
350 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
351 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000352 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 }
John McCall7f040a92010-12-24 02:08:15 +0000355}
356
Peter Collingbournef315fa82011-02-14 01:42:53 +0000357void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
358 // Treat these like attributes
359 while (Tok.is(tok::kw___kernel)) {
360 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000361 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
362 AttrNameLoc, 0, AttrNameLoc, 0,
363 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000364 }
365}
366
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000367void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
368 SourceLocation Loc = Tok.getLocation();
369 switch(Tok.getKind()) {
370 // OpenCL qualifiers:
371 case tok::kw___private:
372 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413 default: break;
414 }
415}
416
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000417/// \brief Parse a version number.
418///
419/// version:
420/// simple-integer
421/// simple-integer ',' simple-integer
422/// simple-integer ',' simple-integer ',' simple-integer
423VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
424 Range = Tok.getLocation();
425
426 if (!Tok.is(tok::numeric_constant)) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 // Parse the major (and possibly minor and subminor) versions, which
433 // are stored in the numeric constant. We utilize a quirk of the
434 // lexer, which is that it handles something like 1.2.3 as a single
435 // numeric constant, rather than two separate tokens.
436 llvm::SmallString<512> Buffer;
437 Buffer.resize(Tok.getLength()+1);
438 const char *ThisTokBegin = &Buffer[0];
439
440 // Get the spelling of the token, which eliminates trigraphs, etc.
441 bool Invalid = false;
442 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
443 if (Invalid)
444 return VersionTuple();
445
446 // Parse the major version.
447 unsigned AfterMajor = 0;
448 unsigned Major = 0;
449 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
450 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
451 ++AfterMajor;
452 }
453
454 if (AfterMajor == 0) {
455 Diag(Tok, diag::err_expected_version);
456 SkipUntil(tok::comma, tok::r_paren, true, true, true);
457 return VersionTuple();
458 }
459
460 if (AfterMajor == ActualLength) {
461 ConsumeToken();
462
463 // We only had a single version component.
464 if (Major == 0) {
465 Diag(Tok, diag::err_zero_version);
466 return VersionTuple();
467 }
468
469 return VersionTuple(Major);
470 }
471
472 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 // Parse the minor version.
479 unsigned AfterMinor = AfterMajor + 1;
480 unsigned Minor = 0;
481 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
482 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
483 ++AfterMinor;
484 }
485
486 if (AfterMinor == ActualLength) {
487 ConsumeToken();
488
489 // We had major.minor.
490 if (Major == 0 && Minor == 0) {
491 Diag(Tok, diag::err_zero_version);
492 return VersionTuple();
493 }
494
495 return VersionTuple(Major, Minor);
496 }
497
498 // If what follows is not a '.', we have a problem.
499 if (ThisTokBegin[AfterMinor] != '.') {
500 Diag(Tok, diag::err_expected_version);
501 SkipUntil(tok::comma, tok::r_paren, true, true, true);
502 return VersionTuple();
503 }
504
505 // Parse the subminor version.
506 unsigned AfterSubminor = AfterMinor + 1;
507 unsigned Subminor = 0;
508 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
509 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
510 ++AfterSubminor;
511 }
512
513 if (AfterSubminor != ActualLength) {
514 Diag(Tok, diag::err_expected_version);
515 SkipUntil(tok::comma, tok::r_paren, true, true, true);
516 return VersionTuple();
517 }
518 ConsumeToken();
519 return VersionTuple(Major, Minor, Subminor);
520}
521
522/// \brief Parse the contents of the "availability" attribute.
523///
524/// availability-attribute:
525/// 'availability' '(' platform ',' version-arg-list ')'
526///
527/// platform:
528/// identifier
529///
530/// version-arg-list:
531/// version-arg
532/// version-arg ',' version-arg-list
533///
534/// version-arg:
535/// 'introduced' '=' version
536/// 'deprecated' '=' version
537/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000538/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
540 SourceLocation AvailabilityLoc,
541 ParsedAttributes &attrs,
542 SourceLocation *endLoc) {
543 SourceLocation PlatformLoc;
544 IdentifierInfo *Platform = 0;
545
546 enum { Introduced, Deprecated, Obsoleted, Unknown };
547 AvailabilityChange Changes[Unknown];
548
549 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
551 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000552 Diag(Tok, diag::err_expected_lparen);
553 return;
554 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555
556 // Parse the platform name,
557 if (Tok.isNot(tok::identifier)) {
558 Diag(Tok, diag::err_availability_expected_platform);
559 SkipUntil(tok::r_paren);
560 return;
561 }
562 Platform = Tok.getIdentifierInfo();
563 PlatformLoc = ConsumeToken();
564
565 // Parse the ',' following the platform name.
566 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
567 return;
568
569 // If we haven't grabbed the pointers for the identifiers
570 // "introduced", "deprecated", and "obsoleted", do so now.
571 if (!Ident_introduced) {
572 Ident_introduced = PP.getIdentifierInfo("introduced");
573 Ident_deprecated = PP.getIdentifierInfo("deprecated");
574 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000575 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000576 }
577
578 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000579 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000580 do {
581 if (Tok.isNot(tok::identifier)) {
582 Diag(Tok, diag::err_availability_expected_change);
583 SkipUntil(tok::r_paren);
584 return;
585 }
586 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
587 SourceLocation KeywordLoc = ConsumeToken();
588
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 if (Keyword == Ident_unavailable) {
590 if (UnavailableLoc.isValid()) {
591 Diag(KeywordLoc, diag::err_availability_redundant)
592 << Keyword << SourceRange(UnavailableLoc);
593 }
594 UnavailableLoc = KeywordLoc;
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 continue;
601 }
602
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000603 if (Tok.isNot(tok::equal)) {
604 Diag(Tok, diag::err_expected_equal_after)
605 << Keyword;
606 SkipUntil(tok::r_paren);
607 return;
608 }
609 ConsumeToken();
610
611 SourceRange VersionRange;
612 VersionTuple Version = ParseVersionTuple(VersionRange);
613
614 if (Version.empty()) {
615 SkipUntil(tok::r_paren);
616 return;
617 }
618
619 unsigned Index;
620 if (Keyword == Ident_introduced)
621 Index = Introduced;
622 else if (Keyword == Ident_deprecated)
623 Index = Deprecated;
624 else if (Keyword == Ident_obsoleted)
625 Index = Obsoleted;
626 else
627 Index = Unknown;
628
629 if (Index < Unknown) {
630 if (!Changes[Index].KeywordLoc.isInvalid()) {
631 Diag(KeywordLoc, diag::err_availability_redundant)
632 << Keyword
633 << SourceRange(Changes[Index].KeywordLoc,
634 Changes[Index].VersionRange.getEnd());
635 }
636
637 Changes[Index].KeywordLoc = KeywordLoc;
638 Changes[Index].Version = Version;
639 Changes[Index].VersionRange = VersionRange;
640 } else {
641 Diag(KeywordLoc, diag::err_availability_unknown_change)
642 << Keyword << VersionRange;
643 }
644
645 if (Tok.isNot(tok::comma))
646 break;
647
648 ConsumeToken();
649 } while (true);
650
651 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000653 return;
654
655 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000656 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000657
Douglas Gregorb53e4172011-03-26 03:35:55 +0000658 // The 'unavailable' availability cannot be combined with any other
659 // availability changes. Make sure that hasn't happened.
660 if (UnavailableLoc.isValid()) {
661 bool Complained = false;
662 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
663 if (Changes[Index].KeywordLoc.isValid()) {
664 if (!Complained) {
665 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
666 << SourceRange(Changes[Index].KeywordLoc,
667 Changes[Index].VersionRange.getEnd());
668 Complained = true;
669 }
670
671 // Clear out the availability.
672 Changes[Index] = AvailabilityChange();
673 }
674 }
675 }
676
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000678 attrs.addNew(&Availability,
679 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000680 0, SourceLocation(),
681 Platform, PlatformLoc,
682 Changes[Introduced],
683 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000684 Changes[Obsoleted],
685 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686}
687
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000688
689// Late Parsed Attributes:
690// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
691
692void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
693
694void Parser::LateParsedClass::ParseLexedAttributes() {
695 Self->ParseLexedAttributes(*Class);
696}
697
698void Parser::LateParsedAttribute::ParseLexedAttributes() {
699 Self->ParseLexedAttribute(*this);
700}
701
702/// Wrapper class which calls ParseLexedAttribute, after setting up the
703/// scope appropriately.
704void Parser::ParseLexedAttributes(ParsingClass &Class) {
705 // Deal with templates
706 // FIXME: Test cases to make sure this does the right thing for templates.
707 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
708 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
709 HasTemplateScope);
710 if (HasTemplateScope)
711 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
712
713 // Set or update the scope flags to include Scope::ThisScope.
714 bool AlreadyHasClassScope = Class.TopLevelClass;
715 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
716 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
717 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
718
719 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
720 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
721 }
722}
723
724/// \brief Finish parsing an attribute for which parsing was delayed.
725/// This will be called at the end of parsing a class declaration
726/// for each LateParsedAttribute. We consume the saved tokens and
727/// create an attribute with the arguments filled in. We add this
728/// to the Attribute list for the decl.
729void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
730 // Save the current token position.
731 SourceLocation OrigLoc = Tok.getLocation();
732
733 // Append the current token at the end of the new token stream so that it
734 // doesn't get lost.
735 LA.Toks.push_back(Tok);
736 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
737 // Consume the previously pushed token.
738 ConsumeAnyToken();
739
740 ParsedAttributes Attrs(AttrFactory);
741 SourceLocation endLoc;
742
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000743 // If the Decl is templatized, add template parameters to scope.
744 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
745 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
746 if (HasTemplateScope)
747 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
748
749 // If the Decl is on a function, add function parameters to the scope.
750 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
751 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
752 if (HasFunctionScope)
753 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
754
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000755 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
756
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000757 if (HasFunctionScope) {
758 Actions.ActOnExitFunctionContext();
759 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
760 }
761 if (HasTemplateScope) {
762 TempScope.Exit();
763 }
764
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000765 // Late parsed attributes must be attached to Decls by hand. If the
766 // LA.D is not set, then this was not done properly.
767 assert(LA.D && "No decl attached to late parsed attribute");
768 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
769
770 if (Tok.getLocation() != OrigLoc) {
771 // Due to a parsing error, we either went over the cached tokens or
772 // there are still cached tokens left, so we skip the leftover tokens.
773 // Since this is an uncommon situation that should be avoided, use the
774 // expensive isBeforeInTranslationUnit call.
775 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
776 OrigLoc))
777 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
778 ConsumeAnyToken();
779 }
780}
781
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000782/// \brief Wrapper around a case statement checking if AttrName is
783/// one of the thread safety attributes
784bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
785 return llvm::StringSwitch<bool>(AttrName)
786 .Case("guarded_by", true)
787 .Case("guarded_var", true)
788 .Case("pt_guarded_by", true)
789 .Case("pt_guarded_var", true)
790 .Case("lockable", true)
791 .Case("scoped_lockable", true)
792 .Case("no_thread_safety_analysis", true)
793 .Case("acquired_after", true)
794 .Case("acquired_before", true)
795 .Case("exclusive_lock_function", true)
796 .Case("shared_lock_function", true)
797 .Case("exclusive_trylock_function", true)
798 .Case("shared_trylock_function", true)
799 .Case("unlock_function", true)
800 .Case("lock_returned", true)
801 .Case("locks_excluded", true)
802 .Case("exclusive_locks_required", true)
803 .Case("shared_locks_required", true)
804 .Default(false);
805}
806
807/// \brief Parse the contents of thread safety attributes. These
808/// should always be parsed as an expression list.
809///
810/// We need to special case the parsing due to the fact that if the first token
811/// of the first argument is an identifier, the main parse loop will store
812/// that token as a "parameter" and the rest of
813/// the arguments will be added to a list of "arguments". However,
814/// subsequent tokens in the first argument are lost. We instead parse each
815/// argument as an expression and add all arguments to the list of "arguments".
816/// In future, we will take advantage of this special case to also
817/// deal with some argument scoping issues here (for example, referring to a
818/// function parameter in the attribute on that function).
819void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
820 SourceLocation AttrNameLoc,
821 ParsedAttributes &Attrs,
822 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000823 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000824
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000825 BalancedDelimiterTracker T(*this, tok::l_paren);
826 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827
828 ExprVector ArgExprs(Actions);
829 bool ArgExprsOk = true;
830
831 // now parse the list of expressions
832 while (1) {
833 ExprResult ArgExpr(ParseAssignmentExpression());
834 if (ArgExpr.isInvalid()) {
835 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000836 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 break;
838 } else {
839 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 if (Tok.isNot(tok::comma))
842 break;
843 ConsumeToken(); // Eat the comma, move to the next argument
844 }
845 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000846 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000847 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
848 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000850 if (EndLoc)
851 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000852}
853
John McCall7f040a92010-12-24 02:08:15 +0000854void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
855 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
856 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000857}
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859/// ParseDeclaration - Parse a full 'declaration', which consists of
860/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000861/// 'Context' should be a Declarator::TheContext value. This returns the
862/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000863///
864/// declaration: [C99 6.7]
865/// block-declaration ->
866/// simple-declaration
867/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000868/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000869/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000870/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000871/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000872/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000873/// others... [FIXME]
874///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000875Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
876 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000877 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000878 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000879 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000880 // Must temporarily exit the objective-c container scope for
881 // parsing c none objective-c decls.
882 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000883
John McCalld226f652010-08-21 09:40:31 +0000884 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000885 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000886 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000887 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000888 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000889 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000890 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000891 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000892 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000893 // Could be the start of an inline namespace. Allowed as an ext in C++03.
894 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000895 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000896 SourceLocation InlineLoc = ConsumeToken();
897 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
898 break;
899 }
John McCall7f040a92010-12-24 02:08:15 +0000900 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000901 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000902 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000904 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000905 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000906 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000907 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000908 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000909 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000910 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000911 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000912 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000913 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000914 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000915 default:
John McCall7f040a92010-12-24 02:08:15 +0000916 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000917 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000918
Chris Lattner682bf922009-03-29 16:50:03 +0000919 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000920 // single decl, convert it now. Alias declarations can also declare a type;
921 // include that too if it is present.
922 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000923}
924
925/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
926/// declaration-specifiers init-declarator-list[opt] ';'
927///[C90/C++]init-declarator-list ';' [TODO]
928/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000929///
Richard Smithad762fc2011-04-14 22:09:26 +0000930/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
931/// attribute-specifier-seq[opt] type-specifier-seq declarator
932///
Chris Lattnercd147752009-03-29 17:27:48 +0000933/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000934/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000935///
936/// If FRI is non-null, we might be parsing a for-range-declaration instead
937/// of a simple-declaration. If we find that we are, we also parse the
938/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000939Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
940 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000941 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000942 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000943 bool RequireSemi,
944 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000946 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000947 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000948
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000949 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000950 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000951 StmtResult R = Actions.ActOnVlaStmt(DS);
952 if (R.isUsable())
953 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
956 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000957 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000958 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000959 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000960 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000961 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000962 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000964
965 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000966}
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Richard Smith0706df42011-10-19 21:33:05 +0000968/// Returns true if this might be the start of a declarator, or a common typo
969/// for a declarator.
970bool Parser::MightBeDeclarator(unsigned Context) {
971 switch (Tok.getKind()) {
972 case tok::annot_cxxscope:
973 case tok::annot_template_id:
974 case tok::caret:
975 case tok::code_completion:
976 case tok::coloncolon:
977 case tok::ellipsis:
978 case tok::kw___attribute:
979 case tok::kw_operator:
980 case tok::l_paren:
981 case tok::star:
982 return true;
983
984 case tok::amp:
985 case tok::ampamp:
986 case tok::colon: // Might be a typo for '::'.
987 return getLang().CPlusPlus;
988
989 case tok::identifier:
990 switch (NextToken().getKind()) {
991 case tok::code_completion:
992 case tok::coloncolon:
993 case tok::comma:
994 case tok::equal:
995 case tok::equalequal: // Might be a typo for '='.
996 case tok::kw_alignas:
997 case tok::kw_asm:
998 case tok::kw___attribute:
999 case tok::l_brace:
1000 case tok::l_paren:
1001 case tok::l_square:
1002 case tok::less:
1003 case tok::r_brace:
1004 case tok::r_paren:
1005 case tok::r_square:
1006 case tok::semi:
1007 return true;
1008
1009 case tok::colon:
1010 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
1011 // and in block scope it's probably a label.
1012 return getLang().CPlusPlus && Context == Declarator::FileContext;
1013
1014 default:
1015 return false;
1016 }
1017
1018 default:
1019 return false;
1020 }
1021}
1022
John McCalld8ac0572009-11-03 19:26:08 +00001023/// ParseDeclGroup - Having concluded that this is either a function
1024/// definition or a group of object declarations, actually parse the
1025/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001026Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1027 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001028 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001029 SourceLocation *DeclEnd,
1030 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001031 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001032 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001033 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001034
John McCalld8ac0572009-11-03 19:26:08 +00001035 // Bail out if the first declarator didn't seem well-formed.
1036 if (!D.hasName() && !D.mayOmitIdentifier()) {
1037 // Skip until ; or }.
1038 SkipUntil(tok::r_brace, true, true);
1039 if (Tok.is(tok::semi))
1040 ConsumeToken();
1041 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattnerc82daef2010-07-11 22:24:20 +00001044 // Check to see if we have a function *definition* which must have a body.
1045 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1046 // Look at the next token to make sure that this isn't a function
1047 // declaration. We have to check this because __attribute__ might be the
1048 // start of a function definition in GCC-extended K&R C.
1049 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001050
Chris Lattner004659a2010-07-11 22:42:07 +00001051 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001052 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1053 Diag(Tok, diag::err_function_declared_typedef);
1054
1055 // Recover by treating the 'typedef' as spurious.
1056 DS.ClearStorageClassSpecs();
1057 }
1058
John McCalld226f652010-08-21 09:40:31 +00001059 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001060 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001061 }
1062
1063 if (isDeclarationSpecifier()) {
1064 // If there is an invalid declaration specifier right after the function
1065 // prototype, then we must be in a missing semicolon case where this isn't
1066 // actually a body. Just fall through into the code that handles it as a
1067 // prototype, and let the top-level code handle the erroneous declspec
1068 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001069 } else {
1070 Diag(Tok, diag::err_expected_fn_body);
1071 SkipUntil(tok::semi);
1072 return DeclGroupPtrTy();
1073 }
1074 }
1075
Richard Smithad762fc2011-04-14 22:09:26 +00001076 if (ParseAttributesAfterDeclarator(D))
1077 return DeclGroupPtrTy();
1078
1079 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1080 // must parse and analyze the for-range-initializer before the declaration is
1081 // analyzed.
1082 if (FRI && Tok.is(tok::colon)) {
1083 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001084 if (Tok.is(tok::l_brace))
1085 FRI->RangeExpr = ParseBraceInitializer();
1086 else
1087 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001088 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1089 Actions.ActOnCXXForRangeDecl(ThisDecl);
1090 Actions.FinalizeDeclaration(ThisDecl);
1091 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1092 }
1093
Chris Lattner5f9e2722011-07-23 10:55:15 +00001094 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001095 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001096 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001097 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001098 DeclsInGroup.push_back(FirstDecl);
1099
Richard Smith0706df42011-10-19 21:33:05 +00001100 bool ExpectSemi = Context != Declarator::ForContext;
1101
John McCalld8ac0572009-11-03 19:26:08 +00001102 // If we don't have a comma, it is either the end of the list (a ';') or an
1103 // error, bail out.
1104 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001105 SourceLocation CommaLoc = ConsumeToken();
1106
1107 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1108 // This comma was followed by a line-break and something which can't be
1109 // the start of a declarator. The comma was probably a typo for a
1110 // semicolon.
1111 Diag(CommaLoc, diag::err_expected_semi_declaration)
1112 << FixItHint::CreateReplacement(CommaLoc, ";");
1113 ExpectSemi = false;
1114 break;
1115 }
John McCalld8ac0572009-11-03 19:26:08 +00001116
1117 // Parse the next declarator.
1118 D.clear();
1119
1120 // Accept attributes in an init-declarator. In the first declarator in a
1121 // declaration, these would be part of the declspec. In subsequent
1122 // declarators, they become part of the declarator itself, so that they
1123 // don't apply to declarators after *this* one. Examples:
1124 // short __attribute__((common)) var; -> declspec
1125 // short var __attribute__((common)); -> declarator
1126 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001127 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001128
1129 ParseDeclarator(D);
1130
John McCalld226f652010-08-21 09:40:31 +00001131 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001132 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001133 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001134 DeclsInGroup.push_back(ThisDecl);
1135 }
1136
1137 if (DeclEnd)
1138 *DeclEnd = Tok.getLocation();
1139
Richard Smith0706df42011-10-19 21:33:05 +00001140 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001141 ExpectAndConsume(tok::semi,
1142 Context == Declarator::FileContext
1143 ? diag::err_invalid_token_after_toplevel_declarator
1144 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001145 // Okay, there was no semicolon and one was expected. If we see a
1146 // declaration specifier, just assume it was missing and continue parsing.
1147 // Otherwise things are very confused and we skip to recover.
1148 if (!isDeclarationSpecifier()) {
1149 SkipUntil(tok::r_brace, true, true);
1150 if (Tok.is(tok::semi))
1151 ConsumeToken();
1152 }
John McCalld8ac0572009-11-03 19:26:08 +00001153 }
1154
Douglas Gregor23c94db2010-07-02 17:43:08 +00001155 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001156 DeclsInGroup.data(),
1157 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001158}
1159
Richard Smithad762fc2011-04-14 22:09:26 +00001160/// Parse an optional simple-asm-expr and attributes, and attach them to a
1161/// declarator. Returns true on an error.
1162bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1163 // If a simple-asm-expr is present, parse it.
1164 if (Tok.is(tok::kw_asm)) {
1165 SourceLocation Loc;
1166 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1167 if (AsmLabel.isInvalid()) {
1168 SkipUntil(tok::semi, true, true);
1169 return true;
1170 }
1171
1172 D.setAsmLabel(AsmLabel.release());
1173 D.SetRangeEnd(Loc);
1174 }
1175
1176 MaybeParseGNUAttributes(D);
1177 return false;
1178}
1179
Douglas Gregor1426e532009-05-12 21:31:51 +00001180/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1181/// declarator'. This method parses the remainder of the declaration
1182/// (including any attributes or initializer, among other things) and
1183/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001184///
Reid Spencer5f016e22007-07-11 17:01:13 +00001185/// init-declarator: [C99 6.7]
1186/// declarator
1187/// declarator '=' initializer
1188/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1189/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001190/// [C++] declarator initializer[opt]
1191///
1192/// [C++] initializer:
1193/// [C++] '=' initializer-clause
1194/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001195/// [C++0x] '=' 'default' [TODO]
1196/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001197/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001198///
1199/// According to the standard grammar, =default and =delete are function
1200/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001201///
John McCalld226f652010-08-21 09:40:31 +00001202Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001203 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001204 if (ParseAttributesAfterDeclarator(D))
1205 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Richard Smithad762fc2011-04-14 22:09:26 +00001207 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1208}
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Richard Smithad762fc2011-04-14 22:09:26 +00001210Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1211 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001212 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001213 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001214 switch (TemplateInfo.Kind) {
1215 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001216 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001217 break;
1218
1219 case ParsedTemplateInfo::Template:
1220 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001221 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001222 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001223 TemplateInfo.TemplateParams->data(),
1224 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001225 D);
1226 break;
1227
1228 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001229 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001231 TemplateInfo.ExternLoc,
1232 TemplateInfo.TemplateLoc,
1233 D);
1234 if (ThisRes.isInvalid()) {
1235 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001236 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001237 }
1238
1239 ThisDecl = ThisRes.get();
1240 break;
1241 }
1242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Richard Smith34b41d92011-02-20 03:19:35 +00001244 bool TypeContainsAuto =
1245 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1246
Douglas Gregor1426e532009-05-12 21:31:51 +00001247 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001248 if (isTokenEqualOrMistypedEqualEqual(
1249 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001250 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001251 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001252 if (D.isFunctionDeclarator())
1253 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1254 << 1 /* delete */;
1255 else
1256 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001257 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001258 if (D.isFunctionDeclarator())
1259 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1260 << 1 /* delete */;
1261 else
1262 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001263 } else {
John McCall731ad842009-12-19 09:28:58 +00001264 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1265 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001266 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001267 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001268
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001269 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001270 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001271 cutOffParsing();
1272 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001273 }
1274
John McCall60d7b3a2010-08-24 06:29:42 +00001275 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001276
John McCall731ad842009-12-19 09:28:58 +00001277 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001278 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001279 ExitScope();
1280 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001281
Douglas Gregor1426e532009-05-12 21:31:51 +00001282 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001283 SkipUntil(tok::comma, true, true);
1284 Actions.ActOnInitializerError(ThisDecl);
1285 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001286 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1287 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001288 }
1289 } else if (Tok.is(tok::l_paren)) {
1290 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001291 BalancedDelimiterTracker T(*this, tok::l_paren);
1292 T.consumeOpen();
1293
Douglas Gregor1426e532009-05-12 21:31:51 +00001294 ExprVector Exprs(Actions);
1295 CommaLocsTy CommaLocs;
1296
Douglas Gregorb4debae2009-12-22 17:47:17 +00001297 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1298 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001299 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001300 }
1301
Douglas Gregor1426e532009-05-12 21:31:51 +00001302 if (ParseExpressionList(Exprs, CommaLocs)) {
1303 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001304
1305 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001306 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001307 ExitScope();
1308 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001309 } else {
1310 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001311 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001312
1313 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1314 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001315
1316 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001317 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001318 ExitScope();
1319 }
1320
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001321 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001322 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001323 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001324 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001325 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001326 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1327 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001328 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1329
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001330 if (D.getCXXScopeSpec().isSet()) {
1331 EnterScope(0);
1332 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1333 }
1334
1335 ExprResult Init(ParseBraceInitializer());
1336
1337 if (D.getCXXScopeSpec().isSet()) {
1338 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1339 ExitScope();
1340 }
1341
1342 if (Init.isInvalid()) {
1343 Actions.ActOnInitializerError(ThisDecl);
1344 } else
1345 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1346 /*DirectInit=*/true, TypeContainsAuto);
1347
Douglas Gregor1426e532009-05-12 21:31:51 +00001348 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001349 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001350 }
1351
Richard Smith483b9f32011-02-21 20:05:19 +00001352 Actions.FinalizeDeclaration(ThisDecl);
1353
Douglas Gregor1426e532009-05-12 21:31:51 +00001354 return ThisDecl;
1355}
1356
Reid Spencer5f016e22007-07-11 17:01:13 +00001357/// ParseSpecifierQualifierList
1358/// specifier-qualifier-list:
1359/// type-specifier specifier-qualifier-list[opt]
1360/// type-qualifier specifier-qualifier-list[opt]
1361/// [GNU] attributes specifier-qualifier-list[opt]
1362///
Richard Smithc89edf52011-07-01 19:46:12 +00001363void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1365 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001366 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001367 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 // Validate declspec for type-name.
1370 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001371 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001372 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 // Issue diagnostic and remove storage class if present.
1376 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1377 if (DS.getStorageClassSpecLoc().isValid())
1378 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1379 else
1380 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1381 DS.ClearStorageClassSpecs();
1382 }
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // Issue diagnostic and remove function specfier if present.
1385 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001386 if (DS.isInlineSpecified())
1387 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1388 if (DS.isVirtualSpecified())
1389 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1390 if (DS.isExplicitSpecified())
1391 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 DS.ClearFunctionSpecs();
1393 }
1394}
1395
Chris Lattnerc199ab32009-04-12 20:42:31 +00001396/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1397/// specified token is valid after the identifier in a declarator which
1398/// immediately follows the declspec. For example, these things are valid:
1399///
1400/// int x [ 4]; // direct-declarator
1401/// int x ( int y); // direct-declarator
1402/// int(int x ) // direct-declarator
1403/// int x ; // simple-declaration
1404/// int x = 17; // init-declarator-list
1405/// int x , y; // init-declarator-list
1406/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001407/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001408/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001409///
1410/// This is not, because 'x' does not immediately follow the declspec (though
1411/// ')' happens to be valid anyway).
1412/// int (x)
1413///
1414static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1415 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1416 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001417 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001418}
1419
Chris Lattnere40c2952009-04-14 21:34:55 +00001420
1421/// ParseImplicitInt - This method is called when we have an non-typename
1422/// identifier in a declspec (which normally terminates the decl spec) when
1423/// the declspec has no type specifier. In this case, the declspec is either
1424/// malformed or is "implicit int" (in K&R and C89).
1425///
1426/// This method handles diagnosing this prettily and returns false if the
1427/// declspec is done being processed. If it recovers and thinks there may be
1428/// other pieces of declspec after it, it returns true.
1429///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001430bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001431 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001432 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001433 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Chris Lattnere40c2952009-04-14 21:34:55 +00001435 SourceLocation Loc = Tok.getLocation();
1436 // If we see an identifier that is not a type name, we normally would
1437 // parse it as the identifer being declared. However, when a typename
1438 // is typo'd or the definition is not included, this will incorrectly
1439 // parse the typename as the identifier name and fall over misparsing
1440 // later parts of the diagnostic.
1441 //
1442 // As such, we try to do some look-ahead in cases where this would
1443 // otherwise be an "implicit-int" case to see if this is invalid. For
1444 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1445 // an identifier with implicit int, we'd get a parse error because the
1446 // next token is obviously invalid for a type. Parse these as a case
1447 // with an invalid type specifier.
1448 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Chris Lattnere40c2952009-04-14 21:34:55 +00001450 // Since we know that this either implicit int (which is rare) or an
1451 // error, we'd do lookahead to try to do better recovery.
1452 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1453 // If this token is valid for implicit int, e.g. "static x = 4", then
1454 // we just avoid eating the identifier, so it will be parsed as the
1455 // identifier in the declarator.
1456 return false;
1457 }
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Chris Lattnere40c2952009-04-14 21:34:55 +00001459 // Otherwise, if we don't consume this token, we are going to emit an
1460 // error anyway. Try to recover from various common problems. Check
1461 // to see if this was a reference to a tag name without a tag specified.
1462 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001463 //
1464 // C++ doesn't need this, and isTagName doesn't take SS.
1465 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001466 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001467 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Douglas Gregor23c94db2010-07-02 17:43:08 +00001469 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001470 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001471 case DeclSpec::TST_enum:
1472 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1473 case DeclSpec::TST_union:
1474 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1475 case DeclSpec::TST_struct:
1476 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1477 case DeclSpec::TST_class:
1478 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Chris Lattnerf4382f52009-04-14 22:17:06 +00001481 if (TagName) {
1482 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001483 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001484 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Chris Lattnerf4382f52009-04-14 22:17:06 +00001486 // Parse this as a tag as if the missing tag were present.
1487 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001488 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001489 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001490 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001491 return true;
1492 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregora786fdb2009-10-13 23:27:22 +00001495 // This is almost certainly an invalid type name. Let the action emit a
1496 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001497 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001498 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001499 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001500 // The action emitted a diagnostic, so we don't have to.
1501 if (T) {
1502 // The action has suggested that the type T could be used. Set that as
1503 // the type in the declaration specifiers, consume the would-be type
1504 // name token, and we're done.
1505 const char *PrevSpec;
1506 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001507 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001508 DS.SetRangeEnd(Tok.getLocation());
1509 ConsumeToken();
1510
1511 // There may be other declaration specifiers after this.
1512 return true;
1513 }
1514
1515 // Fall through; the action had no suggestion for us.
1516 } else {
1517 // The action did not emit a diagnostic, so emit one now.
1518 SourceRange R;
1519 if (SS) R = SS->getRange();
1520 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Douglas Gregora786fdb2009-10-13 23:27:22 +00001523 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001524 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001525 unsigned DiagID;
1526 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001527 DS.SetRangeEnd(Tok.getLocation());
1528 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattnere40c2952009-04-14 21:34:55 +00001530 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1531 // avoid rippling error messages on subsequent uses of the same type,
1532 // could be useful if #include was forgotten.
1533 return false;
1534}
1535
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001536/// \brief Determine the declaration specifier context from the declarator
1537/// context.
1538///
1539/// \param Context the declarator context, which is one of the
1540/// Declarator::TheContext enumerator values.
1541Parser::DeclSpecContext
1542Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1543 if (Context == Declarator::MemberContext)
1544 return DSC_class;
1545 if (Context == Declarator::FileContext)
1546 return DSC_top_level;
1547 return DSC_normal;
1548}
1549
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001550/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1551///
1552/// FIXME: Simply returns an alignof() expression if the argument is a
1553/// type. Ideally, the type should be propagated directly into Sema.
1554///
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001555/// [C1X] type-id
1556/// [C1X] constant-expression
1557/// [C++0x] type-id ...[opt]
1558/// [C++0x] assignment-expression ...[opt]
1559ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1560 SourceLocation &EllipsisLoc) {
1561 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001562 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001563 SourceLocation TypeLoc = Tok.getLocation();
1564 ParsedType Ty = ParseTypeName().get();
1565 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001566 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1567 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001568 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001569 ER = ParseConstantExpression();
1570
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001571 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1572 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001573
1574 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001575}
1576
1577/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1578/// attribute to Attrs.
1579///
1580/// alignment-specifier:
1581/// [C1X] '_Alignas' '(' type-id ')'
1582/// [C1X] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001583/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1584/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001585void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1586 SourceLocation *endLoc) {
1587 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1588 "Not an alignment-specifier!");
1589
1590 SourceLocation KWLoc = Tok.getLocation();
1591 ConsumeToken();
1592
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001593 BalancedDelimiterTracker T(*this, tok::l_paren);
1594 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001595 return;
1596
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001597 SourceLocation EllipsisLoc;
1598 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001599 if (ArgExpr.isInvalid()) {
1600 SkipUntil(tok::r_paren);
1601 return;
1602 }
1603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001604 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001605 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001606 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001607
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001608 // FIXME: Handle pack-expansions here.
1609 if (EllipsisLoc.isValid()) {
1610 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1611 return;
1612 }
1613
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001614 ExprVector ArgExprs(Actions);
1615 ArgExprs.push_back(ArgExpr.release());
1616 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001617 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001618}
1619
Reid Spencer5f016e22007-07-11 17:01:13 +00001620/// ParseDeclarationSpecifiers
1621/// declaration-specifiers: [C99 6.7]
1622/// storage-class-specifier declaration-specifiers[opt]
1623/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001624/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001625/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001626/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001627/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001628///
1629/// storage-class-specifier: [C99 6.7.1]
1630/// 'typedef'
1631/// 'extern'
1632/// 'static'
1633/// 'auto'
1634/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001635/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001636/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001637/// function-specifier: [C99 6.7.4]
1638/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001639/// [C++] 'virtual'
1640/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001641/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001642/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001643/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001644
Reid Spencer5f016e22007-07-11 17:01:13 +00001645///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001646void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001647 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001648 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001649 DeclSpecContext DSContext) {
1650 if (DS.getSourceRange().isInvalid()) {
1651 DS.SetRangeStart(Tok.getLocation());
1652 DS.SetRangeEnd(Tok.getLocation());
1653 }
1654
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001655 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001657 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001659 unsigned DiagID = 0;
1660
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001662
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001664 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001665 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001666 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1667 MaybeParseCXX0XAttributes(DS.getAttributes());
1668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 // If this is not a declaration specifier token, we're done reading decl
1670 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001671 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001674 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001675 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001676 if (DS.hasTypeSpecifier()) {
1677 bool AllowNonIdentifiers
1678 = (getCurScope()->getFlags() & (Scope::ControlScope |
1679 Scope::BlockScope |
1680 Scope::TemplateParamScope |
1681 Scope::FunctionPrototypeScope |
1682 Scope::AtCatchScope)) == 0;
1683 bool AllowNestedNameSpecifiers
1684 = DSContext == DSC_top_level ||
1685 (DSContext == DSC_class && DS.isFriendSpecified());
1686
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001687 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1688 AllowNonIdentifiers,
1689 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001690 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001691 }
1692
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001693 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1694 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1695 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001696 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1697 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001698 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001699 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001700 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001701 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001702
1703 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001704 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001705 }
1706
Chris Lattner5e02c472009-01-05 00:07:25 +00001707 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001708 // C++ scope specifier. Annotate and loop, or bail out on error.
1709 if (TryAnnotateCXXScopeToken(true)) {
1710 if (!DS.hasTypeSpecifier())
1711 DS.SetTypeSpecError();
1712 goto DoneWithDeclSpec;
1713 }
John McCall2e0a7152010-03-01 18:20:46 +00001714 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1715 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001716 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001717
1718 case tok::annot_cxxscope: {
1719 if (DS.hasTypeSpecifier())
1720 goto DoneWithDeclSpec;
1721
John McCallaa87d332009-12-12 11:40:51 +00001722 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001723 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1724 Tok.getAnnotationRange(),
1725 SS);
John McCallaa87d332009-12-12 11:40:51 +00001726
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001727 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001728 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001729 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001730 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001731 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001732 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001733
1734 // C++ [class.qual]p2:
1735 // In a lookup in which the constructor is an acceptable lookup
1736 // result and the nested-name-specifier nominates a class C:
1737 //
1738 // - if the name specified after the
1739 // nested-name-specifier, when looked up in C, is the
1740 // injected-class-name of C (Clause 9), or
1741 //
1742 // - if the name specified after the nested-name-specifier
1743 // is the same as the identifier or the
1744 // simple-template-id's template-name in the last
1745 // component of the nested-name-specifier,
1746 //
1747 // the name is instead considered to name the constructor of
1748 // class C.
1749 //
1750 // Thus, if the template-name is actually the constructor
1751 // name, then the code is ill-formed; this interpretation is
1752 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001753 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001754 if ((DSContext == DSC_top_level ||
1755 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1756 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001757 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001758 if (isConstructorDeclarator()) {
1759 // The user meant this to be an out-of-line constructor
1760 // definition, but template arguments are not allowed
1761 // there. Just allow this as a constructor; we'll
1762 // complain about it later.
1763 goto DoneWithDeclSpec;
1764 }
1765
1766 // The user meant this to name a type, but it actually names
1767 // a constructor with some extraneous template
1768 // arguments. Complain, then parse it as a type as the user
1769 // intended.
1770 Diag(TemplateId->TemplateNameLoc,
1771 diag::err_out_of_line_template_id_names_constructor)
1772 << TemplateId->Name;
1773 }
1774
John McCallaa87d332009-12-12 11:40:51 +00001775 DS.getTypeSpecScope() = SS;
1776 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001777 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001778 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001779 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001780 continue;
1781 }
1782
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001783 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001784 DS.getTypeSpecScope() = SS;
1785 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001786 if (Tok.getAnnotationValue()) {
1787 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1789 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001790 PrevSpec, DiagID, T);
1791 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001792 else
1793 DS.SetTypeSpecError();
1794 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1795 ConsumeToken(); // The typename
1796 }
1797
Douglas Gregor9135c722009-03-25 15:40:00 +00001798 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001799 goto DoneWithDeclSpec;
1800
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001801 // If we're in a context where the identifier could be a class name,
1802 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001803 if ((DSContext == DSC_top_level ||
1804 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001805 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001806 &SS)) {
1807 if (isConstructorDeclarator())
1808 goto DoneWithDeclSpec;
1809
1810 // As noted in C++ [class.qual]p2 (cited above), when the name
1811 // of the class is qualified in a context where it could name
1812 // a constructor, its a constructor name. However, we've
1813 // looked at the declarator, and the user probably meant this
1814 // to be a type. Complain that it isn't supposed to be treated
1815 // as a type, then proceed to parse it as a type.
1816 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1817 << Next.getIdentifierInfo();
1818 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001819
John McCallb3d87482010-08-24 05:47:05 +00001820 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1821 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001822 getCurScope(), &SS,
1823 false, false, ParsedType(),
1824 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001825
Chris Lattnerf4382f52009-04-14 22:17:06 +00001826 // If the referenced identifier is not a type, then this declspec is
1827 // erroneous: We already checked about that it has no type specifier, and
1828 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001829 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001830 if (TypeRep == 0) {
1831 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001832 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001833 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
John McCallaa87d332009-12-12 11:40:51 +00001836 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001837 ConsumeToken(); // The C++ scope.
1838
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001839 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001840 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001841 if (isInvalid)
1842 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001844 DS.SetRangeEnd(Tok.getLocation());
1845 ConsumeToken(); // The typename.
1846
1847 continue;
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Chris Lattner80d0c892009-01-21 19:48:37 +00001850 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001851 if (Tok.getAnnotationValue()) {
1852 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001854 DiagID, T);
1855 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001856 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001857
1858 if (isInvalid)
1859 break;
1860
Chris Lattner80d0c892009-01-21 19:48:37 +00001861 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1862 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Chris Lattner80d0c892009-01-21 19:48:37 +00001864 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1865 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001866 // Objective-C interface.
1867 if (Tok.is(tok::less) && getLang().ObjC1)
1868 ParseObjCProtocolQualifiers(DS);
1869
Chris Lattner80d0c892009-01-21 19:48:37 +00001870 continue;
1871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Douglas Gregorbfad9152011-04-28 15:48:45 +00001873 case tok::kw___is_signed:
1874 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1875 // typically treats it as a trait. If we see __is_signed as it appears
1876 // in libstdc++, e.g.,
1877 //
1878 // static const bool __is_signed;
1879 //
1880 // then treat __is_signed as an identifier rather than as a keyword.
1881 if (DS.getTypeSpecType() == TST_bool &&
1882 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1883 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1884 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1885 Tok.setKind(tok::identifier);
1886 }
1887
1888 // We're done with the declaration-specifiers.
1889 goto DoneWithDeclSpec;
1890
Chris Lattner3bd934a2008-07-26 01:18:38 +00001891 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001892 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001893 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001894 // In C++, check to see if this is a scope specifier like foo::bar::, if
1895 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001896 if (getLang().CPlusPlus) {
1897 if (TryAnnotateCXXScopeToken(true)) {
1898 if (!DS.hasTypeSpecifier())
1899 DS.SetTypeSpecError();
1900 goto DoneWithDeclSpec;
1901 }
1902 if (!Tok.is(tok::identifier))
1903 continue;
1904 }
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Chris Lattner3bd934a2008-07-26 01:18:38 +00001906 // This identifier can only be a typedef name if we haven't already seen
1907 // a type-specifier. Without this check we misparse:
1908 // typedef int X; struct Y { short X; }; as 'short int'.
1909 if (DS.hasTypeSpecifier())
1910 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001911
John Thompson82287d12010-02-05 00:12:22 +00001912 // Check for need to substitute AltiVec keyword tokens.
1913 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1914 break;
1915
Chris Lattner3bd934a2008-07-26 01:18:38 +00001916 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001917 ParsedType TypeRep =
1918 Actions.getTypeName(*Tok.getIdentifierInfo(),
1919 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001920
Chris Lattnerc199ab32009-04-12 20:42:31 +00001921 // If this is not a typedef name, don't parse it as part of the declspec,
1922 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001923 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001924 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001925 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001926 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001927
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001928 // If we're in a context where the identifier could be a class name,
1929 // check whether this is a constructor declaration.
1930 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001932 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001933 goto DoneWithDeclSpec;
1934
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001936 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001937 if (isInvalid)
1938 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Chris Lattner3bd934a2008-07-26 01:18:38 +00001940 DS.SetRangeEnd(Tok.getLocation());
1941 ConsumeToken(); // The identifier
1942
1943 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1944 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001945 // Objective-C interface.
1946 if (Tok.is(tok::less) && getLang().ObjC1)
1947 ParseObjCProtocolQualifiers(DS);
1948
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001949 // Need to support trailing type qualifiers (e.g. "id<p> const").
1950 // If a type specifier follows, it will be diagnosed elsewhere.
1951 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001952 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001953
1954 // type-name
1955 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001956 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001957 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001958 // This template-id does not refer to a type name, so we're
1959 // done with the type-specifiers.
1960 goto DoneWithDeclSpec;
1961 }
1962
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001963 // If we're in a context where the template-id could be a
1964 // constructor name or specialization, check whether this is a
1965 // constructor declaration.
1966 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001967 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001968 isConstructorDeclarator())
1969 goto DoneWithDeclSpec;
1970
Douglas Gregor39a8de12009-02-25 19:37:18 +00001971 // Turn the template-id annotation token into a type annotation
1972 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001973 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001974 continue;
1975 }
1976
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 // GNU attributes support.
1978 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001979 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001980 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001981
1982 // Microsoft declspec support.
1983 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001984 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001985 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Steve Naroff239f0732008-12-25 14:16:32 +00001987 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001988 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001989 // FIXME: Add handling here!
1990 break;
1991
1992 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001993 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001994 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001995 case tok::kw___cdecl:
1996 case tok::kw___stdcall:
1997 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001998 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001999 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002000 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002001 continue;
2002
Dawn Perchik52fc3142010-09-03 01:29:35 +00002003 // Borland single token adornments.
2004 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002005 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002006 continue;
2007
Peter Collingbournef315fa82011-02-14 01:42:53 +00002008 // OpenCL single token adornments.
2009 case tok::kw___kernel:
2010 ParseOpenCLAttributes(DS.getAttributes());
2011 continue;
2012
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 // storage-class-specifier
2014 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002015 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2016 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 break;
2018 case tok::kw_extern:
2019 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002020 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002021 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2022 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002024 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002025 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2026 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002027 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 case tok::kw_static:
2029 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002030 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002031 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2032 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002033 break;
2034 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002035 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002036 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002037 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2038 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002039 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002040 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002041 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002042 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2044 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002045 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002046 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2047 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002048 break;
2049 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002050 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2051 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002053 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002054 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2055 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002056 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002058 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 // function-specifier
2062 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002064 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002065 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002066 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002067 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002068 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002070 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002071
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002072 // alignment-specifier
2073 case tok::kw__Alignas:
2074 if (!getLang().C1X)
2075 Diag(Tok, diag::ext_c1x_alignas);
2076 ParseAlignmentSpecifier(DS.getAttributes());
2077 continue;
2078
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002079 // friend
2080 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002081 if (DSContext == DSC_class)
2082 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2083 else {
2084 PrevSpec = ""; // not actually used by the diagnostic
2085 DiagID = diag::err_friend_invalid_in_context;
2086 isInvalid = true;
2087 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002088 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Douglas Gregor8d267c52011-09-09 02:06:17 +00002090 // Modules
2091 case tok::kw___module_private__:
2092 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2093 break;
2094
Sebastian Redl2ac67232009-11-05 15:47:02 +00002095 // constexpr
2096 case tok::kw_constexpr:
2097 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2098 break;
2099
Chris Lattner80d0c892009-01-21 19:48:37 +00002100 // type-specifier
2101 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002102 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2103 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002104 break;
2105 case tok::kw_long:
2106 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002107 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2108 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002109 else
John McCallfec54012009-08-03 20:12:06 +00002110 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2111 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002112 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002113 case tok::kw___int64:
2114 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2115 DiagID);
2116 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002117 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002118 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2119 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002120 break;
2121 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002122 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2123 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002124 break;
2125 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002126 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2127 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 break;
2129 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002130 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2131 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 break;
2133 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2135 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002136 break;
2137 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2139 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002140 break;
2141 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002142 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2143 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002144 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002145 case tok::kw_half:
2146 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2147 DiagID);
2148 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002149 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2151 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002152 break;
2153 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2155 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002156 break;
2157 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002158 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2159 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002160 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002161 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2163 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002164 break;
2165 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2167 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002168 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002169 case tok::kw_bool:
2170 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002171 if (Tok.is(tok::kw_bool) &&
2172 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2173 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2174 PrevSpec = ""; // Not used by the diagnostic.
2175 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002176 // For better error recovery.
2177 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002178 isInvalid = true;
2179 } else {
2180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2181 DiagID);
2182 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002183 break;
2184 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2186 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002187 break;
2188 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002189 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2190 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002191 break;
2192 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2194 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002195 break;
John Thompson82287d12010-02-05 00:12:22 +00002196 case tok::kw___vector:
2197 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2198 break;
2199 case tok::kw___pixel:
2200 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2201 break;
John McCalla5fc4722011-04-09 22:50:59 +00002202 case tok::kw___unknown_anytype:
2203 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2204 PrevSpec, DiagID);
2205 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002206
2207 // class-specifier:
2208 case tok::kw_class:
2209 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002210 case tok::kw_union: {
2211 tok::TokenKind Kind = Tok.getKind();
2212 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002213 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002214 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002215 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002216
2217 // enum-specifier:
2218 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002219 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002220 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002221 continue;
2222
2223 // cv-qualifier:
2224 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002225 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2226 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002227 break;
2228 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002229 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2230 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002231 break;
2232 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002233 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2234 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 break;
2236
Douglas Gregord57959a2009-03-27 23:10:48 +00002237 // C++ typename-specifier:
2238 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002239 if (TryAnnotateTypeOrScopeToken()) {
2240 DS.SetTypeSpecError();
2241 goto DoneWithDeclSpec;
2242 }
2243 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002244 continue;
2245 break;
2246
Chris Lattner80d0c892009-01-21 19:48:37 +00002247 // GNU typeof support.
2248 case tok::kw_typeof:
2249 ParseTypeofSpecifier(DS);
2250 continue;
2251
David Blaikie42d6d0c2011-12-04 05:04:18 +00002252 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002253 ParseDecltypeSpecifier(DS);
2254 continue;
2255
Sean Huntdb5d44b2011-05-19 05:37:45 +00002256 case tok::kw___underlying_type:
2257 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002258 continue;
2259
2260 case tok::kw__Atomic:
2261 ParseAtomicSpecifier(DS);
2262 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002263
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002264 // OpenCL qualifiers:
2265 case tok::kw_private:
2266 if (!getLang().OpenCL)
2267 goto DoneWithDeclSpec;
2268 case tok::kw___private:
2269 case tok::kw___global:
2270 case tok::kw___local:
2271 case tok::kw___constant:
2272 case tok::kw___read_only:
2273 case tok::kw___write_only:
2274 case tok::kw___read_write:
2275 ParseOpenCLQualifiers(DS);
2276 break;
2277
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002278 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002279 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002280 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2281 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002282 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002283 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Douglas Gregor46f936e2010-11-19 17:10:50 +00002285 if (!ParseObjCProtocolQualifiers(DS))
2286 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2287 << FixItHint::CreateInsertion(Loc, "id")
2288 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002289
2290 // Need to support trailing type qualifiers (e.g. "id<p> const").
2291 // If a type specifier follows, it will be diagnosed elsewhere.
2292 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 }
John McCallfec54012009-08-03 20:12:06 +00002294 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 if (isInvalid) {
2296 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002297 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002298
2299 if (DiagID == diag::ext_duplicate_declspec)
2300 Diag(Tok, DiagID)
2301 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2302 else
2303 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002304 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002305
Chris Lattner81c018d2008-03-13 06:29:04 +00002306 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002307 if (DiagID != diag::err_bool_redeclaration)
2308 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 }
2310}
Douglas Gregoradcac882008-12-01 23:54:00 +00002311
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002312/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002313/// primarily follow the C++ grammar with additions for C99 and GNU,
2314/// which together subsume the C grammar. Note that the C++
2315/// type-specifier also includes the C type-qualifier (for const,
2316/// volatile, and C99 restrict). Returns true if a type-specifier was
2317/// found (and parsed), false otherwise.
2318///
2319/// type-specifier: [C++ 7.1.5]
2320/// simple-type-specifier
2321/// class-specifier
2322/// enum-specifier
2323/// elaborated-type-specifier [TODO]
2324/// cv-qualifier
2325///
2326/// cv-qualifier: [C++ 7.1.5.1]
2327/// 'const'
2328/// 'volatile'
2329/// [C99] 'restrict'
2330///
2331/// simple-type-specifier: [ C++ 7.1.5.2]
2332/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2333/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2334/// 'char'
2335/// 'wchar_t'
2336/// 'bool'
2337/// 'short'
2338/// 'int'
2339/// 'long'
2340/// 'signed'
2341/// 'unsigned'
2342/// 'float'
2343/// 'double'
2344/// 'void'
2345/// [C99] '_Bool'
2346/// [C99] '_Complex'
2347/// [C99] '_Imaginary' // Removed in TC2?
2348/// [GNU] '_Decimal32'
2349/// [GNU] '_Decimal64'
2350/// [GNU] '_Decimal128'
2351/// [GNU] typeof-specifier
2352/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2353/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002354/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002355/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002356bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002357 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002358 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002359 const ParsedTemplateInfo &TemplateInfo,
2360 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002361 SourceLocation Loc = Tok.getLocation();
2362
2363 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002364 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002365 // If we already have a type specifier, this identifier is not a type.
2366 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2367 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2368 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2369 return false;
John Thompson82287d12010-02-05 00:12:22 +00002370 // Check for need to substitute AltiVec keyword tokens.
2371 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2372 break;
2373 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002374 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002375 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002376 // Annotate typenames and C++ scope specifiers. If we get one, just
2377 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002378 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2379 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002380 return true;
2381 if (Tok.is(tok::identifier))
2382 return false;
2383 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2384 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002385 case tok::coloncolon: // ::foo::bar
2386 if (NextToken().is(tok::kw_new) || // ::new
2387 NextToken().is(tok::kw_delete)) // ::delete
2388 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Chris Lattner166a8fc2009-01-04 23:41:41 +00002390 // Annotate typenames and C++ scope specifiers. If we get one, just
2391 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002392 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2393 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002394 return true;
2395 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2396 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Douglas Gregor12e083c2008-11-07 15:42:26 +00002398 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002399 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002400 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2402 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002403 DiagID, T);
2404 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002405 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002406 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2407 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002408
Douglas Gregor12e083c2008-11-07 15:42:26 +00002409 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2410 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2411 // Objective-C interface. If we don't have Objective-C or a '<', this is
2412 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002413 if (Tok.is(tok::less) && getLang().ObjC1)
2414 ParseObjCProtocolQualifiers(DS);
2415
Douglas Gregor12e083c2008-11-07 15:42:26 +00002416 return true;
2417 }
2418
2419 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002421 break;
2422 case tok::kw_long:
2423 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2425 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002426 else
John McCallfec54012009-08-03 20:12:06 +00002427 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2428 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002429 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002430 case tok::kw___int64:
2431 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2432 DiagID);
2433 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002434 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002435 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002436 break;
2437 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002438 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2439 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002440 break;
2441 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002442 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2443 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002444 break;
2445 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2447 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002448 break;
2449 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002450 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002451 break;
2452 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002453 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002454 break;
2455 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002456 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002457 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002458 case tok::kw_half:
2459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2460 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002461 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002463 break;
2464 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002466 break;
2467 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002468 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002469 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002470 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002471 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002472 break;
2473 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002475 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002476 case tok::kw_bool:
2477 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002479 break;
2480 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2482 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002483 break;
2484 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2486 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002487 break;
2488 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2490 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002491 break;
John Thompson82287d12010-02-05 00:12:22 +00002492 case tok::kw___vector:
2493 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2494 break;
2495 case tok::kw___pixel:
2496 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2497 break;
2498
Douglas Gregor12e083c2008-11-07 15:42:26 +00002499 // class-specifier:
2500 case tok::kw_class:
2501 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002502 case tok::kw_union: {
2503 tok::TokenKind Kind = Tok.getKind();
2504 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002505 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002506 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002507 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002508 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002509 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002510
2511 // enum-specifier:
2512 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002513 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002514 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002515 return true;
2516
2517 // cv-qualifier:
2518 case tok::kw_const:
2519 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002520 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002521 break;
2522 case tok::kw_volatile:
2523 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002524 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002525 break;
2526 case tok::kw_restrict:
2527 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002528 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002529 break;
2530
2531 // GNU typeof support.
2532 case tok::kw_typeof:
2533 ParseTypeofSpecifier(DS);
2534 return true;
2535
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002536 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002537 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002538 ParseDecltypeSpecifier(DS);
2539 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002540
Sean Huntdb5d44b2011-05-19 05:37:45 +00002541 // C++0x type traits support.
2542 case tok::kw___underlying_type:
2543 ParseUnderlyingTypeSpecifier(DS);
2544 return true;
2545
Eli Friedmanb001de72011-10-06 23:00:33 +00002546 case tok::kw__Atomic:
2547 ParseAtomicSpecifier(DS);
2548 return true;
2549
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002550 // OpenCL qualifiers:
2551 case tok::kw_private:
2552 if (!getLang().OpenCL)
2553 return false;
2554 case tok::kw___private:
2555 case tok::kw___global:
2556 case tok::kw___local:
2557 case tok::kw___constant:
2558 case tok::kw___read_only:
2559 case tok::kw___write_only:
2560 case tok::kw___read_write:
2561 ParseOpenCLQualifiers(DS);
2562 break;
2563
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002564 // C++0x auto support.
2565 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002566 // This is only called in situations where a storage-class specifier is
2567 // illegal, so we can assume an auto type specifier was intended even in
2568 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2569 // extension diagnostic.
2570 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002571 return false;
2572
John McCallfec54012009-08-03 20:12:06 +00002573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002574 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002575
Eli Friedman290eeb02009-06-08 23:27:34 +00002576 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002577 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002578 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002579 case tok::kw___cdecl:
2580 case tok::kw___stdcall:
2581 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002582 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002583 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002584 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002585 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002586
Dawn Perchik52fc3142010-09-03 01:29:35 +00002587 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002588 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002589 return true;
2590
Douglas Gregor12e083c2008-11-07 15:42:26 +00002591 default:
2592 // Not a type-specifier; do nothing.
2593 return false;
2594 }
2595
2596 // If the specifier combination wasn't legal, issue a diagnostic.
2597 if (isInvalid) {
2598 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002599 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002600 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002601 }
2602 DS.SetRangeEnd(Tok.getLocation());
2603 ConsumeToken(); // whatever we parsed above.
2604 return true;
2605}
Reid Spencer5f016e22007-07-11 17:01:13 +00002606
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002607/// ParseStructDeclaration - Parse a struct declaration without the terminating
2608/// semicolon.
2609///
Reid Spencer5f016e22007-07-11 17:01:13 +00002610/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002611/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002612/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002613/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002614/// struct-declarator-list:
2615/// struct-declarator
2616/// struct-declarator-list ',' struct-declarator
2617/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2618/// struct-declarator:
2619/// declarator
2620/// [GNU] declarator attributes[opt]
2621/// declarator[opt] ':' constant-expression
2622/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2623///
Chris Lattnere1359422008-04-10 06:46:29 +00002624void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002625ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002626
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002627 if (Tok.is(tok::kw___extension__)) {
2628 // __extension__ silences extension warnings in the subexpression.
2629 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002630 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002631 return ParseStructDeclaration(DS, Fields);
2632 }
Mike Stump1eb44332009-09-09 15:08:12 +00002633
Steve Naroff28a7ca82007-08-20 22:28:22 +00002634 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002635 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002637 // If there are no declarators, this is a free-standing declaration
2638 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002639 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002640 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002641 return;
2642 }
2643
2644 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002645 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002646 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002647 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002648 FieldDeclarator DeclaratorInfo(DS);
2649
2650 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002651 if (!FirstDeclarator)
2652 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Steve Naroff28a7ca82007-08-20 22:28:22 +00002654 /// struct-declarator: declarator
2655 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002656 if (Tok.isNot(tok::colon)) {
2657 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2658 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002659 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002660 }
Mike Stump1eb44332009-09-09 15:08:12 +00002661
Chris Lattner04d66662007-10-09 17:33:22 +00002662 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002663 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002664 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002665 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002666 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002667 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002668 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002669 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002670
Steve Naroff28a7ca82007-08-20 22:28:22 +00002671 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002672 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002673
John McCallbdd563e2009-11-03 02:38:08 +00002674 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002675 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002676 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002677
Steve Naroff28a7ca82007-08-20 22:28:22 +00002678 // If we don't have a comma, it is either the end of the list (a ';')
2679 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002680 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002681 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002682
Steve Naroff28a7ca82007-08-20 22:28:22 +00002683 // Consume the comma.
2684 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002685
John McCallbdd563e2009-11-03 02:38:08 +00002686 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002687 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002688}
2689
2690/// ParseStructUnionBody
2691/// struct-contents:
2692/// struct-declaration-list
2693/// [EXT] empty
2694/// [GNU] "struct-declaration-list" without terminatoring ';'
2695/// struct-declaration-list:
2696/// struct-declaration
2697/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002698/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002699///
Reid Spencer5f016e22007-07-11 17:01:13 +00002700void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002701 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002702 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2703 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002705 BalancedDelimiterTracker T(*this, tok::l_brace);
2706 if (T.consumeOpen())
2707 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002709 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002710 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002711
Reid Spencer5f016e22007-07-11 17:01:13 +00002712 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2713 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002714 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002715 Diag(Tok, diag::ext_empty_struct_union)
2716 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002717
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002719
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002721 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002722 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Reid Spencer5f016e22007-07-11 17:01:13 +00002724 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002725 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002726 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002727 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002728 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 ConsumeToken();
2730 continue;
2731 }
Chris Lattnere1359422008-04-10 06:46:29 +00002732
2733 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002734 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002735
John McCallbdd563e2009-11-03 02:38:08 +00002736 if (!Tok.is(tok::at)) {
2737 struct CFieldCallback : FieldCallback {
2738 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002739 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002740 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002741
John McCalld226f652010-08-21 09:40:31 +00002742 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002743 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002744 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2745
John McCalld226f652010-08-21 09:40:31 +00002746 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002747 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002748 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002749 FD.D.getDeclSpec().getSourceRange().getBegin(),
2750 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002751 FieldDecls.push_back(Field);
2752 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002753 }
John McCallbdd563e2009-11-03 02:38:08 +00002754 } Callback(*this, TagDecl, FieldDecls);
2755
2756 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002757 } else { // Handle @defs
2758 ConsumeToken();
2759 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2760 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002761 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002762 continue;
2763 }
2764 ConsumeToken();
2765 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2766 if (!Tok.is(tok::identifier)) {
2767 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002768 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002769 continue;
2770 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002771 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002772 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002773 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002774 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2775 ConsumeToken();
2776 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002777 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002778
Chris Lattner04d66662007-10-09 17:33:22 +00002779 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002780 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002781 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002782 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002783 break;
2784 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002785 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2786 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002787 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002788 // If we stopped at a ';', eat it.
2789 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002790 }
2791 }
Mike Stump1eb44332009-09-09 15:08:12 +00002792
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002793 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002794
John McCall0b7e6782011-03-24 11:26:52 +00002795 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002796 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002797 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002798
Douglas Gregor23c94db2010-07-02 17:43:08 +00002799 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002800 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002801 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002802 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002803 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002804 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2805 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002806}
2807
Reid Spencer5f016e22007-07-11 17:01:13 +00002808/// ParseEnumSpecifier
2809/// enum-specifier: [C99 6.7.2.2]
2810/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002811///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002812/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2813/// '}' attributes[opt]
2814/// 'enum' identifier
2815/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002816///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002817/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2818/// [C++0x] enum-head '{' enumerator-list ',' '}'
2819///
2820/// enum-head: [C++0x]
2821/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2822/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2823///
2824/// enum-key: [C++0x]
2825/// 'enum'
2826/// 'enum' 'class'
2827/// 'enum' 'struct'
2828///
2829/// enum-base: [C++0x]
2830/// ':' type-specifier-seq
2831///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002832/// [C++] elaborated-type-specifier:
2833/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2834///
Chris Lattner4c97d762009-04-12 21:49:30 +00002835void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002836 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002837 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002838 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002839 if (Tok.is(tok::code_completion)) {
2840 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002841 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002842 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002843 }
John McCall57c13002011-07-06 05:58:41 +00002844
2845 bool IsScopedEnum = false;
2846 bool IsScopedUsingClassTag = false;
2847
2848 if (getLang().CPlusPlus0x &&
2849 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002850 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002851 IsScopedEnum = true;
2852 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2853 ConsumeToken();
2854 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002855
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002856 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002857 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002858 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002859
Douglas Gregor5471bc82011-09-08 17:18:35 +00002860 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002861 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002862
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002863 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002864 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002865 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2866 // if a fixed underlying type is allowed.
2867 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2868
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002869 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2870 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002871 return;
2872
2873 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002874 Diag(Tok, diag::err_expected_ident);
2875 if (Tok.isNot(tok::l_brace)) {
2876 // Has no name and is not a definition.
2877 // Skip the rest of this declarator, up until the comma or semicolon.
2878 SkipUntil(tok::comma, true);
2879 return;
2880 }
2881 }
2882 }
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002884 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002885 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2886 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002887 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002889 // Skip the rest of this declarator, up until the comma or semicolon.
2890 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002891 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002892 }
Mike Stump1eb44332009-09-09 15:08:12 +00002893
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002894 // If an identifier is present, consume and remember it.
2895 IdentifierInfo *Name = 0;
2896 SourceLocation NameLoc;
2897 if (Tok.is(tok::identifier)) {
2898 Name = Tok.getIdentifierInfo();
2899 NameLoc = ConsumeToken();
2900 }
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002902 if (!Name && IsScopedEnum) {
2903 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2904 // declaration of a scoped enumeration.
2905 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2906 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002907 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002908 }
2909
2910 TypeResult BaseType;
2911
Douglas Gregora61b3e72010-12-01 17:42:47 +00002912 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002913 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002914 bool PossibleBitfield = false;
2915 if (getCurScope()->getFlags() & Scope::ClassScope) {
2916 // If we're in class scope, this can either be an enum declaration with
2917 // an underlying type, or a declaration of a bitfield member. We try to
2918 // use a simple disambiguation scheme first to catch the common cases
2919 // (integer literal, sizeof); if it's still ambiguous, we then consider
2920 // anything that's a simple-type-specifier followed by '(' as an
2921 // expression. This suffices because function types are not valid
2922 // underlying types anyway.
2923 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2924 // If the next token starts an expression, we know we're parsing a
2925 // bit-field. This is the common case.
2926 if (TPR == TPResult::True())
2927 PossibleBitfield = true;
2928 // If the next token starts a type-specifier-seq, it may be either a
2929 // a fixed underlying type or the start of a function-style cast in C++;
2930 // lookahead one more token to see if it's obvious that we have a
2931 // fixed underlying type.
2932 else if (TPR == TPResult::False() &&
2933 GetLookAheadToken(2).getKind() == tok::semi) {
2934 // Consume the ':'.
2935 ConsumeToken();
2936 } else {
2937 // We have the start of a type-specifier-seq, so we have to perform
2938 // tentative parsing to determine whether we have an expression or a
2939 // type.
2940 TentativeParsingAction TPA(*this);
2941
2942 // Consume the ':'.
2943 ConsumeToken();
2944
Douglas Gregor86f208c2011-02-22 20:32:04 +00002945 if ((getLang().CPlusPlus &&
2946 isCXXDeclarationSpecifier() != TPResult::True()) ||
2947 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002948 // We'll parse this as a bitfield later.
2949 PossibleBitfield = true;
2950 TPA.Revert();
2951 } else {
2952 // We have a type-specifier-seq.
2953 TPA.Commit();
2954 }
2955 }
2956 } else {
2957 // Consume the ':'.
2958 ConsumeToken();
2959 }
2960
2961 if (!PossibleBitfield) {
2962 SourceRange Range;
2963 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002964
Douglas Gregor5471bc82011-09-08 17:18:35 +00002965 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002966 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2967 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002968 if (getLang().CPlusPlus0x)
2969 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002970 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002971 }
2972
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002973 // There are three options here. If we have 'enum foo;', then this is a
2974 // forward declaration. If we have 'enum foo {...' then this is a
2975 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2976 //
2977 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2978 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2979 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2980 //
John McCallf312b1e2010-08-26 23:41:50 +00002981 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002982 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002983 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002984 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002985 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002986 else
John McCallf312b1e2010-08-26 23:41:50 +00002987 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002988
2989 // enums cannot be templates, although they can be referenced from a
2990 // template.
2991 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002992 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002993 Diag(Tok, diag::err_enum_template);
2994
2995 // Skip the rest of this declarator, up until the comma or semicolon.
2996 SkipUntil(tok::comma, true);
2997 return;
2998 }
2999
Douglas Gregorb9075602011-02-22 02:55:24 +00003000 if (!Name && TUK != Sema::TUK_Definition) {
3001 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3002
3003 // Skip the rest of this declarator, up until the comma or semicolon.
3004 SkipUntil(tok::comma, true);
3005 return;
3006 }
3007
Douglas Gregor402abb52009-05-28 23:31:59 +00003008 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003009 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003010 const char *PrevSpec = 0;
3011 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003012 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003013 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003014 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003015 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003016 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003017 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003018
Douglas Gregor48c89f42010-04-24 16:38:41 +00003019 if (IsDependent) {
3020 // This enum has a dependent nested-name-specifier. Handle it as a
3021 // dependent tag.
3022 if (!Name) {
3023 DS.SetTypeSpecError();
3024 Diag(Tok, diag::err_expected_type_name_after_typename);
3025 return;
3026 }
3027
Douglas Gregor23c94db2010-07-02 17:43:08 +00003028 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003029 TUK, SS, Name, StartLoc,
3030 NameLoc);
3031 if (Type.isInvalid()) {
3032 DS.SetTypeSpecError();
3033 return;
3034 }
3035
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003036 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3037 NameLoc.isValid() ? NameLoc : StartLoc,
3038 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003039 Diag(StartLoc, DiagID) << PrevSpec;
3040
3041 return;
3042 }
Mike Stump1eb44332009-09-09 15:08:12 +00003043
John McCalld226f652010-08-21 09:40:31 +00003044 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003045 // The action failed to produce an enumeration tag. If this is a
3046 // definition, consume the entire definition.
3047 if (Tok.is(tok::l_brace)) {
3048 ConsumeBrace();
3049 SkipUntil(tok::r_brace);
3050 }
3051
3052 DS.SetTypeSpecError();
3053 return;
3054 }
3055
Chris Lattner04d66662007-10-09 17:33:22 +00003056 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003058
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003059 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3060 NameLoc.isValid() ? NameLoc : StartLoc,
3061 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003062 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003063}
3064
3065/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3066/// enumerator-list:
3067/// enumerator
3068/// enumerator-list ',' enumerator
3069/// enumerator:
3070/// enumeration-constant
3071/// enumeration-constant '=' constant-expression
3072/// enumeration-constant:
3073/// identifier
3074///
John McCalld226f652010-08-21 09:40:31 +00003075void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003076 // Enter the scope of the enum body and start the definition.
3077 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003078 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003079
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003080 BalancedDelimiterTracker T(*this, tok::l_brace);
3081 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003082
Chris Lattner7946dd32007-08-27 17:24:30 +00003083 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003084 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003085 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Chris Lattner5f9e2722011-07-23 10:55:15 +00003087 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003088
John McCalld226f652010-08-21 09:40:31 +00003089 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003092 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3094 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003095
John McCall5b629aa2010-10-22 23:36:17 +00003096 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003097 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003098 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003099
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003101 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003102 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003103 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003104 AssignedVal = ParseConstantExpression();
3105 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003107 }
Mike Stump1eb44332009-09-09 15:08:12 +00003108
Reid Spencer5f016e22007-07-11 17:01:13 +00003109 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003110 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3111 LastEnumConstDecl,
3112 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003113 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003114 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003115 EnumConstantDecls.push_back(EnumConstDecl);
3116 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Douglas Gregor751f6922010-09-07 14:51:08 +00003118 if (Tok.is(tok::identifier)) {
3119 // We're missing a comma between enumerators.
3120 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3121 Diag(Loc, diag::err_enumerator_list_missing_comma)
3122 << FixItHint::CreateInsertion(Loc, ", ");
3123 continue;
3124 }
3125
Chris Lattner04d66662007-10-09 17:33:22 +00003126 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 break;
3128 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Richard Smith7fe62082011-10-15 05:09:34 +00003130 if (Tok.isNot(tok::identifier)) {
3131 if (!getLang().C99 && !getLang().CPlusPlus0x)
3132 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3133 << getLang().CPlusPlus
3134 << FixItHint::CreateRemoval(CommaLoc);
3135 else if (getLang().CPlusPlus0x)
3136 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3137 << FixItHint::CreateRemoval(CommaLoc);
3138 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 }
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003142 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003143
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003145 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003146 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003147
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003148 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3149 EnumDecl, EnumConstantDecls.data(),
3150 EnumConstantDecls.size(), getCurScope(),
3151 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Douglas Gregor72de6672009-01-08 20:45:30 +00003153 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003154 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3155 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003156}
3157
3158/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003159/// start of a type-qualifier-list.
3160bool Parser::isTypeQualifier() const {
3161 switch (Tok.getKind()) {
3162 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003163
3164 // type-qualifier only in OpenCL
3165 case tok::kw_private:
3166 return getLang().OpenCL;
3167
Steve Naroff5f8aa692008-02-11 23:15:56 +00003168 // type-qualifier
3169 case tok::kw_const:
3170 case tok::kw_volatile:
3171 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003172 case tok::kw___private:
3173 case tok::kw___local:
3174 case tok::kw___global:
3175 case tok::kw___constant:
3176 case tok::kw___read_only:
3177 case tok::kw___read_write:
3178 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003179 return true;
3180 }
3181}
3182
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003183/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3184/// is definitely a type-specifier. Return false if it isn't part of a type
3185/// specifier or if we're not sure.
3186bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3187 switch (Tok.getKind()) {
3188 default: return false;
3189 // type-specifiers
3190 case tok::kw_short:
3191 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003192 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003193 case tok::kw_signed:
3194 case tok::kw_unsigned:
3195 case tok::kw__Complex:
3196 case tok::kw__Imaginary:
3197 case tok::kw_void:
3198 case tok::kw_char:
3199 case tok::kw_wchar_t:
3200 case tok::kw_char16_t:
3201 case tok::kw_char32_t:
3202 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003203 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003204 case tok::kw_float:
3205 case tok::kw_double:
3206 case tok::kw_bool:
3207 case tok::kw__Bool:
3208 case tok::kw__Decimal32:
3209 case tok::kw__Decimal64:
3210 case tok::kw__Decimal128:
3211 case tok::kw___vector:
3212
3213 // struct-or-union-specifier (C99) or class-specifier (C++)
3214 case tok::kw_class:
3215 case tok::kw_struct:
3216 case tok::kw_union:
3217 // enum-specifier
3218 case tok::kw_enum:
3219
3220 // typedef-name
3221 case tok::annot_typename:
3222 return true;
3223 }
3224}
3225
Steve Naroff5f8aa692008-02-11 23:15:56 +00003226/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003227/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003228bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003229 switch (Tok.getKind()) {
3230 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003231
Chris Lattner166a8fc2009-01-04 23:41:41 +00003232 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003233 if (TryAltiVecVectorToken())
3234 return true;
3235 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003236 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003237 // Annotate typenames and C++ scope specifiers. If we get one, just
3238 // recurse to handle whatever we get.
3239 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003240 return true;
3241 if (Tok.is(tok::identifier))
3242 return false;
3243 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003244
Chris Lattner166a8fc2009-01-04 23:41:41 +00003245 case tok::coloncolon: // ::foo::bar
3246 if (NextToken().is(tok::kw_new) || // ::new
3247 NextToken().is(tok::kw_delete)) // ::delete
3248 return false;
3249
Chris Lattner166a8fc2009-01-04 23:41:41 +00003250 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003251 return true;
3252 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Reid Spencer5f016e22007-07-11 17:01:13 +00003254 // GNU attributes support.
3255 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003256 // GNU typeof support.
3257 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 // type-specifiers
3260 case tok::kw_short:
3261 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003262 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 case tok::kw_signed:
3264 case tok::kw_unsigned:
3265 case tok::kw__Complex:
3266 case tok::kw__Imaginary:
3267 case tok::kw_void:
3268 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003269 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003270 case tok::kw_char16_t:
3271 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003272 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003273 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003274 case tok::kw_float:
3275 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003276 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003277 case tok::kw__Bool:
3278 case tok::kw__Decimal32:
3279 case tok::kw__Decimal64:
3280 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003281 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Chris Lattner99dc9142008-04-13 18:59:07 +00003283 // struct-or-union-specifier (C99) or class-specifier (C++)
3284 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 case tok::kw_struct:
3286 case tok::kw_union:
3287 // enum-specifier
3288 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 // type-qualifier
3291 case tok::kw_const:
3292 case tok::kw_volatile:
3293 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003294
3295 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003296 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003297 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Chris Lattner7c186be2008-10-20 00:25:30 +00003299 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3300 case tok::less:
3301 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Steve Naroff239f0732008-12-25 14:16:32 +00003303 case tok::kw___cdecl:
3304 case tok::kw___stdcall:
3305 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003306 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003307 case tok::kw___w64:
3308 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003309 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003310 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003311 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003312
3313 case tok::kw___private:
3314 case tok::kw___local:
3315 case tok::kw___global:
3316 case tok::kw___constant:
3317 case tok::kw___read_only:
3318 case tok::kw___read_write:
3319 case tok::kw___write_only:
3320
Eli Friedman290eeb02009-06-08 23:27:34 +00003321 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003322
3323 case tok::kw_private:
3324 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003325
3326 // C1x _Atomic()
3327 case tok::kw__Atomic:
3328 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 }
3330}
3331
3332/// isDeclarationSpecifier() - Return true if the current token is part of a
3333/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003334///
3335/// \param DisambiguatingWithExpression True to indicate that the purpose of
3336/// this check is to disambiguate between an expression and a declaration.
3337bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003338 switch (Tok.getKind()) {
3339 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003341 case tok::kw_private:
3342 return getLang().OpenCL;
3343
Chris Lattner166a8fc2009-01-04 23:41:41 +00003344 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003345 // Unfortunate hack to support "Class.factoryMethod" notation.
3346 if (getLang().ObjC1 && NextToken().is(tok::period))
3347 return false;
John Thompson82287d12010-02-05 00:12:22 +00003348 if (TryAltiVecVectorToken())
3349 return true;
3350 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003351 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003352 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003353 // Annotate typenames and C++ scope specifiers. If we get one, just
3354 // recurse to handle whatever we get.
3355 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003356 return true;
3357 if (Tok.is(tok::identifier))
3358 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003359
3360 // If we're in Objective-C and we have an Objective-C class type followed
3361 // by an identifier and then either ':' or ']', in a place where an
3362 // expression is permitted, then this is probably a class message send
3363 // missing the initial '['. In this case, we won't consider this to be
3364 // the start of a declaration.
3365 if (DisambiguatingWithExpression &&
3366 isStartOfObjCClassMessageMissingOpenBracket())
3367 return false;
3368
John McCall9ba61662010-02-26 08:45:28 +00003369 return isDeclarationSpecifier();
3370
Chris Lattner166a8fc2009-01-04 23:41:41 +00003371 case tok::coloncolon: // ::foo::bar
3372 if (NextToken().is(tok::kw_new) || // ::new
3373 NextToken().is(tok::kw_delete)) // ::delete
3374 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Chris Lattner166a8fc2009-01-04 23:41:41 +00003376 // Annotate typenames and C++ scope specifiers. If we get one, just
3377 // recurse to handle whatever we get.
3378 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003379 return true;
3380 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Reid Spencer5f016e22007-07-11 17:01:13 +00003382 // storage-class-specifier
3383 case tok::kw_typedef:
3384 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003385 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003386 case tok::kw_static:
3387 case tok::kw_auto:
3388 case tok::kw_register:
3389 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003390
Douglas Gregor8d267c52011-09-09 02:06:17 +00003391 // Modules
3392 case tok::kw___module_private__:
3393
Reid Spencer5f016e22007-07-11 17:01:13 +00003394 // type-specifiers
3395 case tok::kw_short:
3396 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003397 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003398 case tok::kw_signed:
3399 case tok::kw_unsigned:
3400 case tok::kw__Complex:
3401 case tok::kw__Imaginary:
3402 case tok::kw_void:
3403 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003404 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003405 case tok::kw_char16_t:
3406 case tok::kw_char32_t:
3407
Reid Spencer5f016e22007-07-11 17:01:13 +00003408 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003409 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003410 case tok::kw_float:
3411 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003412 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003413 case tok::kw__Bool:
3414 case tok::kw__Decimal32:
3415 case tok::kw__Decimal64:
3416 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003417 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003418
Chris Lattner99dc9142008-04-13 18:59:07 +00003419 // struct-or-union-specifier (C99) or class-specifier (C++)
3420 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003421 case tok::kw_struct:
3422 case tok::kw_union:
3423 // enum-specifier
3424 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003425
Reid Spencer5f016e22007-07-11 17:01:13 +00003426 // type-qualifier
3427 case tok::kw_const:
3428 case tok::kw_volatile:
3429 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003430
Reid Spencer5f016e22007-07-11 17:01:13 +00003431 // function-specifier
3432 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003433 case tok::kw_virtual:
3434 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003435
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003436 // static_assert-declaration
3437 case tok::kw__Static_assert:
3438
Chris Lattner1ef08762007-08-09 17:01:07 +00003439 // GNU typeof support.
3440 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003441
Chris Lattner1ef08762007-08-09 17:01:07 +00003442 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003443 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003445
Francois Pichete3d49b42011-06-19 08:02:06 +00003446 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003447 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003448 return true;
3449
Eli Friedmanb001de72011-10-06 23:00:33 +00003450 // C1x _Atomic()
3451 case tok::kw__Atomic:
3452 return true;
3453
Chris Lattnerf3948c42008-07-26 03:38:44 +00003454 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3455 case tok::less:
3456 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Douglas Gregord9d75e52011-04-27 05:41:15 +00003458 // typedef-name
3459 case tok::annot_typename:
3460 return !DisambiguatingWithExpression ||
3461 !isStartOfObjCClassMessageMissingOpenBracket();
3462
Steve Naroff47f52092009-01-06 19:34:12 +00003463 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003464 case tok::kw___cdecl:
3465 case tok::kw___stdcall:
3466 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003467 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003468 case tok::kw___w64:
3469 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003470 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003471 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003472 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003473 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003474
3475 case tok::kw___private:
3476 case tok::kw___local:
3477 case tok::kw___global:
3478 case tok::kw___constant:
3479 case tok::kw___read_only:
3480 case tok::kw___read_write:
3481 case tok::kw___write_only:
3482
Eli Friedman290eeb02009-06-08 23:27:34 +00003483 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003484 }
3485}
3486
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003487bool Parser::isConstructorDeclarator() {
3488 TentativeParsingAction TPA(*this);
3489
3490 // Parse the C++ scope specifier.
3491 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003492 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3493 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003494 TPA.Revert();
3495 return false;
3496 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003497
3498 // Parse the constructor name.
3499 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3500 // We already know that we have a constructor name; just consume
3501 // the token.
3502 ConsumeToken();
3503 } else {
3504 TPA.Revert();
3505 return false;
3506 }
3507
3508 // Current class name must be followed by a left parentheses.
3509 if (Tok.isNot(tok::l_paren)) {
3510 TPA.Revert();
3511 return false;
3512 }
3513 ConsumeParen();
3514
3515 // A right parentheses or ellipsis signals that we have a constructor.
3516 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3517 TPA.Revert();
3518 return true;
3519 }
3520
3521 // If we need to, enter the specified scope.
3522 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003523 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003524 DeclScopeObj.EnterDeclaratorScope();
3525
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003526 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003527 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003528 MaybeParseMicrosoftAttributes(Attrs);
3529
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003530 // Check whether the next token(s) are part of a declaration
3531 // specifier, in which case we have the start of a parameter and,
3532 // therefore, we know that this is a constructor.
3533 bool IsConstructor = isDeclarationSpecifier();
3534 TPA.Revert();
3535 return IsConstructor;
3536}
Reid Spencer5f016e22007-07-11 17:01:13 +00003537
3538/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003539/// type-qualifier-list: [C99 6.7.5]
3540/// type-qualifier
3541/// [vendor] attributes
3542/// [ only if VendorAttributesAllowed=true ]
3543/// type-qualifier-list type-qualifier
3544/// [vendor] type-qualifier-list attributes
3545/// [ only if VendorAttributesAllowed=true ]
3546/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3547/// [ only if CXX0XAttributesAllowed=true ]
3548/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003549///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003550void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3551 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003552 bool CXX0XAttributesAllowed) {
3553 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3554 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003555 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003556 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003557 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003558 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003559 else
3560 Diag(Loc, diag::err_attributes_not_allowed);
3561 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003562
3563 SourceLocation EndLoc;
3564
Reid Spencer5f016e22007-07-11 17:01:13 +00003565 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003566 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003567 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003568 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003569 SourceLocation Loc = Tok.getLocation();
3570
3571 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003572 case tok::code_completion:
3573 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003574 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003575
Reid Spencer5f016e22007-07-11 17:01:13 +00003576 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003577 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3578 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003579 break;
3580 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003581 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3582 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 break;
3584 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003585 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3586 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003587 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003588
3589 // OpenCL qualifiers:
3590 case tok::kw_private:
3591 if (!getLang().OpenCL)
3592 goto DoneWithTypeQuals;
3593 case tok::kw___private:
3594 case tok::kw___global:
3595 case tok::kw___local:
3596 case tok::kw___constant:
3597 case tok::kw___read_only:
3598 case tok::kw___write_only:
3599 case tok::kw___read_write:
3600 ParseOpenCLQualifiers(DS);
3601 break;
3602
Eli Friedman290eeb02009-06-08 23:27:34 +00003603 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003604 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003605 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003606 case tok::kw___cdecl:
3607 case tok::kw___stdcall:
3608 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003609 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003610 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003611 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003612 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003613 continue;
3614 }
3615 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003616 case tok::kw___pascal:
3617 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003618 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003619 continue;
3620 }
3621 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003622 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003623 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003624 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003625 continue; // do *not* consume the next token!
3626 }
3627 // otherwise, FALL THROUGH!
3628 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003629 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003630 // If this is not a type-qualifier token, we're done reading type
3631 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003632 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003633 if (EndLoc.isValid())
3634 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003635 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003636 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003637
Reid Spencer5f016e22007-07-11 17:01:13 +00003638 // If the specifier combination wasn't legal, issue a diagnostic.
3639 if (isInvalid) {
3640 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003641 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003643 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003644 }
3645}
3646
3647
3648/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3649///
3650void Parser::ParseDeclarator(Declarator &D) {
3651 /// This implements the 'declarator' production in the C grammar, then checks
3652 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003653 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003654}
3655
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003656/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3657/// is parsed by the function passed to it. Pass null, and the direct-declarator
3658/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003659/// ptr-operator production.
3660///
Richard Smith0706df42011-10-19 21:33:05 +00003661/// If the grammar of this construct is extended, matching changes must also be
3662/// made to TryParseDeclarator and MightBeDeclarator.
3663///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003664/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3665/// [C] pointer[opt] direct-declarator
3666/// [C++] direct-declarator
3667/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003668///
3669/// pointer: [C99 6.7.5]
3670/// '*' type-qualifier-list[opt]
3671/// '*' type-qualifier-list[opt] pointer
3672///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003673/// ptr-operator:
3674/// '*' cv-qualifier-seq[opt]
3675/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003676/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003677/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003678/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003679/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003680void Parser::ParseDeclaratorInternal(Declarator &D,
3681 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003682 if (Diags.hasAllExtensionsSilenced())
3683 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003684
Sebastian Redlf30208a2009-01-24 21:16:55 +00003685 // C++ member pointers start with a '::' or a nested-name.
3686 // Member pointers get special handling, since there's no place for the
3687 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003688 if (getLang().CPlusPlus &&
3689 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3690 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003691 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3692 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003693 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003694 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003695
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003696 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003697 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003698 // The scope spec really belongs to the direct-declarator.
3699 D.getCXXScopeSpec() = SS;
3700 if (DirectDeclParser)
3701 (this->*DirectDeclParser)(D);
3702 return;
3703 }
3704
3705 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003706 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003707 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003708 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003709 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003710
3711 // Recurse to parse whatever is left.
3712 ParseDeclaratorInternal(D, DirectDeclParser);
3713
3714 // Sema will have to catch (syntactically invalid) pointers into global
3715 // scope. It has to catch pointers into namespace scope anyway.
3716 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003717 Loc),
3718 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003719 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003720 return;
3721 }
3722 }
3723
3724 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003725 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003726 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003727 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003728 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003729 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003730 if (DirectDeclParser)
3731 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003732 return;
3733 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003734
Sebastian Redl05532f22009-03-15 22:02:01 +00003735 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3736 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003737 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003738 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003739
Chris Lattner9af55002009-03-27 04:18:06 +00003740 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003741 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003742 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003743
Reid Spencer5f016e22007-07-11 17:01:13 +00003744 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003745 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003746
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003748 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003749 if (Kind == tok::star)
3750 // Remember that we parsed a pointer type, and remember the type-quals.
3751 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003752 DS.getConstSpecLoc(),
3753 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003754 DS.getRestrictSpecLoc()),
3755 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003756 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003757 else
3758 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003759 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003760 Loc),
3761 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003762 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003763 } else {
3764 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003765 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003766
Sebastian Redl743de1f2009-03-23 00:00:23 +00003767 // Complain about rvalue references in C++03, but then go on and build
3768 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003769 if (Kind == tok::ampamp)
3770 Diag(Loc, getLang().CPlusPlus0x ?
3771 diag::warn_cxx98_compat_rvalue_reference :
3772 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003773
Reid Spencer5f016e22007-07-11 17:01:13 +00003774 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3775 // cv-qualifiers are introduced through the use of a typedef or of a
3776 // template type argument, in which case the cv-qualifiers are ignored.
3777 //
3778 // [GNU] Retricted references are allowed.
3779 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003780 // [C++0x] Attributes on references are not allowed.
3781 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003782 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003783
3784 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3785 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3786 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003787 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3789 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003790 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003791 }
3792
3793 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003794 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003795
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003796 if (D.getNumTypeObjects() > 0) {
3797 // C++ [dcl.ref]p4: There shall be no references to references.
3798 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3799 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003800 if (const IdentifierInfo *II = D.getIdentifier())
3801 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3802 << II;
3803 else
3804 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3805 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003806
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003807 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003808 // can go ahead and build the (technically ill-formed)
3809 // declarator: reference collapsing will take care of it.
3810 }
3811 }
3812
Reid Spencer5f016e22007-07-11 17:01:13 +00003813 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003814 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003815 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003816 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003817 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003818 }
3819}
3820
3821/// ParseDirectDeclarator
3822/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003823/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003824/// '(' declarator ')'
3825/// [GNU] '(' attributes declarator ')'
3826/// [C90] direct-declarator '[' constant-expression[opt] ']'
3827/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3828/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3829/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3830/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3831/// direct-declarator '(' parameter-type-list ')'
3832/// direct-declarator '(' identifier-list[opt] ')'
3833/// [GNU] direct-declarator '(' parameter-forward-declarations
3834/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003835/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3836/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003837/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003838///
3839/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003840/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003841/// '::'[opt] nested-name-specifier[opt] type-name
3842///
3843/// id-expression: [C++ 5.1]
3844/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003845/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003846///
3847/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003848/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003849/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003850/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003851/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003852/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003853///
Reid Spencer5f016e22007-07-11 17:01:13 +00003854void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003855 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003856
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003857 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3858 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003859 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003860 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3861 D.getContext() == Declarator::MemberContext;
3862 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3863 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003864 }
3865
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003866 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003867 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003868 // Change the declaration context for name lookup, until this function
3869 // is exited (and the declarator has been parsed).
3870 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003871 }
3872
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003873 // C++0x [dcl.fct]p14:
3874 // There is a syntactic ambiguity when an ellipsis occurs at the end
3875 // of a parameter-declaration-clause without a preceding comma. In
3876 // this case, the ellipsis is parsed as part of the
3877 // abstract-declarator if the type of the parameter names a template
3878 // parameter pack that has not been expanded; otherwise, it is parsed
3879 // as part of the parameter-declaration-clause.
3880 if (Tok.is(tok::ellipsis) &&
3881 !((D.getContext() == Declarator::PrototypeContext ||
3882 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003883 NextToken().is(tok::r_paren) &&
3884 !Actions.containsUnexpandedParameterPacks(D)))
3885 D.setEllipsisLoc(ConsumeToken());
3886
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003887 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3888 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3889 // We found something that indicates the start of an unqualified-id.
3890 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003891 bool AllowConstructorName;
3892 if (D.getDeclSpec().hasTypeSpecifier())
3893 AllowConstructorName = false;
3894 else if (D.getCXXScopeSpec().isSet())
3895 AllowConstructorName =
3896 (D.getContext() == Declarator::FileContext ||
3897 (D.getContext() == Declarator::MemberContext &&
3898 D.getDeclSpec().isFriendSpecified()));
3899 else
3900 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3901
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003902 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3903 /*EnteringContext=*/true,
3904 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003905 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003906 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003907 D.getName()) ||
3908 // Once we're past the identifier, if the scope was bad, mark the
3909 // whole declarator bad.
3910 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003911 D.SetIdentifier(0, Tok.getLocation());
3912 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003913 } else {
3914 // Parsed the unqualified-id; update range information and move along.
3915 if (D.getSourceRange().getBegin().isInvalid())
3916 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3917 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003918 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003919 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003920 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003921 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003922 assert(!getLang().CPlusPlus &&
3923 "There's a C++-specific check for tok::identifier above");
3924 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3925 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3926 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003927 goto PastIdentifier;
3928 }
3929
3930 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003931 // direct-declarator: '(' declarator ')'
3932 // direct-declarator: '(' attributes declarator ')'
3933 // Example: 'char (*X)' or 'int (*XX)(void)'
3934 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003935
3936 // If the declarator was parenthesized, we entered the declarator
3937 // scope when parsing the parenthesized declarator, then exited
3938 // the scope already. Re-enter the scope, if we need to.
3939 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003940 // If there was an error parsing parenthesized declarator, declarator
3941 // scope may have been enterred before. Don't do it again.
3942 if (!D.isInvalidType() &&
3943 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003944 // Change the declaration context for name lookup, until this function
3945 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003946 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003947 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003948 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003949 // This could be something simple like "int" (in which case the declarator
3950 // portion is empty), if an abstract-declarator is allowed.
3951 D.SetIdentifier(0, Tok.getLocation());
3952 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003953 if (D.getContext() == Declarator::MemberContext)
3954 Diag(Tok, diag::err_expected_member_name_or_semi)
3955 << D.getDeclSpec().getSourceRange();
3956 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003957 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003958 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003959 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003960 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003961 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003962 }
Mike Stump1eb44332009-09-09 15:08:12 +00003963
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003964 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003965 assert(D.isPastIdentifier() &&
3966 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003967
Sean Huntbbd37c62009-11-21 08:43:09 +00003968 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003969 if (D.getIdentifier())
3970 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003971
Reid Spencer5f016e22007-07-11 17:01:13 +00003972 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003973 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003974 // Enter function-declaration scope, limiting any declarators to the
3975 // function prototype scope, including parameter declarators.
3976 ParseScope PrototypeScope(this,
3977 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003978 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3979 // In such a case, check if we actually have a function declarator; if it
3980 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003981 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3982 // When not in file scope, warn for ambiguous function declarators, just
3983 // in case the author intended it as a variable definition.
3984 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3985 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3986 break;
3987 }
John McCall0b7e6782011-03-24 11:26:52 +00003988 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003989 BalancedDelimiterTracker T(*this, tok::l_paren);
3990 T.consumeOpen();
3991 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003992 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003993 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003994 ParseBracketDeclarator(D);
3995 } else {
3996 break;
3997 }
3998 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003999}
Reid Spencer5f016e22007-07-11 17:01:13 +00004000
Chris Lattneref4715c2008-04-06 05:45:57 +00004001/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4002/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004003/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004004/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4005///
4006/// direct-declarator:
4007/// '(' declarator ')'
4008/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004009/// direct-declarator '(' parameter-type-list ')'
4010/// direct-declarator '(' identifier-list[opt] ')'
4011/// [GNU] direct-declarator '(' parameter-forward-declarations
4012/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004013///
4014void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004015 BalancedDelimiterTracker T(*this, tok::l_paren);
4016 T.consumeOpen();
4017
Chris Lattneref4715c2008-04-06 05:45:57 +00004018 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004019
Chris Lattner7399ee02008-10-20 02:05:46 +00004020 // Eat any attributes before we look at whether this is a grouping or function
4021 // declarator paren. If this is a grouping paren, the attribute applies to
4022 // the type being built up, for example:
4023 // int (__attribute__(()) *x)(long y)
4024 // If this ends up not being a grouping paren, the attribute applies to the
4025 // first argument, for example:
4026 // int (__attribute__(()) int x)
4027 // In either case, we need to eat any attributes to be able to determine what
4028 // sort of paren this is.
4029 //
John McCall0b7e6782011-03-24 11:26:52 +00004030 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004031 bool RequiresArg = false;
4032 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004033 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Chris Lattner7399ee02008-10-20 02:05:46 +00004035 // We require that the argument list (if this is a non-grouping paren) be
4036 // present even if the attribute list was empty.
4037 RequiresArg = true;
4038 }
Steve Naroff239f0732008-12-25 14:16:32 +00004039 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004040 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004041 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004042 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004043 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004044 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004045 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004046 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004047 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004048 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004049
Chris Lattneref4715c2008-04-06 05:45:57 +00004050 // If we haven't past the identifier yet (or where the identifier would be
4051 // stored, if this is an abstract declarator), then this is probably just
4052 // grouping parens. However, if this could be an abstract-declarator, then
4053 // this could also be the start of function arguments (consider 'void()').
4054 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004055
Chris Lattneref4715c2008-04-06 05:45:57 +00004056 if (!D.mayOmitIdentifier()) {
4057 // If this can't be an abstract-declarator, this *must* be a grouping
4058 // paren, because we haven't seen the identifier yet.
4059 isGrouping = true;
4060 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004061 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004062 isDeclarationSpecifier()) { // 'int(int)' is a function.
4063 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4064 // considered to be a type, not a K&R identifier-list.
4065 isGrouping = false;
4066 } else {
4067 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4068 isGrouping = true;
4069 }
Mike Stump1eb44332009-09-09 15:08:12 +00004070
Chris Lattneref4715c2008-04-06 05:45:57 +00004071 // If this is a grouping paren, handle:
4072 // direct-declarator: '(' declarator ')'
4073 // direct-declarator: '(' attributes declarator ')'
4074 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004075 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004076 D.setGroupingParens(true);
4077
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004078 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004079 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004080 T.consumeClose();
4081 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4082 T.getCloseLocation()),
4083 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004084
4085 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004086 return;
4087 }
Mike Stump1eb44332009-09-09 15:08:12 +00004088
Chris Lattneref4715c2008-04-06 05:45:57 +00004089 // Okay, if this wasn't a grouping paren, it must be the start of a function
4090 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004091 // identifier (and remember where it would have been), then call into
4092 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004093 D.SetIdentifier(0, Tok.getLocation());
4094
David Blaikie42d6d0c2011-12-04 05:04:18 +00004095 // Enter function-declaration scope, limiting any declarators to the
4096 // function prototype scope, including parameter declarators.
4097 ParseScope PrototypeScope(this,
4098 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004099 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004100 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004101}
4102
4103/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4104/// declarator D up to a paren, which indicates that we are parsing function
4105/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004106///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004107/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004108/// after the open paren - they should be considered to be the first argument of
4109/// a parameter. If RequiresArg is true, then the first argument of the
4110/// function is required to be present and required to not be an identifier
4111/// list.
4112///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004113/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4114/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4115/// (C++0x) trailing-return-type[opt].
4116///
4117/// [C++0x] exception-specification:
4118/// dynamic-exception-specification
4119/// noexcept-specification
4120///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004121void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004122 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004123 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004124 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004125 assert(getCurScope()->isFunctionPrototypeScope() &&
4126 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004127 // lparen is already consumed!
4128 assert(D.isPastIdentifier() && "Should not call before identifier!");
4129
4130 // This should be true when the function has typed arguments.
4131 // Otherwise, it is treated as a K&R-style function.
4132 bool HasProto = false;
4133 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004134 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004135 // Remember where we see an ellipsis, if any.
4136 SourceLocation EllipsisLoc;
4137
4138 DeclSpec DS(AttrFactory);
4139 bool RefQualifierIsLValueRef = true;
4140 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004141 SourceLocation ConstQualifierLoc;
4142 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004143 ExceptionSpecificationType ESpecType = EST_None;
4144 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004145 SmallVector<ParsedType, 2> DynamicExceptions;
4146 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004147 ExprResult NoexceptExpr;
4148 ParsedType TrailingReturnType;
4149
4150 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151 if (isFunctionDeclaratorIdentifierList()) {
4152 if (RequiresArg)
4153 Diag(Tok, diag::err_argument_required_after_attribute);
4154
4155 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4156
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004157 Tracker.consumeClose();
4158 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004159 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004160 if (Tok.isNot(tok::r_paren))
4161 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4162 else if (RequiresArg)
4163 Diag(Tok, diag::err_argument_required_after_attribute);
4164
4165 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4166
4167 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004168 Tracker.consumeClose();
4169 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004170
4171 if (getLang().CPlusPlus) {
4172 MaybeParseCXX0XAttributes(attrs);
4173
4174 // Parse cv-qualifier-seq[opt].
4175 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004176 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004177 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004178 ConstQualifierLoc = DS.getConstSpecLoc();
4179 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4180 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004181
4182 // Parse ref-qualifier[opt].
4183 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004184 Diag(Tok, getLang().CPlusPlus0x ?
4185 diag::warn_cxx98_compat_ref_qualifier :
4186 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004187
4188 RefQualifierIsLValueRef = Tok.is(tok::amp);
4189 RefQualifierLoc = ConsumeToken();
4190 EndLoc = RefQualifierLoc;
4191 }
4192
4193 // Parse exception-specification[opt].
4194 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4195 DynamicExceptions,
4196 DynamicExceptionRanges,
4197 NoexceptExpr);
4198 if (ESpecType != EST_None)
4199 EndLoc = ESpecRange.getEnd();
4200
4201 // Parse trailing-return-type[opt].
4202 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004203 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004204 SourceRange Range;
4205 TrailingReturnType = ParseTrailingReturnType(Range).get();
4206 if (Range.getEnd().isValid())
4207 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004208 }
4209 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004210 }
4211
4212 // Remember that we parsed a function type, and remember the attributes.
4213 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4214 /*isVariadic=*/EllipsisLoc.isValid(),
4215 EllipsisLoc,
4216 ParamInfo.data(), ParamInfo.size(),
4217 DS.getTypeQualifiers(),
4218 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004219 RefQualifierLoc, ConstQualifierLoc,
4220 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004221 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004222 ESpecType, ESpecRange.getBegin(),
4223 DynamicExceptions.data(),
4224 DynamicExceptionRanges.data(),
4225 DynamicExceptions.size(),
4226 NoexceptExpr.isUsable() ?
4227 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004228 Tracker.getOpenLocation(),
4229 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004230 TrailingReturnType),
4231 attrs, EndLoc);
4232}
4233
4234/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4235/// identifier list form for a K&R-style function: void foo(a,b,c)
4236///
4237/// Note that identifier-lists are only allowed for normal declarators, not for
4238/// abstract-declarators.
4239bool Parser::isFunctionDeclaratorIdentifierList() {
4240 return !getLang().CPlusPlus
4241 && Tok.is(tok::identifier)
4242 && !TryAltiVecVectorToken()
4243 // K&R identifier lists can't have typedefs as identifiers, per C99
4244 // 6.7.5.3p11.
4245 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4246 // Identifier lists follow a really simple grammar: the identifiers can
4247 // be followed *only* by a ", identifier" or ")". However, K&R
4248 // identifier lists are really rare in the brave new modern world, and
4249 // it is very common for someone to typo a type in a non-K&R style
4250 // list. If we are presented with something like: "void foo(intptr x,
4251 // float y)", we don't want to start parsing the function declarator as
4252 // though it is a K&R style declarator just because intptr is an
4253 // invalid type.
4254 //
4255 // To handle this, we check to see if the token after the first
4256 // identifier is a "," or ")". Only then do we parse it as an
4257 // identifier list.
4258 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4259}
4260
4261/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4262/// we found a K&R-style identifier list instead of a typed parameter list.
4263///
4264/// After returning, ParamInfo will hold the parsed parameters.
4265///
4266/// identifier-list: [C99 6.7.5]
4267/// identifier
4268/// identifier-list ',' identifier
4269///
4270void Parser::ParseFunctionDeclaratorIdentifierList(
4271 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004272 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004273 // If there was no identifier specified for the declarator, either we are in
4274 // an abstract-declarator, or we are in a parameter declarator which was found
4275 // to be abstract. In abstract-declarators, identifier lists are not valid:
4276 // diagnose this.
4277 if (!D.getIdentifier())
4278 Diag(Tok, diag::ext_ident_list_in_param);
4279
4280 // Maintain an efficient lookup of params we have seen so far.
4281 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4282
4283 while (1) {
4284 // If this isn't an identifier, report the error and skip until ')'.
4285 if (Tok.isNot(tok::identifier)) {
4286 Diag(Tok, diag::err_expected_ident);
4287 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4288 // Forget we parsed anything.
4289 ParamInfo.clear();
4290 return;
4291 }
4292
4293 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4294
4295 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4296 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4297 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4298
4299 // Verify that the argument identifier has not already been mentioned.
4300 if (!ParamsSoFar.insert(ParmII)) {
4301 Diag(Tok, diag::err_param_redefinition) << ParmII;
4302 } else {
4303 // Remember this identifier in ParamInfo.
4304 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4305 Tok.getLocation(),
4306 0));
4307 }
4308
4309 // Eat the identifier.
4310 ConsumeToken();
4311
4312 // The list continues if we see a comma.
4313 if (Tok.isNot(tok::comma))
4314 break;
4315 ConsumeToken();
4316 }
4317}
4318
4319/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4320/// after the opening parenthesis. This function will not parse a K&R-style
4321/// identifier list.
4322///
4323/// D is the declarator being parsed. If attrs is non-null, then the caller
4324/// parsed those arguments immediately after the open paren - they should be
4325/// considered to be the first argument of a parameter.
4326///
4327/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4328/// be the location of the ellipsis, if any was parsed.
4329///
Reid Spencer5f016e22007-07-11 17:01:13 +00004330/// parameter-type-list: [C99 6.7.5]
4331/// parameter-list
4332/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004333/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004334///
4335/// parameter-list: [C99 6.7.5]
4336/// parameter-declaration
4337/// parameter-list ',' parameter-declaration
4338///
4339/// parameter-declaration: [C99 6.7.5]
4340/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004341/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004342/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004343/// declaration-specifiers abstract-declarator[opt]
4344/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004345/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004346/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4347///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004348void Parser::ParseParameterDeclarationClause(
4349 Declarator &D,
4350 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004351 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004352 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004353
Chris Lattnerf97409f2008-04-06 06:57:35 +00004354 while (1) {
4355 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004356 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004357 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004358 }
Mike Stump1eb44332009-09-09 15:08:12 +00004359
Chris Lattnerf97409f2008-04-06 06:57:35 +00004360 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004361 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004362 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004363
John McCall7f040a92010-12-24 02:08:15 +00004364 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004365 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004366 ParseMicrosoftAttributes(DS.getAttributes());
4367
4368 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004369
4370 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004371 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004372 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4373 // attributes lost? Should they even be allowed?
4374 // FIXME: If we can leave the attributes in the token stream somehow, we can
4375 // get rid of a parameter (attrs) and this statement. It might be too much
4376 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004377 DS.takeAttributesFrom(attrs);
4378
Chris Lattnere64c5492009-02-27 18:38:20 +00004379 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004380
Chris Lattnerf97409f2008-04-06 06:57:35 +00004381 // Parse the declarator. This is "PrototypeContext", because we must
4382 // accept either 'declarator' or 'abstract-declarator' here.
4383 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4384 ParseDeclarator(ParmDecl);
4385
4386 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004387 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004388
Chris Lattnerf97409f2008-04-06 06:57:35 +00004389 // Remember this parsed parameter in ParamInfo.
4390 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004391
Douglas Gregor72b505b2008-12-16 21:30:33 +00004392 // DefArgToks is used when the parsing of default arguments needs
4393 // to be delayed.
4394 CachedTokens *DefArgToks = 0;
4395
Chris Lattnerf97409f2008-04-06 06:57:35 +00004396 // If no parameter was specified, verify that *something* was specified,
4397 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004398 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4399 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004400 // Completely missing, emit error.
4401 Diag(DSStart, diag::err_missing_param);
4402 } else {
4403 // Otherwise, we have something. Add it and let semantic analysis try
4404 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004405
Chris Lattnerf97409f2008-04-06 06:57:35 +00004406 // Inform the actions module about the parameter declarator, so it gets
4407 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004408 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004409
4410 // Parse the default argument, if any. We parse the default
4411 // arguments in all dialects; the semantic analysis in
4412 // ActOnParamDefaultArgument will reject the default argument in
4413 // C.
4414 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004415 SourceLocation EqualLoc = Tok.getLocation();
4416
Chris Lattner04421082008-04-08 04:40:51 +00004417 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004418 if (D.getContext() == Declarator::MemberContext) {
4419 // If we're inside a class definition, cache the tokens
4420 // corresponding to the default argument. We'll actually parse
4421 // them when we see the end of the class definition.
4422 // FIXME: Templates will require something similar.
4423 // FIXME: Can we use a smart pointer for Toks?
4424 DefArgToks = new CachedTokens;
4425
Mike Stump1eb44332009-09-09 15:08:12 +00004426 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004427 /*StopAtSemi=*/true,
4428 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004429 delete DefArgToks;
4430 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004431 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004432 } else {
4433 // Mark the end of the default argument so that we know when to
4434 // stop when we parse it later on.
4435 Token DefArgEnd;
4436 DefArgEnd.startToken();
4437 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4438 DefArgEnd.setLocation(Tok.getLocation());
4439 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004440 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004441 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004442 }
Chris Lattner04421082008-04-08 04:40:51 +00004443 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004444 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004445 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004446
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004447 // The argument isn't actually potentially evaluated unless it is
4448 // used.
4449 EnterExpressionEvaluationContext Eval(Actions,
4450 Sema::PotentiallyEvaluatedIfUsed);
4451
John McCall60d7b3a2010-08-24 06:29:42 +00004452 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004453 if (DefArgResult.isInvalid()) {
4454 Actions.ActOnParamDefaultArgumentError(Param);
4455 SkipUntil(tok::comma, tok::r_paren, true, true);
4456 } else {
4457 // Inform the actions module about the default argument
4458 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004459 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004460 }
Chris Lattner04421082008-04-08 04:40:51 +00004461 }
4462 }
Mike Stump1eb44332009-09-09 15:08:12 +00004463
4464 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4465 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004466 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004467 }
4468
4469 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004470 if (Tok.isNot(tok::comma)) {
4471 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004472 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4473
4474 if (!getLang().CPlusPlus) {
4475 // We have ellipsis without a preceding ',', which is ill-formed
4476 // in C. Complain and provide the fix.
4477 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004478 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004479 }
4480 }
4481
4482 break;
4483 }
Mike Stump1eb44332009-09-09 15:08:12 +00004484
Chris Lattnerf97409f2008-04-06 06:57:35 +00004485 // Consume the comma.
4486 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004487 }
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Chris Lattner66d28652008-04-06 06:34:08 +00004489}
Chris Lattneref4715c2008-04-06 05:45:57 +00004490
Reid Spencer5f016e22007-07-11 17:01:13 +00004491/// [C90] direct-declarator '[' constant-expression[opt] ']'
4492/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4493/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4494/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4495/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4496void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004497 BalancedDelimiterTracker T(*this, tok::l_square);
4498 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004499
Chris Lattner378c7e42008-12-18 07:27:21 +00004500 // C array syntax has many features, but by-far the most common is [] and [4].
4501 // This code does a fast path to handle some of the most obvious cases.
4502 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004503 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004504 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004505 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004506
Chris Lattner378c7e42008-12-18 07:27:21 +00004507 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004508 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004509 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004510 T.getOpenLocation(),
4511 T.getCloseLocation()),
4512 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004513 return;
4514 } else if (Tok.getKind() == tok::numeric_constant &&
4515 GetLookAheadToken(1).is(tok::r_square)) {
4516 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004517 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004518 ConsumeToken();
4519
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004520 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004521 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004522 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004523
Chris Lattner378c7e42008-12-18 07:27:21 +00004524 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004525 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004526 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004527 T.getOpenLocation(),
4528 T.getCloseLocation()),
4529 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004530 return;
4531 }
Mike Stump1eb44332009-09-09 15:08:12 +00004532
Reid Spencer5f016e22007-07-11 17:01:13 +00004533 // If valid, this location is the position where we read the 'static' keyword.
4534 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004535 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004536 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004537
Reid Spencer5f016e22007-07-11 17:01:13 +00004538 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004539 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004540 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004541 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004542
Reid Spencer5f016e22007-07-11 17:01:13 +00004543 // If we haven't already read 'static', check to see if there is one after the
4544 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004545 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004546 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004547
Reid Spencer5f016e22007-07-11 17:01:13 +00004548 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4549 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004550 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004552 // Handle the case where we have '[*]' as the array size. However, a leading
4553 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4554 // the the token after the star is a ']'. Since stars in arrays are
4555 // infrequent, use of lookahead is not costly here.
4556 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004557 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004558
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004559 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004560 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004561 StaticLoc = SourceLocation(); // Drop the static.
4562 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004563 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004564 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004565 // Note, in C89, this production uses the constant-expr production instead
4566 // of assignment-expr. The only difference is that assignment-expr allows
4567 // things like '=' and '*='. Sema rejects these in C89 mode because they
4568 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004569
Douglas Gregore0762c92009-06-19 23:52:42 +00004570 // Parse the constant-expression or assignment-expression now (depending
4571 // on dialect).
4572 if (getLang().CPlusPlus)
4573 NumElements = ParseConstantExpression();
4574 else
4575 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004576 }
Mike Stump1eb44332009-09-09 15:08:12 +00004577
Reid Spencer5f016e22007-07-11 17:01:13 +00004578 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004579 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004580 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004581 // If the expression was invalid, skip it.
4582 SkipUntil(tok::r_square);
4583 return;
4584 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004585
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004586 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004587
John McCall0b7e6782011-03-24 11:26:52 +00004588 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004589 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004590
Chris Lattner378c7e42008-12-18 07:27:21 +00004591 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004592 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004593 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004594 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004595 T.getOpenLocation(),
4596 T.getCloseLocation()),
4597 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004598}
4599
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004600/// [GNU] typeof-specifier:
4601/// typeof ( expressions )
4602/// typeof ( type-name )
4603/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004604///
4605void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004606 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004607 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004608 SourceLocation StartLoc = ConsumeToken();
4609
John McCallcfb708c2010-01-13 20:03:27 +00004610 const bool hasParens = Tok.is(tok::l_paren);
4611
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004612 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004613 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004614 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004615 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4616 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004617 if (hasParens)
4618 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004619
4620 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004621 // FIXME: Not accurate, the range gets one token more than it should.
4622 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004623 else
4624 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004625
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004626 if (isCastExpr) {
4627 if (!CastTy) {
4628 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004629 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004630 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004631
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004632 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004633 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004634 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4635 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004636 DiagID, CastTy))
4637 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004638 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004639 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004640
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004641 // If we get here, the operand to the typeof was an expresion.
4642 if (Operand.isInvalid()) {
4643 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004644 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004645 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004646
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004647 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004648 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004649 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4650 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004651 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004652 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004653}
Chris Lattner1b492422010-02-28 18:33:55 +00004654
Eli Friedmanb001de72011-10-06 23:00:33 +00004655/// [C1X] atomic-specifier:
4656/// _Atomic ( type-name )
4657///
4658void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4659 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4660
4661 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004662 BalancedDelimiterTracker T(*this, tok::l_paren);
4663 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004664 SkipUntil(tok::r_paren);
4665 return;
4666 }
4667
4668 TypeResult Result = ParseTypeName();
4669 if (Result.isInvalid()) {
4670 SkipUntil(tok::r_paren);
4671 return;
4672 }
4673
4674 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004675 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004676
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004677 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004678 return;
4679
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004680 DS.setTypeofParensRange(T.getRange());
4681 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004682
4683 const char *PrevSpec = 0;
4684 unsigned DiagID;
4685 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4686 DiagID, Result.release()))
4687 Diag(StartLoc, DiagID) << PrevSpec;
4688}
4689
Chris Lattner1b492422010-02-28 18:33:55 +00004690
4691/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4692/// from TryAltiVecVectorToken.
4693bool Parser::TryAltiVecVectorTokenOutOfLine() {
4694 Token Next = NextToken();
4695 switch (Next.getKind()) {
4696 default: return false;
4697 case tok::kw_short:
4698 case tok::kw_long:
4699 case tok::kw_signed:
4700 case tok::kw_unsigned:
4701 case tok::kw_void:
4702 case tok::kw_char:
4703 case tok::kw_int:
4704 case tok::kw_float:
4705 case tok::kw_double:
4706 case tok::kw_bool:
4707 case tok::kw___pixel:
4708 Tok.setKind(tok::kw___vector);
4709 return true;
4710 case tok::identifier:
4711 if (Next.getIdentifierInfo() == Ident_pixel) {
4712 Tok.setKind(tok::kw___vector);
4713 return true;
4714 }
4715 return false;
4716 }
4717}
4718
4719bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4720 const char *&PrevSpec, unsigned &DiagID,
4721 bool &isInvalid) {
4722 if (Tok.getIdentifierInfo() == Ident_vector) {
4723 Token Next = NextToken();
4724 switch (Next.getKind()) {
4725 case tok::kw_short:
4726 case tok::kw_long:
4727 case tok::kw_signed:
4728 case tok::kw_unsigned:
4729 case tok::kw_void:
4730 case tok::kw_char:
4731 case tok::kw_int:
4732 case tok::kw_float:
4733 case tok::kw_double:
4734 case tok::kw_bool:
4735 case tok::kw___pixel:
4736 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4737 return true;
4738 case tok::identifier:
4739 if (Next.getIdentifierInfo() == Ident_pixel) {
4740 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4741 return true;
4742 }
4743 break;
4744 default:
4745 break;
4746 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004747 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004748 DS.isTypeAltiVecVector()) {
4749 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4750 return true;
4751 }
4752 return false;
4753}