blob: 87c392f4a38c8eb0da99cf37453e6e66fd823e8c [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()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000693 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000694 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 Hutchins23323e02012-01-20 22:50:54 +0000860 if (ArgExprsOk && !T.consumeClose()) {
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
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000886/// [C++0x/C11] 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));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
967 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000968 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000969 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000970 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000971 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000972 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000973 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000975
976 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000977}
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Richard Smith0706df42011-10-19 21:33:05 +0000979/// Returns true if this might be the start of a declarator, or a common typo
980/// for a declarator.
981bool Parser::MightBeDeclarator(unsigned Context) {
982 switch (Tok.getKind()) {
983 case tok::annot_cxxscope:
984 case tok::annot_template_id:
985 case tok::caret:
986 case tok::code_completion:
987 case tok::coloncolon:
988 case tok::ellipsis:
989 case tok::kw___attribute:
990 case tok::kw_operator:
991 case tok::l_paren:
992 case tok::star:
993 return true;
994
995 case tok::amp:
996 case tok::ampamp:
Richard Smith0706df42011-10-19 21:33:05 +0000997 return getLang().CPlusPlus;
998
Richard Smith1c94c162012-01-09 22:31:44 +0000999 case tok::l_square: // Might be an attribute on an unnamed bit-field.
1000 return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
1001 NextToken().is(tok::l_square);
1002
1003 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1004 return Context == Declarator::MemberContext || getLang().CPlusPlus;
1005
Richard Smith0706df42011-10-19 21:33:05 +00001006 case tok::identifier:
1007 switch (NextToken().getKind()) {
1008 case tok::code_completion:
1009 case tok::coloncolon:
1010 case tok::comma:
1011 case tok::equal:
1012 case tok::equalequal: // Might be a typo for '='.
1013 case tok::kw_alignas:
1014 case tok::kw_asm:
1015 case tok::kw___attribute:
1016 case tok::l_brace:
1017 case tok::l_paren:
1018 case tok::l_square:
1019 case tok::less:
1020 case tok::r_brace:
1021 case tok::r_paren:
1022 case tok::r_square:
1023 case tok::semi:
1024 return true;
1025
1026 case tok::colon:
1027 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001028 // and in block scope it's probably a label. Inside a class definition,
1029 // this is a bit-field.
1030 return Context == Declarator::MemberContext ||
1031 (getLang().CPlusPlus && Context == Declarator::FileContext);
1032
1033 case tok::identifier: // Possible virt-specifier.
1034 return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001035
1036 default:
1037 return false;
1038 }
1039
1040 default:
1041 return false;
1042 }
1043}
1044
John McCalld8ac0572009-11-03 19:26:08 +00001045/// ParseDeclGroup - Having concluded that this is either a function
1046/// definition or a group of object declarations, actually parse the
1047/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001048Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1049 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001050 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001051 SourceLocation *DeclEnd,
1052 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001053 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001054 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001055 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001056
John McCalld8ac0572009-11-03 19:26:08 +00001057 // Bail out if the first declarator didn't seem well-formed.
1058 if (!D.hasName() && !D.mayOmitIdentifier()) {
1059 // Skip until ; or }.
1060 SkipUntil(tok::r_brace, true, true);
1061 if (Tok.is(tok::semi))
1062 ConsumeToken();
1063 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattnerc82daef2010-07-11 22:24:20 +00001066 // Check to see if we have a function *definition* which must have a body.
1067 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1068 // Look at the next token to make sure that this isn't a function
1069 // declaration. We have to check this because __attribute__ might be the
1070 // start of a function definition in GCC-extended K&R C.
1071 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001072
Chris Lattner004659a2010-07-11 22:42:07 +00001073 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001074 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1075 Diag(Tok, diag::err_function_declared_typedef);
1076
1077 // Recover by treating the 'typedef' as spurious.
1078 DS.ClearStorageClassSpecs();
1079 }
1080
John McCalld226f652010-08-21 09:40:31 +00001081 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001082 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001083 }
1084
1085 if (isDeclarationSpecifier()) {
1086 // If there is an invalid declaration specifier right after the function
1087 // prototype, then we must be in a missing semicolon case where this isn't
1088 // actually a body. Just fall through into the code that handles it as a
1089 // prototype, and let the top-level code handle the erroneous declspec
1090 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001091 } else {
1092 Diag(Tok, diag::err_expected_fn_body);
1093 SkipUntil(tok::semi);
1094 return DeclGroupPtrTy();
1095 }
1096 }
1097
Richard Smithad762fc2011-04-14 22:09:26 +00001098 if (ParseAttributesAfterDeclarator(D))
1099 return DeclGroupPtrTy();
1100
1101 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1102 // must parse and analyze the for-range-initializer before the declaration is
1103 // analyzed.
1104 if (FRI && Tok.is(tok::colon)) {
1105 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001106 if (Tok.is(tok::l_brace))
1107 FRI->RangeExpr = ParseBraceInitializer();
1108 else
1109 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001110 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1111 Actions.ActOnCXXForRangeDecl(ThisDecl);
1112 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001113 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001114 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1115 }
1116
Chris Lattner5f9e2722011-07-23 10:55:15 +00001117 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001118 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001119 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001120 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001121 DeclsInGroup.push_back(FirstDecl);
1122
Richard Smith0706df42011-10-19 21:33:05 +00001123 bool ExpectSemi = Context != Declarator::ForContext;
1124
John McCalld8ac0572009-11-03 19:26:08 +00001125 // If we don't have a comma, it is either the end of the list (a ';') or an
1126 // error, bail out.
1127 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001128 SourceLocation CommaLoc = ConsumeToken();
1129
1130 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1131 // This comma was followed by a line-break and something which can't be
1132 // the start of a declarator. The comma was probably a typo for a
1133 // semicolon.
1134 Diag(CommaLoc, diag::err_expected_semi_declaration)
1135 << FixItHint::CreateReplacement(CommaLoc, ";");
1136 ExpectSemi = false;
1137 break;
1138 }
John McCalld8ac0572009-11-03 19:26:08 +00001139
1140 // Parse the next declarator.
1141 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001142 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001143
1144 // Accept attributes in an init-declarator. In the first declarator in a
1145 // declaration, these would be part of the declspec. In subsequent
1146 // declarators, they become part of the declarator itself, so that they
1147 // don't apply to declarators after *this* one. Examples:
1148 // short __attribute__((common)) var; -> declspec
1149 // short var __attribute__((common)); -> declarator
1150 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001151 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001152
1153 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001154 if (!D.isInvalidType()) {
1155 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1156 D.complete(ThisDecl);
1157 if (ThisDecl)
1158 DeclsInGroup.push_back(ThisDecl);
1159 }
John McCalld8ac0572009-11-03 19:26:08 +00001160 }
1161
1162 if (DeclEnd)
1163 *DeclEnd = Tok.getLocation();
1164
Richard Smith0706df42011-10-19 21:33:05 +00001165 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001166 ExpectAndConsume(tok::semi,
1167 Context == Declarator::FileContext
1168 ? diag::err_invalid_token_after_toplevel_declarator
1169 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001170 // Okay, there was no semicolon and one was expected. If we see a
1171 // declaration specifier, just assume it was missing and continue parsing.
1172 // Otherwise things are very confused and we skip to recover.
1173 if (!isDeclarationSpecifier()) {
1174 SkipUntil(tok::r_brace, true, true);
1175 if (Tok.is(tok::semi))
1176 ConsumeToken();
1177 }
John McCalld8ac0572009-11-03 19:26:08 +00001178 }
1179
Douglas Gregor23c94db2010-07-02 17:43:08 +00001180 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001181 DeclsInGroup.data(),
1182 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
Richard Smithad762fc2011-04-14 22:09:26 +00001185/// Parse an optional simple-asm-expr and attributes, and attach them to a
1186/// declarator. Returns true on an error.
1187bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1188 // If a simple-asm-expr is present, parse it.
1189 if (Tok.is(tok::kw_asm)) {
1190 SourceLocation Loc;
1191 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1192 if (AsmLabel.isInvalid()) {
1193 SkipUntil(tok::semi, true, true);
1194 return true;
1195 }
1196
1197 D.setAsmLabel(AsmLabel.release());
1198 D.SetRangeEnd(Loc);
1199 }
1200
1201 MaybeParseGNUAttributes(D);
1202 return false;
1203}
1204
Douglas Gregor1426e532009-05-12 21:31:51 +00001205/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1206/// declarator'. This method parses the remainder of the declaration
1207/// (including any attributes or initializer, among other things) and
1208/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001209///
Reid Spencer5f016e22007-07-11 17:01:13 +00001210/// init-declarator: [C99 6.7]
1211/// declarator
1212/// declarator '=' initializer
1213/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1214/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001215/// [C++] declarator initializer[opt]
1216///
1217/// [C++] initializer:
1218/// [C++] '=' initializer-clause
1219/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001220/// [C++0x] '=' 'default' [TODO]
1221/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001222/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001223///
1224/// According to the standard grammar, =default and =delete are function
1225/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001226///
John McCalld226f652010-08-21 09:40:31 +00001227Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001228 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001229 if (ParseAttributesAfterDeclarator(D))
1230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Richard Smithad762fc2011-04-14 22:09:26 +00001232 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1233}
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Richard Smithad762fc2011-04-14 22:09:26 +00001235Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1236 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001237 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001238 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001239 switch (TemplateInfo.Kind) {
1240 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001241 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001242 break;
1243
1244 case ParsedTemplateInfo::Template:
1245 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001246 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001247 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001248 TemplateInfo.TemplateParams->data(),
1249 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001250 D);
1251 break;
1252
1253 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001254 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001255 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001256 TemplateInfo.ExternLoc,
1257 TemplateInfo.TemplateLoc,
1258 D);
1259 if (ThisRes.isInvalid()) {
1260 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001261 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001262 }
1263
1264 ThisDecl = ThisRes.get();
1265 break;
1266 }
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Richard Smith34b41d92011-02-20 03:19:35 +00001269 bool TypeContainsAuto =
1270 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1271
Douglas Gregor1426e532009-05-12 21:31:51 +00001272 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001273 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001274 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001275 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001276 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001277 if (D.isFunctionDeclarator())
1278 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1279 << 1 /* delete */;
1280 else
1281 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001282 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001283 if (D.isFunctionDeclarator())
1284 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1285 << 1 /* delete */;
1286 else
1287 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001288 } else {
John McCall731ad842009-12-19 09:28:58 +00001289 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1290 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001291 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001292 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001293
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001294 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001295 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001296 cutOffParsing();
1297 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001298 }
1299
John McCall60d7b3a2010-08-24 06:29:42 +00001300 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001301
John McCall731ad842009-12-19 09:28:58 +00001302 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001303 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001304 ExitScope();
1305 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001306
Douglas Gregor1426e532009-05-12 21:31:51 +00001307 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001308 SkipUntil(tok::comma, true, true);
1309 Actions.ActOnInitializerError(ThisDecl);
1310 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001311 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1312 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001313 }
1314 } else if (Tok.is(tok::l_paren)) {
1315 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001316 BalancedDelimiterTracker T(*this, tok::l_paren);
1317 T.consumeOpen();
1318
Douglas Gregor1426e532009-05-12 21:31:51 +00001319 ExprVector Exprs(Actions);
1320 CommaLocsTy CommaLocs;
1321
Douglas Gregorb4debae2009-12-22 17:47:17 +00001322 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1323 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001324 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001325 }
1326
Douglas Gregor1426e532009-05-12 21:31:51 +00001327 if (ParseExpressionList(Exprs, CommaLocs)) {
1328 SkipUntil(tok::r_paren);
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 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001334 } else {
1335 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001336 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001337
1338 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1339 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001340
1341 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001342 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001343 ExitScope();
1344 }
1345
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001346 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001347 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001348 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001349 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001350 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001351 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1352 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001353 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1354
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001355 if (D.getCXXScopeSpec().isSet()) {
1356 EnterScope(0);
1357 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1358 }
1359
1360 ExprResult Init(ParseBraceInitializer());
1361
1362 if (D.getCXXScopeSpec().isSet()) {
1363 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1364 ExitScope();
1365 }
1366
1367 if (Init.isInvalid()) {
1368 Actions.ActOnInitializerError(ThisDecl);
1369 } else
1370 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1371 /*DirectInit=*/true, TypeContainsAuto);
1372
Douglas Gregor1426e532009-05-12 21:31:51 +00001373 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001374 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001375 }
1376
Richard Smith483b9f32011-02-21 20:05:19 +00001377 Actions.FinalizeDeclaration(ThisDecl);
1378
Douglas Gregor1426e532009-05-12 21:31:51 +00001379 return ThisDecl;
1380}
1381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382/// ParseSpecifierQualifierList
1383/// specifier-qualifier-list:
1384/// type-specifier specifier-qualifier-list[opt]
1385/// type-qualifier specifier-qualifier-list[opt]
1386/// [GNU] attributes specifier-qualifier-list[opt]
1387///
Richard Smithc89edf52011-07-01 19:46:12 +00001388void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1390 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001391 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001392 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 // Validate declspec for type-name.
1395 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001396 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001397 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 // Issue diagnostic and remove storage class if present.
1401 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1402 if (DS.getStorageClassSpecLoc().isValid())
1403 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1404 else
1405 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1406 DS.ClearStorageClassSpecs();
1407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 // Issue diagnostic and remove function specfier if present.
1410 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001411 if (DS.isInlineSpecified())
1412 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1413 if (DS.isVirtualSpecified())
1414 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1415 if (DS.isExplicitSpecified())
1416 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 DS.ClearFunctionSpecs();
1418 }
1419}
1420
Chris Lattnerc199ab32009-04-12 20:42:31 +00001421/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1422/// specified token is valid after the identifier in a declarator which
1423/// immediately follows the declspec. For example, these things are valid:
1424///
1425/// int x [ 4]; // direct-declarator
1426/// int x ( int y); // direct-declarator
1427/// int(int x ) // direct-declarator
1428/// int x ; // simple-declaration
1429/// int x = 17; // init-declarator-list
1430/// int x , y; // init-declarator-list
1431/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001432/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001433/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001434///
1435/// This is not, because 'x' does not immediately follow the declspec (though
1436/// ')' happens to be valid anyway).
1437/// int (x)
1438///
1439static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1440 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1441 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001442 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001443}
1444
Chris Lattnere40c2952009-04-14 21:34:55 +00001445
1446/// ParseImplicitInt - This method is called when we have an non-typename
1447/// identifier in a declspec (which normally terminates the decl spec) when
1448/// the declspec has no type specifier. In this case, the declspec is either
1449/// malformed or is "implicit int" (in K&R and C89).
1450///
1451/// This method handles diagnosing this prettily and returns false if the
1452/// declspec is done being processed. If it recovers and thinks there may be
1453/// other pieces of declspec after it, it returns true.
1454///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001455bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001456 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001457 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001458 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattnere40c2952009-04-14 21:34:55 +00001460 SourceLocation Loc = Tok.getLocation();
1461 // If we see an identifier that is not a type name, we normally would
1462 // parse it as the identifer being declared. However, when a typename
1463 // is typo'd or the definition is not included, this will incorrectly
1464 // parse the typename as the identifier name and fall over misparsing
1465 // later parts of the diagnostic.
1466 //
1467 // As such, we try to do some look-ahead in cases where this would
1468 // otherwise be an "implicit-int" case to see if this is invalid. For
1469 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1470 // an identifier with implicit int, we'd get a parse error because the
1471 // next token is obviously invalid for a type. Parse these as a case
1472 // with an invalid type specifier.
1473 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Chris Lattnere40c2952009-04-14 21:34:55 +00001475 // Since we know that this either implicit int (which is rare) or an
1476 // error, we'd do lookahead to try to do better recovery.
1477 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1478 // If this token is valid for implicit int, e.g. "static x = 4", then
1479 // we just avoid eating the identifier, so it will be parsed as the
1480 // identifier in the declarator.
1481 return false;
1482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Chris Lattnere40c2952009-04-14 21:34:55 +00001484 // Otherwise, if we don't consume this token, we are going to emit an
1485 // error anyway. Try to recover from various common problems. Check
1486 // to see if this was a reference to a tag name without a tag specified.
1487 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001488 //
1489 // C++ doesn't need this, and isTagName doesn't take SS.
1490 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001491 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001492 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregor23c94db2010-07-02 17:43:08 +00001494 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001495 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001496 case DeclSpec::TST_enum:
1497 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1498 case DeclSpec::TST_union:
1499 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1500 case DeclSpec::TST_struct:
1501 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1502 case DeclSpec::TST_class:
1503 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Chris Lattnerf4382f52009-04-14 22:17:06 +00001506 if (TagName) {
1507 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001508 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001509 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattnerf4382f52009-04-14 22:17:06 +00001511 // Parse this as a tag as if the missing tag were present.
1512 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001513 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001514 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001515 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001516 return true;
1517 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregora786fdb2009-10-13 23:27:22 +00001520 // This is almost certainly an invalid type name. Let the action emit a
1521 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001522 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001523 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001524 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001525 // The action emitted a diagnostic, so we don't have to.
1526 if (T) {
1527 // The action has suggested that the type T could be used. Set that as
1528 // the type in the declaration specifiers, consume the would-be type
1529 // name token, and we're done.
1530 const char *PrevSpec;
1531 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001532 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001533 DS.SetRangeEnd(Tok.getLocation());
1534 ConsumeToken();
1535
1536 // There may be other declaration specifiers after this.
1537 return true;
1538 }
1539
1540 // Fall through; the action had no suggestion for us.
1541 } else {
1542 // The action did not emit a diagnostic, so emit one now.
1543 SourceRange R;
1544 if (SS) R = SS->getRange();
1545 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregora786fdb2009-10-13 23:27:22 +00001548 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001549 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001550 unsigned DiagID;
1551 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001552 DS.SetRangeEnd(Tok.getLocation());
1553 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Chris Lattnere40c2952009-04-14 21:34:55 +00001555 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1556 // avoid rippling error messages on subsequent uses of the same type,
1557 // could be useful if #include was forgotten.
1558 return false;
1559}
1560
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001561/// \brief Determine the declaration specifier context from the declarator
1562/// context.
1563///
1564/// \param Context the declarator context, which is one of the
1565/// Declarator::TheContext enumerator values.
1566Parser::DeclSpecContext
1567Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1568 if (Context == Declarator::MemberContext)
1569 return DSC_class;
1570 if (Context == Declarator::FileContext)
1571 return DSC_top_level;
1572 return DSC_normal;
1573}
1574
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001575/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1576///
1577/// FIXME: Simply returns an alignof() expression if the argument is a
1578/// type. Ideally, the type should be propagated directly into Sema.
1579///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001580/// [C11] type-id
1581/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001582/// [C++0x] type-id ...[opt]
1583/// [C++0x] assignment-expression ...[opt]
1584ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1585 SourceLocation &EllipsisLoc) {
1586 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001587 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001588 SourceLocation TypeLoc = Tok.getLocation();
1589 ParsedType Ty = ParseTypeName().get();
1590 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001591 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1592 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001593 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001594 ER = ParseConstantExpression();
1595
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001596 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1597 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001598
1599 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001600}
1601
1602/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1603/// attribute to Attrs.
1604///
1605/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001606/// [C11] '_Alignas' '(' type-id ')'
1607/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001608/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1609/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001610void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1611 SourceLocation *endLoc) {
1612 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1613 "Not an alignment-specifier!");
1614
1615 SourceLocation KWLoc = Tok.getLocation();
1616 ConsumeToken();
1617
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001618 BalancedDelimiterTracker T(*this, tok::l_paren);
1619 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001620 return;
1621
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001622 SourceLocation EllipsisLoc;
1623 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001624 if (ArgExpr.isInvalid()) {
1625 SkipUntil(tok::r_paren);
1626 return;
1627 }
1628
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001629 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001630 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001631 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001632
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001633 // FIXME: Handle pack-expansions here.
1634 if (EllipsisLoc.isValid()) {
1635 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1636 return;
1637 }
1638
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001639 ExprVector ArgExprs(Actions);
1640 ArgExprs.push_back(ArgExpr.release());
1641 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001642 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001643}
1644
Reid Spencer5f016e22007-07-11 17:01:13 +00001645/// ParseDeclarationSpecifiers
1646/// declaration-specifiers: [C99 6.7]
1647/// storage-class-specifier declaration-specifiers[opt]
1648/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001649/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001650/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001651/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001652/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001653///
1654/// storage-class-specifier: [C99 6.7.1]
1655/// 'typedef'
1656/// 'extern'
1657/// 'static'
1658/// 'auto'
1659/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001660/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001661/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001662/// function-specifier: [C99 6.7.4]
1663/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001664/// [C++] 'virtual'
1665/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001666/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001667/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001668/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001669
Reid Spencer5f016e22007-07-11 17:01:13 +00001670///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001671void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001672 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001673 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001674 DeclSpecContext DSContext) {
1675 if (DS.getSourceRange().isInvalid()) {
1676 DS.SetRangeStart(Tok.getLocation());
1677 DS.SetRangeEnd(Tok.getLocation());
1678 }
1679
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001680 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001682 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001684 unsigned DiagID = 0;
1685
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001687
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001689 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001690 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001691 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1692 MaybeParseCXX0XAttributes(DS.getAttributes());
1693
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 // If this is not a declaration specifier token, we're done reading decl
1695 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001696 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001699 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001700 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001701 if (DS.hasTypeSpecifier()) {
1702 bool AllowNonIdentifiers
1703 = (getCurScope()->getFlags() & (Scope::ControlScope |
1704 Scope::BlockScope |
1705 Scope::TemplateParamScope |
1706 Scope::FunctionPrototypeScope |
1707 Scope::AtCatchScope)) == 0;
1708 bool AllowNestedNameSpecifiers
1709 = DSContext == DSC_top_level ||
1710 (DSContext == DSC_class && DS.isFriendSpecified());
1711
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001712 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1713 AllowNonIdentifiers,
1714 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001715 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001716 }
1717
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001718 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1719 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1720 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001721 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1722 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001723 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001724 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001725 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001726 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001727
1728 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001729 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001730 }
1731
Chris Lattner5e02c472009-01-05 00:07:25 +00001732 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001733 // C++ scope specifier. Annotate and loop, or bail out on error.
1734 if (TryAnnotateCXXScopeToken(true)) {
1735 if (!DS.hasTypeSpecifier())
1736 DS.SetTypeSpecError();
1737 goto DoneWithDeclSpec;
1738 }
John McCall2e0a7152010-03-01 18:20:46 +00001739 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1740 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001741 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001742
1743 case tok::annot_cxxscope: {
1744 if (DS.hasTypeSpecifier())
1745 goto DoneWithDeclSpec;
1746
John McCallaa87d332009-12-12 11:40:51 +00001747 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001748 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1749 Tok.getAnnotationRange(),
1750 SS);
John McCallaa87d332009-12-12 11:40:51 +00001751
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001752 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001753 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001754 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001755 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001756 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001757 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001758
1759 // C++ [class.qual]p2:
1760 // In a lookup in which the constructor is an acceptable lookup
1761 // result and the nested-name-specifier nominates a class C:
1762 //
1763 // - if the name specified after the
1764 // nested-name-specifier, when looked up in C, is the
1765 // injected-class-name of C (Clause 9), or
1766 //
1767 // - if the name specified after the nested-name-specifier
1768 // is the same as the identifier or the
1769 // simple-template-id's template-name in the last
1770 // component of the nested-name-specifier,
1771 //
1772 // the name is instead considered to name the constructor of
1773 // class C.
1774 //
1775 // Thus, if the template-name is actually the constructor
1776 // name, then the code is ill-formed; this interpretation is
1777 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001778 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001779 if ((DSContext == DSC_top_level ||
1780 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1781 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001782 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001783 if (isConstructorDeclarator()) {
1784 // The user meant this to be an out-of-line constructor
1785 // definition, but template arguments are not allowed
1786 // there. Just allow this as a constructor; we'll
1787 // complain about it later.
1788 goto DoneWithDeclSpec;
1789 }
1790
1791 // The user meant this to name a type, but it actually names
1792 // a constructor with some extraneous template
1793 // arguments. Complain, then parse it as a type as the user
1794 // intended.
1795 Diag(TemplateId->TemplateNameLoc,
1796 diag::err_out_of_line_template_id_names_constructor)
1797 << TemplateId->Name;
1798 }
1799
John McCallaa87d332009-12-12 11:40:51 +00001800 DS.getTypeSpecScope() = SS;
1801 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001802 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001803 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001804 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001805 continue;
1806 }
1807
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001808 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001809 DS.getTypeSpecScope() = SS;
1810 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001811 if (Tok.getAnnotationValue()) {
1812 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001813 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1814 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001815 PrevSpec, DiagID, T);
1816 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001817 else
1818 DS.SetTypeSpecError();
1819 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1820 ConsumeToken(); // The typename
1821 }
1822
Douglas Gregor9135c722009-03-25 15:40:00 +00001823 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001824 goto DoneWithDeclSpec;
1825
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001826 // If we're in a context where the identifier could be a class name,
1827 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001828 if ((DSContext == DSC_top_level ||
1829 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001830 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001831 &SS)) {
1832 if (isConstructorDeclarator())
1833 goto DoneWithDeclSpec;
1834
1835 // As noted in C++ [class.qual]p2 (cited above), when the name
1836 // of the class is qualified in a context where it could name
1837 // a constructor, its a constructor name. However, we've
1838 // looked at the declarator, and the user probably meant this
1839 // to be a type. Complain that it isn't supposed to be treated
1840 // as a type, then proceed to parse it as a type.
1841 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1842 << Next.getIdentifierInfo();
1843 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001844
John McCallb3d87482010-08-24 05:47:05 +00001845 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1846 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001847 getCurScope(), &SS,
1848 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001849 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001850 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001851
Chris Lattnerf4382f52009-04-14 22:17:06 +00001852 // If the referenced identifier is not a type, then this declspec is
1853 // erroneous: We already checked about that it has no type specifier, and
1854 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001855 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001856 if (TypeRep == 0) {
1857 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001858 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001859 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001860 }
Mike Stump1eb44332009-09-09 15:08:12 +00001861
John McCallaa87d332009-12-12 11:40:51 +00001862 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001863 ConsumeToken(); // The C++ scope.
1864
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001865 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001866 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001867 if (isInvalid)
1868 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001870 DS.SetRangeEnd(Tok.getLocation());
1871 ConsumeToken(); // The typename.
1872
1873 continue;
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Chris Lattner80d0c892009-01-21 19:48:37 +00001876 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001877 if (Tok.getAnnotationValue()) {
1878 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001879 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001880 DiagID, T);
1881 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001882 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001883
1884 if (isInvalid)
1885 break;
1886
Chris Lattner80d0c892009-01-21 19:48:37 +00001887 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1888 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Chris Lattner80d0c892009-01-21 19:48:37 +00001890 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1891 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001892 // Objective-C interface.
1893 if (Tok.is(tok::less) && getLang().ObjC1)
1894 ParseObjCProtocolQualifiers(DS);
1895
Chris Lattner80d0c892009-01-21 19:48:37 +00001896 continue;
1897 }
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Douglas Gregorbfad9152011-04-28 15:48:45 +00001899 case tok::kw___is_signed:
1900 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1901 // typically treats it as a trait. If we see __is_signed as it appears
1902 // in libstdc++, e.g.,
1903 //
1904 // static const bool __is_signed;
1905 //
1906 // then treat __is_signed as an identifier rather than as a keyword.
1907 if (DS.getTypeSpecType() == TST_bool &&
1908 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1909 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1910 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1911 Tok.setKind(tok::identifier);
1912 }
1913
1914 // We're done with the declaration-specifiers.
1915 goto DoneWithDeclSpec;
1916
Chris Lattner3bd934a2008-07-26 01:18:38 +00001917 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001918 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001919 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001920 // In C++, check to see if this is a scope specifier like foo::bar::, if
1921 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001922 if (getLang().CPlusPlus) {
1923 if (TryAnnotateCXXScopeToken(true)) {
1924 if (!DS.hasTypeSpecifier())
1925 DS.SetTypeSpecError();
1926 goto DoneWithDeclSpec;
1927 }
1928 if (!Tok.is(tok::identifier))
1929 continue;
1930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Chris Lattner3bd934a2008-07-26 01:18:38 +00001932 // This identifier can only be a typedef name if we haven't already seen
1933 // a type-specifier. Without this check we misparse:
1934 // typedef int X; struct Y { short X; }; as 'short int'.
1935 if (DS.hasTypeSpecifier())
1936 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001937
John Thompson82287d12010-02-05 00:12:22 +00001938 // Check for need to substitute AltiVec keyword tokens.
1939 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1940 break;
1941
Chris Lattner3bd934a2008-07-26 01:18:38 +00001942 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001943 ParsedType TypeRep =
1944 Actions.getTypeName(*Tok.getIdentifierInfo(),
1945 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001946
Chris Lattnerc199ab32009-04-12 20:42:31 +00001947 // If this is not a typedef name, don't parse it as part of the declspec,
1948 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001949 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001950 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001951 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001952 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001953
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001954 // If we're in a context where the identifier could be a class name,
1955 // check whether this is a constructor declaration.
1956 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001957 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001958 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001959 goto DoneWithDeclSpec;
1960
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001962 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001963 if (isInvalid)
1964 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Chris Lattner3bd934a2008-07-26 01:18:38 +00001966 DS.SetRangeEnd(Tok.getLocation());
1967 ConsumeToken(); // The identifier
1968
1969 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1970 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001971 // Objective-C interface.
1972 if (Tok.is(tok::less) && getLang().ObjC1)
1973 ParseObjCProtocolQualifiers(DS);
1974
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001975 // Need to support trailing type qualifiers (e.g. "id<p> const").
1976 // If a type specifier follows, it will be diagnosed elsewhere.
1977 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001978 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001979
1980 // type-name
1981 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001982 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001983 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001984 // This template-id does not refer to a type name, so we're
1985 // done with the type-specifiers.
1986 goto DoneWithDeclSpec;
1987 }
1988
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001989 // If we're in a context where the template-id could be a
1990 // constructor name or specialization, check whether this is a
1991 // constructor declaration.
1992 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001993 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001994 isConstructorDeclarator())
1995 goto DoneWithDeclSpec;
1996
Douglas Gregor39a8de12009-02-25 19:37:18 +00001997 // Turn the template-id annotation token into a type annotation
1998 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001999 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002000 continue;
2001 }
2002
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 // GNU attributes support.
2004 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00002005 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002007
2008 // Microsoft declspec support.
2009 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002010 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002011 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Steve Naroff239f0732008-12-25 14:16:32 +00002013 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002014 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002015 // FIXME: Add handling here!
2016 break;
2017
2018 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002019 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002020 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002021 case tok::kw___cdecl:
2022 case tok::kw___stdcall:
2023 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002024 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002025 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002026 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002027 continue;
2028
Dawn Perchik52fc3142010-09-03 01:29:35 +00002029 // Borland single token adornments.
2030 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002031 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002032 continue;
2033
Peter Collingbournef315fa82011-02-14 01:42:53 +00002034 // OpenCL single token adornments.
2035 case tok::kw___kernel:
2036 ParseOpenCLAttributes(DS.getAttributes());
2037 continue;
2038
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 // storage-class-specifier
2040 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002041 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2042 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 break;
2044 case tok::kw_extern:
2045 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002046 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002047 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2048 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002050 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002051 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2052 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002053 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 case tok::kw_static:
2055 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002056 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002057 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2058 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 break;
2060 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002061 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002062 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002063 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2064 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002065 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002066 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002067 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002068 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2070 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002071 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002072 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2073 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 break;
2075 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002076 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2077 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002079 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002080 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2081 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002082 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002084 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 // function-specifier
2088 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002090 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002091 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002092 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002093 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002094 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002095 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002096 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002097
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002098 // alignment-specifier
2099 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002100 if (!getLang().C11)
2101 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002102 ParseAlignmentSpecifier(DS.getAttributes());
2103 continue;
2104
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002105 // friend
2106 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002107 if (DSContext == DSC_class)
2108 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2109 else {
2110 PrevSpec = ""; // not actually used by the diagnostic
2111 DiagID = diag::err_friend_invalid_in_context;
2112 isInvalid = true;
2113 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002114 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Douglas Gregor8d267c52011-09-09 02:06:17 +00002116 // Modules
2117 case tok::kw___module_private__:
2118 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2119 break;
2120
Sebastian Redl2ac67232009-11-05 15:47:02 +00002121 // constexpr
2122 case tok::kw_constexpr:
2123 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2124 break;
2125
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 // type-specifier
2127 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002128 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2129 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002130 break;
2131 case tok::kw_long:
2132 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002133 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2134 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002135 else
John McCallfec54012009-08-03 20:12:06 +00002136 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2137 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002138 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002139 case tok::kw___int64:
2140 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2141 DiagID);
2142 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002143 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002144 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2145 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002146 break;
2147 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2149 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002150 break;
2151 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2153 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002154 break;
2155 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2157 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002158 break;
2159 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2161 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002162 break;
2163 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
2167 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2169 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002170 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002171 case tok::kw_half:
2172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2173 DiagID);
2174 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002175 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002176 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2177 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002178 break;
2179 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2181 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002182 break;
2183 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2185 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002186 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002187 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2189 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002190 break;
2191 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002192 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2193 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002194 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002195 case tok::kw_bool:
2196 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002197 if (Tok.is(tok::kw_bool) &&
2198 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2199 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2200 PrevSpec = ""; // Not used by the diagnostic.
2201 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002202 // For better error recovery.
2203 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002204 isInvalid = true;
2205 } else {
2206 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2207 DiagID);
2208 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002209 break;
2210 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002211 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2212 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002213 break;
2214 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002215 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2216 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002217 break;
2218 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002219 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2220 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002221 break;
John Thompson82287d12010-02-05 00:12:22 +00002222 case tok::kw___vector:
2223 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2224 break;
2225 case tok::kw___pixel:
2226 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2227 break;
John McCalla5fc4722011-04-09 22:50:59 +00002228 case tok::kw___unknown_anytype:
2229 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2230 PrevSpec, DiagID);
2231 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002232
2233 // class-specifier:
2234 case tok::kw_class:
2235 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002236 case tok::kw_union: {
2237 tok::TokenKind Kind = Tok.getKind();
2238 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002239 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002240 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002241 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002242
2243 // enum-specifier:
2244 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002245 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002246 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002247 continue;
2248
2249 // cv-qualifier:
2250 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002251 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2252 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002253 break;
2254 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002255 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2256 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002257 break;
2258 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002259 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2260 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002261 break;
2262
Douglas Gregord57959a2009-03-27 23:10:48 +00002263 // C++ typename-specifier:
2264 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002265 if (TryAnnotateTypeOrScopeToken()) {
2266 DS.SetTypeSpecError();
2267 goto DoneWithDeclSpec;
2268 }
2269 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002270 continue;
2271 break;
2272
Chris Lattner80d0c892009-01-21 19:48:37 +00002273 // GNU typeof support.
2274 case tok::kw_typeof:
2275 ParseTypeofSpecifier(DS);
2276 continue;
2277
David Blaikie42d6d0c2011-12-04 05:04:18 +00002278 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002279 ParseDecltypeSpecifier(DS);
2280 continue;
2281
Sean Huntdb5d44b2011-05-19 05:37:45 +00002282 case tok::kw___underlying_type:
2283 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002284 continue;
2285
2286 case tok::kw__Atomic:
2287 ParseAtomicSpecifier(DS);
2288 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002289
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002290 // OpenCL qualifiers:
2291 case tok::kw_private:
2292 if (!getLang().OpenCL)
2293 goto DoneWithDeclSpec;
2294 case tok::kw___private:
2295 case tok::kw___global:
2296 case tok::kw___local:
2297 case tok::kw___constant:
2298 case tok::kw___read_only:
2299 case tok::kw___write_only:
2300 case tok::kw___read_write:
2301 ParseOpenCLQualifiers(DS);
2302 break;
2303
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002304 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002305 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002306 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2307 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002308 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002309 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Douglas Gregor46f936e2010-11-19 17:10:50 +00002311 if (!ParseObjCProtocolQualifiers(DS))
2312 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2313 << FixItHint::CreateInsertion(Loc, "id")
2314 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002315
2316 // Need to support trailing type qualifiers (e.g. "id<p> const").
2317 // If a type specifier follows, it will be diagnosed elsewhere.
2318 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 }
John McCallfec54012009-08-03 20:12:06 +00002320 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 if (isInvalid) {
2322 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002323 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002324
2325 if (DiagID == diag::ext_duplicate_declspec)
2326 Diag(Tok, DiagID)
2327 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2328 else
2329 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002331
Chris Lattner81c018d2008-03-13 06:29:04 +00002332 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002333 if (DiagID != diag::err_bool_redeclaration)
2334 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002335 }
2336}
Douglas Gregoradcac882008-12-01 23:54:00 +00002337
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002338/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002339/// primarily follow the C++ grammar with additions for C99 and GNU,
2340/// which together subsume the C grammar. Note that the C++
2341/// type-specifier also includes the C type-qualifier (for const,
2342/// volatile, and C99 restrict). Returns true if a type-specifier was
2343/// found (and parsed), false otherwise.
2344///
2345/// type-specifier: [C++ 7.1.5]
2346/// simple-type-specifier
2347/// class-specifier
2348/// enum-specifier
2349/// elaborated-type-specifier [TODO]
2350/// cv-qualifier
2351///
2352/// cv-qualifier: [C++ 7.1.5.1]
2353/// 'const'
2354/// 'volatile'
2355/// [C99] 'restrict'
2356///
2357/// simple-type-specifier: [ C++ 7.1.5.2]
2358/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2359/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2360/// 'char'
2361/// 'wchar_t'
2362/// 'bool'
2363/// 'short'
2364/// 'int'
2365/// 'long'
2366/// 'signed'
2367/// 'unsigned'
2368/// 'float'
2369/// 'double'
2370/// 'void'
2371/// [C99] '_Bool'
2372/// [C99] '_Complex'
2373/// [C99] '_Imaginary' // Removed in TC2?
2374/// [GNU] '_Decimal32'
2375/// [GNU] '_Decimal64'
2376/// [GNU] '_Decimal128'
2377/// [GNU] typeof-specifier
2378/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2379/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002380/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002381/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002382bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002383 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002384 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002385 const ParsedTemplateInfo &TemplateInfo,
2386 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002387 SourceLocation Loc = Tok.getLocation();
2388
2389 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002390 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002391 // If we already have a type specifier, this identifier is not a type.
2392 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2393 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2394 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2395 return false;
John Thompson82287d12010-02-05 00:12:22 +00002396 // Check for need to substitute AltiVec keyword tokens.
2397 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2398 break;
2399 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002400 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002401 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002402 // Annotate typenames and C++ scope specifiers. If we get one, just
2403 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002404 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2405 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002406 return true;
2407 if (Tok.is(tok::identifier))
2408 return false;
2409 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2410 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002411 case tok::coloncolon: // ::foo::bar
2412 if (NextToken().is(tok::kw_new) || // ::new
2413 NextToken().is(tok::kw_delete)) // ::delete
2414 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Chris Lattner166a8fc2009-01-04 23:41:41 +00002416 // Annotate typenames and C++ scope specifiers. If we get one, just
2417 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002418 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2419 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002420 return true;
2421 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2422 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Douglas Gregor12e083c2008-11-07 15:42:26 +00002424 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002425 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002426 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002427 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2428 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002429 DiagID, T);
2430 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002431 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002432 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2433 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Douglas Gregor12e083c2008-11-07 15:42:26 +00002435 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2436 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2437 // Objective-C interface. If we don't have Objective-C or a '<', this is
2438 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002439 if (Tok.is(tok::less) && getLang().ObjC1)
2440 ParseObjCProtocolQualifiers(DS);
2441
Douglas Gregor12e083c2008-11-07 15:42:26 +00002442 return true;
2443 }
2444
2445 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002447 break;
2448 case tok::kw_long:
2449 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002450 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2451 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002452 else
John McCallfec54012009-08-03 20:12:06 +00002453 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2454 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002455 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002456 case tok::kw___int64:
2457 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2458 DiagID);
2459 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002460 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002461 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 break;
2463 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2465 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002466 break;
2467 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002468 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2469 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002470 break;
2471 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002472 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2473 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002474 break;
2475 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002477 break;
2478 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002480 break;
2481 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002483 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002484 case tok::kw_half:
2485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2486 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002487 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002489 break;
2490 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002492 break;
2493 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002495 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002496 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002498 break;
2499 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002501 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002502 case tok::kw_bool:
2503 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002505 break;
2506 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2508 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002509 break;
2510 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2512 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002513 break;
2514 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2516 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002517 break;
John Thompson82287d12010-02-05 00:12:22 +00002518 case tok::kw___vector:
2519 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2520 break;
2521 case tok::kw___pixel:
2522 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2523 break;
2524
Douglas Gregor12e083c2008-11-07 15:42:26 +00002525 // class-specifier:
2526 case tok::kw_class:
2527 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002528 case tok::kw_union: {
2529 tok::TokenKind Kind = Tok.getKind();
2530 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002531 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002532 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002533 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002534 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002535 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002536
2537 // enum-specifier:
2538 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002539 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002540 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002541 return true;
2542
2543 // cv-qualifier:
2544 case tok::kw_const:
2545 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002546 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002547 break;
2548 case tok::kw_volatile:
2549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002550 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002551 break;
2552 case tok::kw_restrict:
2553 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002554 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002555 break;
2556
2557 // GNU typeof support.
2558 case tok::kw_typeof:
2559 ParseTypeofSpecifier(DS);
2560 return true;
2561
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002562 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002563 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002564 ParseDecltypeSpecifier(DS);
2565 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Sean Huntdb5d44b2011-05-19 05:37:45 +00002567 // C++0x type traits support.
2568 case tok::kw___underlying_type:
2569 ParseUnderlyingTypeSpecifier(DS);
2570 return true;
2571
Eli Friedmanb001de72011-10-06 23:00:33 +00002572 case tok::kw__Atomic:
2573 ParseAtomicSpecifier(DS);
2574 return true;
2575
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002576 // OpenCL qualifiers:
2577 case tok::kw_private:
2578 if (!getLang().OpenCL)
2579 return false;
2580 case tok::kw___private:
2581 case tok::kw___global:
2582 case tok::kw___local:
2583 case tok::kw___constant:
2584 case tok::kw___read_only:
2585 case tok::kw___write_only:
2586 case tok::kw___read_write:
2587 ParseOpenCLQualifiers(DS);
2588 break;
2589
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002590 // C++0x auto support.
2591 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002592 // This is only called in situations where a storage-class specifier is
2593 // illegal, so we can assume an auto type specifier was intended even in
2594 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2595 // extension diagnostic.
2596 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002597 return false;
2598
John McCallfec54012009-08-03 20:12:06 +00002599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002600 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002601
Eli Friedman290eeb02009-06-08 23:27:34 +00002602 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002603 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002604 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002605 case tok::kw___cdecl:
2606 case tok::kw___stdcall:
2607 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002608 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002609 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002610 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002611 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002612
Dawn Perchik52fc3142010-09-03 01:29:35 +00002613 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002614 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002615 return true;
2616
Douglas Gregor12e083c2008-11-07 15:42:26 +00002617 default:
2618 // Not a type-specifier; do nothing.
2619 return false;
2620 }
2621
2622 // If the specifier combination wasn't legal, issue a diagnostic.
2623 if (isInvalid) {
2624 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002625 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002626 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002627 }
2628 DS.SetRangeEnd(Tok.getLocation());
2629 ConsumeToken(); // whatever we parsed above.
2630 return true;
2631}
Reid Spencer5f016e22007-07-11 17:01:13 +00002632
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002633/// ParseStructDeclaration - Parse a struct declaration without the terminating
2634/// semicolon.
2635///
Reid Spencer5f016e22007-07-11 17:01:13 +00002636/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002637/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002638/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002639/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002640/// struct-declarator-list:
2641/// struct-declarator
2642/// struct-declarator-list ',' struct-declarator
2643/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2644/// struct-declarator:
2645/// declarator
2646/// [GNU] declarator attributes[opt]
2647/// declarator[opt] ':' constant-expression
2648/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2649///
Chris Lattnere1359422008-04-10 06:46:29 +00002650void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002651ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002652
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002653 if (Tok.is(tok::kw___extension__)) {
2654 // __extension__ silences extension warnings in the subexpression.
2655 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002656 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002657 return ParseStructDeclaration(DS, Fields);
2658 }
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Steve Naroff28a7ca82007-08-20 22:28:22 +00002660 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002661 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002662
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002663 // If there are no declarators, this is a free-standing declaration
2664 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002665 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002666 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002667 return;
2668 }
2669
2670 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002671 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002672 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002673 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002674 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002675 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002676 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002677
2678 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002679 if (!FirstDeclarator)
2680 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002681
Steve Naroff28a7ca82007-08-20 22:28:22 +00002682 /// struct-declarator: declarator
2683 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002684 if (Tok.isNot(tok::colon)) {
2685 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2686 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002687 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Chris Lattner04d66662007-10-09 17:33:22 +00002690 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002691 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002692 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002693 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002694 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002695 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002696 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002697 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002698
Steve Naroff28a7ca82007-08-20 22:28:22 +00002699 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002700 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002701
John McCallbdd563e2009-11-03 02:38:08 +00002702 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002703 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002704 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002705
Steve Naroff28a7ca82007-08-20 22:28:22 +00002706 // If we don't have a comma, it is either the end of the list (a ';')
2707 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002708 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002709 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002710
Steve Naroff28a7ca82007-08-20 22:28:22 +00002711 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002712 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002713
John McCallbdd563e2009-11-03 02:38:08 +00002714 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002715 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002716}
2717
2718/// ParseStructUnionBody
2719/// struct-contents:
2720/// struct-declaration-list
2721/// [EXT] empty
2722/// [GNU] "struct-declaration-list" without terminatoring ';'
2723/// struct-declaration-list:
2724/// struct-declaration
2725/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002726/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002727///
Reid Spencer5f016e22007-07-11 17:01:13 +00002728void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002729 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002730 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2731 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002733 BalancedDelimiterTracker T(*this, tok::l_brace);
2734 if (T.consumeOpen())
2735 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002737 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002738 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002739
Reid Spencer5f016e22007-07-11 17:01:13 +00002740 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2741 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002742 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2743 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2744 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2745 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002746
Chris Lattner5f9e2722011-07-23 10:55:15 +00002747 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002748
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002750 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002751 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Reid Spencer5f016e22007-07-11 17:01:13 +00002753 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002754 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002755 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002756 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002757 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002758 ConsumeToken();
2759 continue;
2760 }
Chris Lattnere1359422008-04-10 06:46:29 +00002761
2762 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002763 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002764
John McCallbdd563e2009-11-03 02:38:08 +00002765 if (!Tok.is(tok::at)) {
2766 struct CFieldCallback : FieldCallback {
2767 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002768 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002769 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002770
John McCalld226f652010-08-21 09:40:31 +00002771 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002772 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002773 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2774
John McCalld226f652010-08-21 09:40:31 +00002775 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002776 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002777 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002778 FD.D.getDeclSpec().getSourceRange().getBegin(),
2779 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002780 FieldDecls.push_back(Field);
2781 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002782 }
John McCallbdd563e2009-11-03 02:38:08 +00002783 } Callback(*this, TagDecl, FieldDecls);
2784
2785 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002786 } else { // Handle @defs
2787 ConsumeToken();
2788 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2789 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002790 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002791 continue;
2792 }
2793 ConsumeToken();
2794 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2795 if (!Tok.is(tok::identifier)) {
2796 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002797 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002798 continue;
2799 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002800 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002801 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002802 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002803 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2804 ConsumeToken();
2805 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002806 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002807
Chris Lattner04d66662007-10-09 17:33:22 +00002808 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002810 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002811 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002812 break;
2813 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002814 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2815 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002817 // If we stopped at a ';', eat it.
2818 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 }
2820 }
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002822 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002823
John McCall0b7e6782011-03-24 11:26:52 +00002824 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002825 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002826 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002827
Douglas Gregor23c94db2010-07-02 17:43:08 +00002828 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002829 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002830 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002831 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002832 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002833 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2834 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002835}
2836
Reid Spencer5f016e22007-07-11 17:01:13 +00002837/// ParseEnumSpecifier
2838/// enum-specifier: [C99 6.7.2.2]
2839/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002840///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002841/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2842/// '}' attributes[opt]
2843/// 'enum' identifier
2844/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002845///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002846/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2847/// [C++0x] enum-head '{' enumerator-list ',' '}'
2848///
2849/// enum-head: [C++0x]
2850/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2851/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2852///
2853/// enum-key: [C++0x]
2854/// 'enum'
2855/// 'enum' 'class'
2856/// 'enum' 'struct'
2857///
2858/// enum-base: [C++0x]
2859/// ':' type-specifier-seq
2860///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002861/// [C++] elaborated-type-specifier:
2862/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2863///
Chris Lattner4c97d762009-04-12 21:49:30 +00002864void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002865 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002866 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002868 if (Tok.is(tok::code_completion)) {
2869 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002870 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002871 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002872 }
John McCall57c13002011-07-06 05:58:41 +00002873
Richard Smithbdad7a22012-01-10 01:33:14 +00002874 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002875 bool IsScopedUsingClassTag = false;
2876
2877 if (getLang().CPlusPlus0x &&
2878 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002879 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002880 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002881 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002882 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002883
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002884 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002885 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002886 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002887
Douglas Gregor5471bc82011-09-08 17:18:35 +00002888 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002889 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002890
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002891 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002892 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002893 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2894 // if a fixed underlying type is allowed.
2895 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2896
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002897 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2898 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002899 return;
2900
2901 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002902 Diag(Tok, diag::err_expected_ident);
2903 if (Tok.isNot(tok::l_brace)) {
2904 // Has no name and is not a definition.
2905 // Skip the rest of this declarator, up until the comma or semicolon.
2906 SkipUntil(tok::comma, true);
2907 return;
2908 }
2909 }
2910 }
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002912 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002913 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2914 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002915 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002916
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002917 // Skip the rest of this declarator, up until the comma or semicolon.
2918 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002920 }
Mike Stump1eb44332009-09-09 15:08:12 +00002921
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002922 // If an identifier is present, consume and remember it.
2923 IdentifierInfo *Name = 0;
2924 SourceLocation NameLoc;
2925 if (Tok.is(tok::identifier)) {
2926 Name = Tok.getIdentifierInfo();
2927 NameLoc = ConsumeToken();
2928 }
Mike Stump1eb44332009-09-09 15:08:12 +00002929
Richard Smithbdad7a22012-01-10 01:33:14 +00002930 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002931 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2932 // declaration of a scoped enumeration.
2933 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002934 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002935 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002936 }
2937
2938 TypeResult BaseType;
2939
Douglas Gregora61b3e72010-12-01 17:42:47 +00002940 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002941 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002942 bool PossibleBitfield = false;
2943 if (getCurScope()->getFlags() & Scope::ClassScope) {
2944 // If we're in class scope, this can either be an enum declaration with
2945 // an underlying type, or a declaration of a bitfield member. We try to
2946 // use a simple disambiguation scheme first to catch the common cases
2947 // (integer literal, sizeof); if it's still ambiguous, we then consider
2948 // anything that's a simple-type-specifier followed by '(' as an
2949 // expression. This suffices because function types are not valid
2950 // underlying types anyway.
2951 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2952 // If the next token starts an expression, we know we're parsing a
2953 // bit-field. This is the common case.
2954 if (TPR == TPResult::True())
2955 PossibleBitfield = true;
2956 // If the next token starts a type-specifier-seq, it may be either a
2957 // a fixed underlying type or the start of a function-style cast in C++;
2958 // lookahead one more token to see if it's obvious that we have a
2959 // fixed underlying type.
2960 else if (TPR == TPResult::False() &&
2961 GetLookAheadToken(2).getKind() == tok::semi) {
2962 // Consume the ':'.
2963 ConsumeToken();
2964 } else {
2965 // We have the start of a type-specifier-seq, so we have to perform
2966 // tentative parsing to determine whether we have an expression or a
2967 // type.
2968 TentativeParsingAction TPA(*this);
2969
2970 // Consume the ':'.
2971 ConsumeToken();
2972
Douglas Gregor86f208c2011-02-22 20:32:04 +00002973 if ((getLang().CPlusPlus &&
2974 isCXXDeclarationSpecifier() != TPResult::True()) ||
2975 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002976 // We'll parse this as a bitfield later.
2977 PossibleBitfield = true;
2978 TPA.Revert();
2979 } else {
2980 // We have a type-specifier-seq.
2981 TPA.Commit();
2982 }
2983 }
2984 } else {
2985 // Consume the ':'.
2986 ConsumeToken();
2987 }
2988
2989 if (!PossibleBitfield) {
2990 SourceRange Range;
2991 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002992
Douglas Gregor5471bc82011-09-08 17:18:35 +00002993 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002994 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2995 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002996 if (getLang().CPlusPlus0x)
2997 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002998 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002999 }
3000
Richard Smithbdad7a22012-01-10 01:33:14 +00003001 // There are four options here. If we have 'friend enum foo;' then this is a
3002 // friend declaration, and cannot have an accompanying definition. If we have
3003 // 'enum foo;', then this is a forward declaration. If we have
3004 // 'enum foo {...' then this is a definition. Otherwise we have something
3005 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003006 //
3007 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3008 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3009 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3010 //
John McCallf312b1e2010-08-26 23:41:50 +00003011 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00003012 if (DS.isFriendSpecified())
3013 TUK = Sema::TUK_Friend;
3014 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00003015 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003016 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00003017 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003018 else
John McCallf312b1e2010-08-26 23:41:50 +00003019 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003020
3021 // enums cannot be templates, although they can be referenced from a
3022 // template.
3023 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003024 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003025 Diag(Tok, diag::err_enum_template);
3026
3027 // Skip the rest of this declarator, up until the comma or semicolon.
3028 SkipUntil(tok::comma, true);
3029 return;
3030 }
3031
Douglas Gregorb9075602011-02-22 02:55:24 +00003032 if (!Name && TUK != Sema::TUK_Definition) {
3033 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3034
3035 // Skip the rest of this declarator, up until the comma or semicolon.
3036 SkipUntil(tok::comma, true);
3037 return;
3038 }
3039
Douglas Gregor402abb52009-05-28 23:31:59 +00003040 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003041 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003042 const char *PrevSpec = 0;
3043 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003044 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003045 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003046 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003047 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00003048 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003049 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003050
Douglas Gregor48c89f42010-04-24 16:38:41 +00003051 if (IsDependent) {
3052 // This enum has a dependent nested-name-specifier. Handle it as a
3053 // dependent tag.
3054 if (!Name) {
3055 DS.SetTypeSpecError();
3056 Diag(Tok, diag::err_expected_type_name_after_typename);
3057 return;
3058 }
3059
Douglas Gregor23c94db2010-07-02 17:43:08 +00003060 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003061 TUK, SS, Name, StartLoc,
3062 NameLoc);
3063 if (Type.isInvalid()) {
3064 DS.SetTypeSpecError();
3065 return;
3066 }
3067
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003068 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3069 NameLoc.isValid() ? NameLoc : StartLoc,
3070 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003071 Diag(StartLoc, DiagID) << PrevSpec;
3072
3073 return;
3074 }
Mike Stump1eb44332009-09-09 15:08:12 +00003075
John McCalld226f652010-08-21 09:40:31 +00003076 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003077 // The action failed to produce an enumeration tag. If this is a
3078 // definition, consume the entire definition.
3079 if (Tok.is(tok::l_brace)) {
3080 ConsumeBrace();
3081 SkipUntil(tok::r_brace);
3082 }
3083
3084 DS.SetTypeSpecError();
3085 return;
3086 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003087
3088 if (Tok.is(tok::l_brace)) {
3089 if (TUK == Sema::TUK_Friend)
3090 Diag(Tok, diag::err_friend_decl_defines_type)
3091 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00003092 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003093 }
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003095 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3096 NameLoc.isValid() ? NameLoc : StartLoc,
3097 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003098 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003099}
3100
3101/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3102/// enumerator-list:
3103/// enumerator
3104/// enumerator-list ',' enumerator
3105/// enumerator:
3106/// enumeration-constant
3107/// enumeration-constant '=' constant-expression
3108/// enumeration-constant:
3109/// identifier
3110///
John McCalld226f652010-08-21 09:40:31 +00003111void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003112 // Enter the scope of the enum body and start the definition.
3113 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003114 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003115
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003116 BalancedDelimiterTracker T(*this, tok::l_brace);
3117 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Chris Lattner7946dd32007-08-27 17:24:30 +00003119 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003120 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003121 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Chris Lattner5f9e2722011-07-23 10:55:15 +00003123 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003124
John McCalld226f652010-08-21 09:40:31 +00003125 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003128 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3130 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003131
John McCall5b629aa2010-10-22 23:36:17 +00003132 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003133 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003134 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003135
Reid Spencer5f016e22007-07-11 17:01:13 +00003136 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003137 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003138 ParsingDeclRAIIObject PD(*this);
3139
Chris Lattner04d66662007-10-09 17:33:22 +00003140 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003142 AssignedVal = ParseConstantExpression();
3143 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 }
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Reid Spencer5f016e22007-07-11 17:01:13 +00003147 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003148 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3149 LastEnumConstDecl,
3150 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003151 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003152 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003153 PD.complete(EnumConstDecl);
3154
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 EnumConstantDecls.push_back(EnumConstDecl);
3156 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Douglas Gregor751f6922010-09-07 14:51:08 +00003158 if (Tok.is(tok::identifier)) {
3159 // We're missing a comma between enumerators.
3160 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3161 Diag(Loc, diag::err_enumerator_list_missing_comma)
3162 << FixItHint::CreateInsertion(Loc, ", ");
3163 continue;
3164 }
3165
Chris Lattner04d66662007-10-09 17:33:22 +00003166 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003167 break;
3168 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003169
Richard Smith7fe62082011-10-15 05:09:34 +00003170 if (Tok.isNot(tok::identifier)) {
3171 if (!getLang().C99 && !getLang().CPlusPlus0x)
3172 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3173 << getLang().CPlusPlus
3174 << FixItHint::CreateRemoval(CommaLoc);
3175 else if (getLang().CPlusPlus0x)
3176 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3177 << FixItHint::CreateRemoval(CommaLoc);
3178 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003179 }
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Reid Spencer5f016e22007-07-11 17:01:13 +00003181 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003182 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003183
Reid Spencer5f016e22007-07-11 17:01:13 +00003184 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003185 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003186 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003187
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003188 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3189 EnumDecl, EnumConstantDecls.data(),
3190 EnumConstantDecls.size(), getCurScope(),
3191 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Douglas Gregor72de6672009-01-08 20:45:30 +00003193 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003194 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3195 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003196}
3197
3198/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003199/// start of a type-qualifier-list.
3200bool Parser::isTypeQualifier() const {
3201 switch (Tok.getKind()) {
3202 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003203
3204 // type-qualifier only in OpenCL
3205 case tok::kw_private:
3206 return getLang().OpenCL;
3207
Steve Naroff5f8aa692008-02-11 23:15:56 +00003208 // type-qualifier
3209 case tok::kw_const:
3210 case tok::kw_volatile:
3211 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003212 case tok::kw___private:
3213 case tok::kw___local:
3214 case tok::kw___global:
3215 case tok::kw___constant:
3216 case tok::kw___read_only:
3217 case tok::kw___read_write:
3218 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003219 return true;
3220 }
3221}
3222
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003223/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3224/// is definitely a type-specifier. Return false if it isn't part of a type
3225/// specifier or if we're not sure.
3226bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3227 switch (Tok.getKind()) {
3228 default: return false;
3229 // type-specifiers
3230 case tok::kw_short:
3231 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003232 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003233 case tok::kw_signed:
3234 case tok::kw_unsigned:
3235 case tok::kw__Complex:
3236 case tok::kw__Imaginary:
3237 case tok::kw_void:
3238 case tok::kw_char:
3239 case tok::kw_wchar_t:
3240 case tok::kw_char16_t:
3241 case tok::kw_char32_t:
3242 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003243 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003244 case tok::kw_float:
3245 case tok::kw_double:
3246 case tok::kw_bool:
3247 case tok::kw__Bool:
3248 case tok::kw__Decimal32:
3249 case tok::kw__Decimal64:
3250 case tok::kw__Decimal128:
3251 case tok::kw___vector:
3252
3253 // struct-or-union-specifier (C99) or class-specifier (C++)
3254 case tok::kw_class:
3255 case tok::kw_struct:
3256 case tok::kw_union:
3257 // enum-specifier
3258 case tok::kw_enum:
3259
3260 // typedef-name
3261 case tok::annot_typename:
3262 return true;
3263 }
3264}
3265
Steve Naroff5f8aa692008-02-11 23:15:56 +00003266/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003267/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003268bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003269 switch (Tok.getKind()) {
3270 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Chris Lattner166a8fc2009-01-04 23:41:41 +00003272 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003273 if (TryAltiVecVectorToken())
3274 return true;
3275 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003276 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003277 // Annotate typenames and C++ scope specifiers. If we get one, just
3278 // recurse to handle whatever we get.
3279 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003280 return true;
3281 if (Tok.is(tok::identifier))
3282 return false;
3283 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003284
Chris Lattner166a8fc2009-01-04 23:41:41 +00003285 case tok::coloncolon: // ::foo::bar
3286 if (NextToken().is(tok::kw_new) || // ::new
3287 NextToken().is(tok::kw_delete)) // ::delete
3288 return false;
3289
Chris Lattner166a8fc2009-01-04 23:41:41 +00003290 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003291 return true;
3292 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003293
Reid Spencer5f016e22007-07-11 17:01:13 +00003294 // GNU attributes support.
3295 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003296 // GNU typeof support.
3297 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Reid Spencer5f016e22007-07-11 17:01:13 +00003299 // type-specifiers
3300 case tok::kw_short:
3301 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003302 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 case tok::kw_signed:
3304 case tok::kw_unsigned:
3305 case tok::kw__Complex:
3306 case tok::kw__Imaginary:
3307 case tok::kw_void:
3308 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003309 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003310 case tok::kw_char16_t:
3311 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003312 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003313 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 case tok::kw_float:
3315 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003316 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 case tok::kw__Bool:
3318 case tok::kw__Decimal32:
3319 case tok::kw__Decimal64:
3320 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003321 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003322
Chris Lattner99dc9142008-04-13 18:59:07 +00003323 // struct-or-union-specifier (C99) or class-specifier (C++)
3324 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 case tok::kw_struct:
3326 case tok::kw_union:
3327 // enum-specifier
3328 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 // type-qualifier
3331 case tok::kw_const:
3332 case tok::kw_volatile:
3333 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003334
3335 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003336 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003337 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Chris Lattner7c186be2008-10-20 00:25:30 +00003339 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3340 case tok::less:
3341 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003342
Steve Naroff239f0732008-12-25 14:16:32 +00003343 case tok::kw___cdecl:
3344 case tok::kw___stdcall:
3345 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003346 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003347 case tok::kw___w64:
3348 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003349 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003350 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003351 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003352
3353 case tok::kw___private:
3354 case tok::kw___local:
3355 case tok::kw___global:
3356 case tok::kw___constant:
3357 case tok::kw___read_only:
3358 case tok::kw___read_write:
3359 case tok::kw___write_only:
3360
Eli Friedman290eeb02009-06-08 23:27:34 +00003361 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003362
3363 case tok::kw_private:
3364 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003365
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003366 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003367 case tok::kw__Atomic:
3368 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003369 }
3370}
3371
3372/// isDeclarationSpecifier() - Return true if the current token is part of a
3373/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003374///
3375/// \param DisambiguatingWithExpression True to indicate that the purpose of
3376/// this check is to disambiguate between an expression and a declaration.
3377bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003378 switch (Tok.getKind()) {
3379 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003380
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003381 case tok::kw_private:
3382 return getLang().OpenCL;
3383
Chris Lattner166a8fc2009-01-04 23:41:41 +00003384 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003385 // Unfortunate hack to support "Class.factoryMethod" notation.
3386 if (getLang().ObjC1 && NextToken().is(tok::period))
3387 return false;
John Thompson82287d12010-02-05 00:12:22 +00003388 if (TryAltiVecVectorToken())
3389 return true;
3390 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003391 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003392 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003393 // Annotate typenames and C++ scope specifiers. If we get one, just
3394 // recurse to handle whatever we get.
3395 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003396 return true;
3397 if (Tok.is(tok::identifier))
3398 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003399
3400 // If we're in Objective-C and we have an Objective-C class type followed
3401 // by an identifier and then either ':' or ']', in a place where an
3402 // expression is permitted, then this is probably a class message send
3403 // missing the initial '['. In this case, we won't consider this to be
3404 // the start of a declaration.
3405 if (DisambiguatingWithExpression &&
3406 isStartOfObjCClassMessageMissingOpenBracket())
3407 return false;
3408
John McCall9ba61662010-02-26 08:45:28 +00003409 return isDeclarationSpecifier();
3410
Chris Lattner166a8fc2009-01-04 23:41:41 +00003411 case tok::coloncolon: // ::foo::bar
3412 if (NextToken().is(tok::kw_new) || // ::new
3413 NextToken().is(tok::kw_delete)) // ::delete
3414 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003415
Chris Lattner166a8fc2009-01-04 23:41:41 +00003416 // Annotate typenames and C++ scope specifiers. If we get one, just
3417 // recurse to handle whatever we get.
3418 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003419 return true;
3420 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Reid Spencer5f016e22007-07-11 17:01:13 +00003422 // storage-class-specifier
3423 case tok::kw_typedef:
3424 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003425 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003426 case tok::kw_static:
3427 case tok::kw_auto:
3428 case tok::kw_register:
3429 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003430
Douglas Gregor8d267c52011-09-09 02:06:17 +00003431 // Modules
3432 case tok::kw___module_private__:
3433
Reid Spencer5f016e22007-07-11 17:01:13 +00003434 // type-specifiers
3435 case tok::kw_short:
3436 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003437 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003438 case tok::kw_signed:
3439 case tok::kw_unsigned:
3440 case tok::kw__Complex:
3441 case tok::kw__Imaginary:
3442 case tok::kw_void:
3443 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003444 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003445 case tok::kw_char16_t:
3446 case tok::kw_char32_t:
3447
Reid Spencer5f016e22007-07-11 17:01:13 +00003448 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003449 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 case tok::kw_float:
3451 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003452 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003453 case tok::kw__Bool:
3454 case tok::kw__Decimal32:
3455 case tok::kw__Decimal64:
3456 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003457 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003458
Chris Lattner99dc9142008-04-13 18:59:07 +00003459 // struct-or-union-specifier (C99) or class-specifier (C++)
3460 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003461 case tok::kw_struct:
3462 case tok::kw_union:
3463 // enum-specifier
3464 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Reid Spencer5f016e22007-07-11 17:01:13 +00003466 // type-qualifier
3467 case tok::kw_const:
3468 case tok::kw_volatile:
3469 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003470
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 // function-specifier
3472 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003473 case tok::kw_virtual:
3474 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003475
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003476 // static_assert-declaration
3477 case tok::kw__Static_assert:
3478
Chris Lattner1ef08762007-08-09 17:01:07 +00003479 // GNU typeof support.
3480 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003481
Chris Lattner1ef08762007-08-09 17:01:07 +00003482 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003483 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003484 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003485
Francois Pichete3d49b42011-06-19 08:02:06 +00003486 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003487 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003488 return true;
3489
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003490 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003491 case tok::kw__Atomic:
3492 return true;
3493
Chris Lattnerf3948c42008-07-26 03:38:44 +00003494 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3495 case tok::less:
3496 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003497
Douglas Gregord9d75e52011-04-27 05:41:15 +00003498 // typedef-name
3499 case tok::annot_typename:
3500 return !DisambiguatingWithExpression ||
3501 !isStartOfObjCClassMessageMissingOpenBracket();
3502
Steve Naroff47f52092009-01-06 19:34:12 +00003503 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003504 case tok::kw___cdecl:
3505 case tok::kw___stdcall:
3506 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003507 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003508 case tok::kw___w64:
3509 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003510 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003511 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003512 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003513 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003514
3515 case tok::kw___private:
3516 case tok::kw___local:
3517 case tok::kw___global:
3518 case tok::kw___constant:
3519 case tok::kw___read_only:
3520 case tok::kw___read_write:
3521 case tok::kw___write_only:
3522
Eli Friedman290eeb02009-06-08 23:27:34 +00003523 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003524 }
3525}
3526
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003527bool Parser::isConstructorDeclarator() {
3528 TentativeParsingAction TPA(*this);
3529
3530 // Parse the C++ scope specifier.
3531 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003532 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3533 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003534 TPA.Revert();
3535 return false;
3536 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003537
3538 // Parse the constructor name.
3539 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3540 // We already know that we have a constructor name; just consume
3541 // the token.
3542 ConsumeToken();
3543 } else {
3544 TPA.Revert();
3545 return false;
3546 }
3547
3548 // Current class name must be followed by a left parentheses.
3549 if (Tok.isNot(tok::l_paren)) {
3550 TPA.Revert();
3551 return false;
3552 }
3553 ConsumeParen();
3554
3555 // A right parentheses or ellipsis signals that we have a constructor.
3556 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3557 TPA.Revert();
3558 return true;
3559 }
3560
3561 // If we need to, enter the specified scope.
3562 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003563 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003564 DeclScopeObj.EnterDeclaratorScope();
3565
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003566 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003567 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003568 MaybeParseMicrosoftAttributes(Attrs);
3569
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003570 // Check whether the next token(s) are part of a declaration
3571 // specifier, in which case we have the start of a parameter and,
3572 // therefore, we know that this is a constructor.
3573 bool IsConstructor = isDeclarationSpecifier();
3574 TPA.Revert();
3575 return IsConstructor;
3576}
Reid Spencer5f016e22007-07-11 17:01:13 +00003577
3578/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003579/// type-qualifier-list: [C99 6.7.5]
3580/// type-qualifier
3581/// [vendor] attributes
3582/// [ only if VendorAttributesAllowed=true ]
3583/// type-qualifier-list type-qualifier
3584/// [vendor] type-qualifier-list attributes
3585/// [ only if VendorAttributesAllowed=true ]
3586/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3587/// [ only if CXX0XAttributesAllowed=true ]
3588/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003589///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003590void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3591 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003592 bool CXX0XAttributesAllowed) {
3593 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3594 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003595 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003596 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003597 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003598 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003599 else
3600 Diag(Loc, diag::err_attributes_not_allowed);
3601 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003602
3603 SourceLocation EndLoc;
3604
Reid Spencer5f016e22007-07-11 17:01:13 +00003605 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003606 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003607 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003608 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003609 SourceLocation Loc = Tok.getLocation();
3610
3611 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003612 case tok::code_completion:
3613 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003614 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003615
Reid Spencer5f016e22007-07-11 17:01:13 +00003616 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003617 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3618 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003619 break;
3620 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003621 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3622 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003623 break;
3624 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003625 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3626 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003627 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003628
3629 // OpenCL qualifiers:
3630 case tok::kw_private:
3631 if (!getLang().OpenCL)
3632 goto DoneWithTypeQuals;
3633 case tok::kw___private:
3634 case tok::kw___global:
3635 case tok::kw___local:
3636 case tok::kw___constant:
3637 case tok::kw___read_only:
3638 case tok::kw___write_only:
3639 case tok::kw___read_write:
3640 ParseOpenCLQualifiers(DS);
3641 break;
3642
Eli Friedman290eeb02009-06-08 23:27:34 +00003643 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003644 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003645 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003646 case tok::kw___cdecl:
3647 case tok::kw___stdcall:
3648 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003649 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003650 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003651 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003652 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003653 continue;
3654 }
3655 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003656 case tok::kw___pascal:
3657 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003658 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003659 continue;
3660 }
3661 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003663 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003664 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003665 continue; // do *not* consume the next token!
3666 }
3667 // otherwise, FALL THROUGH!
3668 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003669 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003670 // If this is not a type-qualifier token, we're done reading type
3671 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003672 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003673 if (EndLoc.isValid())
3674 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003675 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003676 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003677
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 // If the specifier combination wasn't legal, issue a diagnostic.
3679 if (isInvalid) {
3680 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003681 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003682 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003683 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003684 }
3685}
3686
3687
3688/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3689///
3690void Parser::ParseDeclarator(Declarator &D) {
3691 /// This implements the 'declarator' production in the C grammar, then checks
3692 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003693 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003694}
3695
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003696/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3697/// is parsed by the function passed to it. Pass null, and the direct-declarator
3698/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003699/// ptr-operator production.
3700///
Richard Smith0706df42011-10-19 21:33:05 +00003701/// If the grammar of this construct is extended, matching changes must also be
3702/// made to TryParseDeclarator and MightBeDeclarator.
3703///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003704/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3705/// [C] pointer[opt] direct-declarator
3706/// [C++] direct-declarator
3707/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003708///
3709/// pointer: [C99 6.7.5]
3710/// '*' type-qualifier-list[opt]
3711/// '*' type-qualifier-list[opt] pointer
3712///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003713/// ptr-operator:
3714/// '*' cv-qualifier-seq[opt]
3715/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003716/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003717/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003718/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003719/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003720void Parser::ParseDeclaratorInternal(Declarator &D,
3721 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003722 if (Diags.hasAllExtensionsSilenced())
3723 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003724
Sebastian Redlf30208a2009-01-24 21:16:55 +00003725 // C++ member pointers start with a '::' or a nested-name.
3726 // Member pointers get special handling, since there's no place for the
3727 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003728 if (getLang().CPlusPlus &&
3729 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3730 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003731 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3732 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003733 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003734 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003735
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003736 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003737 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003738 // The scope spec really belongs to the direct-declarator.
3739 D.getCXXScopeSpec() = SS;
3740 if (DirectDeclParser)
3741 (this->*DirectDeclParser)(D);
3742 return;
3743 }
3744
3745 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003746 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003747 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003748 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003749 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003750
3751 // Recurse to parse whatever is left.
3752 ParseDeclaratorInternal(D, DirectDeclParser);
3753
3754 // Sema will have to catch (syntactically invalid) pointers into global
3755 // scope. It has to catch pointers into namespace scope anyway.
3756 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003757 Loc),
3758 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003759 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003760 return;
3761 }
3762 }
3763
3764 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003765 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003766 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003767 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003768 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003769 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003770 if (DirectDeclParser)
3771 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003772 return;
3773 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003774
Sebastian Redl05532f22009-03-15 22:02:01 +00003775 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3776 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003777 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003778 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003779
Chris Lattner9af55002009-03-27 04:18:06 +00003780 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003781 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003782 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003783
Reid Spencer5f016e22007-07-11 17:01:13 +00003784 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003785 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003786
Reid Spencer5f016e22007-07-11 17:01:13 +00003787 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003788 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003789 if (Kind == tok::star)
3790 // Remember that we parsed a pointer type, and remember the type-quals.
3791 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003792 DS.getConstSpecLoc(),
3793 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003794 DS.getRestrictSpecLoc()),
3795 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003796 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003797 else
3798 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003799 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003800 Loc),
3801 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003802 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003803 } else {
3804 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003805 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003806
Sebastian Redl743de1f2009-03-23 00:00:23 +00003807 // Complain about rvalue references in C++03, but then go on and build
3808 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003809 if (Kind == tok::ampamp)
3810 Diag(Loc, getLang().CPlusPlus0x ?
3811 diag::warn_cxx98_compat_rvalue_reference :
3812 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003813
Reid Spencer5f016e22007-07-11 17:01:13 +00003814 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3815 // cv-qualifiers are introduced through the use of a typedef or of a
3816 // template type argument, in which case the cv-qualifiers are ignored.
3817 //
3818 // [GNU] Retricted references are allowed.
3819 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003820 // [C++0x] Attributes on references are not allowed.
3821 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003822 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003823
3824 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3825 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3826 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003827 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003828 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3829 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003830 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003831 }
3832
3833 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003834 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003835
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003836 if (D.getNumTypeObjects() > 0) {
3837 // C++ [dcl.ref]p4: There shall be no references to references.
3838 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3839 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003840 if (const IdentifierInfo *II = D.getIdentifier())
3841 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3842 << II;
3843 else
3844 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3845 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003846
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003847 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003848 // can go ahead and build the (technically ill-formed)
3849 // declarator: reference collapsing will take care of it.
3850 }
3851 }
3852
Reid Spencer5f016e22007-07-11 17:01:13 +00003853 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003854 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003855 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003856 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003857 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003858 }
3859}
3860
3861/// ParseDirectDeclarator
3862/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003863/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003864/// '(' declarator ')'
3865/// [GNU] '(' attributes declarator ')'
3866/// [C90] direct-declarator '[' constant-expression[opt] ']'
3867/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3868/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3869/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3870/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3871/// direct-declarator '(' parameter-type-list ')'
3872/// direct-declarator '(' identifier-list[opt] ')'
3873/// [GNU] direct-declarator '(' parameter-forward-declarations
3874/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003875/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3876/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003877/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003878///
3879/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003880/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003881/// '::'[opt] nested-name-specifier[opt] type-name
3882///
3883/// id-expression: [C++ 5.1]
3884/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003885/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003886///
3887/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003888/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003889/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003890/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003891/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003892/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003893///
Reid Spencer5f016e22007-07-11 17:01:13 +00003894void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003895 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003896
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003897 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3898 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003899 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003900 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3901 D.getContext() == Declarator::MemberContext;
3902 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3903 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003904 }
3905
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003906 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003907 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003908 // Change the declaration context for name lookup, until this function
3909 // is exited (and the declarator has been parsed).
3910 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003911 }
3912
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003913 // C++0x [dcl.fct]p14:
3914 // There is a syntactic ambiguity when an ellipsis occurs at the end
3915 // of a parameter-declaration-clause without a preceding comma. In
3916 // this case, the ellipsis is parsed as part of the
3917 // abstract-declarator if the type of the parameter names a template
3918 // parameter pack that has not been expanded; otherwise, it is parsed
3919 // as part of the parameter-declaration-clause.
3920 if (Tok.is(tok::ellipsis) &&
3921 !((D.getContext() == Declarator::PrototypeContext ||
3922 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003923 NextToken().is(tok::r_paren) &&
3924 !Actions.containsUnexpandedParameterPacks(D)))
3925 D.setEllipsisLoc(ConsumeToken());
3926
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003927 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3928 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3929 // We found something that indicates the start of an unqualified-id.
3930 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003931 bool AllowConstructorName;
3932 if (D.getDeclSpec().hasTypeSpecifier())
3933 AllowConstructorName = false;
3934 else if (D.getCXXScopeSpec().isSet())
3935 AllowConstructorName =
3936 (D.getContext() == Declarator::FileContext ||
3937 (D.getContext() == Declarator::MemberContext &&
3938 D.getDeclSpec().isFriendSpecified()));
3939 else
3940 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3941
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003942 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003943 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3944 /*EnteringContext=*/true,
3945 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003946 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003947 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003948 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003949 D.getName()) ||
3950 // Once we're past the identifier, if the scope was bad, mark the
3951 // whole declarator bad.
3952 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003953 D.SetIdentifier(0, Tok.getLocation());
3954 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003955 } else {
3956 // Parsed the unqualified-id; update range information and move along.
3957 if (D.getSourceRange().getBegin().isInvalid())
3958 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3959 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003960 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003961 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003962 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003963 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003964 assert(!getLang().CPlusPlus &&
3965 "There's a C++-specific check for tok::identifier above");
3966 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3967 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3968 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003969 goto PastIdentifier;
3970 }
3971
3972 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003973 // direct-declarator: '(' declarator ')'
3974 // direct-declarator: '(' attributes declarator ')'
3975 // Example: 'char (*X)' or 'int (*XX)(void)'
3976 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003977
3978 // If the declarator was parenthesized, we entered the declarator
3979 // scope when parsing the parenthesized declarator, then exited
3980 // the scope already. Re-enter the scope, if we need to.
3981 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003982 // If there was an error parsing parenthesized declarator, declarator
3983 // scope may have been enterred before. Don't do it again.
3984 if (!D.isInvalidType() &&
3985 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003986 // Change the declaration context for name lookup, until this function
3987 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003988 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003989 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003990 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003991 // This could be something simple like "int" (in which case the declarator
3992 // portion is empty), if an abstract-declarator is allowed.
3993 D.SetIdentifier(0, Tok.getLocation());
3994 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003995 if (D.getContext() == Declarator::MemberContext)
3996 Diag(Tok, diag::err_expected_member_name_or_semi)
3997 << D.getDeclSpec().getSourceRange();
3998 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003999 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004000 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004001 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004002 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004003 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004004 }
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004006 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004007 assert(D.isPastIdentifier() &&
4008 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004009
Sean Huntbbd37c62009-11-21 08:43:09 +00004010 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00004011 if (D.getIdentifier())
4012 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004013
Reid Spencer5f016e22007-07-11 17:01:13 +00004014 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004015 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004016 // Enter function-declaration scope, limiting any declarators to the
4017 // function prototype scope, including parameter declarators.
4018 ParseScope PrototypeScope(this,
4019 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004020 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4021 // In such a case, check if we actually have a function declarator; if it
4022 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00004023 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4024 // When not in file scope, warn for ambiguous function declarators, just
4025 // in case the author intended it as a variable definition.
4026 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4027 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4028 break;
4029 }
John McCall0b7e6782011-03-24 11:26:52 +00004030 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004031 BalancedDelimiterTracker T(*this, tok::l_paren);
4032 T.consumeOpen();
4033 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004034 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004035 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004036 ParseBracketDeclarator(D);
4037 } else {
4038 break;
4039 }
4040 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004041}
Reid Spencer5f016e22007-07-11 17:01:13 +00004042
Chris Lattneref4715c2008-04-06 05:45:57 +00004043/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4044/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004045/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004046/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4047///
4048/// direct-declarator:
4049/// '(' declarator ')'
4050/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004051/// direct-declarator '(' parameter-type-list ')'
4052/// direct-declarator '(' identifier-list[opt] ')'
4053/// [GNU] direct-declarator '(' parameter-forward-declarations
4054/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004055///
4056void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004057 BalancedDelimiterTracker T(*this, tok::l_paren);
4058 T.consumeOpen();
4059
Chris Lattneref4715c2008-04-06 05:45:57 +00004060 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004061
Chris Lattner7399ee02008-10-20 02:05:46 +00004062 // Eat any attributes before we look at whether this is a grouping or function
4063 // declarator paren. If this is a grouping paren, the attribute applies to
4064 // the type being built up, for example:
4065 // int (__attribute__(()) *x)(long y)
4066 // If this ends up not being a grouping paren, the attribute applies to the
4067 // first argument, for example:
4068 // int (__attribute__(()) int x)
4069 // In either case, we need to eat any attributes to be able to determine what
4070 // sort of paren this is.
4071 //
John McCall0b7e6782011-03-24 11:26:52 +00004072 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004073 bool RequiresArg = false;
4074 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004075 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004076
Chris Lattner7399ee02008-10-20 02:05:46 +00004077 // We require that the argument list (if this is a non-grouping paren) be
4078 // present even if the attribute list was empty.
4079 RequiresArg = true;
4080 }
Steve Naroff239f0732008-12-25 14:16:32 +00004081 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004082 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004083 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004084 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004085 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004086 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004087 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004088 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004089 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004090 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004091
Chris Lattneref4715c2008-04-06 05:45:57 +00004092 // If we haven't past the identifier yet (or where the identifier would be
4093 // stored, if this is an abstract declarator), then this is probably just
4094 // grouping parens. However, if this could be an abstract-declarator, then
4095 // this could also be the start of function arguments (consider 'void()').
4096 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004097
Chris Lattneref4715c2008-04-06 05:45:57 +00004098 if (!D.mayOmitIdentifier()) {
4099 // If this can't be an abstract-declarator, this *must* be a grouping
4100 // paren, because we haven't seen the identifier yet.
4101 isGrouping = true;
4102 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004103 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004104 isDeclarationSpecifier()) { // 'int(int)' is a function.
4105 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4106 // considered to be a type, not a K&R identifier-list.
4107 isGrouping = false;
4108 } else {
4109 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4110 isGrouping = true;
4111 }
Mike Stump1eb44332009-09-09 15:08:12 +00004112
Chris Lattneref4715c2008-04-06 05:45:57 +00004113 // If this is a grouping paren, handle:
4114 // direct-declarator: '(' declarator ')'
4115 // direct-declarator: '(' attributes declarator ')'
4116 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004117 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004118 D.setGroupingParens(true);
4119
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004120 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004121 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004122 T.consumeClose();
4123 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4124 T.getCloseLocation()),
4125 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004126
4127 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004128 return;
4129 }
Mike Stump1eb44332009-09-09 15:08:12 +00004130
Chris Lattneref4715c2008-04-06 05:45:57 +00004131 // Okay, if this wasn't a grouping paren, it must be the start of a function
4132 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004133 // identifier (and remember where it would have been), then call into
4134 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004135 D.SetIdentifier(0, Tok.getLocation());
4136
David Blaikie42d6d0c2011-12-04 05:04:18 +00004137 // Enter function-declaration scope, limiting any declarators to the
4138 // function prototype scope, including parameter declarators.
4139 ParseScope PrototypeScope(this,
4140 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004141 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004142 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004143}
4144
4145/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4146/// declarator D up to a paren, which indicates that we are parsing function
4147/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004148///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004149/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004150/// after the open paren - they should be considered to be the first argument of
4151/// a parameter. If RequiresArg is true, then the first argument of the
4152/// function is required to be present and required to not be an identifier
4153/// list.
4154///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004155/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4156/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4157/// (C++0x) trailing-return-type[opt].
4158///
4159/// [C++0x] exception-specification:
4160/// dynamic-exception-specification
4161/// noexcept-specification
4162///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004163void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004164 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004165 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004166 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004167 assert(getCurScope()->isFunctionPrototypeScope() &&
4168 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004169 // lparen is already consumed!
4170 assert(D.isPastIdentifier() && "Should not call before identifier!");
4171
4172 // This should be true when the function has typed arguments.
4173 // Otherwise, it is treated as a K&R-style function.
4174 bool HasProto = false;
4175 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004176 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004177 // Remember where we see an ellipsis, if any.
4178 SourceLocation EllipsisLoc;
4179
4180 DeclSpec DS(AttrFactory);
4181 bool RefQualifierIsLValueRef = true;
4182 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004183 SourceLocation ConstQualifierLoc;
4184 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004185 ExceptionSpecificationType ESpecType = EST_None;
4186 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004187 SmallVector<ParsedType, 2> DynamicExceptions;
4188 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004189 ExprResult NoexceptExpr;
4190 ParsedType TrailingReturnType;
4191
4192 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004193 if (isFunctionDeclaratorIdentifierList()) {
4194 if (RequiresArg)
4195 Diag(Tok, diag::err_argument_required_after_attribute);
4196
4197 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4198
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004199 Tracker.consumeClose();
4200 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004201 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004202 if (Tok.isNot(tok::r_paren))
4203 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4204 else if (RequiresArg)
4205 Diag(Tok, diag::err_argument_required_after_attribute);
4206
4207 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4208
4209 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004210 Tracker.consumeClose();
4211 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004212
4213 if (getLang().CPlusPlus) {
4214 MaybeParseCXX0XAttributes(attrs);
4215
4216 // Parse cv-qualifier-seq[opt].
4217 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004218 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004219 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004220 ConstQualifierLoc = DS.getConstSpecLoc();
4221 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4222 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004223
4224 // Parse ref-qualifier[opt].
4225 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004226 Diag(Tok, getLang().CPlusPlus0x ?
4227 diag::warn_cxx98_compat_ref_qualifier :
4228 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004229
4230 RefQualifierIsLValueRef = Tok.is(tok::amp);
4231 RefQualifierLoc = ConsumeToken();
4232 EndLoc = RefQualifierLoc;
4233 }
4234
4235 // Parse exception-specification[opt].
4236 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4237 DynamicExceptions,
4238 DynamicExceptionRanges,
4239 NoexceptExpr);
4240 if (ESpecType != EST_None)
4241 EndLoc = ESpecRange.getEnd();
4242
4243 // Parse trailing-return-type[opt].
4244 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004245 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004246 SourceRange Range;
4247 TrailingReturnType = ParseTrailingReturnType(Range).get();
4248 if (Range.getEnd().isValid())
4249 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004250 }
4251 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004252 }
4253
4254 // Remember that we parsed a function type, and remember the attributes.
4255 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4256 /*isVariadic=*/EllipsisLoc.isValid(),
4257 EllipsisLoc,
4258 ParamInfo.data(), ParamInfo.size(),
4259 DS.getTypeQualifiers(),
4260 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004261 RefQualifierLoc, ConstQualifierLoc,
4262 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004263 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004264 ESpecType, ESpecRange.getBegin(),
4265 DynamicExceptions.data(),
4266 DynamicExceptionRanges.data(),
4267 DynamicExceptions.size(),
4268 NoexceptExpr.isUsable() ?
4269 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004270 Tracker.getOpenLocation(),
4271 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004272 TrailingReturnType),
4273 attrs, EndLoc);
4274}
4275
4276/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4277/// identifier list form for a K&R-style function: void foo(a,b,c)
4278///
4279/// Note that identifier-lists are only allowed for normal declarators, not for
4280/// abstract-declarators.
4281bool Parser::isFunctionDeclaratorIdentifierList() {
4282 return !getLang().CPlusPlus
4283 && Tok.is(tok::identifier)
4284 && !TryAltiVecVectorToken()
4285 // K&R identifier lists can't have typedefs as identifiers, per C99
4286 // 6.7.5.3p11.
4287 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4288 // Identifier lists follow a really simple grammar: the identifiers can
4289 // be followed *only* by a ", identifier" or ")". However, K&R
4290 // identifier lists are really rare in the brave new modern world, and
4291 // it is very common for someone to typo a type in a non-K&R style
4292 // list. If we are presented with something like: "void foo(intptr x,
4293 // float y)", we don't want to start parsing the function declarator as
4294 // though it is a K&R style declarator just because intptr is an
4295 // invalid type.
4296 //
4297 // To handle this, we check to see if the token after the first
4298 // identifier is a "," or ")". Only then do we parse it as an
4299 // identifier list.
4300 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4301}
4302
4303/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4304/// we found a K&R-style identifier list instead of a typed parameter list.
4305///
4306/// After returning, ParamInfo will hold the parsed parameters.
4307///
4308/// identifier-list: [C99 6.7.5]
4309/// identifier
4310/// identifier-list ',' identifier
4311///
4312void Parser::ParseFunctionDeclaratorIdentifierList(
4313 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004314 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004315 // If there was no identifier specified for the declarator, either we are in
4316 // an abstract-declarator, or we are in a parameter declarator which was found
4317 // to be abstract. In abstract-declarators, identifier lists are not valid:
4318 // diagnose this.
4319 if (!D.getIdentifier())
4320 Diag(Tok, diag::ext_ident_list_in_param);
4321
4322 // Maintain an efficient lookup of params we have seen so far.
4323 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4324
4325 while (1) {
4326 // If this isn't an identifier, report the error and skip until ')'.
4327 if (Tok.isNot(tok::identifier)) {
4328 Diag(Tok, diag::err_expected_ident);
4329 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4330 // Forget we parsed anything.
4331 ParamInfo.clear();
4332 return;
4333 }
4334
4335 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4336
4337 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4338 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4339 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4340
4341 // Verify that the argument identifier has not already been mentioned.
4342 if (!ParamsSoFar.insert(ParmII)) {
4343 Diag(Tok, diag::err_param_redefinition) << ParmII;
4344 } else {
4345 // Remember this identifier in ParamInfo.
4346 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4347 Tok.getLocation(),
4348 0));
4349 }
4350
4351 // Eat the identifier.
4352 ConsumeToken();
4353
4354 // The list continues if we see a comma.
4355 if (Tok.isNot(tok::comma))
4356 break;
4357 ConsumeToken();
4358 }
4359}
4360
4361/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4362/// after the opening parenthesis. This function will not parse a K&R-style
4363/// identifier list.
4364///
4365/// D is the declarator being parsed. If attrs is non-null, then the caller
4366/// parsed those arguments immediately after the open paren - they should be
4367/// considered to be the first argument of a parameter.
4368///
4369/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4370/// be the location of the ellipsis, if any was parsed.
4371///
Reid Spencer5f016e22007-07-11 17:01:13 +00004372/// parameter-type-list: [C99 6.7.5]
4373/// parameter-list
4374/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004375/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004376///
4377/// parameter-list: [C99 6.7.5]
4378/// parameter-declaration
4379/// parameter-list ',' parameter-declaration
4380///
4381/// parameter-declaration: [C99 6.7.5]
4382/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004383/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004384/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004385/// declaration-specifiers abstract-declarator[opt]
4386/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004387/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004388/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4389///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004390void Parser::ParseParameterDeclarationClause(
4391 Declarator &D,
4392 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004393 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004394 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004395
Chris Lattnerf97409f2008-04-06 06:57:35 +00004396 while (1) {
4397 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004398 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004399 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004400 }
Mike Stump1eb44332009-09-09 15:08:12 +00004401
Chris Lattnerf97409f2008-04-06 06:57:35 +00004402 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004403 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004404 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004405
John McCall7f040a92010-12-24 02:08:15 +00004406 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004407 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004408 ParseMicrosoftAttributes(DS.getAttributes());
4409
4410 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004411
4412 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004413 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004414 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4415 // attributes lost? Should they even be allowed?
4416 // FIXME: If we can leave the attributes in the token stream somehow, we can
4417 // get rid of a parameter (attrs) and this statement. It might be too much
4418 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004419 DS.takeAttributesFrom(attrs);
4420
Chris Lattnere64c5492009-02-27 18:38:20 +00004421 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004422
Chris Lattnerf97409f2008-04-06 06:57:35 +00004423 // Parse the declarator. This is "PrototypeContext", because we must
4424 // accept either 'declarator' or 'abstract-declarator' here.
4425 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4426 ParseDeclarator(ParmDecl);
4427
4428 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004429 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004430
Chris Lattnerf97409f2008-04-06 06:57:35 +00004431 // Remember this parsed parameter in ParamInfo.
4432 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004433
Douglas Gregor72b505b2008-12-16 21:30:33 +00004434 // DefArgToks is used when the parsing of default arguments needs
4435 // to be delayed.
4436 CachedTokens *DefArgToks = 0;
4437
Chris Lattnerf97409f2008-04-06 06:57:35 +00004438 // If no parameter was specified, verify that *something* was specified,
4439 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004440 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4441 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004442 // Completely missing, emit error.
4443 Diag(DSStart, diag::err_missing_param);
4444 } else {
4445 // Otherwise, we have something. Add it and let semantic analysis try
4446 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004447
Chris Lattnerf97409f2008-04-06 06:57:35 +00004448 // Inform the actions module about the parameter declarator, so it gets
4449 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004450 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004451
4452 // Parse the default argument, if any. We parse the default
4453 // arguments in all dialects; the semantic analysis in
4454 // ActOnParamDefaultArgument will reject the default argument in
4455 // C.
4456 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004457 SourceLocation EqualLoc = Tok.getLocation();
4458
Chris Lattner04421082008-04-08 04:40:51 +00004459 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004460 if (D.getContext() == Declarator::MemberContext) {
4461 // If we're inside a class definition, cache the tokens
4462 // corresponding to the default argument. We'll actually parse
4463 // them when we see the end of the class definition.
4464 // FIXME: Templates will require something similar.
4465 // FIXME: Can we use a smart pointer for Toks?
4466 DefArgToks = new CachedTokens;
4467
Mike Stump1eb44332009-09-09 15:08:12 +00004468 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004469 /*StopAtSemi=*/true,
4470 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004471 delete DefArgToks;
4472 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004473 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004474 } else {
4475 // Mark the end of the default argument so that we know when to
4476 // stop when we parse it later on.
4477 Token DefArgEnd;
4478 DefArgEnd.startToken();
4479 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4480 DefArgEnd.setLocation(Tok.getLocation());
4481 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004482 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004483 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004484 }
Chris Lattner04421082008-04-08 04:40:51 +00004485 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004486 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004487 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004489 // The argument isn't actually potentially evaluated unless it is
4490 // used.
4491 EnterExpressionEvaluationContext Eval(Actions,
4492 Sema::PotentiallyEvaluatedIfUsed);
4493
John McCall60d7b3a2010-08-24 06:29:42 +00004494 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004495 if (DefArgResult.isInvalid()) {
4496 Actions.ActOnParamDefaultArgumentError(Param);
4497 SkipUntil(tok::comma, tok::r_paren, true, true);
4498 } else {
4499 // Inform the actions module about the default argument
4500 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004501 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004502 }
Chris Lattner04421082008-04-08 04:40:51 +00004503 }
4504 }
Mike Stump1eb44332009-09-09 15:08:12 +00004505
4506 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4507 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004508 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004509 }
4510
4511 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004512 if (Tok.isNot(tok::comma)) {
4513 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004514 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4515
4516 if (!getLang().CPlusPlus) {
4517 // We have ellipsis without a preceding ',', which is ill-formed
4518 // in C. Complain and provide the fix.
4519 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004520 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004521 }
4522 }
4523
4524 break;
4525 }
Mike Stump1eb44332009-09-09 15:08:12 +00004526
Chris Lattnerf97409f2008-04-06 06:57:35 +00004527 // Consume the comma.
4528 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004529 }
Mike Stump1eb44332009-09-09 15:08:12 +00004530
Chris Lattner66d28652008-04-06 06:34:08 +00004531}
Chris Lattneref4715c2008-04-06 05:45:57 +00004532
Reid Spencer5f016e22007-07-11 17:01:13 +00004533/// [C90] direct-declarator '[' constant-expression[opt] ']'
4534/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4535/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4536/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4537/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4538void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004539 BalancedDelimiterTracker T(*this, tok::l_square);
4540 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004541
Chris Lattner378c7e42008-12-18 07:27:21 +00004542 // C array syntax has many features, but by-far the most common is [] and [4].
4543 // This code does a fast path to handle some of the most obvious cases.
4544 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004545 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004546 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004547 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004548
Chris Lattner378c7e42008-12-18 07:27:21 +00004549 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004550 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004551 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004552 T.getOpenLocation(),
4553 T.getCloseLocation()),
4554 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004555 return;
4556 } else if (Tok.getKind() == tok::numeric_constant &&
4557 GetLookAheadToken(1).is(tok::r_square)) {
4558 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004559 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004560 ConsumeToken();
4561
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004562 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004563 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004564 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004565
Chris Lattner378c7e42008-12-18 07:27:21 +00004566 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004567 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004568 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004569 T.getOpenLocation(),
4570 T.getCloseLocation()),
4571 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004572 return;
4573 }
Mike Stump1eb44332009-09-09 15:08:12 +00004574
Reid Spencer5f016e22007-07-11 17:01:13 +00004575 // If valid, this location is the position where we read the 'static' keyword.
4576 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004577 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004578 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004579
Reid Spencer5f016e22007-07-11 17:01:13 +00004580 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004581 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004582 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004583 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004584
Reid Spencer5f016e22007-07-11 17:01:13 +00004585 // If we haven't already read 'static', check to see if there is one after the
4586 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004587 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004588 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004589
Reid Spencer5f016e22007-07-11 17:01:13 +00004590 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4591 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004592 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004593
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004594 // Handle the case where we have '[*]' as the array size. However, a leading
4595 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4596 // the the token after the star is a ']'. Since stars in arrays are
4597 // infrequent, use of lookahead is not costly here.
4598 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004599 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004600
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004601 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004602 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004603 StaticLoc = SourceLocation(); // Drop the static.
4604 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004605 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004606 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004607 // Note, in C89, this production uses the constant-expr production instead
4608 // of assignment-expr. The only difference is that assignment-expr allows
4609 // things like '=' and '*='. Sema rejects these in C89 mode because they
4610 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004611
Douglas Gregore0762c92009-06-19 23:52:42 +00004612 // Parse the constant-expression or assignment-expression now (depending
4613 // on dialect).
Eli Friedman71b8fb52012-01-21 01:01:51 +00004614 if (getLang().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004615 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004616 } else {
4617 EnterExpressionEvaluationContext Unevaluated(Actions,
4618 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004619 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004620 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004621 }
Mike Stump1eb44332009-09-09 15:08:12 +00004622
Reid Spencer5f016e22007-07-11 17:01:13 +00004623 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004624 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004625 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004626 // If the expression was invalid, skip it.
4627 SkipUntil(tok::r_square);
4628 return;
4629 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004630
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004631 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004632
John McCall0b7e6782011-03-24 11:26:52 +00004633 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004634 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004635
Chris Lattner378c7e42008-12-18 07:27:21 +00004636 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004637 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004638 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004639 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004640 T.getOpenLocation(),
4641 T.getCloseLocation()),
4642 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004643}
4644
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004645/// [GNU] typeof-specifier:
4646/// typeof ( expressions )
4647/// typeof ( type-name )
4648/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004649///
4650void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004651 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004652 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004653 SourceLocation StartLoc = ConsumeToken();
4654
John McCallcfb708c2010-01-13 20:03:27 +00004655 const bool hasParens = Tok.is(tok::l_paren);
4656
Eli Friedman71b8fb52012-01-21 01:01:51 +00004657 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4658
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004659 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004660 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004661 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004662 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4663 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004664 if (hasParens)
4665 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004666
4667 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004668 // FIXME: Not accurate, the range gets one token more than it should.
4669 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004670 else
4671 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004672
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004673 if (isCastExpr) {
4674 if (!CastTy) {
4675 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004676 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004677 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004678
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004679 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004680 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004681 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4682 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004683 DiagID, CastTy))
4684 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004685 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004686 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004687
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004688 // If we get here, the operand to the typeof was an expresion.
4689 if (Operand.isInvalid()) {
4690 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004691 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004692 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004693
Eli Friedman71b8fb52012-01-21 01:01:51 +00004694 // We might need to transform the operand if it is potentially evaluated.
4695 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4696 if (Operand.isInvalid()) {
4697 DS.SetTypeSpecError();
4698 return;
4699 }
4700
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004701 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004702 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004703 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4704 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004705 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004706 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004707}
Chris Lattner1b492422010-02-28 18:33:55 +00004708
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004709/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004710/// _Atomic ( type-name )
4711///
4712void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4713 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4714
4715 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004716 BalancedDelimiterTracker T(*this, tok::l_paren);
4717 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004718 SkipUntil(tok::r_paren);
4719 return;
4720 }
4721
4722 TypeResult Result = ParseTypeName();
4723 if (Result.isInvalid()) {
4724 SkipUntil(tok::r_paren);
4725 return;
4726 }
4727
4728 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004729 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004730
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004731 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004732 return;
4733
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004734 DS.setTypeofParensRange(T.getRange());
4735 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004736
4737 const char *PrevSpec = 0;
4738 unsigned DiagID;
4739 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4740 DiagID, Result.release()))
4741 Diag(StartLoc, DiagID) << PrevSpec;
4742}
4743
Chris Lattner1b492422010-02-28 18:33:55 +00004744
4745/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4746/// from TryAltiVecVectorToken.
4747bool Parser::TryAltiVecVectorTokenOutOfLine() {
4748 Token Next = NextToken();
4749 switch (Next.getKind()) {
4750 default: return false;
4751 case tok::kw_short:
4752 case tok::kw_long:
4753 case tok::kw_signed:
4754 case tok::kw_unsigned:
4755 case tok::kw_void:
4756 case tok::kw_char:
4757 case tok::kw_int:
4758 case tok::kw_float:
4759 case tok::kw_double:
4760 case tok::kw_bool:
4761 case tok::kw___pixel:
4762 Tok.setKind(tok::kw___vector);
4763 return true;
4764 case tok::identifier:
4765 if (Next.getIdentifierInfo() == Ident_pixel) {
4766 Tok.setKind(tok::kw___vector);
4767 return true;
4768 }
4769 return false;
4770 }
4771}
4772
4773bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4774 const char *&PrevSpec, unsigned &DiagID,
4775 bool &isInvalid) {
4776 if (Tok.getIdentifierInfo() == Ident_vector) {
4777 Token Next = NextToken();
4778 switch (Next.getKind()) {
4779 case tok::kw_short:
4780 case tok::kw_long:
4781 case tok::kw_signed:
4782 case tok::kw_unsigned:
4783 case tok::kw_void:
4784 case tok::kw_char:
4785 case tok::kw_int:
4786 case tok::kw_float:
4787 case tok::kw_double:
4788 case tok::kw_bool:
4789 case tok::kw___pixel:
4790 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4791 return true;
4792 case tok::identifier:
4793 if (Next.getIdentifierInfo() == Ident_pixel) {
4794 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4795 return true;
4796 }
4797 break;
4798 default:
4799 break;
4800 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004801 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004802 DS.isTypeAltiVecVector()) {
4803 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4804 return true;
4805 }
4806 return false;
4807}