blob: 032326cea71669f8bba72de802cba0de92b96c85 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000038 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithcd1c0552011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000043
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor0be31a22010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000054}
55
Caitlin Sadowski9385dd72011-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
Alexis Hunt96d5c762009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +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
Steve Naroff0f2fe172007-06-01 17:11:19 +000081/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000085///
Steve Naroff0f2fe172007-06-01 17:11:19 +000086/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-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
Steve Naroff0f2fe172007-06-01 17:11:19 +000095/// start with that identifier; otherwise they are an expression list."
96///
Richard Smithb12bf692011-10-17 21:20:17 +000097/// GCC does not require the ',' between attribs in an attribute-list.
98///
Steve Naroff0f2fe172007-06-01 17:11:19 +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.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000103
John McCall53fa7142010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner76c72282007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 McCall53fa7142010-12-24 02:08:15 +0000114 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000118 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Stump11289f42009-09-09 15:08:12 +0000131
Caitlin Sadowski9385dd72011-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 Stump11289f42009-09-09 15:08:12 +0000142
Caitlin Sadowski9385dd72011-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 Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-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);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 } else {
John McCall084e83d2011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000156 }
157 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000159 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall53fa7142010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000167}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000168
Caitlin Sadowski9385dd72011-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 Smithb12bf692011-10-17 21:20:17 +0000192 IdentifierInfo *ParmName = 0;
193 SourceLocation ParmLoc;
194 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000195
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000233 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000234
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000241 }
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000246 }
Richard Smithb12bf692011-10-17 21:20:17 +0000247 }
Fariborz Jahanian6b708652011-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 Jahanian7f733022011-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 Jahanian6b708652011-10-18 17:11:10 +0000262 SkipUntil(tok::r_paren, false, true); // skip until ')'
263 }
264 }
Richard Smithb12bf692011-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 Kyrtzidis635a9b42011-09-13 16:05:53 +0000269 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithb12bf692011-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 Sadowski9385dd72011-09-08 17:42:22 +0000273 }
274}
275
276
Eli Friedman06de2b52009-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 McCall53fa7142010-12-24 02:08:15 +0000286void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000287 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000288
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000289 ConsumeToken();
Eli Friedman06de2b52009-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 McCall53fa7142010-12-24 02:08:15 +0000293 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000294 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000295
Eli Friedman53339e02009-06-08 23:27:34 +0000296 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-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 Friedman06de2b52009-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 McCalldadc5752010-08-24 06:29:42 +0000310 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000311 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000312 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000313 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
314 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-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 McCall084e83d2011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
320 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000321 }
322 }
323 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
324 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000325 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000326}
327
John McCall53fa7142010-12-24 02:08:15 +0000328void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-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 Gregora941dca2010-05-18 16:57:00 +0000332 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000333 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000334 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000335 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000336 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
337 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetf2fb4112011-08-25 00:36:46 +0000338 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
339 Tok.is(tok::kw___ptr32))
Eli Friedman53339e02009-06-08 23:27:34 +0000340 // FIXME: Support these properly!
341 continue;
John McCall084e83d2011-03-24 11:26:52 +0000342 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
343 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000344 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000345}
346
John McCall53fa7142010-12-24 02:08:15 +0000347void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-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 McCall084e83d2011-03-24 11:26:52 +0000352 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
353 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000354 }
John McCall53fa7142010-12-24 02:08:15 +0000355}
356
Peter Collingbourne7ce13fc2011-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 McCall084e83d2011-03-24 11:26:52 +0000361 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
362 AttrNameLoc, 0, AttrNameLoc, 0,
363 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000364 }
365}
366
Peter Collingbourne599cb8e2011-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 McCall084e83d2011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000412 break;
413 default: break;
414 }
415}
416
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000525/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-03-26 03:35:55 +0000538/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000539/// opt-message:
540/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000550 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000551
552 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000553 BalancedDelimiterTracker T(*this, tok::l_paren);
554 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000555 Diag(Tok, diag::err_expected_lparen);
556 return;
557 }
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-03-26 03:35:55 +0000578 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000579 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000580 }
581
582 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000583 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-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 Gregor7ab142b2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000606
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-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 Gregor20b2ebd2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000665 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000666 return;
667
668 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000669 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000670
Douglas Gregor7ab142b2011-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 Gregor20b2ebd2011-03-23 00:50:03 +0000690 // Record this attribute
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000691 attrs.addNew(&Availability,
692 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall084e83d2011-03-24 11:26:52 +0000693 0, SourceLocation(),
694 Platform, PlatformLoc,
695 Changes[Introduced],
696 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000697 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000698 UnavailableLoc, MessageExpr.take(),
699 false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000700}
701
Caitlin Sadowski9385dd72011-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 Sadowski990d5712011-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 Sadowski9385dd72011-09-08 17:42:22 +0000769 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
770
Caitlin Sadowski990d5712011-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 Sadowski9385dd72011-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 Sadowski4b1e8392011-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 Sadowski9385dd72011-09-08 17:42:22 +0000837 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000838
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000839 BalancedDelimiterTracker T(*this, tok::l_paren);
840 T.consumeOpen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000841
842 ExprVector ArgExprs(Actions);
843 bool ArgExprsOk = true;
844
845 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +0000846 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000847 ExprResult ArgExpr(ParseAssignmentExpression());
848 if (ArgExpr.isInvalid()) {
849 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000850 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000851 break;
852 } else {
853 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000854 }
Caitlin Sadowski9385dd72011-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 Hutchins36f5d852011-12-14 19:36:06 +0000860 if (ArgExprsOk && !T.consumeClose() && ArgExprs.size() > 0) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000861 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
862 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000863 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000864 if (EndLoc)
865 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000866}
867
John McCall53fa7142010-12-24 02:08:15 +0000868void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
869 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
870 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000871}
872
Chris Lattner53361ac2006-08-10 05:19:57 +0000873/// ParseDeclaration - Parse a full 'declaration', which consists of
874/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000875/// 'Context' should be a Declarator::TheContext value. This returns the
876/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000877///
878/// declaration: [C99 6.7]
879/// block-declaration ->
880/// simple-declaration
881/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000882/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000883/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000884/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000885/// [C++] using-declaration
Benjamin Kramere56f3932011-12-23 17:00:35 +0000886/// [C++0x/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000887/// others... [FIXME]
888///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000889Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
890 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000891 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000892 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000893 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-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 Kyrtzidis355094e2010-06-17 10:52:18 +0000897
John McCall48871652010-08-21 09:40:31 +0000898 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000899 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000900 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000901 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000902 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000903 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000904 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000905 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000906 case tok::kw_inline:
Sebastian Redl5a5f2c72010-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 McCall53fa7142010-12-24 02:08:15 +0000909 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000910 SourceLocation InlineLoc = ConsumeToken();
911 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
912 break;
913 }
John McCall53fa7142010-12-24 02:08:15 +0000914 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000915 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000916 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000917 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000918 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000919 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000920 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000921 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000922 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000923 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000924 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000925 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000926 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000927 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000928 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000929 default:
John McCall53fa7142010-12-24 02:08:15 +0000930 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000931 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000932
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000933 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-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 Lattnera5235172007-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 Lattner32dc41c2009-03-29 17:27:48 +0000943///
Richard Smith02e85f32011-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 Lattner32dc41c2009-03-29 17:27:48 +0000947/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000948/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-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 Jahanian1db5c942010-09-28 20:42:35 +0000953Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
954 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000955 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000956 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000957 bool RequireSemi,
958 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000959 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000960 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000961 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000962
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000963 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000964 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +0000965
Chris Lattner0e894622006-08-13 19:58:17 +0000966 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
967 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000968 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000969 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000970 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000971 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000972 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000973 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000974 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000975
976 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000977}
Mike Stump11289f42009-09-09 15:08:12 +0000978
Richard Smith09f76ee2011-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 Smith09f76ee2011-10-19 21:33:05 +0000997 return getLang().CPlusPlus;
998
Richard Smithc8a79032012-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 Smith09f76ee2011-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 Smithc8a79032012-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 Smith09f76ee2011-10-19 21:33:05 +00001035
1036 default:
1037 return false;
1038 }
1039
1040 default:
1041 return false;
1042 }
1043}
1044
John McCalld5a36322009-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 McCall28a6aea2009-11-04 02:18:39 +00001048Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1049 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001050 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001051 SourceLocation *DeclEnd,
1052 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001053 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001054 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001055 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001056
John McCalld5a36322009-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 Lattnerefb0f112009-03-29 17:18:04 +00001064 }
Mike Stump11289f42009-09-09 15:08:12 +00001065
Chris Lattnerdbb1e932010-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 Smithb3041fe2011-11-30 23:45:35 +00001072
Chris Lattner13901342010-07-11 22:42:07 +00001073 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-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 McCall48871652010-08-21 09:40:31 +00001081 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +00001082 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-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 McCalld5a36322009-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 Smith02e85f32011-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 Redl3da34892011-06-05 12:23:16 +00001106 if (Tok.is(tok::l_brace))
1107 FRI->RangeExpr = ParseBraceInitializer();
1108 else
1109 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001110 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1111 Actions.ActOnCXXForRangeDecl(ThisDecl);
1112 Actions.FinalizeDeclaration(ThisDecl);
1113 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1114 }
1115
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001116 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001117 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +00001118 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001119 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001120 DeclsInGroup.push_back(FirstDecl);
1121
Richard Smith09f76ee2011-10-19 21:33:05 +00001122 bool ExpectSemi = Context != Declarator::ForContext;
1123
John McCalld5a36322009-11-03 19:26:08 +00001124 // If we don't have a comma, it is either the end of the list (a ';') or an
1125 // error, bail out.
1126 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001127 SourceLocation CommaLoc = ConsumeToken();
1128
1129 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1130 // This comma was followed by a line-break and something which can't be
1131 // the start of a declarator. The comma was probably a typo for a
1132 // semicolon.
1133 Diag(CommaLoc, diag::err_expected_semi_declaration)
1134 << FixItHint::CreateReplacement(CommaLoc, ";");
1135 ExpectSemi = false;
1136 break;
1137 }
John McCalld5a36322009-11-03 19:26:08 +00001138
1139 // Parse the next declarator.
1140 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001141 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001142
1143 // Accept attributes in an init-declarator. In the first declarator in a
1144 // declaration, these would be part of the declspec. In subsequent
1145 // declarators, they become part of the declarator itself, so that they
1146 // don't apply to declarators after *this* one. Examples:
1147 // short __attribute__((common)) var; -> declspec
1148 // short var __attribute__((common)); -> declarator
1149 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001150 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001151
1152 ParseDeclarator(D);
1153
John McCall48871652010-08-21 09:40:31 +00001154 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +00001155 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +00001156 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +00001157 DeclsInGroup.push_back(ThisDecl);
1158 }
1159
1160 if (DeclEnd)
1161 *DeclEnd = Tok.getLocation();
1162
Richard Smith09f76ee2011-10-19 21:33:05 +00001163 if (ExpectSemi &&
John McCalld5a36322009-11-03 19:26:08 +00001164 ExpectAndConsume(tok::semi,
1165 Context == Declarator::FileContext
1166 ? diag::err_invalid_token_after_toplevel_declarator
1167 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001168 // Okay, there was no semicolon and one was expected. If we see a
1169 // declaration specifier, just assume it was missing and continue parsing.
1170 // Otherwise things are very confused and we skip to recover.
1171 if (!isDeclarationSpecifier()) {
1172 SkipUntil(tok::r_brace, true, true);
1173 if (Tok.is(tok::semi))
1174 ConsumeToken();
1175 }
John McCalld5a36322009-11-03 19:26:08 +00001176 }
1177
Douglas Gregor0be31a22010-07-02 17:43:08 +00001178 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001179 DeclsInGroup.data(),
1180 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001181}
1182
Richard Smith02e85f32011-04-14 22:09:26 +00001183/// Parse an optional simple-asm-expr and attributes, and attach them to a
1184/// declarator. Returns true on an error.
1185bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1186 // If a simple-asm-expr is present, parse it.
1187 if (Tok.is(tok::kw_asm)) {
1188 SourceLocation Loc;
1189 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1190 if (AsmLabel.isInvalid()) {
1191 SkipUntil(tok::semi, true, true);
1192 return true;
1193 }
1194
1195 D.setAsmLabel(AsmLabel.release());
1196 D.SetRangeEnd(Loc);
1197 }
1198
1199 MaybeParseGNUAttributes(D);
1200 return false;
1201}
1202
Douglas Gregor23996282009-05-12 21:31:51 +00001203/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1204/// declarator'. This method parses the remainder of the declaration
1205/// (including any attributes or initializer, among other things) and
1206/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001207///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001208/// init-declarator: [C99 6.7]
1209/// declarator
1210/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001211/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1212/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001213/// [C++] declarator initializer[opt]
1214///
1215/// [C++] initializer:
1216/// [C++] '=' initializer-clause
1217/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001218/// [C++0x] '=' 'default' [TODO]
1219/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001220/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001221///
1222/// According to the standard grammar, =default and =delete are function
1223/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001224///
John McCall48871652010-08-21 09:40:31 +00001225Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001226 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001227 if (ParseAttributesAfterDeclarator(D))
1228 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001229
Richard Smith02e85f32011-04-14 22:09:26 +00001230 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1231}
Mike Stump11289f42009-09-09 15:08:12 +00001232
Richard Smith02e85f32011-04-14 22:09:26 +00001233Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1234 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001235 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001236 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001237 switch (TemplateInfo.Kind) {
1238 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001239 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001240 break;
1241
1242 case ParsedTemplateInfo::Template:
1243 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001244 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001245 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001246 TemplateInfo.TemplateParams->data(),
1247 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001248 D);
1249 break;
1250
1251 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001252 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001253 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001254 TemplateInfo.ExternLoc,
1255 TemplateInfo.TemplateLoc,
1256 D);
1257 if (ThisRes.isInvalid()) {
1258 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001259 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001260 }
1261
1262 ThisDecl = ThisRes.get();
1263 break;
1264 }
1265 }
Mike Stump11289f42009-09-09 15:08:12 +00001266
Richard Smith30482bc2011-02-20 03:19:35 +00001267 bool TypeContainsAuto =
1268 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1269
Douglas Gregor23996282009-05-12 21:31:51 +00001270 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001271 if (isTokenEqualOrMistypedEqualEqual(
1272 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001273 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001274 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001275 if (D.isFunctionDeclarator())
1276 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1277 << 1 /* delete */;
1278 else
1279 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001280 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001281 if (D.isFunctionDeclarator())
1282 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1283 << 1 /* delete */;
1284 else
1285 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001286 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001287 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1288 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001289 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001290 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001291
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001292 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001293 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001294 cutOffParsing();
1295 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001296 }
1297
John McCalldadc5752010-08-24 06:29:42 +00001298 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001299
John McCall1f4ee7b2009-12-19 09:28:58 +00001300 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001301 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001302 ExitScope();
1303 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001304
Douglas Gregor23996282009-05-12 21:31:51 +00001305 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001306 SkipUntil(tok::comma, true, true);
1307 Actions.ActOnInitializerError(ThisDecl);
1308 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001309 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1310 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001311 }
1312 } else if (Tok.is(tok::l_paren)) {
1313 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001314 BalancedDelimiterTracker T(*this, tok::l_paren);
1315 T.consumeOpen();
1316
Douglas Gregor23996282009-05-12 21:31:51 +00001317 ExprVector Exprs(Actions);
1318 CommaLocsTy CommaLocs;
1319
Douglas Gregor613bf102009-12-22 17:47:17 +00001320 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1321 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001322 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001323 }
1324
Douglas Gregor23996282009-05-12 21:31:51 +00001325 if (ParseExpressionList(Exprs, CommaLocs)) {
1326 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001327
1328 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001329 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001330 ExitScope();
1331 }
Douglas Gregor23996282009-05-12 21:31:51 +00001332 } else {
1333 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001334 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001335
1336 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1337 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001338
1339 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001340 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001341 ExitScope();
1342 }
1343
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001344 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor23996282009-05-12 21:31:51 +00001345 move_arg(Exprs),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001346 T.getCloseLocation(),
Richard Smith30482bc2011-02-20 03:19:35 +00001347 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001348 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001349 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1350 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001351 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1352
Sebastian Redl3da34892011-06-05 12:23:16 +00001353 if (D.getCXXScopeSpec().isSet()) {
1354 EnterScope(0);
1355 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1356 }
1357
1358 ExprResult Init(ParseBraceInitializer());
1359
1360 if (D.getCXXScopeSpec().isSet()) {
1361 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1362 ExitScope();
1363 }
1364
1365 if (Init.isInvalid()) {
1366 Actions.ActOnInitializerError(ThisDecl);
1367 } else
1368 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1369 /*DirectInit=*/true, TypeContainsAuto);
1370
Douglas Gregor23996282009-05-12 21:31:51 +00001371 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001372 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001373 }
1374
Richard Smithb2bc2e62011-02-21 20:05:19 +00001375 Actions.FinalizeDeclaration(ThisDecl);
1376
Douglas Gregor23996282009-05-12 21:31:51 +00001377 return ThisDecl;
1378}
1379
Chris Lattner1890ac82006-08-13 01:16:23 +00001380/// ParseSpecifierQualifierList
1381/// specifier-qualifier-list:
1382/// type-specifier specifier-qualifier-list[opt]
1383/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001384/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001385///
Richard Smithcd1c0552011-07-01 19:46:12 +00001386void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001387 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1388 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001389 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithcd1c0552011-07-01 19:46:12 +00001390 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001391
Chris Lattner1890ac82006-08-13 01:16:23 +00001392 // Validate declspec for type-name.
1393 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001394 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001395 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001396 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001397
Chris Lattner1b22eed2006-11-28 05:12:07 +00001398 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001399 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001400 if (DS.getStorageClassSpecLoc().isValid())
1401 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1402 else
1403 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001404 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001405 }
Mike Stump11289f42009-09-09 15:08:12 +00001406
Chris Lattner1b22eed2006-11-28 05:12:07 +00001407 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001408 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001409 if (DS.isInlineSpecified())
1410 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1411 if (DS.isVirtualSpecified())
1412 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1413 if (DS.isExplicitSpecified())
1414 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001415 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001416 }
1417}
Chris Lattner53361ac2006-08-10 05:19:57 +00001418
Chris Lattner6cc055a2009-04-12 20:42:31 +00001419/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1420/// specified token is valid after the identifier in a declarator which
1421/// immediately follows the declspec. For example, these things are valid:
1422///
1423/// int x [ 4]; // direct-declarator
1424/// int x ( int y); // direct-declarator
1425/// int(int x ) // direct-declarator
1426/// int x ; // simple-declaration
1427/// int x = 17; // init-declarator-list
1428/// int x , y; // init-declarator-list
1429/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001430/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001431/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001432///
1433/// This is not, because 'x' does not immediately follow the declspec (though
1434/// ')' happens to be valid anyway).
1435/// int (x)
1436///
1437static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1438 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1439 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001440 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001441}
1442
Chris Lattner20a0c612009-04-14 21:34:55 +00001443
1444/// ParseImplicitInt - This method is called when we have an non-typename
1445/// identifier in a declspec (which normally terminates the decl spec) when
1446/// the declspec has no type specifier. In this case, the declspec is either
1447/// malformed or is "implicit int" (in K&R and C89).
1448///
1449/// This method handles diagnosing this prettily and returns false if the
1450/// declspec is done being processed. If it recovers and thinks there may be
1451/// other pieces of declspec after it, it returns true.
1452///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001453bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001454 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001455 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001456 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001457
Chris Lattner20a0c612009-04-14 21:34:55 +00001458 SourceLocation Loc = Tok.getLocation();
1459 // If we see an identifier that is not a type name, we normally would
1460 // parse it as the identifer being declared. However, when a typename
1461 // is typo'd or the definition is not included, this will incorrectly
1462 // parse the typename as the identifier name and fall over misparsing
1463 // later parts of the diagnostic.
1464 //
1465 // As such, we try to do some look-ahead in cases where this would
1466 // otherwise be an "implicit-int" case to see if this is invalid. For
1467 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1468 // an identifier with implicit int, we'd get a parse error because the
1469 // next token is obviously invalid for a type. Parse these as a case
1470 // with an invalid type specifier.
1471 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001472
Chris Lattner20a0c612009-04-14 21:34:55 +00001473 // Since we know that this either implicit int (which is rare) or an
1474 // error, we'd do lookahead to try to do better recovery.
1475 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1476 // If this token is valid for implicit int, e.g. "static x = 4", then
1477 // we just avoid eating the identifier, so it will be parsed as the
1478 // identifier in the declarator.
1479 return false;
1480 }
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner20a0c612009-04-14 21:34:55 +00001482 // Otherwise, if we don't consume this token, we are going to emit an
1483 // error anyway. Try to recover from various common problems. Check
1484 // to see if this was a reference to a tag name without a tag specified.
1485 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001486 //
1487 // C++ doesn't need this, and isTagName doesn't take SS.
1488 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001489 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001490 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001491
Douglas Gregor0be31a22010-07-02 17:43:08 +00001492 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001493 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001494 case DeclSpec::TST_enum:
1495 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1496 case DeclSpec::TST_union:
1497 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1498 case DeclSpec::TST_struct:
1499 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1500 case DeclSpec::TST_class:
1501 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001502 }
Mike Stump11289f42009-09-09 15:08:12 +00001503
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001504 if (TagName) {
1505 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001506 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001507 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001508
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001509 // Parse this as a tag as if the missing tag were present.
1510 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001511 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001512 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001513 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001514 return true;
1515 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001516 }
Mike Stump11289f42009-09-09 15:08:12 +00001517
Douglas Gregor15e56022009-10-13 23:27:22 +00001518 // This is almost certainly an invalid type name. Let the action emit a
1519 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001520 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001521 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001522 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001523 // The action emitted a diagnostic, so we don't have to.
1524 if (T) {
1525 // The action has suggested that the type T could be used. Set that as
1526 // the type in the declaration specifiers, consume the would-be type
1527 // name token, and we're done.
1528 const char *PrevSpec;
1529 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001530 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001531 DS.SetRangeEnd(Tok.getLocation());
1532 ConsumeToken();
1533
1534 // There may be other declaration specifiers after this.
1535 return true;
1536 }
1537
1538 // Fall through; the action had no suggestion for us.
1539 } else {
1540 // The action did not emit a diagnostic, so emit one now.
1541 SourceRange R;
1542 if (SS) R = SS->getRange();
1543 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1544 }
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregor15e56022009-10-13 23:27:22 +00001546 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001547 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001548 unsigned DiagID;
1549 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001550 DS.SetRangeEnd(Tok.getLocation());
1551 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattner20a0c612009-04-14 21:34:55 +00001553 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1554 // avoid rippling error messages on subsequent uses of the same type,
1555 // could be useful if #include was forgotten.
1556 return false;
1557}
1558
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001559/// \brief Determine the declaration specifier context from the declarator
1560/// context.
1561///
1562/// \param Context the declarator context, which is one of the
1563/// Declarator::TheContext enumerator values.
1564Parser::DeclSpecContext
1565Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1566 if (Context == Declarator::MemberContext)
1567 return DSC_class;
1568 if (Context == Declarator::FileContext)
1569 return DSC_top_level;
1570 return DSC_normal;
1571}
1572
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001573/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1574///
1575/// FIXME: Simply returns an alignof() expression if the argument is a
1576/// type. Ideally, the type should be propagated directly into Sema.
1577///
Benjamin Kramere56f3932011-12-23 17:00:35 +00001578/// [C11] type-id
1579/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001580/// [C++0x] type-id ...[opt]
1581/// [C++0x] assignment-expression ...[opt]
1582ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1583 SourceLocation &EllipsisLoc) {
1584 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001585 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001586 SourceLocation TypeLoc = Tok.getLocation();
1587 ParsedType Ty = ParseTypeName().get();
1588 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001589 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1590 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001591 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001592 ER = ParseConstantExpression();
1593
Peter Collingbourneccbcce02011-10-24 17:56:00 +00001594 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1595 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001596
1597 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001598}
1599
1600/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1601/// attribute to Attrs.
1602///
1603/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00001604/// [C11] '_Alignas' '(' type-id ')'
1605/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001606/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1607/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001608void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1609 SourceLocation *endLoc) {
1610 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1611 "Not an alignment-specifier!");
1612
1613 SourceLocation KWLoc = Tok.getLocation();
1614 ConsumeToken();
1615
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001616 BalancedDelimiterTracker T(*this, tok::l_paren);
1617 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001618 return;
1619
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001620 SourceLocation EllipsisLoc;
1621 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001622 if (ArgExpr.isInvalid()) {
1623 SkipUntil(tok::r_paren);
1624 return;
1625 }
1626
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001627 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001628 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001629 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001630
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001631 // FIXME: Handle pack-expansions here.
1632 if (EllipsisLoc.isValid()) {
1633 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1634 return;
1635 }
1636
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001637 ExprVector ArgExprs(Actions);
1638 ArgExprs.push_back(ArgExpr.release());
1639 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001640 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001641}
1642
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001643/// ParseDeclarationSpecifiers
1644/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001645/// storage-class-specifier declaration-specifiers[opt]
1646/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001647/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00001648/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001649/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00001650/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001651///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001652/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001653/// 'typedef'
1654/// 'extern'
1655/// 'static'
1656/// 'auto'
1657/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001658/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001659/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001660/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001661/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001662/// [C++] 'virtual'
1663/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001664/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001665/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001666/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001667
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001668///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001669void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001670 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001671 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001672 DeclSpecContext DSContext) {
1673 if (DS.getSourceRange().isInvalid()) {
1674 DS.SetRangeStart(Tok.getLocation());
1675 DS.SetRangeEnd(Tok.getLocation());
1676 }
1677
Douglas Gregordf593fb2011-11-07 17:33:42 +00001678 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001679 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001680 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001681 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001682 unsigned DiagID = 0;
1683
Chris Lattner4d8f8732006-11-28 05:05:08 +00001684 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001685
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001686 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001687 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001688 DoneWithDeclSpec:
Peter Collingbourne70188b32011-09-29 18:03:57 +00001689 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1690 MaybeParseCXX0XAttributes(DS.getAttributes());
1691
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001692 // If this is not a declaration specifier token, we're done reading decl
1693 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001694 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001695 return;
Mike Stump11289f42009-09-09 15:08:12 +00001696
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001697 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001698 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001699 if (DS.hasTypeSpecifier()) {
1700 bool AllowNonIdentifiers
1701 = (getCurScope()->getFlags() & (Scope::ControlScope |
1702 Scope::BlockScope |
1703 Scope::TemplateParamScope |
1704 Scope::FunctionPrototypeScope |
1705 Scope::AtCatchScope)) == 0;
1706 bool AllowNestedNameSpecifiers
1707 = DSContext == DSC_top_level ||
1708 (DSContext == DSC_class && DS.isFriendSpecified());
1709
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001710 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1711 AllowNonIdentifiers,
1712 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001713 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001714 }
1715
Douglas Gregor80039242011-02-15 20:33:25 +00001716 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1717 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1718 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001719 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1720 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001721 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001722 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001723 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001724 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001725
1726 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001727 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001728 }
1729
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001730 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001731 // C++ scope specifier. Annotate and loop, or bail out on error.
1732 if (TryAnnotateCXXScopeToken(true)) {
1733 if (!DS.hasTypeSpecifier())
1734 DS.SetTypeSpecError();
1735 goto DoneWithDeclSpec;
1736 }
John McCall8bc2a702010-03-01 18:20:46 +00001737 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1738 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001739 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001740
1741 case tok::annot_cxxscope: {
1742 if (DS.hasTypeSpecifier())
1743 goto DoneWithDeclSpec;
1744
John McCall9dab4e62009-12-12 11:40:51 +00001745 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001746 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1747 Tok.getAnnotationRange(),
1748 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001749
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001750 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001751 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001752 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001753 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001754 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001755 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001756
1757 // C++ [class.qual]p2:
1758 // In a lookup in which the constructor is an acceptable lookup
1759 // result and the nested-name-specifier nominates a class C:
1760 //
1761 // - if the name specified after the
1762 // nested-name-specifier, when looked up in C, is the
1763 // injected-class-name of C (Clause 9), or
1764 //
1765 // - if the name specified after the nested-name-specifier
1766 // is the same as the identifier or the
1767 // simple-template-id's template-name in the last
1768 // component of the nested-name-specifier,
1769 //
1770 // the name is instead considered to name the constructor of
1771 // class C.
1772 //
1773 // Thus, if the template-name is actually the constructor
1774 // name, then the code is ill-formed; this interpretation is
1775 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001776 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001777 if ((DSContext == DSC_top_level ||
1778 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1779 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001780 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001781 if (isConstructorDeclarator()) {
1782 // The user meant this to be an out-of-line constructor
1783 // definition, but template arguments are not allowed
1784 // there. Just allow this as a constructor; we'll
1785 // complain about it later.
1786 goto DoneWithDeclSpec;
1787 }
1788
1789 // The user meant this to name a type, but it actually names
1790 // a constructor with some extraneous template
1791 // arguments. Complain, then parse it as a type as the user
1792 // intended.
1793 Diag(TemplateId->TemplateNameLoc,
1794 diag::err_out_of_line_template_id_names_constructor)
1795 << TemplateId->Name;
1796 }
1797
John McCall9dab4e62009-12-12 11:40:51 +00001798 DS.getTypeSpecScope() = SS;
1799 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001800 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001801 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001802 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001803 continue;
1804 }
1805
Douglas Gregorc5790df2009-09-28 07:26:33 +00001806 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001807 DS.getTypeSpecScope() = SS;
1808 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001809 if (Tok.getAnnotationValue()) {
1810 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001811 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1812 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001813 PrevSpec, DiagID, T);
1814 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001815 else
1816 DS.SetTypeSpecError();
1817 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1818 ConsumeToken(); // The typename
1819 }
1820
Douglas Gregor167fa622009-03-25 15:40:00 +00001821 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001822 goto DoneWithDeclSpec;
1823
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001824 // If we're in a context where the identifier could be a class name,
1825 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001826 if ((DSContext == DSC_top_level ||
1827 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001828 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001829 &SS)) {
1830 if (isConstructorDeclarator())
1831 goto DoneWithDeclSpec;
1832
1833 // As noted in C++ [class.qual]p2 (cited above), when the name
1834 // of the class is qualified in a context where it could name
1835 // a constructor, its a constructor name. However, we've
1836 // looked at the declarator, and the user probably meant this
1837 // to be a type. Complain that it isn't supposed to be treated
1838 // as a type, then proceed to parse it as a type.
1839 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1840 << Next.getIdentifierInfo();
1841 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001842
John McCallba7bf592010-08-24 05:47:05 +00001843 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1844 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001845 getCurScope(), &SS,
1846 false, false, ParsedType(),
1847 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001848
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001849 // If the referenced identifier is not a type, then this declspec is
1850 // erroneous: We already checked about that it has no type specifier, and
1851 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001852 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001853 if (TypeRep == 0) {
1854 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001855 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001856 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001857 }
Mike Stump11289f42009-09-09 15:08:12 +00001858
John McCall9dab4e62009-12-12 11:40:51 +00001859 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001860 ConsumeToken(); // The C++ scope.
1861
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001862 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001863 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001864 if (isInvalid)
1865 break;
Mike Stump11289f42009-09-09 15:08:12 +00001866
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001867 DS.SetRangeEnd(Tok.getLocation());
1868 ConsumeToken(); // The typename.
1869
1870 continue;
1871 }
Mike Stump11289f42009-09-09 15:08:12 +00001872
Chris Lattnere387d9e2009-01-21 19:48:37 +00001873 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001874 if (Tok.getAnnotationValue()) {
1875 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001876 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001877 DiagID, T);
1878 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001879 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001880
1881 if (isInvalid)
1882 break;
1883
Chris Lattnere387d9e2009-01-21 19:48:37 +00001884 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1885 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001886
Chris Lattnere387d9e2009-01-21 19:48:37 +00001887 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1888 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001889 // Objective-C interface.
1890 if (Tok.is(tok::less) && getLang().ObjC1)
1891 ParseObjCProtocolQualifiers(DS);
1892
Chris Lattnere387d9e2009-01-21 19:48:37 +00001893 continue;
1894 }
Mike Stump11289f42009-09-09 15:08:12 +00001895
Douglas Gregor06873092011-04-28 15:48:45 +00001896 case tok::kw___is_signed:
1897 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1898 // typically treats it as a trait. If we see __is_signed as it appears
1899 // in libstdc++, e.g.,
1900 //
1901 // static const bool __is_signed;
1902 //
1903 // then treat __is_signed as an identifier rather than as a keyword.
1904 if (DS.getTypeSpecType() == TST_bool &&
1905 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1906 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1907 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1908 Tok.setKind(tok::identifier);
1909 }
1910
1911 // We're done with the declaration-specifiers.
1912 goto DoneWithDeclSpec;
1913
Chris Lattner16fac4f2008-07-26 01:18:38 +00001914 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00001915 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001916 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001917 // In C++, check to see if this is a scope specifier like foo::bar::, if
1918 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001919 if (getLang().CPlusPlus) {
1920 if (TryAnnotateCXXScopeToken(true)) {
1921 if (!DS.hasTypeSpecifier())
1922 DS.SetTypeSpecError();
1923 goto DoneWithDeclSpec;
1924 }
1925 if (!Tok.is(tok::identifier))
1926 continue;
1927 }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Chris Lattner16fac4f2008-07-26 01:18:38 +00001929 // This identifier can only be a typedef name if we haven't already seen
1930 // a type-specifier. Without this check we misparse:
1931 // typedef int X; struct Y { short X; }; as 'short int'.
1932 if (DS.hasTypeSpecifier())
1933 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001934
John Thompson22334602010-02-05 00:12:22 +00001935 // Check for need to substitute AltiVec keyword tokens.
1936 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1937 break;
1938
Chris Lattner16fac4f2008-07-26 01:18:38 +00001939 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001940 ParsedType TypeRep =
1941 Actions.getTypeName(*Tok.getIdentifierInfo(),
1942 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001943
Chris Lattner6cc055a2009-04-12 20:42:31 +00001944 // If this is not a typedef name, don't parse it as part of the declspec,
1945 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001946 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001947 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001948 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001949 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001950
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001951 // If we're in a context where the identifier could be a class name,
1952 // check whether this is a constructor declaration.
1953 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001954 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001955 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001956 goto DoneWithDeclSpec;
1957
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001959 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001960 if (isInvalid)
1961 break;
Mike Stump11289f42009-09-09 15:08:12 +00001962
Chris Lattner16fac4f2008-07-26 01:18:38 +00001963 DS.SetRangeEnd(Tok.getLocation());
1964 ConsumeToken(); // The identifier
1965
1966 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1967 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001968 // Objective-C interface.
1969 if (Tok.is(tok::less) && getLang().ObjC1)
1970 ParseObjCProtocolQualifiers(DS);
1971
Steve Naroffcd5e7822008-09-22 10:28:57 +00001972 // Need to support trailing type qualifiers (e.g. "id<p> const").
1973 // If a type specifier follows, it will be diagnosed elsewhere.
1974 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001975 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001976
1977 // type-name
1978 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001979 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001980 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001981 // This template-id does not refer to a type name, so we're
1982 // done with the type-specifiers.
1983 goto DoneWithDeclSpec;
1984 }
1985
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001986 // If we're in a context where the template-id could be a
1987 // constructor name or specialization, check whether this is a
1988 // constructor declaration.
1989 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001990 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001991 isConstructorDeclarator())
1992 goto DoneWithDeclSpec;
1993
Douglas Gregor7f741122009-02-25 19:37:18 +00001994 // Turn the template-id annotation token into a type annotation
1995 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001996 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001997 continue;
1998 }
1999
Chris Lattnere37e2332006-08-15 04:50:22 +00002000 // GNU attributes support.
2001 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00002002 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00002003 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002004
2005 // Microsoft declspec support.
2006 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002007 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002008 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002009
Steve Naroff44ac7772008-12-25 14:16:32 +00002010 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00002011 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00002012 // FIXME: Add handling here!
2013 break;
2014
2015 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002016 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002017 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002018 case tok::kw___cdecl:
2019 case tok::kw___stdcall:
2020 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002021 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002022 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002023 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002024 continue;
2025
Dawn Perchik335e16b2010-09-03 01:29:35 +00002026 // Borland single token adornments.
2027 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002028 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002029 continue;
2030
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002031 // OpenCL single token adornments.
2032 case tok::kw___kernel:
2033 ParseOpenCLAttributes(DS.getAttributes());
2034 continue;
2035
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002036 // storage-class-specifier
2037 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002038 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2039 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002040 break;
2041 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002042 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002043 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002044 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2045 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002046 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002047 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002048 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2049 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002050 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002051 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002052 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002053 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002054 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2055 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002056 break;
2057 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00002058 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002059 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002060 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2061 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002062 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002063 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002064 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002065 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2067 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002068 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002069 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2070 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002071 break;
2072 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002073 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2074 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002075 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002076 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002077 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2078 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002079 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002080 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002081 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002082 break;
Mike Stump11289f42009-09-09 15:08:12 +00002083
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002084 // function-specifier
2085 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002086 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002087 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002088 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002089 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002090 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002091 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002092 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002093 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002094
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002095 // alignment-specifier
2096 case tok::kw__Alignas:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002097 if (!getLang().C11)
2098 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002099 ParseAlignmentSpecifier(DS.getAttributes());
2100 continue;
2101
Anders Carlssoncd8db412009-05-06 04:46:28 +00002102 // friend
2103 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002104 if (DSContext == DSC_class)
2105 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2106 else {
2107 PrevSpec = ""; // not actually used by the diagnostic
2108 DiagID = diag::err_friend_invalid_in_context;
2109 isInvalid = true;
2110 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002111 break;
Mike Stump11289f42009-09-09 15:08:12 +00002112
Douglas Gregor26701a42011-09-09 02:06:17 +00002113 // Modules
2114 case tok::kw___module_private__:
2115 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2116 break;
2117
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002118 // constexpr
2119 case tok::kw_constexpr:
2120 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2121 break;
2122
Chris Lattnere387d9e2009-01-21 19:48:37 +00002123 // type-specifier
2124 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002125 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2126 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002127 break;
2128 case tok::kw_long:
2129 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002130 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2131 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002132 else
John McCall49bfce42009-08-03 20:12:06 +00002133 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2134 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002135 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002136 case tok::kw___int64:
2137 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2138 DiagID);
2139 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002140 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002141 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2142 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002143 break;
2144 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002145 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2146 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002147 break;
2148 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002149 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2150 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002151 break;
2152 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002153 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2154 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002155 break;
2156 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002157 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2158 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002159 break;
2160 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002161 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2162 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002163 break;
2164 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2166 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002167 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00002168 case tok::kw_half:
2169 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2170 DiagID);
2171 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002172 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002173 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2174 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002175 break;
2176 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2178 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002179 break;
2180 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002181 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2182 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002183 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002184 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2186 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002187 break;
2188 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002189 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2190 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002191 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002192 case tok::kw_bool:
2193 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002194 if (Tok.is(tok::kw_bool) &&
2195 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2196 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2197 PrevSpec = ""; // Not used by the diagnostic.
2198 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002199 // For better error recovery.
2200 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002201 isInvalid = true;
2202 } else {
2203 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2204 DiagID);
2205 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002206 break;
2207 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002208 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2209 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002210 break;
2211 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002212 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2213 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002214 break;
2215 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2217 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002218 break;
John Thompson22334602010-02-05 00:12:22 +00002219 case tok::kw___vector:
2220 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2221 break;
2222 case tok::kw___pixel:
2223 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2224 break;
John McCall39439732011-04-09 22:50:59 +00002225 case tok::kw___unknown_anytype:
2226 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2227 PrevSpec, DiagID);
2228 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002229
2230 // class-specifier:
2231 case tok::kw_class:
2232 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002233 case tok::kw_union: {
2234 tok::TokenKind Kind = Tok.getKind();
2235 ConsumeToken();
Douglas Gregordf593fb2011-11-07 17:33:42 +00002236 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002237 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002238 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002239
2240 // enum-specifier:
2241 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002242 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002243 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002244 continue;
2245
2246 // cv-qualifier:
2247 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002248 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2249 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002250 break;
2251 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002252 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2253 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002254 break;
2255 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002256 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2257 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002258 break;
2259
Douglas Gregor333489b2009-03-27 23:10:48 +00002260 // C++ typename-specifier:
2261 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002262 if (TryAnnotateTypeOrScopeToken()) {
2263 DS.SetTypeSpecError();
2264 goto DoneWithDeclSpec;
2265 }
2266 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002267 continue;
2268 break;
2269
Chris Lattnere387d9e2009-01-21 19:48:37 +00002270 // GNU typeof support.
2271 case tok::kw_typeof:
2272 ParseTypeofSpecifier(DS);
2273 continue;
2274
David Blaikie15a430a2011-12-04 05:04:18 +00002275 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002276 ParseDecltypeSpecifier(DS);
2277 continue;
2278
Alexis Hunt4a257072011-05-19 05:37:45 +00002279 case tok::kw___underlying_type:
2280 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002281 continue;
2282
2283 case tok::kw__Atomic:
2284 ParseAtomicSpecifier(DS);
2285 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002286
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002287 // OpenCL qualifiers:
2288 case tok::kw_private:
2289 if (!getLang().OpenCL)
2290 goto DoneWithDeclSpec;
2291 case tok::kw___private:
2292 case tok::kw___global:
2293 case tok::kw___local:
2294 case tok::kw___constant:
2295 case tok::kw___read_only:
2296 case tok::kw___write_only:
2297 case tok::kw___read_write:
2298 ParseOpenCLQualifiers(DS);
2299 break;
2300
Steve Naroffcfdf6162008-06-05 00:02:44 +00002301 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002302 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002303 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2304 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00002305 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002306 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002307
Douglas Gregor3a001f42010-11-19 17:10:50 +00002308 if (!ParseObjCProtocolQualifiers(DS))
2309 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2310 << FixItHint::CreateInsertion(Loc, "id")
2311 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002312
2313 // Need to support trailing type qualifiers (e.g. "id<p> const").
2314 // If a type specifier follows, it will be diagnosed elsewhere.
2315 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002316 }
John McCall49bfce42009-08-03 20:12:06 +00002317 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002318 if (isInvalid) {
2319 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002320 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002321
2322 if (DiagID == diag::ext_duplicate_declspec)
2323 Diag(Tok, DiagID)
2324 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2325 else
2326 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002327 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002328
Chris Lattner2e232092008-03-13 06:29:04 +00002329 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002330 if (DiagID != diag::err_bool_redeclaration)
2331 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002332 }
2333}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002334
Chris Lattnera448d752009-01-06 06:59:53 +00002335/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002336/// primarily follow the C++ grammar with additions for C99 and GNU,
2337/// which together subsume the C grammar. Note that the C++
2338/// type-specifier also includes the C type-qualifier (for const,
2339/// volatile, and C99 restrict). Returns true if a type-specifier was
2340/// found (and parsed), false otherwise.
2341///
2342/// type-specifier: [C++ 7.1.5]
2343/// simple-type-specifier
2344/// class-specifier
2345/// enum-specifier
2346/// elaborated-type-specifier [TODO]
2347/// cv-qualifier
2348///
2349/// cv-qualifier: [C++ 7.1.5.1]
2350/// 'const'
2351/// 'volatile'
2352/// [C99] 'restrict'
2353///
2354/// simple-type-specifier: [ C++ 7.1.5.2]
2355/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2356/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2357/// 'char'
2358/// 'wchar_t'
2359/// 'bool'
2360/// 'short'
2361/// 'int'
2362/// 'long'
2363/// 'signed'
2364/// 'unsigned'
2365/// 'float'
2366/// 'double'
2367/// 'void'
2368/// [C99] '_Bool'
2369/// [C99] '_Complex'
2370/// [C99] '_Imaginary' // Removed in TC2?
2371/// [GNU] '_Decimal32'
2372/// [GNU] '_Decimal64'
2373/// [GNU] '_Decimal128'
2374/// [GNU] typeof-specifier
2375/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2376/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002377/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002378/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002379bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002380 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002381 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002382 const ParsedTemplateInfo &TemplateInfo,
2383 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002384 SourceLocation Loc = Tok.getLocation();
2385
2386 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002387 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002388 // If we already have a type specifier, this identifier is not a type.
2389 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2390 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2391 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2392 return false;
John Thompson22334602010-02-05 00:12:22 +00002393 // Check for need to substitute AltiVec keyword tokens.
2394 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2395 break;
2396 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00002397 case tok::kw_decltype:
Douglas Gregor333489b2009-03-27 23:10:48 +00002398 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002399 // Annotate typenames and C++ scope specifiers. If we get one, just
2400 // recurse to handle whatever we get.
Kaelyn Uhrain85308c62011-10-11 01:02:41 +00002401 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2402 /*NeedType=*/true))
John McCall1f476a12010-02-26 08:45:28 +00002403 return true;
2404 if (Tok.is(tok::identifier))
2405 return false;
2406 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2407 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002408 case tok::coloncolon: // ::foo::bar
2409 if (NextToken().is(tok::kw_new) || // ::new
2410 NextToken().is(tok::kw_delete)) // ::delete
2411 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002412
Chris Lattner020bab92009-01-04 23:41:41 +00002413 // Annotate typenames and C++ scope specifiers. If we get one, just
2414 // recurse to handle whatever we get.
Kaelyn Uhrain85308c62011-10-11 01:02:41 +00002415 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2416 /*NeedType=*/true))
John McCall1f476a12010-02-26 08:45:28 +00002417 return true;
2418 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2419 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002420
Douglas Gregor450c75a2008-11-07 15:42:26 +00002421 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002422 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002423 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2425 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002426 DiagID, T);
2427 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002428 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002429 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2430 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002431
Douglas Gregor450c75a2008-11-07 15:42:26 +00002432 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2433 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2434 // Objective-C interface. If we don't have Objective-C or a '<', this is
2435 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002436 if (Tok.is(tok::less) && getLang().ObjC1)
2437 ParseObjCProtocolQualifiers(DS);
2438
Douglas Gregor450c75a2008-11-07 15:42:26 +00002439 return true;
2440 }
2441
2442 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002443 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002444 break;
2445 case tok::kw_long:
2446 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002447 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2448 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002449 else
John McCall49bfce42009-08-03 20:12:06 +00002450 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2451 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002452 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002453 case tok::kw___int64:
2454 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2455 DiagID);
2456 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002457 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002458 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002459 break;
2460 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002461 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2462 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002463 break;
2464 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002465 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2466 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002467 break;
2468 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002469 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2470 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002471 break;
2472 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002474 break;
2475 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002477 break;
2478 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002480 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00002481 case tok::kw_half:
2482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2483 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002484 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002486 break;
2487 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002489 break;
2490 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002492 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002493 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002495 break;
2496 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002498 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002499 case tok::kw_bool:
2500 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002501 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002502 break;
2503 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2505 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002506 break;
2507 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2509 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002510 break;
2511 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2513 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002514 break;
John Thompson22334602010-02-05 00:12:22 +00002515 case tok::kw___vector:
2516 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2517 break;
2518 case tok::kw___pixel:
2519 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2520 break;
2521
Douglas Gregor450c75a2008-11-07 15:42:26 +00002522 // class-specifier:
2523 case tok::kw_class:
2524 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002525 case tok::kw_union: {
2526 tok::TokenKind Kind = Tok.getKind();
2527 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002528 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregordf593fb2011-11-07 17:33:42 +00002529 /*EnteringContext=*/false,
Sebastian Redl2b372722010-02-03 21:21:43 +00002530 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002531 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002532 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002533
2534 // enum-specifier:
2535 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002536 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002537 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002538 return true;
2539
2540 // cv-qualifier:
2541 case tok::kw_const:
2542 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002543 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002544 break;
2545 case tok::kw_volatile:
2546 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002547 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002548 break;
2549 case tok::kw_restrict:
2550 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002551 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002552 break;
2553
2554 // GNU typeof support.
2555 case tok::kw_typeof:
2556 ParseTypeofSpecifier(DS);
2557 return true;
2558
Anders Carlsson74948d02009-06-24 17:47:40 +00002559 // C++0x decltype support.
David Blaikie15a430a2011-12-04 05:04:18 +00002560 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002561 ParseDecltypeSpecifier(DS);
2562 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002563
Alexis Hunt4a257072011-05-19 05:37:45 +00002564 // C++0x type traits support.
2565 case tok::kw___underlying_type:
2566 ParseUnderlyingTypeSpecifier(DS);
2567 return true;
2568
Eli Friedman0dfb8892011-10-06 23:00:33 +00002569 case tok::kw__Atomic:
2570 ParseAtomicSpecifier(DS);
2571 return true;
2572
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002573 // OpenCL qualifiers:
2574 case tok::kw_private:
2575 if (!getLang().OpenCL)
2576 return false;
2577 case tok::kw___private:
2578 case tok::kw___global:
2579 case tok::kw___local:
2580 case tok::kw___constant:
2581 case tok::kw___read_only:
2582 case tok::kw___write_only:
2583 case tok::kw___read_write:
2584 ParseOpenCLQualifiers(DS);
2585 break;
2586
Anders Carlssonbae27372009-06-26 23:44:14 +00002587 // C++0x auto support.
2588 case tok::kw_auto:
Richard Smith50658642011-09-04 20:24:20 +00002589 // This is only called in situations where a storage-class specifier is
2590 // illegal, so we can assume an auto type specifier was intended even in
2591 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2592 // extension diagnostic.
2593 if (!getLang().CPlusPlus)
Anders Carlssonbae27372009-06-26 23:44:14 +00002594 return false;
2595
John McCall49bfce42009-08-03 20:12:06 +00002596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002597 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002598
Eli Friedman53339e02009-06-08 23:27:34 +00002599 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002600 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00002601 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002602 case tok::kw___cdecl:
2603 case tok::kw___stdcall:
2604 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002605 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002606 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002607 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002608 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002609
Dawn Perchik335e16b2010-09-03 01:29:35 +00002610 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002611 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002612 return true;
2613
Douglas Gregor450c75a2008-11-07 15:42:26 +00002614 default:
2615 // Not a type-specifier; do nothing.
2616 return false;
2617 }
2618
2619 // If the specifier combination wasn't legal, issue a diagnostic.
2620 if (isInvalid) {
2621 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002622 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002623 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002624 }
2625 DS.SetRangeEnd(Tok.getLocation());
2626 ConsumeToken(); // whatever we parsed above.
2627 return true;
2628}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002629
Chris Lattner70ae4912007-10-29 04:42:53 +00002630/// ParseStructDeclaration - Parse a struct declaration without the terminating
2631/// semicolon.
2632///
Chris Lattner90a26b02007-01-23 04:38:16 +00002633/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002634/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002635/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002636/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002637/// struct-declarator-list:
2638/// struct-declarator
2639/// struct-declarator-list ',' struct-declarator
2640/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2641/// struct-declarator:
2642/// declarator
2643/// [GNU] declarator attributes[opt]
2644/// declarator[opt] ':' constant-expression
2645/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2646///
Chris Lattnera12405b2008-04-10 06:46:29 +00002647void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002648ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002649
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002650 if (Tok.is(tok::kw___extension__)) {
2651 // __extension__ silences extension warnings in the subexpression.
2652 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002653 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002654 return ParseStructDeclaration(DS, Fields);
2655 }
Mike Stump11289f42009-09-09 15:08:12 +00002656
Steve Naroff97170802007-08-20 22:28:22 +00002657 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002658 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002659
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002660 // If there are no declarators, this is a free-standing declaration
2661 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002662 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002663 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002664 return;
2665 }
2666
2667 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002668 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002669 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002670 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002671 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002672 FieldDeclarator DeclaratorInfo(DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002673 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002674
2675 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002676 if (!FirstDeclarator)
2677 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002678
Steve Naroff97170802007-08-20 22:28:22 +00002679 /// struct-declarator: declarator
2680 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002681 if (Tok.isNot(tok::colon)) {
2682 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2683 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002684 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002685 }
Mike Stump11289f42009-09-09 15:08:12 +00002686
Chris Lattner76c72282007-10-09 17:33:22 +00002687 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002688 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002689 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002690 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002691 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002692 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002693 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002694 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002695
Steve Naroff97170802007-08-20 22:28:22 +00002696 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002697 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002698
John McCallcfefb6d2009-11-03 02:38:08 +00002699 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002700 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002701 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002702
Steve Naroff97170802007-08-20 22:28:22 +00002703 // If we don't have a comma, it is either the end of the list (a ';')
2704 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002705 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002706 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002707
Steve Naroff97170802007-08-20 22:28:22 +00002708 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002709 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002710
John McCallcfefb6d2009-11-03 02:38:08 +00002711 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002712 }
Steve Naroff97170802007-08-20 22:28:22 +00002713}
2714
2715/// ParseStructUnionBody
2716/// struct-contents:
2717/// struct-declaration-list
2718/// [EXT] empty
2719/// [GNU] "struct-declaration-list" without terminatoring ';'
2720/// struct-declaration-list:
2721/// struct-declaration
2722/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002723/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002724///
Chris Lattner1300fb92007-01-23 23:42:53 +00002725void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002726 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002727 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2728 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002729
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002730 BalancedDelimiterTracker T(*this, tok::l_brace);
2731 if (T.consumeOpen())
2732 return;
Mike Stump11289f42009-09-09 15:08:12 +00002733
Douglas Gregor658b9552009-01-09 22:42:13 +00002734 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002735 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002736
Chris Lattner7b9ace62007-01-23 20:11:08 +00002737 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2738 // C++.
Richard Smithe4345902011-12-29 21:57:33 +00002739 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2740 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2741 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2742 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002743
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002744 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002745
Chris Lattner7b9ace62007-01-23 20:11:08 +00002746 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002747 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002748 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002749
Chris Lattner736ed5d2007-06-09 05:59:07 +00002750 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002751 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002752 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002753 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002754 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002755 ConsumeToken();
2756 continue;
2757 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002758
2759 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002760 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002761
John McCallcfefb6d2009-11-03 02:38:08 +00002762 if (!Tok.is(tok::at)) {
2763 struct CFieldCallback : FieldCallback {
2764 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002765 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002766 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002767
John McCall48871652010-08-21 09:40:31 +00002768 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002769 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002770 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2771
John McCall48871652010-08-21 09:40:31 +00002772 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002773 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002774 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002775 FD.D.getDeclSpec().getSourceRange().getBegin(),
2776 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002777 FieldDecls.push_back(Field);
2778 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002779 }
John McCallcfefb6d2009-11-03 02:38:08 +00002780 } Callback(*this, TagDecl, FieldDecls);
2781
2782 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002783 } else { // Handle @defs
2784 ConsumeToken();
2785 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2786 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002787 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002788 continue;
2789 }
2790 ConsumeToken();
2791 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2792 if (!Tok.is(tok::identifier)) {
2793 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002794 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002795 continue;
2796 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002797 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002798 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002799 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002800 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2801 ConsumeToken();
2802 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002803 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002804
Chris Lattner76c72282007-10-09 17:33:22 +00002805 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002806 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002807 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002808 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002809 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002810 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002811 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2812 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002813 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002814 // If we stopped at a ';', eat it.
2815 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002816 }
2817 }
Mike Stump11289f42009-09-09 15:08:12 +00002818
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002819 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00002820
John McCall084e83d2011-03-24 11:26:52 +00002821 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002822 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002823 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002824
Douglas Gregor0be31a22010-07-02 17:43:08 +00002825 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00002826 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002827 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002828 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002829 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002830 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2831 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00002832}
2833
Chris Lattner3b561a32006-08-13 00:12:11 +00002834/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002835/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002836/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002837///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002838/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2839/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002840/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002841/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002842///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002843/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2844/// [C++0x] enum-head '{' enumerator-list ',' '}'
2845///
2846/// enum-head: [C++0x]
2847/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2848/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2849///
2850/// enum-key: [C++0x]
2851/// 'enum'
2852/// 'enum' 'class'
2853/// 'enum' 'struct'
2854///
2855/// enum-base: [C++0x]
2856/// ':' type-specifier-seq
2857///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002858/// [C++] elaborated-type-specifier:
2859/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2860///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002861void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002862 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002863 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002864 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002865 if (Tok.is(tok::code_completion)) {
2866 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002867 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002868 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002869 }
John McCallcb432fa2011-07-06 05:58:41 +00002870
Richard Smith0f8ee222012-01-10 01:33:14 +00002871 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00002872 bool IsScopedUsingClassTag = false;
2873
2874 if (getLang().CPlusPlus0x &&
2875 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002876 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00002877 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00002878 ScopedEnumKWLoc = ConsumeToken();
John McCallcb432fa2011-07-06 05:58:41 +00002879 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002880
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002881 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002882 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002883 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002884
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002885 bool AllowFixedUnderlyingType
Francois Pichet0706d202011-09-17 17:15:52 +00002886 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCallcb432fa2011-07-06 05:58:41 +00002887
Abramo Bagnarad7548482010-05-19 21:37:53 +00002888 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002889 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002890 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2891 // if a fixed underlying type is allowed.
2892 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2893
Douglas Gregordf593fb2011-11-07 17:33:42 +00002894 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2895 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00002896 return;
2897
2898 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002899 Diag(Tok, diag::err_expected_ident);
2900 if (Tok.isNot(tok::l_brace)) {
2901 // Has no name and is not a definition.
2902 // Skip the rest of this declarator, up until the comma or semicolon.
2903 SkipUntil(tok::comma, true);
2904 return;
2905 }
2906 }
2907 }
Mike Stump11289f42009-09-09 15:08:12 +00002908
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002909 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002910 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2911 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002912 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002913
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002914 // Skip the rest of this declarator, up until the comma or semicolon.
2915 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002916 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002917 }
Mike Stump11289f42009-09-09 15:08:12 +00002918
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002919 // If an identifier is present, consume and remember it.
2920 IdentifierInfo *Name = 0;
2921 SourceLocation NameLoc;
2922 if (Tok.is(tok::identifier)) {
2923 Name = Tok.getIdentifierInfo();
2924 NameLoc = ConsumeToken();
2925 }
Mike Stump11289f42009-09-09 15:08:12 +00002926
Richard Smith0f8ee222012-01-10 01:33:14 +00002927 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002928 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2929 // declaration of a scoped enumeration.
2930 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00002931 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002932 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002933 }
2934
2935 TypeResult BaseType;
2936
Douglas Gregord1f69f62010-12-01 17:42:47 +00002937 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002938 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002939 bool PossibleBitfield = false;
2940 if (getCurScope()->getFlags() & Scope::ClassScope) {
2941 // If we're in class scope, this can either be an enum declaration with
2942 // an underlying type, or a declaration of a bitfield member. We try to
2943 // use a simple disambiguation scheme first to catch the common cases
2944 // (integer literal, sizeof); if it's still ambiguous, we then consider
2945 // anything that's a simple-type-specifier followed by '(' as an
2946 // expression. This suffices because function types are not valid
2947 // underlying types anyway.
2948 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2949 // If the next token starts an expression, we know we're parsing a
2950 // bit-field. This is the common case.
2951 if (TPR == TPResult::True())
2952 PossibleBitfield = true;
2953 // If the next token starts a type-specifier-seq, it may be either a
2954 // a fixed underlying type or the start of a function-style cast in C++;
2955 // lookahead one more token to see if it's obvious that we have a
2956 // fixed underlying type.
2957 else if (TPR == TPResult::False() &&
2958 GetLookAheadToken(2).getKind() == tok::semi) {
2959 // Consume the ':'.
2960 ConsumeToken();
2961 } else {
2962 // We have the start of a type-specifier-seq, so we have to perform
2963 // tentative parsing to determine whether we have an expression or a
2964 // type.
2965 TentativeParsingAction TPA(*this);
2966
2967 // Consume the ':'.
2968 ConsumeToken();
2969
Douglas Gregora1aec292011-02-22 20:32:04 +00002970 if ((getLang().CPlusPlus &&
2971 isCXXDeclarationSpecifier() != TPResult::True()) ||
2972 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002973 // We'll parse this as a bitfield later.
2974 PossibleBitfield = true;
2975 TPA.Revert();
2976 } else {
2977 // We have a type-specifier-seq.
2978 TPA.Commit();
2979 }
2980 }
2981 } else {
2982 // Consume the ':'.
2983 ConsumeToken();
2984 }
2985
2986 if (!PossibleBitfield) {
2987 SourceRange Range;
2988 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002989
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002990 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00002991 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2992 << Range;
Richard Smith5d164bc2011-10-15 05:09:34 +00002993 if (getLang().CPlusPlus0x)
2994 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00002995 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002996 }
2997
Richard Smith0f8ee222012-01-10 01:33:14 +00002998 // There are four options here. If we have 'friend enum foo;' then this is a
2999 // friend declaration, and cannot have an accompanying definition. If we have
3000 // 'enum foo;', then this is a forward declaration. If we have
3001 // 'enum foo {...' then this is a definition. Otherwise we have something
3002 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003003 //
3004 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3005 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3006 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3007 //
John McCallfaf5fb42010-08-26 23:41:50 +00003008 Sema::TagUseKind TUK;
Richard Smith0f8ee222012-01-10 01:33:14 +00003009 if (DS.isFriendSpecified())
3010 TUK = Sema::TUK_Friend;
3011 else if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00003012 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003013 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00003014 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003015 else
John McCallfaf5fb42010-08-26 23:41:50 +00003016 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003017
3018 // enums cannot be templates, although they can be referenced from a
3019 // template.
3020 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003021 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003022 Diag(Tok, diag::err_enum_template);
3023
3024 // Skip the rest of this declarator, up until the comma or semicolon.
3025 SkipUntil(tok::comma, true);
3026 return;
3027 }
3028
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003029 if (!Name && TUK != Sema::TUK_Definition) {
3030 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3031
3032 // Skip the rest of this declarator, up until the comma or semicolon.
3033 SkipUntil(tok::comma, true);
3034 return;
3035 }
3036
Douglas Gregord6ab8742009-05-28 23:31:59 +00003037 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003038 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003039 const char *PrevSpec = 0;
3040 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003041 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003042 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregor2820e692011-09-09 19:05:14 +00003043 AS, DS.getModulePrivateSpecLoc(),
John McCallfaf5fb42010-08-26 23:41:50 +00003044 MultiTemplateParamsArg(Actions),
Richard Smith0f8ee222012-01-10 01:33:14 +00003045 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003046 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003047
Douglas Gregorba41d012010-04-24 16:38:41 +00003048 if (IsDependent) {
3049 // This enum has a dependent nested-name-specifier. Handle it as a
3050 // dependent tag.
3051 if (!Name) {
3052 DS.SetTypeSpecError();
3053 Diag(Tok, diag::err_expected_type_name_after_typename);
3054 return;
3055 }
3056
Douglas Gregor0be31a22010-07-02 17:43:08 +00003057 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00003058 TUK, SS, Name, StartLoc,
3059 NameLoc);
3060 if (Type.isInvalid()) {
3061 DS.SetTypeSpecError();
3062 return;
3063 }
3064
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003065 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3066 NameLoc.isValid() ? NameLoc : StartLoc,
3067 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003068 Diag(StartLoc, DiagID) << PrevSpec;
3069
3070 return;
3071 }
Mike Stump11289f42009-09-09 15:08:12 +00003072
John McCall48871652010-08-21 09:40:31 +00003073 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003074 // The action failed to produce an enumeration tag. If this is a
3075 // definition, consume the entire definition.
3076 if (Tok.is(tok::l_brace)) {
3077 ConsumeBrace();
3078 SkipUntil(tok::r_brace);
3079 }
3080
3081 DS.SetTypeSpecError();
3082 return;
3083 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003084
3085 if (Tok.is(tok::l_brace)) {
3086 if (TUK == Sema::TUK_Friend)
3087 Diag(Tok, diag::err_friend_decl_defines_type)
3088 << SourceRange(DS.getFriendSpecLoc());
Chris Lattnerc1915e22007-01-25 07:29:02 +00003089 ParseEnumBody(StartLoc, TagDecl);
Richard Smith0f8ee222012-01-10 01:33:14 +00003090 }
Mike Stump11289f42009-09-09 15:08:12 +00003091
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003092 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3093 NameLoc.isValid() ? NameLoc : StartLoc,
3094 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003095 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003096}
3097
Chris Lattnerc1915e22007-01-25 07:29:02 +00003098/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3099/// enumerator-list:
3100/// enumerator
3101/// enumerator-list ',' enumerator
3102/// enumerator:
3103/// enumeration-constant
3104/// enumeration-constant '=' constant-expression
3105/// enumeration-constant:
3106/// identifier
3107///
John McCall48871652010-08-21 09:40:31 +00003108void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003109 // Enter the scope of the enum body and start the definition.
3110 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003111 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003112
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003113 BalancedDelimiterTracker T(*this, tok::l_brace);
3114 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003115
Chris Lattner37256fb2007-08-27 17:24:30 +00003116 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00003117 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003118 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003119
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003120 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003121
John McCall48871652010-08-21 09:40:31 +00003122 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003123
Chris Lattnerc1915e22007-01-25 07:29:02 +00003124 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003125 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003126 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3127 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003128
John McCall811a0f52010-10-22 23:36:17 +00003129 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003130 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003131 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003132
Chris Lattnerc1915e22007-01-25 07:29:02 +00003133 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003134 ExprResult AssignedVal;
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003135 ParsingDeclRAIIObject PD(*this);
3136
Chris Lattner76c72282007-10-09 17:33:22 +00003137 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003138 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003139 AssignedVal = ParseConstantExpression();
3140 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003141 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003142 }
Mike Stump11289f42009-09-09 15:08:12 +00003143
Chris Lattnerc1915e22007-01-25 07:29:02 +00003144 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003145 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3146 LastEnumConstDecl,
3147 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003148 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003149 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003150 PD.complete(EnumConstDecl);
3151
Chris Lattner4ef40012007-06-11 01:28:17 +00003152 EnumConstantDecls.push_back(EnumConstDecl);
3153 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003154
Douglas Gregorce66d022010-09-07 14:51:08 +00003155 if (Tok.is(tok::identifier)) {
3156 // We're missing a comma between enumerators.
3157 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3158 Diag(Loc, diag::err_enumerator_list_missing_comma)
3159 << FixItHint::CreateInsertion(Loc, ", ");
3160 continue;
3161 }
3162
Chris Lattner76c72282007-10-09 17:33:22 +00003163 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003164 break;
3165 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003166
Richard Smith5d164bc2011-10-15 05:09:34 +00003167 if (Tok.isNot(tok::identifier)) {
3168 if (!getLang().C99 && !getLang().CPlusPlus0x)
3169 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3170 << getLang().CPlusPlus
3171 << FixItHint::CreateRemoval(CommaLoc);
3172 else if (getLang().CPlusPlus0x)
3173 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3174 << FixItHint::CreateRemoval(CommaLoc);
3175 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003176 }
Mike Stump11289f42009-09-09 15:08:12 +00003177
Chris Lattnerc1915e22007-01-25 07:29:02 +00003178 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003179 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003180
Chris Lattnerc1915e22007-01-25 07:29:02 +00003181 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003182 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003183 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003184
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003185 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3186 EnumDecl, EnumConstantDecls.data(),
3187 EnumConstantDecls.size(), getCurScope(),
3188 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003189
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003190 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003191 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3192 T.getCloseLocation());
Chris Lattnerc1915e22007-01-25 07:29:02 +00003193}
Chris Lattner3b561a32006-08-13 00:12:11 +00003194
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003195/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003196/// start of a type-qualifier-list.
3197bool Parser::isTypeQualifier() const {
3198 switch (Tok.getKind()) {
3199 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003200
3201 // type-qualifier only in OpenCL
3202 case tok::kw_private:
3203 return getLang().OpenCL;
3204
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003205 // type-qualifier
3206 case tok::kw_const:
3207 case tok::kw_volatile:
3208 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003209 case tok::kw___private:
3210 case tok::kw___local:
3211 case tok::kw___global:
3212 case tok::kw___constant:
3213 case tok::kw___read_only:
3214 case tok::kw___read_write:
3215 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003216 return true;
3217 }
3218}
3219
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003220/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3221/// is definitely a type-specifier. Return false if it isn't part of a type
3222/// specifier or if we're not sure.
3223bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3224 switch (Tok.getKind()) {
3225 default: return false;
3226 // type-specifiers
3227 case tok::kw_short:
3228 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003229 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003230 case tok::kw_signed:
3231 case tok::kw_unsigned:
3232 case tok::kw__Complex:
3233 case tok::kw__Imaginary:
3234 case tok::kw_void:
3235 case tok::kw_char:
3236 case tok::kw_wchar_t:
3237 case tok::kw_char16_t:
3238 case tok::kw_char32_t:
3239 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003240 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003241 case tok::kw_float:
3242 case tok::kw_double:
3243 case tok::kw_bool:
3244 case tok::kw__Bool:
3245 case tok::kw__Decimal32:
3246 case tok::kw__Decimal64:
3247 case tok::kw__Decimal128:
3248 case tok::kw___vector:
3249
3250 // struct-or-union-specifier (C99) or class-specifier (C++)
3251 case tok::kw_class:
3252 case tok::kw_struct:
3253 case tok::kw_union:
3254 // enum-specifier
3255 case tok::kw_enum:
3256
3257 // typedef-name
3258 case tok::annot_typename:
3259 return true;
3260 }
3261}
3262
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003263/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003264/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003265bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003266 switch (Tok.getKind()) {
3267 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003268
Chris Lattner020bab92009-01-04 23:41:41 +00003269 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003270 if (TryAltiVecVectorToken())
3271 return true;
3272 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003273 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003274 // Annotate typenames and C++ scope specifiers. If we get one, just
3275 // recurse to handle whatever we get.
3276 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003277 return true;
3278 if (Tok.is(tok::identifier))
3279 return false;
3280 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003281
Chris Lattner020bab92009-01-04 23:41:41 +00003282 case tok::coloncolon: // ::foo::bar
3283 if (NextToken().is(tok::kw_new) || // ::new
3284 NextToken().is(tok::kw_delete)) // ::delete
3285 return false;
3286
Chris Lattner020bab92009-01-04 23:41:41 +00003287 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003288 return true;
3289 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003290
Chris Lattnere37e2332006-08-15 04:50:22 +00003291 // GNU attributes support.
3292 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003293 // GNU typeof support.
3294 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003295
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003296 // type-specifiers
3297 case tok::kw_short:
3298 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003299 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003300 case tok::kw_signed:
3301 case tok::kw_unsigned:
3302 case tok::kw__Complex:
3303 case tok::kw__Imaginary:
3304 case tok::kw_void:
3305 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003306 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003307 case tok::kw_char16_t:
3308 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003309 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003310 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003311 case tok::kw_float:
3312 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003313 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003314 case tok::kw__Bool:
3315 case tok::kw__Decimal32:
3316 case tok::kw__Decimal64:
3317 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003318 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003319
Chris Lattner861a2262008-04-13 18:59:07 +00003320 // struct-or-union-specifier (C99) or class-specifier (C++)
3321 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003322 case tok::kw_struct:
3323 case tok::kw_union:
3324 // enum-specifier
3325 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003326
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003327 // type-qualifier
3328 case tok::kw_const:
3329 case tok::kw_volatile:
3330 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003331
3332 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003333 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003334 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003335
Chris Lattner409bf7d2008-10-20 00:25:30 +00003336 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3337 case tok::less:
3338 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003339
Steve Naroff44ac7772008-12-25 14:16:32 +00003340 case tok::kw___cdecl:
3341 case tok::kw___stdcall:
3342 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003343 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003344 case tok::kw___w64:
3345 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003346 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003347 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003348 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003349
3350 case tok::kw___private:
3351 case tok::kw___local:
3352 case tok::kw___global:
3353 case tok::kw___constant:
3354 case tok::kw___read_only:
3355 case tok::kw___read_write:
3356 case tok::kw___write_only:
3357
Eli Friedman53339e02009-06-08 23:27:34 +00003358 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003359
3360 case tok::kw_private:
3361 return getLang().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003362
Benjamin Kramere56f3932011-12-23 17:00:35 +00003363 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003364 case tok::kw__Atomic:
3365 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003366 }
3367}
3368
Chris Lattneracd58a32006-08-06 17:24:14 +00003369/// isDeclarationSpecifier() - Return true if the current token is part of a
3370/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003371///
3372/// \param DisambiguatingWithExpression True to indicate that the purpose of
3373/// this check is to disambiguate between an expression and a declaration.
3374bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003375 switch (Tok.getKind()) {
3376 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003377
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003378 case tok::kw_private:
3379 return getLang().OpenCL;
3380
Chris Lattner020bab92009-01-04 23:41:41 +00003381 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003382 // Unfortunate hack to support "Class.factoryMethod" notation.
3383 if (getLang().ObjC1 && NextToken().is(tok::period))
3384 return false;
John Thompson22334602010-02-05 00:12:22 +00003385 if (TryAltiVecVectorToken())
3386 return true;
3387 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003388 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003389 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003390 // Annotate typenames and C++ scope specifiers. If we get one, just
3391 // recurse to handle whatever we get.
3392 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003393 return true;
3394 if (Tok.is(tok::identifier))
3395 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003396
3397 // If we're in Objective-C and we have an Objective-C class type followed
3398 // by an identifier and then either ':' or ']', in a place where an
3399 // expression is permitted, then this is probably a class message send
3400 // missing the initial '['. In this case, we won't consider this to be
3401 // the start of a declaration.
3402 if (DisambiguatingWithExpression &&
3403 isStartOfObjCClassMessageMissingOpenBracket())
3404 return false;
3405
John McCall1f476a12010-02-26 08:45:28 +00003406 return isDeclarationSpecifier();
3407
Chris Lattner020bab92009-01-04 23:41:41 +00003408 case tok::coloncolon: // ::foo::bar
3409 if (NextToken().is(tok::kw_new) || // ::new
3410 NextToken().is(tok::kw_delete)) // ::delete
3411 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003412
Chris Lattner020bab92009-01-04 23:41:41 +00003413 // Annotate typenames and C++ scope specifiers. If we get one, just
3414 // recurse to handle whatever we get.
3415 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003416 return true;
3417 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003418
Chris Lattneracd58a32006-08-06 17:24:14 +00003419 // storage-class-specifier
3420 case tok::kw_typedef:
3421 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003422 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003423 case tok::kw_static:
3424 case tok::kw_auto:
3425 case tok::kw_register:
3426 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003427
Douglas Gregor26701a42011-09-09 02:06:17 +00003428 // Modules
3429 case tok::kw___module_private__:
3430
Chris Lattneracd58a32006-08-06 17:24:14 +00003431 // type-specifiers
3432 case tok::kw_short:
3433 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003434 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003435 case tok::kw_signed:
3436 case tok::kw_unsigned:
3437 case tok::kw__Complex:
3438 case tok::kw__Imaginary:
3439 case tok::kw_void:
3440 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003441 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003442 case tok::kw_char16_t:
3443 case tok::kw_char32_t:
3444
Chris Lattneracd58a32006-08-06 17:24:14 +00003445 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003446 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003447 case tok::kw_float:
3448 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003449 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003450 case tok::kw__Bool:
3451 case tok::kw__Decimal32:
3452 case tok::kw__Decimal64:
3453 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003454 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003455
Chris Lattner861a2262008-04-13 18:59:07 +00003456 // struct-or-union-specifier (C99) or class-specifier (C++)
3457 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003458 case tok::kw_struct:
3459 case tok::kw_union:
3460 // enum-specifier
3461 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003462
Chris Lattneracd58a32006-08-06 17:24:14 +00003463 // type-qualifier
3464 case tok::kw_const:
3465 case tok::kw_volatile:
3466 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003467
Chris Lattneracd58a32006-08-06 17:24:14 +00003468 // function-specifier
3469 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003470 case tok::kw_virtual:
3471 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003472
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003473 // static_assert-declaration
3474 case tok::kw__Static_assert:
3475
Chris Lattner599e47e2007-08-09 17:01:07 +00003476 // GNU typeof support.
3477 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003478
Chris Lattner599e47e2007-08-09 17:01:07 +00003479 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003480 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003481 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003482
Francois Pichete878cb62011-06-19 08:02:06 +00003483 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003484 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003485 return true;
3486
Benjamin Kramere56f3932011-12-23 17:00:35 +00003487 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003488 case tok::kw__Atomic:
3489 return true;
3490
Chris Lattner8b2ec162008-07-26 03:38:44 +00003491 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3492 case tok::less:
3493 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003494
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003495 // typedef-name
3496 case tok::annot_typename:
3497 return !DisambiguatingWithExpression ||
3498 !isStartOfObjCClassMessageMissingOpenBracket();
3499
Steve Narofff192fab2009-01-06 19:34:12 +00003500 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003501 case tok::kw___cdecl:
3502 case tok::kw___stdcall:
3503 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003504 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003505 case tok::kw___w64:
3506 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003507 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003508 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003509 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003510 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003511
3512 case tok::kw___private:
3513 case tok::kw___local:
3514 case tok::kw___global:
3515 case tok::kw___constant:
3516 case tok::kw___read_only:
3517 case tok::kw___read_write:
3518 case tok::kw___write_only:
3519
Eli Friedman53339e02009-06-08 23:27:34 +00003520 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003521 }
3522}
3523
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003524bool Parser::isConstructorDeclarator() {
3525 TentativeParsingAction TPA(*this);
3526
3527 // Parse the C++ scope specifier.
3528 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003529 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3530 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003531 TPA.Revert();
3532 return false;
3533 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003534
3535 // Parse the constructor name.
3536 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3537 // We already know that we have a constructor name; just consume
3538 // the token.
3539 ConsumeToken();
3540 } else {
3541 TPA.Revert();
3542 return false;
3543 }
3544
3545 // Current class name must be followed by a left parentheses.
3546 if (Tok.isNot(tok::l_paren)) {
3547 TPA.Revert();
3548 return false;
3549 }
3550 ConsumeParen();
3551
3552 // A right parentheses or ellipsis signals that we have a constructor.
3553 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3554 TPA.Revert();
3555 return true;
3556 }
3557
3558 // If we need to, enter the specified scope.
3559 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003560 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003561 DeclScopeObj.EnterDeclaratorScope();
3562
Francois Pichet79f3a872011-01-31 04:54:32 +00003563 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003564 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003565 MaybeParseMicrosoftAttributes(Attrs);
3566
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003567 // Check whether the next token(s) are part of a declaration
3568 // specifier, in which case we have the start of a parameter and,
3569 // therefore, we know that this is a constructor.
3570 bool IsConstructor = isDeclarationSpecifier();
3571 TPA.Revert();
3572 return IsConstructor;
3573}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003574
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003575/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003576/// type-qualifier-list: [C99 6.7.5]
3577/// type-qualifier
3578/// [vendor] attributes
3579/// [ only if VendorAttributesAllowed=true ]
3580/// type-qualifier-list type-qualifier
3581/// [vendor] type-qualifier-list attributes
3582/// [ only if VendorAttributesAllowed=true ]
3583/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3584/// [ only if CXX0XAttributesAllowed=true ]
3585/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003586///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003587void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3588 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003589 bool CXX0XAttributesAllowed) {
3590 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3591 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003592 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003593 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003594 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003595 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003596 else
3597 Diag(Loc, diag::err_attributes_not_allowed);
3598 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003599
3600 SourceLocation EndLoc;
3601
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003602 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003603 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003604 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003605 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003606 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003607
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003608 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003609 case tok::code_completion:
3610 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003611 return cutOffParsing();
Douglas Gregor28c78432010-08-27 17:35:51 +00003612
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003613 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003614 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3615 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003616 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003617 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003618 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3619 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003620 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003621 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003622 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3623 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003624 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003625
3626 // OpenCL qualifiers:
3627 case tok::kw_private:
3628 if (!getLang().OpenCL)
3629 goto DoneWithTypeQuals;
3630 case tok::kw___private:
3631 case tok::kw___global:
3632 case tok::kw___local:
3633 case tok::kw___constant:
3634 case tok::kw___read_only:
3635 case tok::kw___write_only:
3636 case tok::kw___read_write:
3637 ParseOpenCLQualifiers(DS);
3638 break;
3639
Eli Friedman53339e02009-06-08 23:27:34 +00003640 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003641 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003642 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003643 case tok::kw___cdecl:
3644 case tok::kw___stdcall:
3645 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003646 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003647 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003648 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003649 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003650 continue;
3651 }
3652 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003653 case tok::kw___pascal:
3654 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003655 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003656 continue;
3657 }
3658 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003659 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003660 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003661 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003662 continue; // do *not* consume the next token!
3663 }
3664 // otherwise, FALL THROUGH!
3665 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003666 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003667 // If this is not a type-qualifier token, we're done reading type
3668 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003669 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003670 if (EndLoc.isValid())
3671 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003672 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003673 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003674
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003675 // If the specifier combination wasn't legal, issue a diagnostic.
3676 if (isInvalid) {
3677 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003678 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003679 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003680 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003681 }
3682}
3683
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003684
3685/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3686///
3687void Parser::ParseDeclarator(Declarator &D) {
3688 /// This implements the 'declarator' production in the C grammar, then checks
3689 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003690 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003691}
3692
Sebastian Redlbd150f42008-11-21 19:14:01 +00003693/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3694/// is parsed by the function passed to it. Pass null, and the direct-declarator
3695/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003696/// ptr-operator production.
3697///
Richard Smith09f76ee2011-10-19 21:33:05 +00003698/// If the grammar of this construct is extended, matching changes must also be
3699/// made to TryParseDeclarator and MightBeDeclarator.
3700///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003701/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3702/// [C] pointer[opt] direct-declarator
3703/// [C++] direct-declarator
3704/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003705///
3706/// pointer: [C99 6.7.5]
3707/// '*' type-qualifier-list[opt]
3708/// '*' type-qualifier-list[opt] pointer
3709///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003710/// ptr-operator:
3711/// '*' cv-qualifier-seq[opt]
3712/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003713/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003714/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003715/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003716/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003717void Parser::ParseDeclaratorInternal(Declarator &D,
3718 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003719 if (Diags.hasAllExtensionsSilenced())
3720 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003721
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003722 // C++ member pointers start with a '::' or a nested-name.
3723 // Member pointers get special handling, since there's no place for the
3724 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003725 if (getLang().CPlusPlus &&
3726 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3727 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00003728 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3729 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003730 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003731 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00003732
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003733 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003734 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003735 // The scope spec really belongs to the direct-declarator.
3736 D.getCXXScopeSpec() = SS;
3737 if (DirectDeclParser)
3738 (this->*DirectDeclParser)(D);
3739 return;
3740 }
3741
3742 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003743 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003744 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003745 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003746 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003747
3748 // Recurse to parse whatever is left.
3749 ParseDeclaratorInternal(D, DirectDeclParser);
3750
3751 // Sema will have to catch (syntactically invalid) pointers into global
3752 // scope. It has to catch pointers into namespace scope anyway.
3753 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003754 Loc),
3755 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003756 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003757 return;
3758 }
3759 }
3760
3761 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003762 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003763 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003764 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003765 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003766 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003767 if (DirectDeclParser)
3768 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003769 return;
3770 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003771
Sebastian Redled0f3b02009-03-15 22:02:01 +00003772 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3773 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003774 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003775 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003776
Chris Lattner9eac9312009-03-27 04:18:06 +00003777 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003778 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003779 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003780
Bill Wendling3708c182007-05-27 10:15:43 +00003781 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003782 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003783
Bill Wendling3708c182007-05-27 10:15:43 +00003784 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003785 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003786 if (Kind == tok::star)
3787 // Remember that we parsed a pointer type, and remember the type-quals.
3788 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003789 DS.getConstSpecLoc(),
3790 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003791 DS.getRestrictSpecLoc()),
3792 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003793 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003794 else
3795 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003796 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003797 Loc),
3798 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003799 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003800 } else {
3801 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003802 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003803
Sebastian Redl3b27be62009-03-23 00:00:23 +00003804 // Complain about rvalue references in C++03, but then go on and build
3805 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00003806 if (Kind == tok::ampamp)
3807 Diag(Loc, getLang().CPlusPlus0x ?
3808 diag::warn_cxx98_compat_rvalue_reference :
3809 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003810
Bill Wendling93efb222007-06-02 23:28:54 +00003811 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3812 // cv-qualifiers are introduced through the use of a typedef or of a
3813 // template type argument, in which case the cv-qualifiers are ignored.
3814 //
3815 // [GNU] Retricted references are allowed.
3816 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003817 // [C++0x] Attributes on references are not allowed.
3818 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003819 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003820
3821 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3822 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3823 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003824 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003825 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3826 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003827 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003828 }
Bill Wendling3708c182007-05-27 10:15:43 +00003829
3830 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003831 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003832
Douglas Gregor66583c52008-11-03 15:51:28 +00003833 if (D.getNumTypeObjects() > 0) {
3834 // C++ [dcl.ref]p4: There shall be no references to references.
3835 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3836 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003837 if (const IdentifierInfo *II = D.getIdentifier())
3838 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3839 << II;
3840 else
3841 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3842 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003843
Sebastian Redlbd150f42008-11-21 19:14:01 +00003844 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003845 // can go ahead and build the (technically ill-formed)
3846 // declarator: reference collapsing will take care of it.
3847 }
3848 }
3849
Bill Wendling3708c182007-05-27 10:15:43 +00003850 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003851 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003852 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003853 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003854 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003855 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003856}
3857
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003858/// ParseDirectDeclarator
3859/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003860/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003861/// '(' declarator ')'
3862/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003863/// [C90] direct-declarator '[' constant-expression[opt] ']'
3864/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3865/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3866/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3867/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003868/// direct-declarator '(' parameter-type-list ')'
3869/// direct-declarator '(' identifier-list[opt] ')'
3870/// [GNU] direct-declarator '(' parameter-forward-declarations
3871/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003872/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3873/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003874/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003875///
3876/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003877/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003878/// '::'[opt] nested-name-specifier[opt] type-name
3879///
3880/// id-expression: [C++ 5.1]
3881/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003882/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003883///
3884/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003885/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003886/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003887/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003888/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003889/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003890///
Chris Lattneracd58a32006-08-06 17:24:14 +00003891void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003892 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003893
Douglas Gregor7861a802009-11-03 01:35:08 +00003894 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3895 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003896 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00003897 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3898 D.getContext() == Declarator::MemberContext;
3899 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3900 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00003901 }
3902
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003903 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003904 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003905 // Change the declaration context for name lookup, until this function
3906 // is exited (and the declarator has been parsed).
3907 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003908 }
3909
Douglas Gregor27b4c162010-12-23 22:44:42 +00003910 // C++0x [dcl.fct]p14:
3911 // There is a syntactic ambiguity when an ellipsis occurs at the end
3912 // of a parameter-declaration-clause without a preceding comma. In
3913 // this case, the ellipsis is parsed as part of the
3914 // abstract-declarator if the type of the parameter names a template
3915 // parameter pack that has not been expanded; otherwise, it is parsed
3916 // as part of the parameter-declaration-clause.
3917 if (Tok.is(tok::ellipsis) &&
3918 !((D.getContext() == Declarator::PrototypeContext ||
3919 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003920 NextToken().is(tok::r_paren) &&
3921 !Actions.containsUnexpandedParameterPacks(D)))
3922 D.setEllipsisLoc(ConsumeToken());
3923
Douglas Gregor7861a802009-11-03 01:35:08 +00003924 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3925 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3926 // We found something that indicates the start of an unqualified-id.
3927 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003928 bool AllowConstructorName;
3929 if (D.getDeclSpec().hasTypeSpecifier())
3930 AllowConstructorName = false;
3931 else if (D.getCXXScopeSpec().isSet())
3932 AllowConstructorName =
3933 (D.getContext() == Declarator::FileContext ||
3934 (D.getContext() == Declarator::MemberContext &&
3935 D.getDeclSpec().isFriendSpecified()));
3936 else
3937 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3938
Douglas Gregor7861a802009-11-03 01:35:08 +00003939 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3940 /*EnteringContext=*/true,
3941 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003942 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003943 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003944 D.getName()) ||
3945 // Once we're past the identifier, if the scope was bad, mark the
3946 // whole declarator bad.
3947 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003948 D.SetIdentifier(0, Tok.getLocation());
3949 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003950 } else {
3951 // Parsed the unqualified-id; update range information and move along.
3952 if (D.getSourceRange().getBegin().isInvalid())
3953 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3954 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003955 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003956 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003957 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003958 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003959 assert(!getLang().CPlusPlus &&
3960 "There's a C++-specific check for tok::identifier above");
3961 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3962 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3963 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003964 goto PastIdentifier;
3965 }
3966
3967 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003968 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003969 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003970 // Example: 'char (*X)' or 'int (*XX)(void)'
3971 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003972
3973 // If the declarator was parenthesized, we entered the declarator
3974 // scope when parsing the parenthesized declarator, then exited
3975 // the scope already. Re-enter the scope, if we need to.
3976 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003977 // If there was an error parsing parenthesized declarator, declarator
3978 // scope may have been enterred before. Don't do it again.
3979 if (!D.isInvalidType() &&
3980 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003981 // Change the declaration context for name lookup, until this function
3982 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003983 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003984 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003985 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003986 // This could be something simple like "int" (in which case the declarator
3987 // portion is empty), if an abstract-declarator is allowed.
3988 D.SetIdentifier(0, Tok.getLocation());
3989 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003990 if (D.getContext() == Declarator::MemberContext)
3991 Diag(Tok, diag::err_expected_member_name_or_semi)
3992 << D.getDeclSpec().getSourceRange();
3993 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003994 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003995 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003996 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003997 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003998 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003999 }
Mike Stump11289f42009-09-09 15:08:12 +00004000
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004001 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004002 assert(D.isPastIdentifier() &&
4003 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004004
Alexis Hunt96d5c762009-11-21 08:43:09 +00004005 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00004006 if (D.getIdentifier())
4007 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004008
Chris Lattneracd58a32006-08-06 17:24:14 +00004009 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004010 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004011 // Enter function-declaration scope, limiting any declarators to the
4012 // function prototype scope, including parameter declarators.
4013 ParseScope PrototypeScope(this,
4014 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004015 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4016 // In such a case, check if we actually have a function declarator; if it
4017 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004018 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4019 // When not in file scope, warn for ambiguous function declarators, just
4020 // in case the author intended it as a variable definition.
4021 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4022 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4023 break;
4024 }
John McCall084e83d2011-03-24 11:26:52 +00004025 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004026 BalancedDelimiterTracker T(*this, tok::l_paren);
4027 T.consumeOpen();
4028 ParseFunctionDeclarator(D, attrs, T);
David Blaikie15a430a2011-12-04 05:04:18 +00004029 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004030 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004031 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004032 } else {
4033 break;
4034 }
4035 }
David Blaikie15a430a2011-12-04 05:04:18 +00004036}
Chris Lattneracd58a32006-08-06 17:24:14 +00004037
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004038/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4039/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004040/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004041/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4042///
4043/// direct-declarator:
4044/// '(' declarator ')'
4045/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004046/// direct-declarator '(' parameter-type-list ')'
4047/// direct-declarator '(' identifier-list[opt] ')'
4048/// [GNU] direct-declarator '(' parameter-forward-declarations
4049/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004050///
4051void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004052 BalancedDelimiterTracker T(*this, tok::l_paren);
4053 T.consumeOpen();
4054
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004055 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004056
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004057 // Eat any attributes before we look at whether this is a grouping or function
4058 // declarator paren. If this is a grouping paren, the attribute applies to
4059 // the type being built up, for example:
4060 // int (__attribute__(()) *x)(long y)
4061 // If this ends up not being a grouping paren, the attribute applies to the
4062 // first argument, for example:
4063 // int (__attribute__(()) int x)
4064 // In either case, we need to eat any attributes to be able to determine what
4065 // sort of paren this is.
4066 //
John McCall084e83d2011-03-24 11:26:52 +00004067 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004068 bool RequiresArg = false;
4069 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004070 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004071
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004072 // We require that the argument list (if this is a non-grouping paren) be
4073 // present even if the attribute list was empty.
4074 RequiresArg = true;
4075 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004076 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004077 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004078 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004079 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004080 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004081 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004082 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004083 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004084 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004085 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004086
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004087 // If we haven't past the identifier yet (or where the identifier would be
4088 // stored, if this is an abstract declarator), then this is probably just
4089 // grouping parens. However, if this could be an abstract-declarator, then
4090 // this could also be the start of function arguments (consider 'void()').
4091 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004092
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004093 if (!D.mayOmitIdentifier()) {
4094 // If this can't be an abstract-declarator, this *must* be a grouping
4095 // paren, because we haven't seen the identifier yet.
4096 isGrouping = true;
4097 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00004098 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004099 isDeclarationSpecifier()) { // 'int(int)' is a function.
4100 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4101 // considered to be a type, not a K&R identifier-list.
4102 isGrouping = false;
4103 } else {
4104 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4105 isGrouping = true;
4106 }
Mike Stump11289f42009-09-09 15:08:12 +00004107
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004108 // If this is a grouping paren, handle:
4109 // direct-declarator: '(' declarator ')'
4110 // direct-declarator: '(' attributes declarator ')'
4111 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004112 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004113 D.setGroupingParens(true);
4114
Sebastian Redlbd150f42008-11-21 19:14:01 +00004115 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004116 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004117 T.consumeClose();
4118 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4119 T.getCloseLocation()),
4120 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004121
4122 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004123 return;
4124 }
Mike Stump11289f42009-09-09 15:08:12 +00004125
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004126 // Okay, if this wasn't a grouping paren, it must be the start of a function
4127 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004128 // identifier (and remember where it would have been), then call into
4129 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004130 D.SetIdentifier(0, Tok.getLocation());
4131
David Blaikie15a430a2011-12-04 05:04:18 +00004132 // Enter function-declaration scope, limiting any declarators to the
4133 // function prototype scope, including parameter declarators.
4134 ParseScope PrototypeScope(this,
4135 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004136 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004137 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004138}
4139
4140/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4141/// declarator D up to a paren, which indicates that we are parsing function
4142/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004143///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004144/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004145/// after the open paren - they should be considered to be the first argument of
4146/// a parameter. If RequiresArg is true, then the first argument of the
4147/// function is required to be present and required to not be an identifier
4148/// list.
4149///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004150/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4151/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4152/// (C++0x) trailing-return-type[opt].
4153///
4154/// [C++0x] exception-specification:
4155/// dynamic-exception-specification
4156/// noexcept-specification
4157///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004158void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004159 ParsedAttributes &attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004160 BalancedDelimiterTracker &Tracker,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004161 bool RequiresArg) {
David Blaikie15a430a2011-12-04 05:04:18 +00004162 assert(getCurScope()->isFunctionPrototypeScope() &&
4163 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004164 // lparen is already consumed!
4165 assert(D.isPastIdentifier() && "Should not call before identifier!");
4166
4167 // This should be true when the function has typed arguments.
4168 // Otherwise, it is treated as a K&R-style function.
4169 bool HasProto = false;
4170 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004171 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004172 // Remember where we see an ellipsis, if any.
4173 SourceLocation EllipsisLoc;
4174
4175 DeclSpec DS(AttrFactory);
4176 bool RefQualifierIsLValueRef = true;
4177 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004178 SourceLocation ConstQualifierLoc;
4179 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004180 ExceptionSpecificationType ESpecType = EST_None;
4181 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004182 SmallVector<ParsedType, 2> DynamicExceptions;
4183 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004184 ExprResult NoexceptExpr;
4185 ParsedType TrailingReturnType;
4186
4187 SourceLocation EndLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004188 if (isFunctionDeclaratorIdentifierList()) {
4189 if (RequiresArg)
4190 Diag(Tok, diag::err_argument_required_after_attribute);
4191
4192 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4193
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004194 Tracker.consumeClose();
4195 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004196 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004197 if (Tok.isNot(tok::r_paren))
4198 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4199 else if (RequiresArg)
4200 Diag(Tok, diag::err_argument_required_after_attribute);
4201
4202 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4203
4204 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004205 Tracker.consumeClose();
4206 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004207
4208 if (getLang().CPlusPlus) {
4209 MaybeParseCXX0XAttributes(attrs);
4210
4211 // Parse cv-qualifier-seq[opt].
4212 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregore248eea2011-10-19 06:04:55 +00004213 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004214 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregore248eea2011-10-19 06:04:55 +00004215 ConstQualifierLoc = DS.getConstSpecLoc();
4216 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4217 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004218
4219 // Parse ref-qualifier[opt].
4220 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004221 Diag(Tok, getLang().CPlusPlus0x ?
4222 diag::warn_cxx98_compat_ref_qualifier :
4223 diag::ext_ref_qualifier);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004224
4225 RefQualifierIsLValueRef = Tok.is(tok::amp);
4226 RefQualifierLoc = ConsumeToken();
4227 EndLoc = RefQualifierLoc;
4228 }
4229
4230 // Parse exception-specification[opt].
4231 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4232 DynamicExceptions,
4233 DynamicExceptionRanges,
4234 NoexceptExpr);
4235 if (ESpecType != EST_None)
4236 EndLoc = ESpecRange.getEnd();
4237
4238 // Parse trailing-return-type[opt].
4239 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004240 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004241 SourceRange Range;
4242 TrailingReturnType = ParseTrailingReturnType(Range).get();
4243 if (Range.getEnd().isValid())
4244 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004245 }
4246 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004247 }
4248
4249 // Remember that we parsed a function type, and remember the attributes.
4250 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4251 /*isVariadic=*/EllipsisLoc.isValid(),
4252 EllipsisLoc,
4253 ParamInfo.data(), ParamInfo.size(),
4254 DS.getTypeQualifiers(),
4255 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004256 RefQualifierLoc, ConstQualifierLoc,
4257 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004258 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004259 ESpecType, ESpecRange.getBegin(),
4260 DynamicExceptions.data(),
4261 DynamicExceptionRanges.data(),
4262 DynamicExceptions.size(),
4263 NoexceptExpr.isUsable() ?
4264 NoexceptExpr.get() : 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004265 Tracker.getOpenLocation(),
4266 EndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004267 TrailingReturnType),
4268 attrs, EndLoc);
4269}
4270
4271/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4272/// identifier list form for a K&R-style function: void foo(a,b,c)
4273///
4274/// Note that identifier-lists are only allowed for normal declarators, not for
4275/// abstract-declarators.
4276bool Parser::isFunctionDeclaratorIdentifierList() {
4277 return !getLang().CPlusPlus
4278 && Tok.is(tok::identifier)
4279 && !TryAltiVecVectorToken()
4280 // K&R identifier lists can't have typedefs as identifiers, per C99
4281 // 6.7.5.3p11.
4282 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4283 // Identifier lists follow a really simple grammar: the identifiers can
4284 // be followed *only* by a ", identifier" or ")". However, K&R
4285 // identifier lists are really rare in the brave new modern world, and
4286 // it is very common for someone to typo a type in a non-K&R style
4287 // list. If we are presented with something like: "void foo(intptr x,
4288 // float y)", we don't want to start parsing the function declarator as
4289 // though it is a K&R style declarator just because intptr is an
4290 // invalid type.
4291 //
4292 // To handle this, we check to see if the token after the first
4293 // identifier is a "," or ")". Only then do we parse it as an
4294 // identifier list.
4295 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4296}
4297
4298/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4299/// we found a K&R-style identifier list instead of a typed parameter list.
4300///
4301/// After returning, ParamInfo will hold the parsed parameters.
4302///
4303/// identifier-list: [C99 6.7.5]
4304/// identifier
4305/// identifier-list ',' identifier
4306///
4307void Parser::ParseFunctionDeclaratorIdentifierList(
4308 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004309 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004310 // If there was no identifier specified for the declarator, either we are in
4311 // an abstract-declarator, or we are in a parameter declarator which was found
4312 // to be abstract. In abstract-declarators, identifier lists are not valid:
4313 // diagnose this.
4314 if (!D.getIdentifier())
4315 Diag(Tok, diag::ext_ident_list_in_param);
4316
4317 // Maintain an efficient lookup of params we have seen so far.
4318 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4319
4320 while (1) {
4321 // If this isn't an identifier, report the error and skip until ')'.
4322 if (Tok.isNot(tok::identifier)) {
4323 Diag(Tok, diag::err_expected_ident);
4324 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4325 // Forget we parsed anything.
4326 ParamInfo.clear();
4327 return;
4328 }
4329
4330 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4331
4332 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4333 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4334 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4335
4336 // Verify that the argument identifier has not already been mentioned.
4337 if (!ParamsSoFar.insert(ParmII)) {
4338 Diag(Tok, diag::err_param_redefinition) << ParmII;
4339 } else {
4340 // Remember this identifier in ParamInfo.
4341 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4342 Tok.getLocation(),
4343 0));
4344 }
4345
4346 // Eat the identifier.
4347 ConsumeToken();
4348
4349 // The list continues if we see a comma.
4350 if (Tok.isNot(tok::comma))
4351 break;
4352 ConsumeToken();
4353 }
4354}
4355
4356/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4357/// after the opening parenthesis. This function will not parse a K&R-style
4358/// identifier list.
4359///
4360/// D is the declarator being parsed. If attrs is non-null, then the caller
4361/// parsed those arguments immediately after the open paren - they should be
4362/// considered to be the first argument of a parameter.
4363///
4364/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4365/// be the location of the ellipsis, if any was parsed.
4366///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004367/// parameter-type-list: [C99 6.7.5]
4368/// parameter-list
4369/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004370/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004371///
4372/// parameter-list: [C99 6.7.5]
4373/// parameter-declaration
4374/// parameter-list ',' parameter-declaration
4375///
4376/// parameter-declaration: [C99 6.7.5]
4377/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004378/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004379/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004380/// declaration-specifiers abstract-declarator[opt]
4381/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004382/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004383/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004384///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004385void Parser::ParseParameterDeclarationClause(
4386 Declarator &D,
4387 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004388 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004389 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004390
Chris Lattner371ed4e2008-04-06 06:57:35 +00004391 while (1) {
4392 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00004393 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004394 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004395 }
Mike Stump11289f42009-09-09 15:08:12 +00004396
Chris Lattner371ed4e2008-04-06 06:57:35 +00004397 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004398 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004399 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004400
John McCall53fa7142010-12-24 02:08:15 +00004401 // Skip any Microsoft attributes before a param.
Francois Pichet0706d202011-09-17 17:15:52 +00004402 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004403 ParseMicrosoftAttributes(DS.getAttributes());
4404
4405 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004406
4407 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004408 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004409 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4410 // attributes lost? Should they even be allowed?
4411 // FIXME: If we can leave the attributes in the token stream somehow, we can
4412 // get rid of a parameter (attrs) and this statement. It might be too much
4413 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004414 DS.takeAttributesFrom(attrs);
4415
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004416 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004417
Chris Lattner371ed4e2008-04-06 06:57:35 +00004418 // Parse the declarator. This is "PrototypeContext", because we must
4419 // accept either 'declarator' or 'abstract-declarator' here.
4420 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4421 ParseDeclarator(ParmDecl);
4422
4423 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004424 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004425
Chris Lattner371ed4e2008-04-06 06:57:35 +00004426 // Remember this parsed parameter in ParamInfo.
4427 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004428
Douglas Gregor4d87df52008-12-16 21:30:33 +00004429 // DefArgToks is used when the parsing of default arguments needs
4430 // to be delayed.
4431 CachedTokens *DefArgToks = 0;
4432
Chris Lattner371ed4e2008-04-06 06:57:35 +00004433 // If no parameter was specified, verify that *something* was specified,
4434 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004435 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4436 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004437 // Completely missing, emit error.
4438 Diag(DSStart, diag::err_missing_param);
4439 } else {
4440 // Otherwise, we have something. Add it and let semantic analysis try
4441 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004442
Chris Lattner371ed4e2008-04-06 06:57:35 +00004443 // Inform the actions module about the parameter declarator, so it gets
4444 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004445 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004446
4447 // Parse the default argument, if any. We parse the default
4448 // arguments in all dialects; the semantic analysis in
4449 // ActOnParamDefaultArgument will reject the default argument in
4450 // C.
4451 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004452 SourceLocation EqualLoc = Tok.getLocation();
4453
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004454 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004455 if (D.getContext() == Declarator::MemberContext) {
4456 // If we're inside a class definition, cache the tokens
4457 // corresponding to the default argument. We'll actually parse
4458 // them when we see the end of the class definition.
4459 // FIXME: Templates will require something similar.
4460 // FIXME: Can we use a smart pointer for Toks?
4461 DefArgToks = new CachedTokens;
4462
Mike Stump11289f42009-09-09 15:08:12 +00004463 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004464 /*StopAtSemi=*/true,
4465 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004466 delete DefArgToks;
4467 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004468 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004469 } else {
4470 // Mark the end of the default argument so that we know when to
4471 // stop when we parse it later on.
4472 Token DefArgEnd;
4473 DefArgEnd.startToken();
4474 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4475 DefArgEnd.setLocation(Tok.getLocation());
4476 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004477 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004478 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004479 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004480 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004481 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004482 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004483
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004484 // The argument isn't actually potentially evaluated unless it is
4485 // used.
4486 EnterExpressionEvaluationContext Eval(Actions,
4487 Sema::PotentiallyEvaluatedIfUsed);
4488
John McCalldadc5752010-08-24 06:29:42 +00004489 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004490 if (DefArgResult.isInvalid()) {
4491 Actions.ActOnParamDefaultArgumentError(Param);
4492 SkipUntil(tok::comma, tok::r_paren, true, true);
4493 } else {
4494 // Inform the actions module about the default argument
4495 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004496 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004497 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004498 }
4499 }
Mike Stump11289f42009-09-09 15:08:12 +00004500
4501 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4502 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004503 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004504 }
4505
4506 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004507 if (Tok.isNot(tok::comma)) {
4508 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004509 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4510
4511 if (!getLang().CPlusPlus) {
4512 // We have ellipsis without a preceding ',', which is ill-formed
4513 // in C. Complain and provide the fix.
4514 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004515 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004516 }
4517 }
4518
4519 break;
4520 }
Mike Stump11289f42009-09-09 15:08:12 +00004521
Chris Lattner371ed4e2008-04-06 06:57:35 +00004522 // Consume the comma.
4523 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004524 }
Mike Stump11289f42009-09-09 15:08:12 +00004525
Chris Lattner6c940e62008-04-06 06:34:08 +00004526}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004527
Chris Lattnere8074e62006-08-06 18:30:15 +00004528/// [C90] direct-declarator '[' constant-expression[opt] ']'
4529/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4530/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4531/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4532/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4533void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004534 BalancedDelimiterTracker T(*this, tok::l_square);
4535 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004536
Chris Lattner84a11622008-12-18 07:27:21 +00004537 // C array syntax has many features, but by-far the most common is [] and [4].
4538 // This code does a fast path to handle some of the most obvious cases.
4539 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004540 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004541 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004542 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004543
Chris Lattner84a11622008-12-18 07:27:21 +00004544 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004545 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004546 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004547 T.getOpenLocation(),
4548 T.getCloseLocation()),
4549 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004550 return;
4551 } else if (Tok.getKind() == tok::numeric_constant &&
4552 GetLookAheadToken(1).is(tok::r_square)) {
4553 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004554 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004555 ConsumeToken();
4556
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004557 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004558 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004559 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004560
Chris Lattner84a11622008-12-18 07:27:21 +00004561 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004562 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004563 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004564 T.getOpenLocation(),
4565 T.getCloseLocation()),
4566 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004567 return;
4568 }
Mike Stump11289f42009-09-09 15:08:12 +00004569
Chris Lattnere8074e62006-08-06 18:30:15 +00004570 // If valid, this location is the position where we read the 'static' keyword.
4571 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004572 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004573 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004574
Chris Lattnere8074e62006-08-06 18:30:15 +00004575 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004576 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004577 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004578 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004579
Chris Lattnere8074e62006-08-06 18:30:15 +00004580 // If we haven't already read 'static', check to see if there is one after the
4581 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004582 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004583 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004584
Chris Lattnere8074e62006-08-06 18:30:15 +00004585 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004586 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004587 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004588
Chris Lattner521ff2b2008-04-06 05:26:30 +00004589 // Handle the case where we have '[*]' as the array size. However, a leading
4590 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4591 // the the token after the star is a ']'. Since stars in arrays are
4592 // infrequent, use of lookahead is not costly here.
4593 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004594 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004595
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004596 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004597 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004598 StaticLoc = SourceLocation(); // Drop the static.
4599 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004600 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004601 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004602 // Note, in C89, this production uses the constant-expr production instead
4603 // of assignment-expr. The only difference is that assignment-expr allows
4604 // things like '=' and '*='. Sema rejects these in C89 mode because they
4605 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004606
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004607 // Parse the constant-expression or assignment-expression now (depending
4608 // on dialect).
4609 if (getLang().CPlusPlus)
4610 NumElements = ParseConstantExpression();
4611 else
4612 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004613 }
Mike Stump11289f42009-09-09 15:08:12 +00004614
Chris Lattner62591722006-08-12 18:40:58 +00004615 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004616 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004617 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004618 // If the expression was invalid, skip it.
4619 SkipUntil(tok::r_square);
4620 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004621 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004622
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004623 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004624
John McCall084e83d2011-03-24 11:26:52 +00004625 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004626 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004627
Chris Lattner84a11622008-12-18 07:27:21 +00004628 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004629 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004630 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004631 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004632 T.getOpenLocation(),
4633 T.getCloseLocation()),
4634 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00004635}
4636
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004637/// [GNU] typeof-specifier:
4638/// typeof ( expressions )
4639/// typeof ( type-name )
4640/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004641///
4642void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004643 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004644 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004645 SourceLocation StartLoc = ConsumeToken();
4646
John McCalle8595032010-01-13 20:03:27 +00004647 const bool hasParens = Tok.is(tok::l_paren);
4648
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004649 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004650 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004651 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004652 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4653 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004654 if (hasParens)
4655 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004656
4657 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004658 // FIXME: Not accurate, the range gets one token more than it should.
4659 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004660 else
4661 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004662
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004663 if (isCastExpr) {
4664 if (!CastTy) {
4665 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004666 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004667 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004668
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004669 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004670 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004671 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4672 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004673 DiagID, CastTy))
4674 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004675 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004676 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004677
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004678 // If we get here, the operand to the typeof was an expresion.
4679 if (Operand.isInvalid()) {
4680 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004681 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004682 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004683
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004684 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004685 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004686 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4687 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004688 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004689 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004690}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004691
Benjamin Kramere56f3932011-12-23 17:00:35 +00004692/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00004693/// _Atomic ( type-name )
4694///
4695void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4696 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4697
4698 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004699 BalancedDelimiterTracker T(*this, tok::l_paren);
4700 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00004701 SkipUntil(tok::r_paren);
4702 return;
4703 }
4704
4705 TypeResult Result = ParseTypeName();
4706 if (Result.isInvalid()) {
4707 SkipUntil(tok::r_paren);
4708 return;
4709 }
4710
4711 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004712 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00004713
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004714 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00004715 return;
4716
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004717 DS.setTypeofParensRange(T.getRange());
4718 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00004719
4720 const char *PrevSpec = 0;
4721 unsigned DiagID;
4722 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4723 DiagID, Result.release()))
4724 Diag(StartLoc, DiagID) << PrevSpec;
4725}
4726
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004727
4728/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4729/// from TryAltiVecVectorToken.
4730bool Parser::TryAltiVecVectorTokenOutOfLine() {
4731 Token Next = NextToken();
4732 switch (Next.getKind()) {
4733 default: return false;
4734 case tok::kw_short:
4735 case tok::kw_long:
4736 case tok::kw_signed:
4737 case tok::kw_unsigned:
4738 case tok::kw_void:
4739 case tok::kw_char:
4740 case tok::kw_int:
4741 case tok::kw_float:
4742 case tok::kw_double:
4743 case tok::kw_bool:
4744 case tok::kw___pixel:
4745 Tok.setKind(tok::kw___vector);
4746 return true;
4747 case tok::identifier:
4748 if (Next.getIdentifierInfo() == Ident_pixel) {
4749 Tok.setKind(tok::kw___vector);
4750 return true;
4751 }
4752 return false;
4753 }
4754}
4755
4756bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4757 const char *&PrevSpec, unsigned &DiagID,
4758 bool &isInvalid) {
4759 if (Tok.getIdentifierInfo() == Ident_vector) {
4760 Token Next = NextToken();
4761 switch (Next.getKind()) {
4762 case tok::kw_short:
4763 case tok::kw_long:
4764 case tok::kw_signed:
4765 case tok::kw_unsigned:
4766 case tok::kw_void:
4767 case tok::kw_char:
4768 case tok::kw_int:
4769 case tok::kw_float:
4770 case tok::kw_double:
4771 case tok::kw_bool:
4772 case tok::kw___pixel:
4773 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4774 return true;
4775 case tok::identifier:
4776 if (Next.getIdentifierInfo() == Ident_pixel) {
4777 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4778 return true;
4779 }
4780 break;
4781 default:
4782 break;
4783 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004784 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004785 DS.isTypeAltiVecVector()) {
4786 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4787 return true;
4788 }
4789 return false;
4790}