blob: 9644d62bd2192f2de4b89fb1d0f2f93b1ac6fff0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnereaaebc72009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000056
57/// isAttributeLateParsed - Return true if the attribute has arguments that
58/// require late parsing.
59static bool isAttributeLateParsed(const IdentifierInfo &II) {
60 return llvm::StringSwitch<bool>(II.getName())
61#include "clang/Parse/AttrLateParsed.inc"
62 .Default(false);
63}
64
65
Sean Huntbbd37c62009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000067///
68/// [GNU] attributes:
69/// attribute
70/// attributes attribute
71///
72/// [GNU] attribute:
73/// '__attribute__' '(' '(' attribute-list ')' ')'
74///
75/// [GNU] attribute-list:
76/// attrib
77/// attribute_list ',' attrib
78///
79/// [GNU] attrib:
80/// empty
81/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
85///
86/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000091///
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000093/// token lookahead. Comment from gcc: "If they start with an identifier
94/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// start with that identifier; otherwise they are an expression list."
96///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000097/// GCC does not require the ',' between attribs in an attribute-list.
98///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
103
John McCall7f040a92010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000114 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall7f040a92010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000192 IdentifierInfo *ParmName = 0;
193 SourceLocation ParmLoc;
194 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 switch (Tok.getKind()) {
197 case tok::kw_char:
198 case tok::kw_wchar_t:
199 case tok::kw_char16_t:
200 case tok::kw_char32_t:
201 case tok::kw_bool:
202 case tok::kw_short:
203 case tok::kw_int:
204 case tok::kw_long:
205 case tok::kw___int64:
206 case tok::kw_signed:
207 case tok::kw_unsigned:
208 case tok::kw_float:
209 case tok::kw_double:
210 case tok::kw_void:
211 case tok::kw_typeof:
212 // __attribute__(( vec_type_hint(char) ))
213 // FIXME: Don't just discard the builtin type token.
214 ConsumeToken();
215 BuiltinType = true;
216 break;
217
218 case tok::identifier:
219 ParmName = Tok.getIdentifierInfo();
220 ParmLoc = ConsumeToken();
221 break;
222
223 default:
224 break;
225 }
226
227 ExprVector ArgExprs(Actions);
228
229 if (!BuiltinType &&
230 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
231 // Eat the comma.
232 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000233 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000235 // Parse the non-empty comma-separated list of expressions.
236 while (1) {
237 ExprResult ArgExpr(ParseAssignmentExpression());
238 if (ArgExpr.isInvalid()) {
239 SkipUntil(tok::r_paren);
240 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000242 ArgExprs.push_back(ArgExpr.release());
243 if (Tok.isNot(tok::comma))
244 break;
245 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000248 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
249 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
250 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000251 while (Tok.is(tok::identifier)) {
252 ConsumeToken();
253 if (Tok.is(tok::greater))
254 break;
255 if (Tok.is(tok::comma)) {
256 ConsumeToken();
257 continue;
258 }
259 }
260 if (Tok.isNot(tok::greater))
261 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000262 SkipUntil(tok::r_paren, false, true); // skip until ')'
263 }
264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265
266 SourceLocation RParen = Tok.getLocation();
267 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
268 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000269 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
271 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
272 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000273 }
274}
275
276
Eli Friedmana23b4852009-06-08 07:21:15 +0000277/// ParseMicrosoftDeclSpec - Parse an __declspec construct
278///
279/// [MS] decl-specifier:
280/// __declspec ( extended-decl-modifier-seq )
281///
282/// [MS] extended-decl-modifier-seq:
283/// extended-decl-modifier[opt]
284/// extended-decl-modifier extended-decl-modifier-seq
285
John McCall7f040a92010-12-24 02:08:15 +0000286void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000287 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000288
Steve Narofff59e17e2008-12-24 20:59:21 +0000289 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000290 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
291 "declspec")) {
292 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000293 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 }
Francois Pichet373197b2011-05-07 19:04:49 +0000295
Eli Friedman290eeb02009-06-08 23:27:34 +0000296 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000299
300 // FIXME: Remove this when we have proper __declspec(property()) support.
301 // Just skip everything inside property().
302 if (AttrName->getName() == "property") {
303 ConsumeParen();
304 SkipUntil(tok::r_paren);
305 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000306 if (Tok.is(tok::l_paren)) {
307 ConsumeParen();
308 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
309 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000310 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000312 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000313 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
314 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 }
316 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
317 SkipUntil(tok::r_paren, false);
318 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
320 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000321 }
322 }
323 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
324 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000325 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000326}
327
John McCall7f040a92010-12-24 02:08:15 +0000328void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000329 // Treat these like attributes
330 // FIXME: Allow Sema to distinguish between these and real attributes!
331 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000332 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000333 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000334 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000335 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000336 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
337 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000338 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
339 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000340 // FIXME: Support these properly!
341 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000345}
346
John McCall7f040a92010-12-24 02:08:15 +0000347void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000348 // Treat these like attributes
349 while (Tok.is(tok::kw___pascal)) {
350 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
351 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000352 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 }
John McCall7f040a92010-12-24 02:08:15 +0000355}
356
Peter Collingbournef315fa82011-02-14 01:42:53 +0000357void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
358 // Treat these like attributes
359 while (Tok.is(tok::kw___kernel)) {
360 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000361 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
362 AttrNameLoc, 0, AttrNameLoc, 0,
363 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000364 }
365}
366
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000367void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
368 SourceLocation Loc = Tok.getLocation();
369 switch(Tok.getKind()) {
370 // OpenCL qualifiers:
371 case tok::kw___private:
372 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413 default: break;
414 }
415}
416
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000417/// \brief Parse a version number.
418///
419/// version:
420/// simple-integer
421/// simple-integer ',' simple-integer
422/// simple-integer ',' simple-integer ',' simple-integer
423VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
424 Range = Tok.getLocation();
425
426 if (!Tok.is(tok::numeric_constant)) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 // Parse the major (and possibly minor and subminor) versions, which
433 // are stored in the numeric constant. We utilize a quirk of the
434 // lexer, which is that it handles something like 1.2.3 as a single
435 // numeric constant, rather than two separate tokens.
436 llvm::SmallString<512> Buffer;
437 Buffer.resize(Tok.getLength()+1);
438 const char *ThisTokBegin = &Buffer[0];
439
440 // Get the spelling of the token, which eliminates trigraphs, etc.
441 bool Invalid = false;
442 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
443 if (Invalid)
444 return VersionTuple();
445
446 // Parse the major version.
447 unsigned AfterMajor = 0;
448 unsigned Major = 0;
449 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
450 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
451 ++AfterMajor;
452 }
453
454 if (AfterMajor == 0) {
455 Diag(Tok, diag::err_expected_version);
456 SkipUntil(tok::comma, tok::r_paren, true, true, true);
457 return VersionTuple();
458 }
459
460 if (AfterMajor == ActualLength) {
461 ConsumeToken();
462
463 // We only had a single version component.
464 if (Major == 0) {
465 Diag(Tok, diag::err_zero_version);
466 return VersionTuple();
467 }
468
469 return VersionTuple(Major);
470 }
471
472 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 // Parse the minor version.
479 unsigned AfterMinor = AfterMajor + 1;
480 unsigned Minor = 0;
481 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
482 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
483 ++AfterMinor;
484 }
485
486 if (AfterMinor == ActualLength) {
487 ConsumeToken();
488
489 // We had major.minor.
490 if (Major == 0 && Minor == 0) {
491 Diag(Tok, diag::err_zero_version);
492 return VersionTuple();
493 }
494
495 return VersionTuple(Major, Minor);
496 }
497
498 // If what follows is not a '.', we have a problem.
499 if (ThisTokBegin[AfterMinor] != '.') {
500 Diag(Tok, diag::err_expected_version);
501 SkipUntil(tok::comma, tok::r_paren, true, true, true);
502 return VersionTuple();
503 }
504
505 // Parse the subminor version.
506 unsigned AfterSubminor = AfterMinor + 1;
507 unsigned Subminor = 0;
508 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
509 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
510 ++AfterSubminor;
511 }
512
513 if (AfterSubminor != ActualLength) {
514 Diag(Tok, diag::err_expected_version);
515 SkipUntil(tok::comma, tok::r_paren, true, true, true);
516 return VersionTuple();
517 }
518 ConsumeToken();
519 return VersionTuple(Major, Minor, Subminor);
520}
521
522/// \brief Parse the contents of the "availability" attribute.
523///
524/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000525/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000526///
527/// platform:
528/// identifier
529///
530/// version-arg-list:
531/// version-arg
532/// version-arg ',' version-arg-list
533///
534/// version-arg:
535/// 'introduced' '=' version
536/// 'deprecated' '=' version
537/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000538/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000539/// opt-message:
540/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000541void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
542 SourceLocation AvailabilityLoc,
543 ParsedAttributes &attrs,
544 SourceLocation *endLoc) {
545 SourceLocation PlatformLoc;
546 IdentifierInfo *Platform = 0;
547
548 enum { Introduced, Deprecated, Obsoleted, Unknown };
549 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000550 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000551
552 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000553 BalancedDelimiterTracker T(*this, tok::l_paren);
554 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555 Diag(Tok, diag::err_expected_lparen);
556 return;
557 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000558
559 // Parse the platform name,
560 if (Tok.isNot(tok::identifier)) {
561 Diag(Tok, diag::err_availability_expected_platform);
562 SkipUntil(tok::r_paren);
563 return;
564 }
565 Platform = Tok.getIdentifierInfo();
566 PlatformLoc = ConsumeToken();
567
568 // Parse the ',' following the platform name.
569 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
570 return;
571
572 // If we haven't grabbed the pointers for the identifiers
573 // "introduced", "deprecated", and "obsoleted", do so now.
574 if (!Ident_introduced) {
575 Ident_introduced = PP.getIdentifierInfo("introduced");
576 Ident_deprecated = PP.getIdentifierInfo("deprecated");
577 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000578 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000579 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000580 }
581
582 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000583 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000584 do {
585 if (Tok.isNot(tok::identifier)) {
586 Diag(Tok, diag::err_availability_expected_change);
587 SkipUntil(tok::r_paren);
588 return;
589 }
590 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
591 SourceLocation KeywordLoc = ConsumeToken();
592
Douglas Gregorb53e4172011-03-26 03:35:55 +0000593 if (Keyword == Ident_unavailable) {
594 if (UnavailableLoc.isValid()) {
595 Diag(KeywordLoc, diag::err_availability_redundant)
596 << Keyword << SourceRange(UnavailableLoc);
597 }
598 UnavailableLoc = KeywordLoc;
599
600 if (Tok.isNot(tok::comma))
601 break;
602
603 ConsumeToken();
604 continue;
605 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000606
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000607 if (Tok.isNot(tok::equal)) {
608 Diag(Tok, diag::err_expected_equal_after)
609 << Keyword;
610 SkipUntil(tok::r_paren);
611 return;
612 }
613 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000614 if (Keyword == Ident_message) {
615 if (!isTokenStringLiteral()) {
616 Diag(Tok, diag::err_expected_string_literal);
617 SkipUntil(tok::r_paren);
618 return;
619 }
620 MessageExpr = ParseStringLiteralExpression();
621 break;
622 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000623
624 SourceRange VersionRange;
625 VersionTuple Version = ParseVersionTuple(VersionRange);
626
627 if (Version.empty()) {
628 SkipUntil(tok::r_paren);
629 return;
630 }
631
632 unsigned Index;
633 if (Keyword == Ident_introduced)
634 Index = Introduced;
635 else if (Keyword == Ident_deprecated)
636 Index = Deprecated;
637 else if (Keyword == Ident_obsoleted)
638 Index = Obsoleted;
639 else
640 Index = Unknown;
641
642 if (Index < Unknown) {
643 if (!Changes[Index].KeywordLoc.isInvalid()) {
644 Diag(KeywordLoc, diag::err_availability_redundant)
645 << Keyword
646 << SourceRange(Changes[Index].KeywordLoc,
647 Changes[Index].VersionRange.getEnd());
648 }
649
650 Changes[Index].KeywordLoc = KeywordLoc;
651 Changes[Index].Version = Version;
652 Changes[Index].VersionRange = VersionRange;
653 } else {
654 Diag(KeywordLoc, diag::err_availability_unknown_change)
655 << Keyword << VersionRange;
656 }
657
658 if (Tok.isNot(tok::comma))
659 break;
660
661 ConsumeToken();
662 } while (true);
663
664 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000665 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000666 return;
667
668 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670
Douglas Gregorb53e4172011-03-26 03:35:55 +0000671 // The 'unavailable' availability cannot be combined with any other
672 // availability changes. Make sure that hasn't happened.
673 if (UnavailableLoc.isValid()) {
674 bool Complained = false;
675 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
676 if (Changes[Index].KeywordLoc.isValid()) {
677 if (!Complained) {
678 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
679 << SourceRange(Changes[Index].KeywordLoc,
680 Changes[Index].VersionRange.getEnd());
681 Complained = true;
682 }
683
684 // Clear out the availability.
685 Changes[Index] = AvailabilityChange();
686 }
687 }
688 }
689
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000690 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000691 attrs.addNew(&Availability,
692 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000693 0, SourceLocation(),
694 Platform, PlatformLoc,
695 Changes[Introduced],
696 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000697 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000698 UnavailableLoc, MessageExpr.take(),
699 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000700}
701
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000702
703// Late Parsed Attributes:
704// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
705
706void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
707
708void Parser::LateParsedClass::ParseLexedAttributes() {
709 Self->ParseLexedAttributes(*Class);
710}
711
712void Parser::LateParsedAttribute::ParseLexedAttributes() {
713 Self->ParseLexedAttribute(*this);
714}
715
716/// Wrapper class which calls ParseLexedAttribute, after setting up the
717/// scope appropriately.
718void Parser::ParseLexedAttributes(ParsingClass &Class) {
719 // Deal with templates
720 // FIXME: Test cases to make sure this does the right thing for templates.
721 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
722 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
723 HasTemplateScope);
724 if (HasTemplateScope)
725 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
726
727 // Set or update the scope flags to include Scope::ThisScope.
728 bool AlreadyHasClassScope = Class.TopLevelClass;
729 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
730 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
731 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
732
733 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
734 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
735 }
736}
737
738/// \brief Finish parsing an attribute for which parsing was delayed.
739/// This will be called at the end of parsing a class declaration
740/// for each LateParsedAttribute. We consume the saved tokens and
741/// create an attribute with the arguments filled in. We add this
742/// to the Attribute list for the decl.
743void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
744 // Save the current token position.
745 SourceLocation OrigLoc = Tok.getLocation();
746
747 // Append the current token at the end of the new token stream so that it
748 // doesn't get lost.
749 LA.Toks.push_back(Tok);
750 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
751 // Consume the previously pushed token.
752 ConsumeAnyToken();
753
754 ParsedAttributes Attrs(AttrFactory);
755 SourceLocation endLoc;
756
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000757 // If the Decl is templatized, add template parameters to scope.
758 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
759 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
760 if (HasTemplateScope)
761 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
762
763 // If the Decl is on a function, add function parameters to the scope.
764 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
765 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
766 if (HasFunctionScope)
767 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
768
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000769 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
770
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000771 if (HasFunctionScope) {
772 Actions.ActOnExitFunctionContext();
773 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
774 }
775 if (HasTemplateScope) {
776 TempScope.Exit();
777 }
778
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000779 // Late parsed attributes must be attached to Decls by hand. If the
780 // LA.D is not set, then this was not done properly.
781 assert(LA.D && "No decl attached to late parsed attribute");
782 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
783
784 if (Tok.getLocation() != OrigLoc) {
785 // Due to a parsing error, we either went over the cached tokens or
786 // there are still cached tokens left, so we skip the leftover tokens.
787 // Since this is an uncommon situation that should be avoided, use the
788 // expensive isBeforeInTranslationUnit call.
789 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
790 OrigLoc))
791 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
792 ConsumeAnyToken();
793 }
794}
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796/// \brief Wrapper around a case statement checking if AttrName is
797/// one of the thread safety attributes
798bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
799 return llvm::StringSwitch<bool>(AttrName)
800 .Case("guarded_by", true)
801 .Case("guarded_var", true)
802 .Case("pt_guarded_by", true)
803 .Case("pt_guarded_var", true)
804 .Case("lockable", true)
805 .Case("scoped_lockable", true)
806 .Case("no_thread_safety_analysis", true)
807 .Case("acquired_after", true)
808 .Case("acquired_before", true)
809 .Case("exclusive_lock_function", true)
810 .Case("shared_lock_function", true)
811 .Case("exclusive_trylock_function", true)
812 .Case("shared_trylock_function", true)
813 .Case("unlock_function", true)
814 .Case("lock_returned", true)
815 .Case("locks_excluded", true)
816 .Case("exclusive_locks_required", true)
817 .Case("shared_locks_required", true)
818 .Default(false);
819}
820
821/// \brief Parse the contents of thread safety attributes. These
822/// should always be parsed as an expression list.
823///
824/// We need to special case the parsing due to the fact that if the first token
825/// of the first argument is an identifier, the main parse loop will store
826/// that token as a "parameter" and the rest of
827/// the arguments will be added to a list of "arguments". However,
828/// subsequent tokens in the first argument are lost. We instead parse each
829/// argument as an expression and add all arguments to the list of "arguments".
830/// In future, we will take advantage of this special case to also
831/// deal with some argument scoping issues here (for example, referring to a
832/// function parameter in the attribute on that function).
833void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
834 SourceLocation AttrNameLoc,
835 ParsedAttributes &Attrs,
836 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000839 BalancedDelimiterTracker T(*this, tok::l_paren);
840 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841
842 ExprVector ArgExprs(Actions);
843 bool ArgExprsOk = true;
844
845 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000846 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000847 ExprResult ArgExpr(ParseAssignmentExpression());
848 if (ArgExpr.isInvalid()) {
849 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000850 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000851 break;
852 } else {
853 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000854 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000855 if (Tok.isNot(tok::comma))
856 break;
857 ConsumeToken(); // Eat the comma, move to the next argument
858 }
859 // Match the ')'.
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000860 if (ArgExprsOk && !T.consumeClose() && ArgExprs.size() > 0) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000861 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
862 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000863 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000864 if (EndLoc)
865 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000866}
867
John McCall7f040a92010-12-24 02:08:15 +0000868void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
869 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
870 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000871}
872
Reid Spencer5f016e22007-07-11 17:01:13 +0000873/// ParseDeclaration - Parse a full 'declaration', which consists of
874/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000875/// 'Context' should be a Declarator::TheContext value. This returns the
876/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000877///
878/// declaration: [C99 6.7]
879/// block-declaration ->
880/// simple-declaration
881/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000882/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000883/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000884/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000885/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000886/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000887/// others... [FIXME]
888///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000889Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
890 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000891 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000892 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000893 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000894 // Must temporarily exit the objective-c container scope for
895 // parsing c none objective-c decls.
896 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000897
John McCalld226f652010-08-21 09:40:31 +0000898 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000899 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000900 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000901 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000902 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000905 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000906 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000907 // Could be the start of an inline namespace. Allowed as an ext in C++03.
908 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000909 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000910 SourceLocation InlineLoc = ConsumeToken();
911 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
912 break;
913 }
John McCall7f040a92010-12-24 02:08:15 +0000914 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000915 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000916 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000917 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000918 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000919 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000920 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000921 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000922 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000923 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000924 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000925 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000926 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000927 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000928 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000929 default:
John McCall7f040a92010-12-24 02:08:15 +0000930 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000931 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000932
Chris Lattner682bf922009-03-29 16:50:03 +0000933 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000934 // single decl, convert it now. Alias declarations can also declare a type;
935 // include that too if it is present.
936 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000937}
938
939/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
940/// declaration-specifiers init-declarator-list[opt] ';'
941///[C90/C++]init-declarator-list ';' [TODO]
942/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000943///
Richard Smithad762fc2011-04-14 22:09:26 +0000944/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
945/// attribute-specifier-seq[opt] type-specifier-seq declarator
946///
Chris Lattnercd147752009-03-29 17:27:48 +0000947/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000948/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000949///
950/// If FRI is non-null, we might be parsing a for-range-declaration instead
951/// of a simple-declaration. If we find that we are, we also parse the
952/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000953Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
954 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000955 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000956 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000957 bool RequireSemi,
958 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000960 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000961 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000962
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000963 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000964 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000965 StmtResult R = Actions.ActOnVlaStmt(DS);
966 if (R.isUsable())
967 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000968
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
970 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000971 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000972 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000973 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000974 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000975 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000976 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000978
979 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000980}
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Richard Smith0706df42011-10-19 21:33:05 +0000982/// Returns true if this might be the start of a declarator, or a common typo
983/// for a declarator.
984bool Parser::MightBeDeclarator(unsigned Context) {
985 switch (Tok.getKind()) {
986 case tok::annot_cxxscope:
987 case tok::annot_template_id:
988 case tok::caret:
989 case tok::code_completion:
990 case tok::coloncolon:
991 case tok::ellipsis:
992 case tok::kw___attribute:
993 case tok::kw_operator:
994 case tok::l_paren:
995 case tok::star:
996 return true;
997
998 case tok::amp:
999 case tok::ampamp:
1000 case tok::colon: // Might be a typo for '::'.
1001 return getLang().CPlusPlus;
1002
1003 case tok::identifier:
1004 switch (NextToken().getKind()) {
1005 case tok::code_completion:
1006 case tok::coloncolon:
1007 case tok::comma:
1008 case tok::equal:
1009 case tok::equalequal: // Might be a typo for '='.
1010 case tok::kw_alignas:
1011 case tok::kw_asm:
1012 case tok::kw___attribute:
1013 case tok::l_brace:
1014 case tok::l_paren:
1015 case tok::l_square:
1016 case tok::less:
1017 case tok::r_brace:
1018 case tok::r_paren:
1019 case tok::r_square:
1020 case tok::semi:
1021 return true;
1022
1023 case tok::colon:
1024 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
1025 // and in block scope it's probably a label.
1026 return getLang().CPlusPlus && Context == Declarator::FileContext;
1027
1028 default:
1029 return false;
1030 }
1031
1032 default:
1033 return false;
1034 }
1035}
1036
John McCalld8ac0572009-11-03 19:26:08 +00001037/// ParseDeclGroup - Having concluded that this is either a function
1038/// definition or a group of object declarations, actually parse the
1039/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001040Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1041 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001042 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001043 SourceLocation *DeclEnd,
1044 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001045 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001046 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001047 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001048
John McCalld8ac0572009-11-03 19:26:08 +00001049 // Bail out if the first declarator didn't seem well-formed.
1050 if (!D.hasName() && !D.mayOmitIdentifier()) {
1051 // Skip until ; or }.
1052 SkipUntil(tok::r_brace, true, true);
1053 if (Tok.is(tok::semi))
1054 ConsumeToken();
1055 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattnerc82daef2010-07-11 22:24:20 +00001058 // Check to see if we have a function *definition* which must have a body.
1059 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1060 // Look at the next token to make sure that this isn't a function
1061 // declaration. We have to check this because __attribute__ might be the
1062 // start of a function definition in GCC-extended K&R C.
1063 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001064
Chris Lattner004659a2010-07-11 22:42:07 +00001065 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001066 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1067 Diag(Tok, diag::err_function_declared_typedef);
1068
1069 // Recover by treating the 'typedef' as spurious.
1070 DS.ClearStorageClassSpecs();
1071 }
1072
John McCalld226f652010-08-21 09:40:31 +00001073 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001074 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001075 }
1076
1077 if (isDeclarationSpecifier()) {
1078 // If there is an invalid declaration specifier right after the function
1079 // prototype, then we must be in a missing semicolon case where this isn't
1080 // actually a body. Just fall through into the code that handles it as a
1081 // prototype, and let the top-level code handle the erroneous declspec
1082 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001083 } else {
1084 Diag(Tok, diag::err_expected_fn_body);
1085 SkipUntil(tok::semi);
1086 return DeclGroupPtrTy();
1087 }
1088 }
1089
Richard Smithad762fc2011-04-14 22:09:26 +00001090 if (ParseAttributesAfterDeclarator(D))
1091 return DeclGroupPtrTy();
1092
1093 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1094 // must parse and analyze the for-range-initializer before the declaration is
1095 // analyzed.
1096 if (FRI && Tok.is(tok::colon)) {
1097 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001098 if (Tok.is(tok::l_brace))
1099 FRI->RangeExpr = ParseBraceInitializer();
1100 else
1101 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001102 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1103 Actions.ActOnCXXForRangeDecl(ThisDecl);
1104 Actions.FinalizeDeclaration(ThisDecl);
1105 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1106 }
1107
Chris Lattner5f9e2722011-07-23 10:55:15 +00001108 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001109 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001110 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001111 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001112 DeclsInGroup.push_back(FirstDecl);
1113
Richard Smith0706df42011-10-19 21:33:05 +00001114 bool ExpectSemi = Context != Declarator::ForContext;
1115
John McCalld8ac0572009-11-03 19:26:08 +00001116 // If we don't have a comma, it is either the end of the list (a ';') or an
1117 // error, bail out.
1118 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001119 SourceLocation CommaLoc = ConsumeToken();
1120
1121 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1122 // This comma was followed by a line-break and something which can't be
1123 // the start of a declarator. The comma was probably a typo for a
1124 // semicolon.
1125 Diag(CommaLoc, diag::err_expected_semi_declaration)
1126 << FixItHint::CreateReplacement(CommaLoc, ";");
1127 ExpectSemi = false;
1128 break;
1129 }
John McCalld8ac0572009-11-03 19:26:08 +00001130
1131 // Parse the next declarator.
1132 D.clear();
1133
1134 // Accept attributes in an init-declarator. In the first declarator in a
1135 // declaration, these would be part of the declspec. In subsequent
1136 // declarators, they become part of the declarator itself, so that they
1137 // don't apply to declarators after *this* one. Examples:
1138 // short __attribute__((common)) var; -> declspec
1139 // short var __attribute__((common)); -> declarator
1140 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001141 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001142
1143 ParseDeclarator(D);
1144
John McCalld226f652010-08-21 09:40:31 +00001145 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001146 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001147 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001148 DeclsInGroup.push_back(ThisDecl);
1149 }
1150
1151 if (DeclEnd)
1152 *DeclEnd = Tok.getLocation();
1153
Richard Smith0706df42011-10-19 21:33:05 +00001154 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001155 ExpectAndConsume(tok::semi,
1156 Context == Declarator::FileContext
1157 ? diag::err_invalid_token_after_toplevel_declarator
1158 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001159 // Okay, there was no semicolon and one was expected. If we see a
1160 // declaration specifier, just assume it was missing and continue parsing.
1161 // Otherwise things are very confused and we skip to recover.
1162 if (!isDeclarationSpecifier()) {
1163 SkipUntil(tok::r_brace, true, true);
1164 if (Tok.is(tok::semi))
1165 ConsumeToken();
1166 }
John McCalld8ac0572009-11-03 19:26:08 +00001167 }
1168
Douglas Gregor23c94db2010-07-02 17:43:08 +00001169 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001170 DeclsInGroup.data(),
1171 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001172}
1173
Richard Smithad762fc2011-04-14 22:09:26 +00001174/// Parse an optional simple-asm-expr and attributes, and attach them to a
1175/// declarator. Returns true on an error.
1176bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1177 // If a simple-asm-expr is present, parse it.
1178 if (Tok.is(tok::kw_asm)) {
1179 SourceLocation Loc;
1180 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1181 if (AsmLabel.isInvalid()) {
1182 SkipUntil(tok::semi, true, true);
1183 return true;
1184 }
1185
1186 D.setAsmLabel(AsmLabel.release());
1187 D.SetRangeEnd(Loc);
1188 }
1189
1190 MaybeParseGNUAttributes(D);
1191 return false;
1192}
1193
Douglas Gregor1426e532009-05-12 21:31:51 +00001194/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1195/// declarator'. This method parses the remainder of the declaration
1196/// (including any attributes or initializer, among other things) and
1197/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001198///
Reid Spencer5f016e22007-07-11 17:01:13 +00001199/// init-declarator: [C99 6.7]
1200/// declarator
1201/// declarator '=' initializer
1202/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1203/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001204/// [C++] declarator initializer[opt]
1205///
1206/// [C++] initializer:
1207/// [C++] '=' initializer-clause
1208/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001209/// [C++0x] '=' 'default' [TODO]
1210/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001211/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001212///
1213/// According to the standard grammar, =default and =delete are function
1214/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001215///
John McCalld226f652010-08-21 09:40:31 +00001216Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001217 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001218 if (ParseAttributesAfterDeclarator(D))
1219 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Richard Smithad762fc2011-04-14 22:09:26 +00001221 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1222}
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Richard Smithad762fc2011-04-14 22:09:26 +00001224Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1225 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001226 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001227 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001228 switch (TemplateInfo.Kind) {
1229 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001231 break;
1232
1233 case ParsedTemplateInfo::Template:
1234 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001235 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001236 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001237 TemplateInfo.TemplateParams->data(),
1238 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001239 D);
1240 break;
1241
1242 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001243 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001244 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001245 TemplateInfo.ExternLoc,
1246 TemplateInfo.TemplateLoc,
1247 D);
1248 if (ThisRes.isInvalid()) {
1249 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001250 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001251 }
1252
1253 ThisDecl = ThisRes.get();
1254 break;
1255 }
1256 }
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Richard Smith34b41d92011-02-20 03:19:35 +00001258 bool TypeContainsAuto =
1259 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1260
Douglas Gregor1426e532009-05-12 21:31:51 +00001261 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001262 if (isTokenEqualOrMistypedEqualEqual(
1263 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001264 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001265 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001266 if (D.isFunctionDeclarator())
1267 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1268 << 1 /* delete */;
1269 else
1270 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001271 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001272 if (D.isFunctionDeclarator())
1273 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1274 << 1 /* delete */;
1275 else
1276 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001277 } else {
John McCall731ad842009-12-19 09:28:58 +00001278 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1279 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001280 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001281 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001282
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001285 cutOffParsing();
1286 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001287 }
1288
John McCall60d7b3a2010-08-24 06:29:42 +00001289 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001290
John McCall731ad842009-12-19 09:28:58 +00001291 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001292 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001293 ExitScope();
1294 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001295
Douglas Gregor1426e532009-05-12 21:31:51 +00001296 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001297 SkipUntil(tok::comma, true, true);
1298 Actions.ActOnInitializerError(ThisDecl);
1299 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001300 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1301 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001302 }
1303 } else if (Tok.is(tok::l_paren)) {
1304 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001305 BalancedDelimiterTracker T(*this, tok::l_paren);
1306 T.consumeOpen();
1307
Douglas Gregor1426e532009-05-12 21:31:51 +00001308 ExprVector Exprs(Actions);
1309 CommaLocsTy CommaLocs;
1310
Douglas Gregorb4debae2009-12-22 17:47:17 +00001311 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1312 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001313 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001314 }
1315
Douglas Gregor1426e532009-05-12 21:31:51 +00001316 if (ParseExpressionList(Exprs, CommaLocs)) {
1317 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001318
1319 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001320 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001321 ExitScope();
1322 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001323 } else {
1324 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001325 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001326
1327 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1328 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001329
1330 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001332 ExitScope();
1333 }
1334
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001335 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001336 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001337 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001338 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001339 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001340 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1341 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001342 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1343
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001344 if (D.getCXXScopeSpec().isSet()) {
1345 EnterScope(0);
1346 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1347 }
1348
1349 ExprResult Init(ParseBraceInitializer());
1350
1351 if (D.getCXXScopeSpec().isSet()) {
1352 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1353 ExitScope();
1354 }
1355
1356 if (Init.isInvalid()) {
1357 Actions.ActOnInitializerError(ThisDecl);
1358 } else
1359 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1360 /*DirectInit=*/true, TypeContainsAuto);
1361
Douglas Gregor1426e532009-05-12 21:31:51 +00001362 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001363 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001364 }
1365
Richard Smith483b9f32011-02-21 20:05:19 +00001366 Actions.FinalizeDeclaration(ThisDecl);
1367
Douglas Gregor1426e532009-05-12 21:31:51 +00001368 return ThisDecl;
1369}
1370
Reid Spencer5f016e22007-07-11 17:01:13 +00001371/// ParseSpecifierQualifierList
1372/// specifier-qualifier-list:
1373/// type-specifier specifier-qualifier-list[opt]
1374/// type-qualifier specifier-qualifier-list[opt]
1375/// [GNU] attributes specifier-qualifier-list[opt]
1376///
Richard Smithc89edf52011-07-01 19:46:12 +00001377void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1379 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001380 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001381 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 // Validate declspec for type-name.
1384 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001385 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001386 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 // Issue diagnostic and remove storage class if present.
1390 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1391 if (DS.getStorageClassSpecLoc().isValid())
1392 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1393 else
1394 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1395 DS.ClearStorageClassSpecs();
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 // Issue diagnostic and remove function specfier if present.
1399 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001400 if (DS.isInlineSpecified())
1401 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1402 if (DS.isVirtualSpecified())
1403 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1404 if (DS.isExplicitSpecified())
1405 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 DS.ClearFunctionSpecs();
1407 }
1408}
1409
Chris Lattnerc199ab32009-04-12 20:42:31 +00001410/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1411/// specified token is valid after the identifier in a declarator which
1412/// immediately follows the declspec. For example, these things are valid:
1413///
1414/// int x [ 4]; // direct-declarator
1415/// int x ( int y); // direct-declarator
1416/// int(int x ) // direct-declarator
1417/// int x ; // simple-declaration
1418/// int x = 17; // init-declarator-list
1419/// int x , y; // init-declarator-list
1420/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001421/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001422/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001423///
1424/// This is not, because 'x' does not immediately follow the declspec (though
1425/// ')' happens to be valid anyway).
1426/// int (x)
1427///
1428static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1429 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1430 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001431 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001432}
1433
Chris Lattnere40c2952009-04-14 21:34:55 +00001434
1435/// ParseImplicitInt - This method is called when we have an non-typename
1436/// identifier in a declspec (which normally terminates the decl spec) when
1437/// the declspec has no type specifier. In this case, the declspec is either
1438/// malformed or is "implicit int" (in K&R and C89).
1439///
1440/// This method handles diagnosing this prettily and returns false if the
1441/// declspec is done being processed. If it recovers and thinks there may be
1442/// other pieces of declspec after it, it returns true.
1443///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001444bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001446 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001447 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Chris Lattnere40c2952009-04-14 21:34:55 +00001449 SourceLocation Loc = Tok.getLocation();
1450 // If we see an identifier that is not a type name, we normally would
1451 // parse it as the identifer being declared. However, when a typename
1452 // is typo'd or the definition is not included, this will incorrectly
1453 // parse the typename as the identifier name and fall over misparsing
1454 // later parts of the diagnostic.
1455 //
1456 // As such, we try to do some look-ahead in cases where this would
1457 // otherwise be an "implicit-int" case to see if this is invalid. For
1458 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1459 // an identifier with implicit int, we'd get a parse error because the
1460 // next token is obviously invalid for a type. Parse these as a case
1461 // with an invalid type specifier.
1462 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Chris Lattnere40c2952009-04-14 21:34:55 +00001464 // Since we know that this either implicit int (which is rare) or an
1465 // error, we'd do lookahead to try to do better recovery.
1466 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1467 // If this token is valid for implicit int, e.g. "static x = 4", then
1468 // we just avoid eating the identifier, so it will be parsed as the
1469 // identifier in the declarator.
1470 return false;
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Chris Lattnere40c2952009-04-14 21:34:55 +00001473 // Otherwise, if we don't consume this token, we are going to emit an
1474 // error anyway. Try to recover from various common problems. Check
1475 // to see if this was a reference to a tag name without a tag specified.
1476 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001477 //
1478 // C++ doesn't need this, and isTagName doesn't take SS.
1479 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001480 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001481 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregor23c94db2010-07-02 17:43:08 +00001483 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001484 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001485 case DeclSpec::TST_enum:
1486 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1487 case DeclSpec::TST_union:
1488 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1489 case DeclSpec::TST_struct:
1490 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1491 case DeclSpec::TST_class:
1492 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Chris Lattnerf4382f52009-04-14 22:17:06 +00001495 if (TagName) {
1496 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001497 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001498 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Chris Lattnerf4382f52009-04-14 22:17:06 +00001500 // Parse this as a tag as if the missing tag were present.
1501 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001502 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001503 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001504 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001505 return true;
1506 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregora786fdb2009-10-13 23:27:22 +00001509 // This is almost certainly an invalid type name. Let the action emit a
1510 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001511 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001512 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001513 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001514 // The action emitted a diagnostic, so we don't have to.
1515 if (T) {
1516 // The action has suggested that the type T could be used. Set that as
1517 // the type in the declaration specifiers, consume the would-be type
1518 // name token, and we're done.
1519 const char *PrevSpec;
1520 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001521 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001522 DS.SetRangeEnd(Tok.getLocation());
1523 ConsumeToken();
1524
1525 // There may be other declaration specifiers after this.
1526 return true;
1527 }
1528
1529 // Fall through; the action had no suggestion for us.
1530 } else {
1531 // The action did not emit a diagnostic, so emit one now.
1532 SourceRange R;
1533 if (SS) R = SS->getRange();
1534 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregora786fdb2009-10-13 23:27:22 +00001537 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001538 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001539 unsigned DiagID;
1540 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001541 DS.SetRangeEnd(Tok.getLocation());
1542 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Chris Lattnere40c2952009-04-14 21:34:55 +00001544 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1545 // avoid rippling error messages on subsequent uses of the same type,
1546 // could be useful if #include was forgotten.
1547 return false;
1548}
1549
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001550/// \brief Determine the declaration specifier context from the declarator
1551/// context.
1552///
1553/// \param Context the declarator context, which is one of the
1554/// Declarator::TheContext enumerator values.
1555Parser::DeclSpecContext
1556Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1557 if (Context == Declarator::MemberContext)
1558 return DSC_class;
1559 if (Context == Declarator::FileContext)
1560 return DSC_top_level;
1561 return DSC_normal;
1562}
1563
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001564/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1565///
1566/// FIXME: Simply returns an alignof() expression if the argument is a
1567/// type. Ideally, the type should be propagated directly into Sema.
1568///
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001569/// [C1X] type-id
1570/// [C1X] constant-expression
1571/// [C++0x] type-id ...[opt]
1572/// [C++0x] assignment-expression ...[opt]
1573ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1574 SourceLocation &EllipsisLoc) {
1575 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001576 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001577 SourceLocation TypeLoc = Tok.getLocation();
1578 ParsedType Ty = ParseTypeName().get();
1579 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001580 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1581 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001582 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001583 ER = ParseConstantExpression();
1584
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001585 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1586 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001587
1588 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001589}
1590
1591/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1592/// attribute to Attrs.
1593///
1594/// alignment-specifier:
1595/// [C1X] '_Alignas' '(' type-id ')'
1596/// [C1X] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001597/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1598/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001599void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1600 SourceLocation *endLoc) {
1601 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1602 "Not an alignment-specifier!");
1603
1604 SourceLocation KWLoc = Tok.getLocation();
1605 ConsumeToken();
1606
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001607 BalancedDelimiterTracker T(*this, tok::l_paren);
1608 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001609 return;
1610
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001611 SourceLocation EllipsisLoc;
1612 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001613 if (ArgExpr.isInvalid()) {
1614 SkipUntil(tok::r_paren);
1615 return;
1616 }
1617
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001618 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001619 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001620 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001621
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001622 // FIXME: Handle pack-expansions here.
1623 if (EllipsisLoc.isValid()) {
1624 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1625 return;
1626 }
1627
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001628 ExprVector ArgExprs(Actions);
1629 ArgExprs.push_back(ArgExpr.release());
1630 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001631 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001632}
1633
Reid Spencer5f016e22007-07-11 17:01:13 +00001634/// ParseDeclarationSpecifiers
1635/// declaration-specifiers: [C99 6.7]
1636/// storage-class-specifier declaration-specifiers[opt]
1637/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001638/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001639/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001640/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001641/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001642///
1643/// storage-class-specifier: [C99 6.7.1]
1644/// 'typedef'
1645/// 'extern'
1646/// 'static'
1647/// 'auto'
1648/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001649/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001650/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001651/// function-specifier: [C99 6.7.4]
1652/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001653/// [C++] 'virtual'
1654/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001655/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001656/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001657/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001658
Reid Spencer5f016e22007-07-11 17:01:13 +00001659///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001660void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001661 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001662 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001663 DeclSpecContext DSContext) {
1664 if (DS.getSourceRange().isInvalid()) {
1665 DS.SetRangeStart(Tok.getLocation());
1666 DS.SetRangeEnd(Tok.getLocation());
1667 }
1668
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001669 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001671 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001673 unsigned DiagID = 0;
1674
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001676
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001678 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001679 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001680 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1681 MaybeParseCXX0XAttributes(DS.getAttributes());
1682
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 // If this is not a declaration specifier token, we're done reading decl
1684 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001685 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001688 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001689 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001690 if (DS.hasTypeSpecifier()) {
1691 bool AllowNonIdentifiers
1692 = (getCurScope()->getFlags() & (Scope::ControlScope |
1693 Scope::BlockScope |
1694 Scope::TemplateParamScope |
1695 Scope::FunctionPrototypeScope |
1696 Scope::AtCatchScope)) == 0;
1697 bool AllowNestedNameSpecifiers
1698 = DSContext == DSC_top_level ||
1699 (DSContext == DSC_class && DS.isFriendSpecified());
1700
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001701 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1702 AllowNonIdentifiers,
1703 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001704 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001705 }
1706
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001707 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1708 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1709 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001710 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1711 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001712 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001713 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001714 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001715 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001716
1717 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001718 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001719 }
1720
Chris Lattner5e02c472009-01-05 00:07:25 +00001721 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001722 // C++ scope specifier. Annotate and loop, or bail out on error.
1723 if (TryAnnotateCXXScopeToken(true)) {
1724 if (!DS.hasTypeSpecifier())
1725 DS.SetTypeSpecError();
1726 goto DoneWithDeclSpec;
1727 }
John McCall2e0a7152010-03-01 18:20:46 +00001728 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1729 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001730 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001731
1732 case tok::annot_cxxscope: {
1733 if (DS.hasTypeSpecifier())
1734 goto DoneWithDeclSpec;
1735
John McCallaa87d332009-12-12 11:40:51 +00001736 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001737 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1738 Tok.getAnnotationRange(),
1739 SS);
John McCallaa87d332009-12-12 11:40:51 +00001740
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001741 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001742 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001743 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001744 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001745 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001746 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001747
1748 // C++ [class.qual]p2:
1749 // In a lookup in which the constructor is an acceptable lookup
1750 // result and the nested-name-specifier nominates a class C:
1751 //
1752 // - if the name specified after the
1753 // nested-name-specifier, when looked up in C, is the
1754 // injected-class-name of C (Clause 9), or
1755 //
1756 // - if the name specified after the nested-name-specifier
1757 // is the same as the identifier or the
1758 // simple-template-id's template-name in the last
1759 // component of the nested-name-specifier,
1760 //
1761 // the name is instead considered to name the constructor of
1762 // class C.
1763 //
1764 // Thus, if the template-name is actually the constructor
1765 // name, then the code is ill-formed; this interpretation is
1766 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001767 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001768 if ((DSContext == DSC_top_level ||
1769 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1770 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001771 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001772 if (isConstructorDeclarator()) {
1773 // The user meant this to be an out-of-line constructor
1774 // definition, but template arguments are not allowed
1775 // there. Just allow this as a constructor; we'll
1776 // complain about it later.
1777 goto DoneWithDeclSpec;
1778 }
1779
1780 // The user meant this to name a type, but it actually names
1781 // a constructor with some extraneous template
1782 // arguments. Complain, then parse it as a type as the user
1783 // intended.
1784 Diag(TemplateId->TemplateNameLoc,
1785 diag::err_out_of_line_template_id_names_constructor)
1786 << TemplateId->Name;
1787 }
1788
John McCallaa87d332009-12-12 11:40:51 +00001789 DS.getTypeSpecScope() = SS;
1790 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001791 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001792 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001793 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001794 continue;
1795 }
1796
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001797 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001798 DS.getTypeSpecScope() = SS;
1799 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001800 if (Tok.getAnnotationValue()) {
1801 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001802 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1803 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001804 PrevSpec, DiagID, T);
1805 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001806 else
1807 DS.SetTypeSpecError();
1808 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1809 ConsumeToken(); // The typename
1810 }
1811
Douglas Gregor9135c722009-03-25 15:40:00 +00001812 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001813 goto DoneWithDeclSpec;
1814
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001815 // If we're in a context where the identifier could be a class name,
1816 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001817 if ((DSContext == DSC_top_level ||
1818 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001819 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001820 &SS)) {
1821 if (isConstructorDeclarator())
1822 goto DoneWithDeclSpec;
1823
1824 // As noted in C++ [class.qual]p2 (cited above), when the name
1825 // of the class is qualified in a context where it could name
1826 // a constructor, its a constructor name. However, we've
1827 // looked at the declarator, and the user probably meant this
1828 // to be a type. Complain that it isn't supposed to be treated
1829 // as a type, then proceed to parse it as a type.
1830 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1831 << Next.getIdentifierInfo();
1832 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001833
John McCallb3d87482010-08-24 05:47:05 +00001834 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1835 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001836 getCurScope(), &SS,
1837 false, false, ParsedType(),
1838 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001839
Chris Lattnerf4382f52009-04-14 22:17:06 +00001840 // If the referenced identifier is not a type, then this declspec is
1841 // erroneous: We already checked about that it has no type specifier, and
1842 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001843 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001844 if (TypeRep == 0) {
1845 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001846 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001847 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
John McCallaa87d332009-12-12 11:40:51 +00001850 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001851 ConsumeToken(); // The C++ scope.
1852
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001854 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001855 if (isInvalid)
1856 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001858 DS.SetRangeEnd(Tok.getLocation());
1859 ConsumeToken(); // The typename.
1860
1861 continue;
1862 }
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Chris Lattner80d0c892009-01-21 19:48:37 +00001864 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001865 if (Tok.getAnnotationValue()) {
1866 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001868 DiagID, T);
1869 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001870 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001871
1872 if (isInvalid)
1873 break;
1874
Chris Lattner80d0c892009-01-21 19:48:37 +00001875 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1876 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Chris Lattner80d0c892009-01-21 19:48:37 +00001878 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1879 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001880 // Objective-C interface.
1881 if (Tok.is(tok::less) && getLang().ObjC1)
1882 ParseObjCProtocolQualifiers(DS);
1883
Chris Lattner80d0c892009-01-21 19:48:37 +00001884 continue;
1885 }
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Douglas Gregorbfad9152011-04-28 15:48:45 +00001887 case tok::kw___is_signed:
1888 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1889 // typically treats it as a trait. If we see __is_signed as it appears
1890 // in libstdc++, e.g.,
1891 //
1892 // static const bool __is_signed;
1893 //
1894 // then treat __is_signed as an identifier rather than as a keyword.
1895 if (DS.getTypeSpecType() == TST_bool &&
1896 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1897 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1898 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1899 Tok.setKind(tok::identifier);
1900 }
1901
1902 // We're done with the declaration-specifiers.
1903 goto DoneWithDeclSpec;
1904
Chris Lattner3bd934a2008-07-26 01:18:38 +00001905 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001906 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001907 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001908 // In C++, check to see if this is a scope specifier like foo::bar::, if
1909 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001910 if (getLang().CPlusPlus) {
1911 if (TryAnnotateCXXScopeToken(true)) {
1912 if (!DS.hasTypeSpecifier())
1913 DS.SetTypeSpecError();
1914 goto DoneWithDeclSpec;
1915 }
1916 if (!Tok.is(tok::identifier))
1917 continue;
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Chris Lattner3bd934a2008-07-26 01:18:38 +00001920 // This identifier can only be a typedef name if we haven't already seen
1921 // a type-specifier. Without this check we misparse:
1922 // typedef int X; struct Y { short X; }; as 'short int'.
1923 if (DS.hasTypeSpecifier())
1924 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
John Thompson82287d12010-02-05 00:12:22 +00001926 // Check for need to substitute AltiVec keyword tokens.
1927 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1928 break;
1929
Chris Lattner3bd934a2008-07-26 01:18:38 +00001930 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001931 ParsedType TypeRep =
1932 Actions.getTypeName(*Tok.getIdentifierInfo(),
1933 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001934
Chris Lattnerc199ab32009-04-12 20:42:31 +00001935 // If this is not a typedef name, don't parse it as part of the declspec,
1936 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001937 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001938 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001939 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001940 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001941
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001942 // If we're in a context where the identifier could be a class name,
1943 // check whether this is a constructor declaration.
1944 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001945 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001946 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001947 goto DoneWithDeclSpec;
1948
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001950 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001951 if (isInvalid)
1952 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Chris Lattner3bd934a2008-07-26 01:18:38 +00001954 DS.SetRangeEnd(Tok.getLocation());
1955 ConsumeToken(); // The identifier
1956
1957 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1958 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001959 // Objective-C interface.
1960 if (Tok.is(tok::less) && getLang().ObjC1)
1961 ParseObjCProtocolQualifiers(DS);
1962
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001963 // Need to support trailing type qualifiers (e.g. "id<p> const").
1964 // If a type specifier follows, it will be diagnosed elsewhere.
1965 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001966 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001967
1968 // type-name
1969 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001970 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001971 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001972 // This template-id does not refer to a type name, so we're
1973 // done with the type-specifiers.
1974 goto DoneWithDeclSpec;
1975 }
1976
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001977 // If we're in a context where the template-id could be a
1978 // constructor name or specialization, check whether this is a
1979 // constructor declaration.
1980 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001981 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001982 isConstructorDeclarator())
1983 goto DoneWithDeclSpec;
1984
Douglas Gregor39a8de12009-02-25 19:37:18 +00001985 // Turn the template-id annotation token into a type annotation
1986 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001987 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001988 continue;
1989 }
1990
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 // GNU attributes support.
1992 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001993 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001995
1996 // Microsoft declspec support.
1997 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001998 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001999 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Steve Naroff239f0732008-12-25 14:16:32 +00002001 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002002 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002003 // FIXME: Add handling here!
2004 break;
2005
2006 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002007 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002008 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002009 case tok::kw___cdecl:
2010 case tok::kw___stdcall:
2011 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002012 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002013 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002014 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002015 continue;
2016
Dawn Perchik52fc3142010-09-03 01:29:35 +00002017 // Borland single token adornments.
2018 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002019 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002020 continue;
2021
Peter Collingbournef315fa82011-02-14 01:42:53 +00002022 // OpenCL single token adornments.
2023 case tok::kw___kernel:
2024 ParseOpenCLAttributes(DS.getAttributes());
2025 continue;
2026
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 // storage-class-specifier
2028 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002029 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2030 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 break;
2032 case tok::kw_extern:
2033 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002034 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002035 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2036 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002038 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002039 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2040 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002041 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 case tok::kw_static:
2043 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002044 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002045 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2046 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 break;
2048 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002049 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002050 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002051 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2052 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002053 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002054 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002055 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002056 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2058 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002059 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002060 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2061 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 break;
2063 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002064 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2065 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002067 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002068 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2069 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002070 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002072 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 // function-specifier
2076 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002079 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002081 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002082 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002084 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002085
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086 // alignment-specifier
2087 case tok::kw__Alignas:
2088 if (!getLang().C1X)
2089 Diag(Tok, diag::ext_c1x_alignas);
2090 ParseAlignmentSpecifier(DS.getAttributes());
2091 continue;
2092
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002093 // friend
2094 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002095 if (DSContext == DSC_class)
2096 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2097 else {
2098 PrevSpec = ""; // not actually used by the diagnostic
2099 DiagID = diag::err_friend_invalid_in_context;
2100 isInvalid = true;
2101 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002102 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Douglas Gregor8d267c52011-09-09 02:06:17 +00002104 // Modules
2105 case tok::kw___module_private__:
2106 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2107 break;
2108
Sebastian Redl2ac67232009-11-05 15:47:02 +00002109 // constexpr
2110 case tok::kw_constexpr:
2111 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2112 break;
2113
Chris Lattner80d0c892009-01-21 19:48:37 +00002114 // type-specifier
2115 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002116 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2117 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002118 break;
2119 case tok::kw_long:
2120 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002121 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2122 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002123 else
John McCallfec54012009-08-03 20:12:06 +00002124 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002127 case tok::kw___int64:
2128 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2129 DiagID);
2130 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002131 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002132 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2133 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002134 break;
2135 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002136 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2137 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002138 break;
2139 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002140 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2141 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002142 break;
2143 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002144 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2145 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002146 break;
2147 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2149 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002150 break;
2151 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2153 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002154 break;
2155 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2157 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002158 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002159 case tok::kw_half:
2160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2161 DiagID);
2162 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002163 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
2167 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2169 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002170 break;
2171 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2173 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002174 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002175 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002176 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2177 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002178 break;
2179 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2181 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002182 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002183 case tok::kw_bool:
2184 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002185 if (Tok.is(tok::kw_bool) &&
2186 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2187 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2188 PrevSpec = ""; // Not used by the diagnostic.
2189 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002190 // For better error recovery.
2191 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002192 isInvalid = true;
2193 } else {
2194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2195 DiagID);
2196 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002197 break;
2198 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002199 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2200 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002201 break;
2202 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002203 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2204 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002205 break;
2206 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2208 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002209 break;
John Thompson82287d12010-02-05 00:12:22 +00002210 case tok::kw___vector:
2211 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2212 break;
2213 case tok::kw___pixel:
2214 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2215 break;
John McCalla5fc4722011-04-09 22:50:59 +00002216 case tok::kw___unknown_anytype:
2217 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2218 PrevSpec, DiagID);
2219 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002220
2221 // class-specifier:
2222 case tok::kw_class:
2223 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002224 case tok::kw_union: {
2225 tok::TokenKind Kind = Tok.getKind();
2226 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002227 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002228 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002229 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002230
2231 // enum-specifier:
2232 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002233 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002234 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 continue;
2236
2237 // cv-qualifier:
2238 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002239 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2240 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002241 break;
2242 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002243 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2244 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002245 break;
2246 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002247 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2248 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 break;
2250
Douglas Gregord57959a2009-03-27 23:10:48 +00002251 // C++ typename-specifier:
2252 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002253 if (TryAnnotateTypeOrScopeToken()) {
2254 DS.SetTypeSpecError();
2255 goto DoneWithDeclSpec;
2256 }
2257 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002258 continue;
2259 break;
2260
Chris Lattner80d0c892009-01-21 19:48:37 +00002261 // GNU typeof support.
2262 case tok::kw_typeof:
2263 ParseTypeofSpecifier(DS);
2264 continue;
2265
David Blaikie42d6d0c2011-12-04 05:04:18 +00002266 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002267 ParseDecltypeSpecifier(DS);
2268 continue;
2269
Sean Huntdb5d44b2011-05-19 05:37:45 +00002270 case tok::kw___underlying_type:
2271 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002272 continue;
2273
2274 case tok::kw__Atomic:
2275 ParseAtomicSpecifier(DS);
2276 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002277
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002278 // OpenCL qualifiers:
2279 case tok::kw_private:
2280 if (!getLang().OpenCL)
2281 goto DoneWithDeclSpec;
2282 case tok::kw___private:
2283 case tok::kw___global:
2284 case tok::kw___local:
2285 case tok::kw___constant:
2286 case tok::kw___read_only:
2287 case tok::kw___write_only:
2288 case tok::kw___read_write:
2289 ParseOpenCLQualifiers(DS);
2290 break;
2291
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002292 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002293 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002294 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2295 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002296 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002297 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Douglas Gregor46f936e2010-11-19 17:10:50 +00002299 if (!ParseObjCProtocolQualifiers(DS))
2300 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2301 << FixItHint::CreateInsertion(Loc, "id")
2302 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002303
2304 // Need to support trailing type qualifiers (e.g. "id<p> const").
2305 // If a type specifier follows, it will be diagnosed elsewhere.
2306 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 }
John McCallfec54012009-08-03 20:12:06 +00002308 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 if (isInvalid) {
2310 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002311 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002312
2313 if (DiagID == diag::ext_duplicate_declspec)
2314 Diag(Tok, DiagID)
2315 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2316 else
2317 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002319
Chris Lattner81c018d2008-03-13 06:29:04 +00002320 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002321 if (DiagID != diag::err_bool_redeclaration)
2322 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 }
2324}
Douglas Gregoradcac882008-12-01 23:54:00 +00002325
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002326/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002327/// primarily follow the C++ grammar with additions for C99 and GNU,
2328/// which together subsume the C grammar. Note that the C++
2329/// type-specifier also includes the C type-qualifier (for const,
2330/// volatile, and C99 restrict). Returns true if a type-specifier was
2331/// found (and parsed), false otherwise.
2332///
2333/// type-specifier: [C++ 7.1.5]
2334/// simple-type-specifier
2335/// class-specifier
2336/// enum-specifier
2337/// elaborated-type-specifier [TODO]
2338/// cv-qualifier
2339///
2340/// cv-qualifier: [C++ 7.1.5.1]
2341/// 'const'
2342/// 'volatile'
2343/// [C99] 'restrict'
2344///
2345/// simple-type-specifier: [ C++ 7.1.5.2]
2346/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2347/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2348/// 'char'
2349/// 'wchar_t'
2350/// 'bool'
2351/// 'short'
2352/// 'int'
2353/// 'long'
2354/// 'signed'
2355/// 'unsigned'
2356/// 'float'
2357/// 'double'
2358/// 'void'
2359/// [C99] '_Bool'
2360/// [C99] '_Complex'
2361/// [C99] '_Imaginary' // Removed in TC2?
2362/// [GNU] '_Decimal32'
2363/// [GNU] '_Decimal64'
2364/// [GNU] '_Decimal128'
2365/// [GNU] typeof-specifier
2366/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2367/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002368/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002369/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002370bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002371 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002372 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002373 const ParsedTemplateInfo &TemplateInfo,
2374 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002375 SourceLocation Loc = Tok.getLocation();
2376
2377 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002378 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002379 // If we already have a type specifier, this identifier is not a type.
2380 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2381 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2382 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2383 return false;
John Thompson82287d12010-02-05 00:12:22 +00002384 // Check for need to substitute AltiVec keyword tokens.
2385 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2386 break;
2387 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002388 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002389 case tok::kw_typename: // typename foo::bar
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 if (Tok.is(tok::identifier))
2396 return false;
2397 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2398 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002399 case tok::coloncolon: // ::foo::bar
2400 if (NextToken().is(tok::kw_new) || // ::new
2401 NextToken().is(tok::kw_delete)) // ::delete
2402 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002403
Chris Lattner166a8fc2009-01-04 23:41:41 +00002404 // Annotate typenames and C++ scope specifiers. If we get one, just
2405 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002406 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2407 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002408 return true;
2409 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2410 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Douglas Gregor12e083c2008-11-07 15:42:26 +00002412 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002413 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002414 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002415 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2416 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002417 DiagID, T);
2418 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002419 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002420 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2421 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Douglas Gregor12e083c2008-11-07 15:42:26 +00002423 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2424 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2425 // Objective-C interface. If we don't have Objective-C or a '<', this is
2426 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002427 if (Tok.is(tok::less) && getLang().ObjC1)
2428 ParseObjCProtocolQualifiers(DS);
2429
Douglas Gregor12e083c2008-11-07 15:42:26 +00002430 return true;
2431 }
2432
2433 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002434 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002435 break;
2436 case tok::kw_long:
2437 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002438 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2439 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002440 else
John McCallfec54012009-08-03 20:12:06 +00002441 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2442 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002443 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002444 case tok::kw___int64:
2445 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2446 DiagID);
2447 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002448 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002449 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002450 break;
2451 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002452 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2453 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002454 break;
2455 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002456 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2457 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002458 break;
2459 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002460 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2461 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 break;
2463 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002465 break;
2466 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002467 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002468 break;
2469 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002470 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002471 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002472 case tok::kw_half:
2473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2474 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002475 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002477 break;
2478 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002480 break;
2481 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002483 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002484 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002486 break;
2487 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002489 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002490 case tok::kw_bool:
2491 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002493 break;
2494 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2496 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002497 break;
2498 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2500 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002501 break;
2502 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2504 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002505 break;
John Thompson82287d12010-02-05 00:12:22 +00002506 case tok::kw___vector:
2507 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2508 break;
2509 case tok::kw___pixel:
2510 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2511 break;
2512
Douglas Gregor12e083c2008-11-07 15:42:26 +00002513 // class-specifier:
2514 case tok::kw_class:
2515 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002516 case tok::kw_union: {
2517 tok::TokenKind Kind = Tok.getKind();
2518 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002519 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002520 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002521 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002522 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002523 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002524
2525 // enum-specifier:
2526 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002527 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002528 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002529 return true;
2530
2531 // cv-qualifier:
2532 case tok::kw_const:
2533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002534 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002535 break;
2536 case tok::kw_volatile:
2537 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002538 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002539 break;
2540 case tok::kw_restrict:
2541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002542 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002543 break;
2544
2545 // GNU typeof support.
2546 case tok::kw_typeof:
2547 ParseTypeofSpecifier(DS);
2548 return true;
2549
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002550 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002551 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002552 ParseDecltypeSpecifier(DS);
2553 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Sean Huntdb5d44b2011-05-19 05:37:45 +00002555 // C++0x type traits support.
2556 case tok::kw___underlying_type:
2557 ParseUnderlyingTypeSpecifier(DS);
2558 return true;
2559
Eli Friedmanb001de72011-10-06 23:00:33 +00002560 case tok::kw__Atomic:
2561 ParseAtomicSpecifier(DS);
2562 return true;
2563
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002564 // OpenCL qualifiers:
2565 case tok::kw_private:
2566 if (!getLang().OpenCL)
2567 return false;
2568 case tok::kw___private:
2569 case tok::kw___global:
2570 case tok::kw___local:
2571 case tok::kw___constant:
2572 case tok::kw___read_only:
2573 case tok::kw___write_only:
2574 case tok::kw___read_write:
2575 ParseOpenCLQualifiers(DS);
2576 break;
2577
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002578 // C++0x auto support.
2579 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002580 // This is only called in situations where a storage-class specifier is
2581 // illegal, so we can assume an auto type specifier was intended even in
2582 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2583 // extension diagnostic.
2584 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002585 return false;
2586
John McCallfec54012009-08-03 20:12:06 +00002587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002588 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002589
Eli Friedman290eeb02009-06-08 23:27:34 +00002590 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002591 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002592 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002593 case tok::kw___cdecl:
2594 case tok::kw___stdcall:
2595 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002596 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002597 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002598 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002599 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002600
Dawn Perchik52fc3142010-09-03 01:29:35 +00002601 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002602 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002603 return true;
2604
Douglas Gregor12e083c2008-11-07 15:42:26 +00002605 default:
2606 // Not a type-specifier; do nothing.
2607 return false;
2608 }
2609
2610 // If the specifier combination wasn't legal, issue a diagnostic.
2611 if (isInvalid) {
2612 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002613 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002614 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002615 }
2616 DS.SetRangeEnd(Tok.getLocation());
2617 ConsumeToken(); // whatever we parsed above.
2618 return true;
2619}
Reid Spencer5f016e22007-07-11 17:01:13 +00002620
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002621/// ParseStructDeclaration - Parse a struct declaration without the terminating
2622/// semicolon.
2623///
Reid Spencer5f016e22007-07-11 17:01:13 +00002624/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002625/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002626/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002627/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002628/// struct-declarator-list:
2629/// struct-declarator
2630/// struct-declarator-list ',' struct-declarator
2631/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2632/// struct-declarator:
2633/// declarator
2634/// [GNU] declarator attributes[opt]
2635/// declarator[opt] ':' constant-expression
2636/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2637///
Chris Lattnere1359422008-04-10 06:46:29 +00002638void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002639ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002640
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002641 if (Tok.is(tok::kw___extension__)) {
2642 // __extension__ silences extension warnings in the subexpression.
2643 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002644 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002645 return ParseStructDeclaration(DS, Fields);
2646 }
Mike Stump1eb44332009-09-09 15:08:12 +00002647
Steve Naroff28a7ca82007-08-20 22:28:22 +00002648 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002649 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002651 // If there are no declarators, this is a free-standing declaration
2652 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002653 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002654 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002655 return;
2656 }
2657
2658 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002659 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002660 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002661 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002662 FieldDeclarator DeclaratorInfo(DS);
2663
2664 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002665 if (!FirstDeclarator)
2666 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Steve Naroff28a7ca82007-08-20 22:28:22 +00002668 /// struct-declarator: declarator
2669 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002670 if (Tok.isNot(tok::colon)) {
2671 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2672 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002673 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002674 }
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Chris Lattner04d66662007-10-09 17:33:22 +00002676 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002677 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002678 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002679 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002680 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002681 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002682 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002683 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002684
Steve Naroff28a7ca82007-08-20 22:28:22 +00002685 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002686 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002687
John McCallbdd563e2009-11-03 02:38:08 +00002688 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002689 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002690 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002691
Steve Naroff28a7ca82007-08-20 22:28:22 +00002692 // If we don't have a comma, it is either the end of the list (a ';')
2693 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002694 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002695 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002696
Steve Naroff28a7ca82007-08-20 22:28:22 +00002697 // Consume the comma.
2698 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002699
John McCallbdd563e2009-11-03 02:38:08 +00002700 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002701 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002702}
2703
2704/// ParseStructUnionBody
2705/// struct-contents:
2706/// struct-declaration-list
2707/// [EXT] empty
2708/// [GNU] "struct-declaration-list" without terminatoring ';'
2709/// struct-declaration-list:
2710/// struct-declaration
2711/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002712/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002713///
Reid Spencer5f016e22007-07-11 17:01:13 +00002714void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002715 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002716 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2717 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002719 BalancedDelimiterTracker T(*this, tok::l_brace);
2720 if (T.consumeOpen())
2721 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002723 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002724 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002725
Reid Spencer5f016e22007-07-11 17:01:13 +00002726 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2727 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002728 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002729 Diag(Tok, diag::ext_empty_struct_union)
2730 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002731
Chris Lattner5f9e2722011-07-23 10:55:15 +00002732 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002733
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002735 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002736 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002737
Reid Spencer5f016e22007-07-11 17:01:13 +00002738 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002739 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002740 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002741 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002742 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002743 ConsumeToken();
2744 continue;
2745 }
Chris Lattnere1359422008-04-10 06:46:29 +00002746
2747 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002748 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002749
John McCallbdd563e2009-11-03 02:38:08 +00002750 if (!Tok.is(tok::at)) {
2751 struct CFieldCallback : FieldCallback {
2752 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002753 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002754 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002755
John McCalld226f652010-08-21 09:40:31 +00002756 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002757 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002758 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2759
John McCalld226f652010-08-21 09:40:31 +00002760 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002761 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002762 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002763 FD.D.getDeclSpec().getSourceRange().getBegin(),
2764 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002765 FieldDecls.push_back(Field);
2766 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002767 }
John McCallbdd563e2009-11-03 02:38:08 +00002768 } Callback(*this, TagDecl, FieldDecls);
2769
2770 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002771 } else { // Handle @defs
2772 ConsumeToken();
2773 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2774 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002775 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002776 continue;
2777 }
2778 ConsumeToken();
2779 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2780 if (!Tok.is(tok::identifier)) {
2781 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002782 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002783 continue;
2784 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002785 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002786 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002787 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002788 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2789 ConsumeToken();
2790 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002791 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002792
Chris Lattner04d66662007-10-09 17:33:22 +00002793 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002794 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002795 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002796 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 break;
2798 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002799 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2800 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002801 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002802 // If we stopped at a ';', eat it.
2803 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002804 }
2805 }
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002807 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002808
John McCall0b7e6782011-03-24 11:26:52 +00002809 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002811 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002812
Douglas Gregor23c94db2010-07-02 17:43:08 +00002813 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002814 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002815 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002816 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002817 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002818 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2819 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002820}
2821
Reid Spencer5f016e22007-07-11 17:01:13 +00002822/// ParseEnumSpecifier
2823/// enum-specifier: [C99 6.7.2.2]
2824/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002825///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002826/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2827/// '}' attributes[opt]
2828/// 'enum' identifier
2829/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002830///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002831/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2832/// [C++0x] enum-head '{' enumerator-list ',' '}'
2833///
2834/// enum-head: [C++0x]
2835/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2836/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2837///
2838/// enum-key: [C++0x]
2839/// 'enum'
2840/// 'enum' 'class'
2841/// 'enum' 'struct'
2842///
2843/// enum-base: [C++0x]
2844/// ':' type-specifier-seq
2845///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002846/// [C++] elaborated-type-specifier:
2847/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2848///
Chris Lattner4c97d762009-04-12 21:49:30 +00002849void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002850 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002851 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002852 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002853 if (Tok.is(tok::code_completion)) {
2854 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002855 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002856 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002857 }
John McCall57c13002011-07-06 05:58:41 +00002858
2859 bool IsScopedEnum = false;
2860 bool IsScopedUsingClassTag = false;
2861
2862 if (getLang().CPlusPlus0x &&
2863 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002864 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002865 IsScopedEnum = true;
2866 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2867 ConsumeToken();
2868 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002869
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002870 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002871 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002872 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002873
Douglas Gregor5471bc82011-09-08 17:18:35 +00002874 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002875 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002876
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002877 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002878 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002879 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2880 // if a fixed underlying type is allowed.
2881 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2882
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002883 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2884 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002885 return;
2886
2887 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002888 Diag(Tok, diag::err_expected_ident);
2889 if (Tok.isNot(tok::l_brace)) {
2890 // Has no name and is not a definition.
2891 // Skip the rest of this declarator, up until the comma or semicolon.
2892 SkipUntil(tok::comma, true);
2893 return;
2894 }
2895 }
2896 }
Mike Stump1eb44332009-09-09 15:08:12 +00002897
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002898 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002899 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2900 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002901 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002903 // Skip the rest of this declarator, up until the comma or semicolon.
2904 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002905 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002906 }
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002908 // If an identifier is present, consume and remember it.
2909 IdentifierInfo *Name = 0;
2910 SourceLocation NameLoc;
2911 if (Tok.is(tok::identifier)) {
2912 Name = Tok.getIdentifierInfo();
2913 NameLoc = ConsumeToken();
2914 }
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002916 if (!Name && IsScopedEnum) {
2917 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2918 // declaration of a scoped enumeration.
2919 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2920 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002921 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002922 }
2923
2924 TypeResult BaseType;
2925
Douglas Gregora61b3e72010-12-01 17:42:47 +00002926 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002927 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002928 bool PossibleBitfield = false;
2929 if (getCurScope()->getFlags() & Scope::ClassScope) {
2930 // If we're in class scope, this can either be an enum declaration with
2931 // an underlying type, or a declaration of a bitfield member. We try to
2932 // use a simple disambiguation scheme first to catch the common cases
2933 // (integer literal, sizeof); if it's still ambiguous, we then consider
2934 // anything that's a simple-type-specifier followed by '(' as an
2935 // expression. This suffices because function types are not valid
2936 // underlying types anyway.
2937 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2938 // If the next token starts an expression, we know we're parsing a
2939 // bit-field. This is the common case.
2940 if (TPR == TPResult::True())
2941 PossibleBitfield = true;
2942 // If the next token starts a type-specifier-seq, it may be either a
2943 // a fixed underlying type or the start of a function-style cast in C++;
2944 // lookahead one more token to see if it's obvious that we have a
2945 // fixed underlying type.
2946 else if (TPR == TPResult::False() &&
2947 GetLookAheadToken(2).getKind() == tok::semi) {
2948 // Consume the ':'.
2949 ConsumeToken();
2950 } else {
2951 // We have the start of a type-specifier-seq, so we have to perform
2952 // tentative parsing to determine whether we have an expression or a
2953 // type.
2954 TentativeParsingAction TPA(*this);
2955
2956 // Consume the ':'.
2957 ConsumeToken();
2958
Douglas Gregor86f208c2011-02-22 20:32:04 +00002959 if ((getLang().CPlusPlus &&
2960 isCXXDeclarationSpecifier() != TPResult::True()) ||
2961 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002962 // We'll parse this as a bitfield later.
2963 PossibleBitfield = true;
2964 TPA.Revert();
2965 } else {
2966 // We have a type-specifier-seq.
2967 TPA.Commit();
2968 }
2969 }
2970 } else {
2971 // Consume the ':'.
2972 ConsumeToken();
2973 }
2974
2975 if (!PossibleBitfield) {
2976 SourceRange Range;
2977 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002978
Douglas Gregor5471bc82011-09-08 17:18:35 +00002979 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002980 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2981 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002982 if (getLang().CPlusPlus0x)
2983 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002984 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002985 }
2986
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002987 // There are three options here. If we have 'enum foo;', then this is a
2988 // forward declaration. If we have 'enum foo {...' then this is a
2989 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2990 //
2991 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2992 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2993 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2994 //
John McCallf312b1e2010-08-26 23:41:50 +00002995 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002996 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002997 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002998 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002999 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003000 else
John McCallf312b1e2010-08-26 23:41:50 +00003001 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003002
3003 // enums cannot be templates, although they can be referenced from a
3004 // template.
3005 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003006 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003007 Diag(Tok, diag::err_enum_template);
3008
3009 // Skip the rest of this declarator, up until the comma or semicolon.
3010 SkipUntil(tok::comma, true);
3011 return;
3012 }
3013
Douglas Gregorb9075602011-02-22 02:55:24 +00003014 if (!Name && TUK != Sema::TUK_Definition) {
3015 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3016
3017 // Skip the rest of this declarator, up until the comma or semicolon.
3018 SkipUntil(tok::comma, true);
3019 return;
3020 }
3021
Douglas Gregor402abb52009-05-28 23:31:59 +00003022 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003023 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003024 const char *PrevSpec = 0;
3025 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003026 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003027 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003028 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003029 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003030 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003031 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003032
Douglas Gregor48c89f42010-04-24 16:38:41 +00003033 if (IsDependent) {
3034 // This enum has a dependent nested-name-specifier. Handle it as a
3035 // dependent tag.
3036 if (!Name) {
3037 DS.SetTypeSpecError();
3038 Diag(Tok, diag::err_expected_type_name_after_typename);
3039 return;
3040 }
3041
Douglas Gregor23c94db2010-07-02 17:43:08 +00003042 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003043 TUK, SS, Name, StartLoc,
3044 NameLoc);
3045 if (Type.isInvalid()) {
3046 DS.SetTypeSpecError();
3047 return;
3048 }
3049
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003050 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3051 NameLoc.isValid() ? NameLoc : StartLoc,
3052 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003053 Diag(StartLoc, DiagID) << PrevSpec;
3054
3055 return;
3056 }
Mike Stump1eb44332009-09-09 15:08:12 +00003057
John McCalld226f652010-08-21 09:40:31 +00003058 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003059 // The action failed to produce an enumeration tag. If this is a
3060 // definition, consume the entire definition.
3061 if (Tok.is(tok::l_brace)) {
3062 ConsumeBrace();
3063 SkipUntil(tok::r_brace);
3064 }
3065
3066 DS.SetTypeSpecError();
3067 return;
3068 }
3069
Chris Lattner04d66662007-10-09 17:33:22 +00003070 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00003071 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003073 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3074 NameLoc.isValid() ? NameLoc : StartLoc,
3075 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003076 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003077}
3078
3079/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3080/// enumerator-list:
3081/// enumerator
3082/// enumerator-list ',' enumerator
3083/// enumerator:
3084/// enumeration-constant
3085/// enumeration-constant '=' constant-expression
3086/// enumeration-constant:
3087/// identifier
3088///
John McCalld226f652010-08-21 09:40:31 +00003089void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003090 // Enter the scope of the enum body and start the definition.
3091 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003092 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003093
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003094 BalancedDelimiterTracker T(*this, tok::l_brace);
3095 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Chris Lattner7946dd32007-08-27 17:24:30 +00003097 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003098 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003099 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003100
Chris Lattner5f9e2722011-07-23 10:55:15 +00003101 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003102
John McCalld226f652010-08-21 09:40:31 +00003103 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003104
Reid Spencer5f016e22007-07-11 17:01:13 +00003105 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003106 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003107 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3108 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003109
John McCall5b629aa2010-10-22 23:36:17 +00003110 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003111 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003112 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003113
Reid Spencer5f016e22007-07-11 17:01:13 +00003114 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003115 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003116 ParsingDeclRAIIObject PD(*this);
3117
Chris Lattner04d66662007-10-09 17:33:22 +00003118 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003120 AssignedVal = ParseConstantExpression();
3121 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003122 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Reid Spencer5f016e22007-07-11 17:01:13 +00003125 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003126 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3127 LastEnumConstDecl,
3128 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003129 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003130 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003131 PD.complete(EnumConstDecl);
3132
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 EnumConstantDecls.push_back(EnumConstDecl);
3134 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Douglas Gregor751f6922010-09-07 14:51:08 +00003136 if (Tok.is(tok::identifier)) {
3137 // We're missing a comma between enumerators.
3138 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3139 Diag(Loc, diag::err_enumerator_list_missing_comma)
3140 << FixItHint::CreateInsertion(Loc, ", ");
3141 continue;
3142 }
3143
Chris Lattner04d66662007-10-09 17:33:22 +00003144 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 break;
3146 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Richard Smith7fe62082011-10-15 05:09:34 +00003148 if (Tok.isNot(tok::identifier)) {
3149 if (!getLang().C99 && !getLang().CPlusPlus0x)
3150 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3151 << getLang().CPlusPlus
3152 << FixItHint::CreateRemoval(CommaLoc);
3153 else if (getLang().CPlusPlus0x)
3154 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3155 << FixItHint::CreateRemoval(CommaLoc);
3156 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 }
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003160 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003161
Reid Spencer5f016e22007-07-11 17:01:13 +00003162 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003163 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003164 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003165
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003166 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3167 EnumDecl, EnumConstantDecls.data(),
3168 EnumConstantDecls.size(), getCurScope(),
3169 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Douglas Gregor72de6672009-01-08 20:45:30 +00003171 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003172 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3173 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003174}
3175
3176/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003177/// start of a type-qualifier-list.
3178bool Parser::isTypeQualifier() const {
3179 switch (Tok.getKind()) {
3180 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003181
3182 // type-qualifier only in OpenCL
3183 case tok::kw_private:
3184 return getLang().OpenCL;
3185
Steve Naroff5f8aa692008-02-11 23:15:56 +00003186 // type-qualifier
3187 case tok::kw_const:
3188 case tok::kw_volatile:
3189 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003190 case tok::kw___private:
3191 case tok::kw___local:
3192 case tok::kw___global:
3193 case tok::kw___constant:
3194 case tok::kw___read_only:
3195 case tok::kw___read_write:
3196 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003197 return true;
3198 }
3199}
3200
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003201/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3202/// is definitely a type-specifier. Return false if it isn't part of a type
3203/// specifier or if we're not sure.
3204bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3205 switch (Tok.getKind()) {
3206 default: return false;
3207 // type-specifiers
3208 case tok::kw_short:
3209 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003210 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003211 case tok::kw_signed:
3212 case tok::kw_unsigned:
3213 case tok::kw__Complex:
3214 case tok::kw__Imaginary:
3215 case tok::kw_void:
3216 case tok::kw_char:
3217 case tok::kw_wchar_t:
3218 case tok::kw_char16_t:
3219 case tok::kw_char32_t:
3220 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003221 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003222 case tok::kw_float:
3223 case tok::kw_double:
3224 case tok::kw_bool:
3225 case tok::kw__Bool:
3226 case tok::kw__Decimal32:
3227 case tok::kw__Decimal64:
3228 case tok::kw__Decimal128:
3229 case tok::kw___vector:
3230
3231 // struct-or-union-specifier (C99) or class-specifier (C++)
3232 case tok::kw_class:
3233 case tok::kw_struct:
3234 case tok::kw_union:
3235 // enum-specifier
3236 case tok::kw_enum:
3237
3238 // typedef-name
3239 case tok::annot_typename:
3240 return true;
3241 }
3242}
3243
Steve Naroff5f8aa692008-02-11 23:15:56 +00003244/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003245/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003246bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003247 switch (Tok.getKind()) {
3248 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Chris Lattner166a8fc2009-01-04 23:41:41 +00003250 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003251 if (TryAltiVecVectorToken())
3252 return true;
3253 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003254 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003255 // Annotate typenames and C++ scope specifiers. If we get one, just
3256 // recurse to handle whatever we get.
3257 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003258 return true;
3259 if (Tok.is(tok::identifier))
3260 return false;
3261 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003262
Chris Lattner166a8fc2009-01-04 23:41:41 +00003263 case tok::coloncolon: // ::foo::bar
3264 if (NextToken().is(tok::kw_new) || // ::new
3265 NextToken().is(tok::kw_delete)) // ::delete
3266 return false;
3267
Chris Lattner166a8fc2009-01-04 23:41:41 +00003268 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003269 return true;
3270 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Reid Spencer5f016e22007-07-11 17:01:13 +00003272 // GNU attributes support.
3273 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003274 // GNU typeof support.
3275 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003276
Reid Spencer5f016e22007-07-11 17:01:13 +00003277 // type-specifiers
3278 case tok::kw_short:
3279 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003280 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003281 case tok::kw_signed:
3282 case tok::kw_unsigned:
3283 case tok::kw__Complex:
3284 case tok::kw__Imaginary:
3285 case tok::kw_void:
3286 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003287 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003288 case tok::kw_char16_t:
3289 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003290 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003291 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 case tok::kw_float:
3293 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003294 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003295 case tok::kw__Bool:
3296 case tok::kw__Decimal32:
3297 case tok::kw__Decimal64:
3298 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003299 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003300
Chris Lattner99dc9142008-04-13 18:59:07 +00003301 // struct-or-union-specifier (C99) or class-specifier (C++)
3302 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 case tok::kw_struct:
3304 case tok::kw_union:
3305 // enum-specifier
3306 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 // type-qualifier
3309 case tok::kw_const:
3310 case tok::kw_volatile:
3311 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003312
3313 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003314 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003315 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Chris Lattner7c186be2008-10-20 00:25:30 +00003317 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3318 case tok::less:
3319 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003320
Steve Naroff239f0732008-12-25 14:16:32 +00003321 case tok::kw___cdecl:
3322 case tok::kw___stdcall:
3323 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003324 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003325 case tok::kw___w64:
3326 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003327 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003328 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003329 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003330
3331 case tok::kw___private:
3332 case tok::kw___local:
3333 case tok::kw___global:
3334 case tok::kw___constant:
3335 case tok::kw___read_only:
3336 case tok::kw___read_write:
3337 case tok::kw___write_only:
3338
Eli Friedman290eeb02009-06-08 23:27:34 +00003339 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003340
3341 case tok::kw_private:
3342 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003343
3344 // C1x _Atomic()
3345 case tok::kw__Atomic:
3346 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003347 }
3348}
3349
3350/// isDeclarationSpecifier() - Return true if the current token is part of a
3351/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003352///
3353/// \param DisambiguatingWithExpression True to indicate that the purpose of
3354/// this check is to disambiguate between an expression and a declaration.
3355bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003356 switch (Tok.getKind()) {
3357 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003358
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003359 case tok::kw_private:
3360 return getLang().OpenCL;
3361
Chris Lattner166a8fc2009-01-04 23:41:41 +00003362 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003363 // Unfortunate hack to support "Class.factoryMethod" notation.
3364 if (getLang().ObjC1 && NextToken().is(tok::period))
3365 return false;
John Thompson82287d12010-02-05 00:12:22 +00003366 if (TryAltiVecVectorToken())
3367 return true;
3368 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003369 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003370 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003371 // Annotate typenames and C++ scope specifiers. If we get one, just
3372 // recurse to handle whatever we get.
3373 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003374 return true;
3375 if (Tok.is(tok::identifier))
3376 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003377
3378 // If we're in Objective-C and we have an Objective-C class type followed
3379 // by an identifier and then either ':' or ']', in a place where an
3380 // expression is permitted, then this is probably a class message send
3381 // missing the initial '['. In this case, we won't consider this to be
3382 // the start of a declaration.
3383 if (DisambiguatingWithExpression &&
3384 isStartOfObjCClassMessageMissingOpenBracket())
3385 return false;
3386
John McCall9ba61662010-02-26 08:45:28 +00003387 return isDeclarationSpecifier();
3388
Chris Lattner166a8fc2009-01-04 23:41:41 +00003389 case tok::coloncolon: // ::foo::bar
3390 if (NextToken().is(tok::kw_new) || // ::new
3391 NextToken().is(tok::kw_delete)) // ::delete
3392 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003393
Chris Lattner166a8fc2009-01-04 23:41:41 +00003394 // Annotate typenames and C++ scope specifiers. If we get one, just
3395 // recurse to handle whatever we get.
3396 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003397 return true;
3398 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003399
Reid Spencer5f016e22007-07-11 17:01:13 +00003400 // storage-class-specifier
3401 case tok::kw_typedef:
3402 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003403 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003404 case tok::kw_static:
3405 case tok::kw_auto:
3406 case tok::kw_register:
3407 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003408
Douglas Gregor8d267c52011-09-09 02:06:17 +00003409 // Modules
3410 case tok::kw___module_private__:
3411
Reid Spencer5f016e22007-07-11 17:01:13 +00003412 // type-specifiers
3413 case tok::kw_short:
3414 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003415 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003416 case tok::kw_signed:
3417 case tok::kw_unsigned:
3418 case tok::kw__Complex:
3419 case tok::kw__Imaginary:
3420 case tok::kw_void:
3421 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003422 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003423 case tok::kw_char16_t:
3424 case tok::kw_char32_t:
3425
Reid Spencer5f016e22007-07-11 17:01:13 +00003426 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003427 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003428 case tok::kw_float:
3429 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003430 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003431 case tok::kw__Bool:
3432 case tok::kw__Decimal32:
3433 case tok::kw__Decimal64:
3434 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003435 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Chris Lattner99dc9142008-04-13 18:59:07 +00003437 // struct-or-union-specifier (C99) or class-specifier (C++)
3438 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003439 case tok::kw_struct:
3440 case tok::kw_union:
3441 // enum-specifier
3442 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 // type-qualifier
3445 case tok::kw_const:
3446 case tok::kw_volatile:
3447 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003448
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 // function-specifier
3450 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003451 case tok::kw_virtual:
3452 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003453
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003454 // static_assert-declaration
3455 case tok::kw__Static_assert:
3456
Chris Lattner1ef08762007-08-09 17:01:07 +00003457 // GNU typeof support.
3458 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003459
Chris Lattner1ef08762007-08-09 17:01:07 +00003460 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003461 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003462 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003463
Francois Pichete3d49b42011-06-19 08:02:06 +00003464 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003465 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003466 return true;
3467
Eli Friedmanb001de72011-10-06 23:00:33 +00003468 // C1x _Atomic()
3469 case tok::kw__Atomic:
3470 return true;
3471
Chris Lattnerf3948c42008-07-26 03:38:44 +00003472 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3473 case tok::less:
3474 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Douglas Gregord9d75e52011-04-27 05:41:15 +00003476 // typedef-name
3477 case tok::annot_typename:
3478 return !DisambiguatingWithExpression ||
3479 !isStartOfObjCClassMessageMissingOpenBracket();
3480
Steve Naroff47f52092009-01-06 19:34:12 +00003481 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003482 case tok::kw___cdecl:
3483 case tok::kw___stdcall:
3484 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003485 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003486 case tok::kw___w64:
3487 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003488 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003489 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003490 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003491 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003492
3493 case tok::kw___private:
3494 case tok::kw___local:
3495 case tok::kw___global:
3496 case tok::kw___constant:
3497 case tok::kw___read_only:
3498 case tok::kw___read_write:
3499 case tok::kw___write_only:
3500
Eli Friedman290eeb02009-06-08 23:27:34 +00003501 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 }
3503}
3504
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003505bool Parser::isConstructorDeclarator() {
3506 TentativeParsingAction TPA(*this);
3507
3508 // Parse the C++ scope specifier.
3509 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003510 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3511 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003512 TPA.Revert();
3513 return false;
3514 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003515
3516 // Parse the constructor name.
3517 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3518 // We already know that we have a constructor name; just consume
3519 // the token.
3520 ConsumeToken();
3521 } else {
3522 TPA.Revert();
3523 return false;
3524 }
3525
3526 // Current class name must be followed by a left parentheses.
3527 if (Tok.isNot(tok::l_paren)) {
3528 TPA.Revert();
3529 return false;
3530 }
3531 ConsumeParen();
3532
3533 // A right parentheses or ellipsis signals that we have a constructor.
3534 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3535 TPA.Revert();
3536 return true;
3537 }
3538
3539 // If we need to, enter the specified scope.
3540 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003541 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003542 DeclScopeObj.EnterDeclaratorScope();
3543
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003544 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003545 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003546 MaybeParseMicrosoftAttributes(Attrs);
3547
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003548 // Check whether the next token(s) are part of a declaration
3549 // specifier, in which case we have the start of a parameter and,
3550 // therefore, we know that this is a constructor.
3551 bool IsConstructor = isDeclarationSpecifier();
3552 TPA.Revert();
3553 return IsConstructor;
3554}
Reid Spencer5f016e22007-07-11 17:01:13 +00003555
3556/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003557/// type-qualifier-list: [C99 6.7.5]
3558/// type-qualifier
3559/// [vendor] attributes
3560/// [ only if VendorAttributesAllowed=true ]
3561/// type-qualifier-list type-qualifier
3562/// [vendor] type-qualifier-list attributes
3563/// [ only if VendorAttributesAllowed=true ]
3564/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3565/// [ only if CXX0XAttributesAllowed=true ]
3566/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003567///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003568void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3569 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003570 bool CXX0XAttributesAllowed) {
3571 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3572 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003573 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003574 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003575 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003576 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003577 else
3578 Diag(Loc, diag::err_attributes_not_allowed);
3579 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003580
3581 SourceLocation EndLoc;
3582
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003584 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003585 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003586 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003587 SourceLocation Loc = Tok.getLocation();
3588
3589 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003590 case tok::code_completion:
3591 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003592 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003593
Reid Spencer5f016e22007-07-11 17:01:13 +00003594 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003595 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3596 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003597 break;
3598 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003599 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3600 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003601 break;
3602 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003603 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3604 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003605 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003606
3607 // OpenCL qualifiers:
3608 case tok::kw_private:
3609 if (!getLang().OpenCL)
3610 goto DoneWithTypeQuals;
3611 case tok::kw___private:
3612 case tok::kw___global:
3613 case tok::kw___local:
3614 case tok::kw___constant:
3615 case tok::kw___read_only:
3616 case tok::kw___write_only:
3617 case tok::kw___read_write:
3618 ParseOpenCLQualifiers(DS);
3619 break;
3620
Eli Friedman290eeb02009-06-08 23:27:34 +00003621 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003622 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003623 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003624 case tok::kw___cdecl:
3625 case tok::kw___stdcall:
3626 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003627 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003628 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003629 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003630 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003631 continue;
3632 }
3633 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003634 case tok::kw___pascal:
3635 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003636 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003637 continue;
3638 }
3639 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003640 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003641 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003642 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003643 continue; // do *not* consume the next token!
3644 }
3645 // otherwise, FALL THROUGH!
3646 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003647 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003648 // If this is not a type-qualifier token, we're done reading type
3649 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003650 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003651 if (EndLoc.isValid())
3652 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003653 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003654 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003655
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 // If the specifier combination wasn't legal, issue a diagnostic.
3657 if (isInvalid) {
3658 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003659 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003661 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 }
3663}
3664
3665
3666/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3667///
3668void Parser::ParseDeclarator(Declarator &D) {
3669 /// This implements the 'declarator' production in the C grammar, then checks
3670 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003671 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003672}
3673
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003674/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3675/// is parsed by the function passed to it. Pass null, and the direct-declarator
3676/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003677/// ptr-operator production.
3678///
Richard Smith0706df42011-10-19 21:33:05 +00003679/// If the grammar of this construct is extended, matching changes must also be
3680/// made to TryParseDeclarator and MightBeDeclarator.
3681///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003682/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3683/// [C] pointer[opt] direct-declarator
3684/// [C++] direct-declarator
3685/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003686///
3687/// pointer: [C99 6.7.5]
3688/// '*' type-qualifier-list[opt]
3689/// '*' type-qualifier-list[opt] pointer
3690///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003691/// ptr-operator:
3692/// '*' cv-qualifier-seq[opt]
3693/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003694/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003695/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003696/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003697/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003698void Parser::ParseDeclaratorInternal(Declarator &D,
3699 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003700 if (Diags.hasAllExtensionsSilenced())
3701 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003702
Sebastian Redlf30208a2009-01-24 21:16:55 +00003703 // C++ member pointers start with a '::' or a nested-name.
3704 // Member pointers get special handling, since there's no place for the
3705 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003706 if (getLang().CPlusPlus &&
3707 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3708 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003709 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3710 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003711 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003712 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003713
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003714 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003715 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003716 // The scope spec really belongs to the direct-declarator.
3717 D.getCXXScopeSpec() = SS;
3718 if (DirectDeclParser)
3719 (this->*DirectDeclParser)(D);
3720 return;
3721 }
3722
3723 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003724 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003725 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003726 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003727 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003728
3729 // Recurse to parse whatever is left.
3730 ParseDeclaratorInternal(D, DirectDeclParser);
3731
3732 // Sema will have to catch (syntactically invalid) pointers into global
3733 // scope. It has to catch pointers into namespace scope anyway.
3734 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003735 Loc),
3736 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003737 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003738 return;
3739 }
3740 }
3741
3742 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003743 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003744 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003745 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003746 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003747 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003748 if (DirectDeclParser)
3749 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003750 return;
3751 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003752
Sebastian Redl05532f22009-03-15 22:02:01 +00003753 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3754 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003755 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003756 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003757
Chris Lattner9af55002009-03-27 04:18:06 +00003758 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003759 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003760 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003761
Reid Spencer5f016e22007-07-11 17:01:13 +00003762 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003763 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003764
Reid Spencer5f016e22007-07-11 17:01:13 +00003765 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003766 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003767 if (Kind == tok::star)
3768 // Remember that we parsed a pointer type, and remember the type-quals.
3769 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003770 DS.getConstSpecLoc(),
3771 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003772 DS.getRestrictSpecLoc()),
3773 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003774 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003775 else
3776 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003777 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003778 Loc),
3779 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003780 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 } else {
3782 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003783 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003784
Sebastian Redl743de1f2009-03-23 00:00:23 +00003785 // Complain about rvalue references in C++03, but then go on and build
3786 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003787 if (Kind == tok::ampamp)
3788 Diag(Loc, getLang().CPlusPlus0x ?
3789 diag::warn_cxx98_compat_rvalue_reference :
3790 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003791
Reid Spencer5f016e22007-07-11 17:01:13 +00003792 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3793 // cv-qualifiers are introduced through the use of a typedef or of a
3794 // template type argument, in which case the cv-qualifiers are ignored.
3795 //
3796 // [GNU] Retricted references are allowed.
3797 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003798 // [C++0x] Attributes on references are not allowed.
3799 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003800 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003801
3802 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3803 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3804 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003805 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003806 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3807 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003808 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003809 }
3810
3811 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003812 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003813
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003814 if (D.getNumTypeObjects() > 0) {
3815 // C++ [dcl.ref]p4: There shall be no references to references.
3816 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3817 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003818 if (const IdentifierInfo *II = D.getIdentifier())
3819 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3820 << II;
3821 else
3822 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3823 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003824
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003825 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003826 // can go ahead and build the (technically ill-formed)
3827 // declarator: reference collapsing will take care of it.
3828 }
3829 }
3830
Reid Spencer5f016e22007-07-11 17:01:13 +00003831 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003832 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003833 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003834 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003835 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 }
3837}
3838
3839/// ParseDirectDeclarator
3840/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003841/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003842/// '(' declarator ')'
3843/// [GNU] '(' attributes declarator ')'
3844/// [C90] direct-declarator '[' constant-expression[opt] ']'
3845/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3846/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3847/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3848/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3849/// direct-declarator '(' parameter-type-list ')'
3850/// direct-declarator '(' identifier-list[opt] ')'
3851/// [GNU] direct-declarator '(' parameter-forward-declarations
3852/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003853/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3854/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003855/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003856///
3857/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003858/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003859/// '::'[opt] nested-name-specifier[opt] type-name
3860///
3861/// id-expression: [C++ 5.1]
3862/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003863/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003864///
3865/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003866/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003867/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003868/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003869/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003870/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003871///
Reid Spencer5f016e22007-07-11 17:01:13 +00003872void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003873 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003874
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003875 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3876 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003877 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003878 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3879 D.getContext() == Declarator::MemberContext;
3880 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3881 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003882 }
3883
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003884 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003885 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003886 // Change the declaration context for name lookup, until this function
3887 // is exited (and the declarator has been parsed).
3888 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003889 }
3890
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003891 // C++0x [dcl.fct]p14:
3892 // There is a syntactic ambiguity when an ellipsis occurs at the end
3893 // of a parameter-declaration-clause without a preceding comma. In
3894 // this case, the ellipsis is parsed as part of the
3895 // abstract-declarator if the type of the parameter names a template
3896 // parameter pack that has not been expanded; otherwise, it is parsed
3897 // as part of the parameter-declaration-clause.
3898 if (Tok.is(tok::ellipsis) &&
3899 !((D.getContext() == Declarator::PrototypeContext ||
3900 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003901 NextToken().is(tok::r_paren) &&
3902 !Actions.containsUnexpandedParameterPacks(D)))
3903 D.setEllipsisLoc(ConsumeToken());
3904
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003905 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3906 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3907 // We found something that indicates the start of an unqualified-id.
3908 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003909 bool AllowConstructorName;
3910 if (D.getDeclSpec().hasTypeSpecifier())
3911 AllowConstructorName = false;
3912 else if (D.getCXXScopeSpec().isSet())
3913 AllowConstructorName =
3914 (D.getContext() == Declarator::FileContext ||
3915 (D.getContext() == Declarator::MemberContext &&
3916 D.getDeclSpec().isFriendSpecified()));
3917 else
3918 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3919
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003920 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3921 /*EnteringContext=*/true,
3922 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003923 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003924 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003925 D.getName()) ||
3926 // Once we're past the identifier, if the scope was bad, mark the
3927 // whole declarator bad.
3928 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003929 D.SetIdentifier(0, Tok.getLocation());
3930 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003931 } else {
3932 // Parsed the unqualified-id; update range information and move along.
3933 if (D.getSourceRange().getBegin().isInvalid())
3934 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3935 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003936 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003937 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003938 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003939 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003940 assert(!getLang().CPlusPlus &&
3941 "There's a C++-specific check for tok::identifier above");
3942 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3943 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3944 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003945 goto PastIdentifier;
3946 }
3947
3948 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003949 // direct-declarator: '(' declarator ')'
3950 // direct-declarator: '(' attributes declarator ')'
3951 // Example: 'char (*X)' or 'int (*XX)(void)'
3952 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003953
3954 // If the declarator was parenthesized, we entered the declarator
3955 // scope when parsing the parenthesized declarator, then exited
3956 // the scope already. Re-enter the scope, if we need to.
3957 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003958 // If there was an error parsing parenthesized declarator, declarator
3959 // scope may have been enterred before. Don't do it again.
3960 if (!D.isInvalidType() &&
3961 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003962 // Change the declaration context for name lookup, until this function
3963 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003964 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003965 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003966 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003967 // This could be something simple like "int" (in which case the declarator
3968 // portion is empty), if an abstract-declarator is allowed.
3969 D.SetIdentifier(0, Tok.getLocation());
3970 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003971 if (D.getContext() == Declarator::MemberContext)
3972 Diag(Tok, diag::err_expected_member_name_or_semi)
3973 << D.getDeclSpec().getSourceRange();
3974 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003975 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003976 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003977 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003978 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003979 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003980 }
Mike Stump1eb44332009-09-09 15:08:12 +00003981
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003982 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003983 assert(D.isPastIdentifier() &&
3984 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003985
Sean Huntbbd37c62009-11-21 08:43:09 +00003986 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003987 if (D.getIdentifier())
3988 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003989
Reid Spencer5f016e22007-07-11 17:01:13 +00003990 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003991 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003992 // Enter function-declaration scope, limiting any declarators to the
3993 // function prototype scope, including parameter declarators.
3994 ParseScope PrototypeScope(this,
3995 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003996 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3997 // In such a case, check if we actually have a function declarator; if it
3998 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003999 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4000 // When not in file scope, warn for ambiguous function declarators, just
4001 // in case the author intended it as a variable definition.
4002 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4003 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4004 break;
4005 }
John McCall0b7e6782011-03-24 11:26:52 +00004006 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004007 BalancedDelimiterTracker T(*this, tok::l_paren);
4008 T.consumeOpen();
4009 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004010 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004011 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004012 ParseBracketDeclarator(D);
4013 } else {
4014 break;
4015 }
4016 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004017}
Reid Spencer5f016e22007-07-11 17:01:13 +00004018
Chris Lattneref4715c2008-04-06 05:45:57 +00004019/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4020/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004021/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004022/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4023///
4024/// direct-declarator:
4025/// '(' declarator ')'
4026/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004027/// direct-declarator '(' parameter-type-list ')'
4028/// direct-declarator '(' identifier-list[opt] ')'
4029/// [GNU] direct-declarator '(' parameter-forward-declarations
4030/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004031///
4032void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004033 BalancedDelimiterTracker T(*this, tok::l_paren);
4034 T.consumeOpen();
4035
Chris Lattneref4715c2008-04-06 05:45:57 +00004036 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004037
Chris Lattner7399ee02008-10-20 02:05:46 +00004038 // Eat any attributes before we look at whether this is a grouping or function
4039 // declarator paren. If this is a grouping paren, the attribute applies to
4040 // the type being built up, for example:
4041 // int (__attribute__(()) *x)(long y)
4042 // If this ends up not being a grouping paren, the attribute applies to the
4043 // first argument, for example:
4044 // int (__attribute__(()) int x)
4045 // In either case, we need to eat any attributes to be able to determine what
4046 // sort of paren this is.
4047 //
John McCall0b7e6782011-03-24 11:26:52 +00004048 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004049 bool RequiresArg = false;
4050 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004051 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004052
Chris Lattner7399ee02008-10-20 02:05:46 +00004053 // We require that the argument list (if this is a non-grouping paren) be
4054 // present even if the attribute list was empty.
4055 RequiresArg = true;
4056 }
Steve Naroff239f0732008-12-25 14:16:32 +00004057 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004058 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004059 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004060 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004061 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004062 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004063 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004064 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004065 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004066 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Chris Lattneref4715c2008-04-06 05:45:57 +00004068 // If we haven't past the identifier yet (or where the identifier would be
4069 // stored, if this is an abstract declarator), then this is probably just
4070 // grouping parens. However, if this could be an abstract-declarator, then
4071 // this could also be the start of function arguments (consider 'void()').
4072 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004073
Chris Lattneref4715c2008-04-06 05:45:57 +00004074 if (!D.mayOmitIdentifier()) {
4075 // If this can't be an abstract-declarator, this *must* be a grouping
4076 // paren, because we haven't seen the identifier yet.
4077 isGrouping = true;
4078 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004079 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004080 isDeclarationSpecifier()) { // 'int(int)' is a function.
4081 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4082 // considered to be a type, not a K&R identifier-list.
4083 isGrouping = false;
4084 } else {
4085 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4086 isGrouping = true;
4087 }
Mike Stump1eb44332009-09-09 15:08:12 +00004088
Chris Lattneref4715c2008-04-06 05:45:57 +00004089 // If this is a grouping paren, handle:
4090 // direct-declarator: '(' declarator ')'
4091 // direct-declarator: '(' attributes declarator ')'
4092 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004093 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004094 D.setGroupingParens(true);
4095
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004096 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004097 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004098 T.consumeClose();
4099 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4100 T.getCloseLocation()),
4101 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004102
4103 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004104 return;
4105 }
Mike Stump1eb44332009-09-09 15:08:12 +00004106
Chris Lattneref4715c2008-04-06 05:45:57 +00004107 // Okay, if this wasn't a grouping paren, it must be the start of a function
4108 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004109 // identifier (and remember where it would have been), then call into
4110 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004111 D.SetIdentifier(0, Tok.getLocation());
4112
David Blaikie42d6d0c2011-12-04 05:04:18 +00004113 // Enter function-declaration scope, limiting any declarators to the
4114 // function prototype scope, including parameter declarators.
4115 ParseScope PrototypeScope(this,
4116 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004117 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004118 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004119}
4120
4121/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4122/// declarator D up to a paren, which indicates that we are parsing function
4123/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004124///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004125/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004126/// after the open paren - they should be considered to be the first argument of
4127/// a parameter. If RequiresArg is true, then the first argument of the
4128/// function is required to be present and required to not be an identifier
4129/// list.
4130///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004131/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4132/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4133/// (C++0x) trailing-return-type[opt].
4134///
4135/// [C++0x] exception-specification:
4136/// dynamic-exception-specification
4137/// noexcept-specification
4138///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004139void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004140 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004141 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004142 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004143 assert(getCurScope()->isFunctionPrototypeScope() &&
4144 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004145 // lparen is already consumed!
4146 assert(D.isPastIdentifier() && "Should not call before identifier!");
4147
4148 // This should be true when the function has typed arguments.
4149 // Otherwise, it is treated as a K&R-style function.
4150 bool HasProto = false;
4151 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004152 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004153 // Remember where we see an ellipsis, if any.
4154 SourceLocation EllipsisLoc;
4155
4156 DeclSpec DS(AttrFactory);
4157 bool RefQualifierIsLValueRef = true;
4158 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004159 SourceLocation ConstQualifierLoc;
4160 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004161 ExceptionSpecificationType ESpecType = EST_None;
4162 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004163 SmallVector<ParsedType, 2> DynamicExceptions;
4164 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004165 ExprResult NoexceptExpr;
4166 ParsedType TrailingReturnType;
4167
4168 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004169 if (isFunctionDeclaratorIdentifierList()) {
4170 if (RequiresArg)
4171 Diag(Tok, diag::err_argument_required_after_attribute);
4172
4173 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4174
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004175 Tracker.consumeClose();
4176 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004177 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004178 if (Tok.isNot(tok::r_paren))
4179 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4180 else if (RequiresArg)
4181 Diag(Tok, diag::err_argument_required_after_attribute);
4182
4183 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4184
4185 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004186 Tracker.consumeClose();
4187 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004188
4189 if (getLang().CPlusPlus) {
4190 MaybeParseCXX0XAttributes(attrs);
4191
4192 // Parse cv-qualifier-seq[opt].
4193 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004194 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004195 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004196 ConstQualifierLoc = DS.getConstSpecLoc();
4197 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4198 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004199
4200 // Parse ref-qualifier[opt].
4201 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004202 Diag(Tok, getLang().CPlusPlus0x ?
4203 diag::warn_cxx98_compat_ref_qualifier :
4204 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004205
4206 RefQualifierIsLValueRef = Tok.is(tok::amp);
4207 RefQualifierLoc = ConsumeToken();
4208 EndLoc = RefQualifierLoc;
4209 }
4210
4211 // Parse exception-specification[opt].
4212 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4213 DynamicExceptions,
4214 DynamicExceptionRanges,
4215 NoexceptExpr);
4216 if (ESpecType != EST_None)
4217 EndLoc = ESpecRange.getEnd();
4218
4219 // Parse trailing-return-type[opt].
4220 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004221 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004222 SourceRange Range;
4223 TrailingReturnType = ParseTrailingReturnType(Range).get();
4224 if (Range.getEnd().isValid())
4225 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004226 }
4227 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004228 }
4229
4230 // Remember that we parsed a function type, and remember the attributes.
4231 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4232 /*isVariadic=*/EllipsisLoc.isValid(),
4233 EllipsisLoc,
4234 ParamInfo.data(), ParamInfo.size(),
4235 DS.getTypeQualifiers(),
4236 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004237 RefQualifierLoc, ConstQualifierLoc,
4238 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004239 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004240 ESpecType, ESpecRange.getBegin(),
4241 DynamicExceptions.data(),
4242 DynamicExceptionRanges.data(),
4243 DynamicExceptions.size(),
4244 NoexceptExpr.isUsable() ?
4245 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004246 Tracker.getOpenLocation(),
4247 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004248 TrailingReturnType),
4249 attrs, EndLoc);
4250}
4251
4252/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4253/// identifier list form for a K&R-style function: void foo(a,b,c)
4254///
4255/// Note that identifier-lists are only allowed for normal declarators, not for
4256/// abstract-declarators.
4257bool Parser::isFunctionDeclaratorIdentifierList() {
4258 return !getLang().CPlusPlus
4259 && Tok.is(tok::identifier)
4260 && !TryAltiVecVectorToken()
4261 // K&R identifier lists can't have typedefs as identifiers, per C99
4262 // 6.7.5.3p11.
4263 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4264 // Identifier lists follow a really simple grammar: the identifiers can
4265 // be followed *only* by a ", identifier" or ")". However, K&R
4266 // identifier lists are really rare in the brave new modern world, and
4267 // it is very common for someone to typo a type in a non-K&R style
4268 // list. If we are presented with something like: "void foo(intptr x,
4269 // float y)", we don't want to start parsing the function declarator as
4270 // though it is a K&R style declarator just because intptr is an
4271 // invalid type.
4272 //
4273 // To handle this, we check to see if the token after the first
4274 // identifier is a "," or ")". Only then do we parse it as an
4275 // identifier list.
4276 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4277}
4278
4279/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4280/// we found a K&R-style identifier list instead of a typed parameter list.
4281///
4282/// After returning, ParamInfo will hold the parsed parameters.
4283///
4284/// identifier-list: [C99 6.7.5]
4285/// identifier
4286/// identifier-list ',' identifier
4287///
4288void Parser::ParseFunctionDeclaratorIdentifierList(
4289 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004290 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004291 // If there was no identifier specified for the declarator, either we are in
4292 // an abstract-declarator, or we are in a parameter declarator which was found
4293 // to be abstract. In abstract-declarators, identifier lists are not valid:
4294 // diagnose this.
4295 if (!D.getIdentifier())
4296 Diag(Tok, diag::ext_ident_list_in_param);
4297
4298 // Maintain an efficient lookup of params we have seen so far.
4299 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4300
4301 while (1) {
4302 // If this isn't an identifier, report the error and skip until ')'.
4303 if (Tok.isNot(tok::identifier)) {
4304 Diag(Tok, diag::err_expected_ident);
4305 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4306 // Forget we parsed anything.
4307 ParamInfo.clear();
4308 return;
4309 }
4310
4311 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4312
4313 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4314 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4315 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4316
4317 // Verify that the argument identifier has not already been mentioned.
4318 if (!ParamsSoFar.insert(ParmII)) {
4319 Diag(Tok, diag::err_param_redefinition) << ParmII;
4320 } else {
4321 // Remember this identifier in ParamInfo.
4322 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4323 Tok.getLocation(),
4324 0));
4325 }
4326
4327 // Eat the identifier.
4328 ConsumeToken();
4329
4330 // The list continues if we see a comma.
4331 if (Tok.isNot(tok::comma))
4332 break;
4333 ConsumeToken();
4334 }
4335}
4336
4337/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4338/// after the opening parenthesis. This function will not parse a K&R-style
4339/// identifier list.
4340///
4341/// D is the declarator being parsed. If attrs is non-null, then the caller
4342/// parsed those arguments immediately after the open paren - they should be
4343/// considered to be the first argument of a parameter.
4344///
4345/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4346/// be the location of the ellipsis, if any was parsed.
4347///
Reid Spencer5f016e22007-07-11 17:01:13 +00004348/// parameter-type-list: [C99 6.7.5]
4349/// parameter-list
4350/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004351/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004352///
4353/// parameter-list: [C99 6.7.5]
4354/// parameter-declaration
4355/// parameter-list ',' parameter-declaration
4356///
4357/// parameter-declaration: [C99 6.7.5]
4358/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004359/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004360/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004361/// declaration-specifiers abstract-declarator[opt]
4362/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004363/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004364/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4365///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004366void Parser::ParseParameterDeclarationClause(
4367 Declarator &D,
4368 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004369 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004370 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004371
Chris Lattnerf97409f2008-04-06 06:57:35 +00004372 while (1) {
4373 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004374 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004375 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004376 }
Mike Stump1eb44332009-09-09 15:08:12 +00004377
Chris Lattnerf97409f2008-04-06 06:57:35 +00004378 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004379 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004380 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004381
John McCall7f040a92010-12-24 02:08:15 +00004382 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004383 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004384 ParseMicrosoftAttributes(DS.getAttributes());
4385
4386 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004387
4388 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004389 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004390 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4391 // attributes lost? Should they even be allowed?
4392 // FIXME: If we can leave the attributes in the token stream somehow, we can
4393 // get rid of a parameter (attrs) and this statement. It might be too much
4394 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004395 DS.takeAttributesFrom(attrs);
4396
Chris Lattnere64c5492009-02-27 18:38:20 +00004397 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004398
Chris Lattnerf97409f2008-04-06 06:57:35 +00004399 // Parse the declarator. This is "PrototypeContext", because we must
4400 // accept either 'declarator' or 'abstract-declarator' here.
4401 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4402 ParseDeclarator(ParmDecl);
4403
4404 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004405 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004406
Chris Lattnerf97409f2008-04-06 06:57:35 +00004407 // Remember this parsed parameter in ParamInfo.
4408 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004409
Douglas Gregor72b505b2008-12-16 21:30:33 +00004410 // DefArgToks is used when the parsing of default arguments needs
4411 // to be delayed.
4412 CachedTokens *DefArgToks = 0;
4413
Chris Lattnerf97409f2008-04-06 06:57:35 +00004414 // If no parameter was specified, verify that *something* was specified,
4415 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004416 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4417 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004418 // Completely missing, emit error.
4419 Diag(DSStart, diag::err_missing_param);
4420 } else {
4421 // Otherwise, we have something. Add it and let semantic analysis try
4422 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004423
Chris Lattnerf97409f2008-04-06 06:57:35 +00004424 // Inform the actions module about the parameter declarator, so it gets
4425 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004426 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004427
4428 // Parse the default argument, if any. We parse the default
4429 // arguments in all dialects; the semantic analysis in
4430 // ActOnParamDefaultArgument will reject the default argument in
4431 // C.
4432 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004433 SourceLocation EqualLoc = Tok.getLocation();
4434
Chris Lattner04421082008-04-08 04:40:51 +00004435 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004436 if (D.getContext() == Declarator::MemberContext) {
4437 // If we're inside a class definition, cache the tokens
4438 // corresponding to the default argument. We'll actually parse
4439 // them when we see the end of the class definition.
4440 // FIXME: Templates will require something similar.
4441 // FIXME: Can we use a smart pointer for Toks?
4442 DefArgToks = new CachedTokens;
4443
Mike Stump1eb44332009-09-09 15:08:12 +00004444 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004445 /*StopAtSemi=*/true,
4446 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004447 delete DefArgToks;
4448 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004449 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004450 } else {
4451 // Mark the end of the default argument so that we know when to
4452 // stop when we parse it later on.
4453 Token DefArgEnd;
4454 DefArgEnd.startToken();
4455 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4456 DefArgEnd.setLocation(Tok.getLocation());
4457 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004458 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004459 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004460 }
Chris Lattner04421082008-04-08 04:40:51 +00004461 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004462 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004463 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004464
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004465 // The argument isn't actually potentially evaluated unless it is
4466 // used.
4467 EnterExpressionEvaluationContext Eval(Actions,
4468 Sema::PotentiallyEvaluatedIfUsed);
4469
John McCall60d7b3a2010-08-24 06:29:42 +00004470 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004471 if (DefArgResult.isInvalid()) {
4472 Actions.ActOnParamDefaultArgumentError(Param);
4473 SkipUntil(tok::comma, tok::r_paren, true, true);
4474 } else {
4475 // Inform the actions module about the default argument
4476 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004477 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004478 }
Chris Lattner04421082008-04-08 04:40:51 +00004479 }
4480 }
Mike Stump1eb44332009-09-09 15:08:12 +00004481
4482 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4483 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004484 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004485 }
4486
4487 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004488 if (Tok.isNot(tok::comma)) {
4489 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004490 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4491
4492 if (!getLang().CPlusPlus) {
4493 // We have ellipsis without a preceding ',', which is ill-formed
4494 // in C. Complain and provide the fix.
4495 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004496 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004497 }
4498 }
4499
4500 break;
4501 }
Mike Stump1eb44332009-09-09 15:08:12 +00004502
Chris Lattnerf97409f2008-04-06 06:57:35 +00004503 // Consume the comma.
4504 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004505 }
Mike Stump1eb44332009-09-09 15:08:12 +00004506
Chris Lattner66d28652008-04-06 06:34:08 +00004507}
Chris Lattneref4715c2008-04-06 05:45:57 +00004508
Reid Spencer5f016e22007-07-11 17:01:13 +00004509/// [C90] direct-declarator '[' constant-expression[opt] ']'
4510/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4511/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4512/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4513/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4514void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004515 BalancedDelimiterTracker T(*this, tok::l_square);
4516 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004517
Chris Lattner378c7e42008-12-18 07:27:21 +00004518 // C array syntax has many features, but by-far the most common is [] and [4].
4519 // This code does a fast path to handle some of the most obvious cases.
4520 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004521 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004522 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004523 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004524
Chris Lattner378c7e42008-12-18 07:27:21 +00004525 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004526 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004527 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004528 T.getOpenLocation(),
4529 T.getCloseLocation()),
4530 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004531 return;
4532 } else if (Tok.getKind() == tok::numeric_constant &&
4533 GetLookAheadToken(1).is(tok::r_square)) {
4534 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004535 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004536 ConsumeToken();
4537
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004538 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004539 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004540 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004541
Chris Lattner378c7e42008-12-18 07:27:21 +00004542 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004543 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004544 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004545 T.getOpenLocation(),
4546 T.getCloseLocation()),
4547 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004548 return;
4549 }
Mike Stump1eb44332009-09-09 15:08:12 +00004550
Reid Spencer5f016e22007-07-11 17:01:13 +00004551 // If valid, this location is the position where we read the 'static' keyword.
4552 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004553 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004554 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004555
Reid Spencer5f016e22007-07-11 17:01:13 +00004556 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004557 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004558 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004559 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004560
Reid Spencer5f016e22007-07-11 17:01:13 +00004561 // If we haven't already read 'static', check to see if there is one after the
4562 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004563 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004564 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004565
Reid Spencer5f016e22007-07-11 17:01:13 +00004566 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4567 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004568 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004569
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004570 // Handle the case where we have '[*]' as the array size. However, a leading
4571 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4572 // the the token after the star is a ']'. Since stars in arrays are
4573 // infrequent, use of lookahead is not costly here.
4574 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004575 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004576
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004577 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004578 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004579 StaticLoc = SourceLocation(); // Drop the static.
4580 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004581 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004582 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004583 // Note, in C89, this production uses the constant-expr production instead
4584 // of assignment-expr. The only difference is that assignment-expr allows
4585 // things like '=' and '*='. Sema rejects these in C89 mode because they
4586 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004587
Douglas Gregore0762c92009-06-19 23:52:42 +00004588 // Parse the constant-expression or assignment-expression now (depending
4589 // on dialect).
4590 if (getLang().CPlusPlus)
4591 NumElements = ParseConstantExpression();
4592 else
4593 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004594 }
Mike Stump1eb44332009-09-09 15:08:12 +00004595
Reid Spencer5f016e22007-07-11 17:01:13 +00004596 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004597 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004598 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004599 // If the expression was invalid, skip it.
4600 SkipUntil(tok::r_square);
4601 return;
4602 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004603
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004604 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004605
John McCall0b7e6782011-03-24 11:26:52 +00004606 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004607 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004608
Chris Lattner378c7e42008-12-18 07:27:21 +00004609 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004610 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004611 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004612 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004613 T.getOpenLocation(),
4614 T.getCloseLocation()),
4615 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004616}
4617
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004618/// [GNU] typeof-specifier:
4619/// typeof ( expressions )
4620/// typeof ( type-name )
4621/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004622///
4623void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004624 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004625 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004626 SourceLocation StartLoc = ConsumeToken();
4627
John McCallcfb708c2010-01-13 20:03:27 +00004628 const bool hasParens = Tok.is(tok::l_paren);
4629
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004630 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004631 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004632 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004633 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4634 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004635 if (hasParens)
4636 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004637
4638 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004639 // FIXME: Not accurate, the range gets one token more than it should.
4640 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004641 else
4642 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004643
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004644 if (isCastExpr) {
4645 if (!CastTy) {
4646 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004647 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004648 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004649
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004650 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004651 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004652 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4653 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004654 DiagID, CastTy))
4655 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004656 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004657 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004658
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004659 // If we get here, the operand to the typeof was an expresion.
4660 if (Operand.isInvalid()) {
4661 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004662 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004663 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004664
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004665 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004666 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004667 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4668 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004669 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004670 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004671}
Chris Lattner1b492422010-02-28 18:33:55 +00004672
Eli Friedmanb001de72011-10-06 23:00:33 +00004673/// [C1X] atomic-specifier:
4674/// _Atomic ( type-name )
4675///
4676void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4677 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4678
4679 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004680 BalancedDelimiterTracker T(*this, tok::l_paren);
4681 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004682 SkipUntil(tok::r_paren);
4683 return;
4684 }
4685
4686 TypeResult Result = ParseTypeName();
4687 if (Result.isInvalid()) {
4688 SkipUntil(tok::r_paren);
4689 return;
4690 }
4691
4692 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004693 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004694
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004695 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004696 return;
4697
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004698 DS.setTypeofParensRange(T.getRange());
4699 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004700
4701 const char *PrevSpec = 0;
4702 unsigned DiagID;
4703 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4704 DiagID, Result.release()))
4705 Diag(StartLoc, DiagID) << PrevSpec;
4706}
4707
Chris Lattner1b492422010-02-28 18:33:55 +00004708
4709/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4710/// from TryAltiVecVectorToken.
4711bool Parser::TryAltiVecVectorTokenOutOfLine() {
4712 Token Next = NextToken();
4713 switch (Next.getKind()) {
4714 default: return false;
4715 case tok::kw_short:
4716 case tok::kw_long:
4717 case tok::kw_signed:
4718 case tok::kw_unsigned:
4719 case tok::kw_void:
4720 case tok::kw_char:
4721 case tok::kw_int:
4722 case tok::kw_float:
4723 case tok::kw_double:
4724 case tok::kw_bool:
4725 case tok::kw___pixel:
4726 Tok.setKind(tok::kw___vector);
4727 return true;
4728 case tok::identifier:
4729 if (Next.getIdentifierInfo() == Ident_pixel) {
4730 Tok.setKind(tok::kw___vector);
4731 return true;
4732 }
4733 return false;
4734 }
4735}
4736
4737bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4738 const char *&PrevSpec, unsigned &DiagID,
4739 bool &isInvalid) {
4740 if (Tok.getIdentifierInfo() == Ident_vector) {
4741 Token Next = NextToken();
4742 switch (Next.getKind()) {
4743 case tok::kw_short:
4744 case tok::kw_long:
4745 case tok::kw_signed:
4746 case tok::kw_unsigned:
4747 case tok::kw_void:
4748 case tok::kw_char:
4749 case tok::kw_int:
4750 case tok::kw_float:
4751 case tok::kw_double:
4752 case tok::kw_bool:
4753 case tok::kw___pixel:
4754 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4755 return true;
4756 case tok::identifier:
4757 if (Next.getIdentifierInfo() == Ident_pixel) {
4758 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4759 return true;
4760 }
4761 break;
4762 default:
4763 break;
4764 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004765 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004766 DS.isTypeAltiVecVector()) {
4767 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4768 return true;
4769 }
4770 return false;
4771}