blob: a20e90bd0ea38f3d0d7e4e4f201e84afd1ff32de [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"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
34 Declarator::TheContext Context) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000036 DeclSpec DS(AttrFactory);
Chris Lattner1890ac82006-08-13 01:16:23 +000037 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000038
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000040 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000041 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000042 if (Range)
43 *Range = DeclaratorInfo.getSourceRange();
44
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000045 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000046 return true;
47
Douglas Gregor0be31a22010-07-02 17:43:08 +000048 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000049}
50
Alexis Hunt96d5c762009-11-21 08:43:09 +000051/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000052///
53/// [GNU] attributes:
54/// attribute
55/// attributes attribute
56///
57/// [GNU] attribute:
58/// '__attribute__' '(' '(' attribute-list ')' ')'
59///
60/// [GNU] attribute-list:
61/// attrib
62/// attribute_list ',' attrib
63///
64/// [GNU] attrib:
65/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000066/// attrib-name
67/// attrib-name '(' identifier ')'
68/// attrib-name '(' identifier ',' nonempty-expr-list ')'
69/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000070///
Steve Naroff0f2fe172007-06-01 17:11:19 +000071/// [GNU] attrib-name:
72/// identifier
73/// typespec
74/// typequal
75/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000076///
Steve Naroff0f2fe172007-06-01 17:11:19 +000077/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000078/// token lookahead. Comment from gcc: "If they start with an identifier
79/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000080/// start with that identifier; otherwise they are an expression list."
81///
82/// At the moment, I am not doing 2 token lookahead. I am also unaware of
83/// any attributes that don't work (based on my limited testing). Most
84/// attributes are very simple in practice. Until we find a bug, I don't see
85/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000086
John McCall53fa7142010-12-24 02:08:15 +000087void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
88 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000089 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner76c72282007-10-09 17:33:22 +000091 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000092 ConsumeToken();
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
94 "attribute")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000096 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000097 }
98 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
99 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000100 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101 }
102 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000103 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
104 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000105
106 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
108 ConsumeToken();
109 continue;
110 }
111 // we have an identifier or declaration specifier (const, int, etc.)
112 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
113 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000114
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000115 // Availability attributes have their own grammar.
116 if (AttrName->isStr("availability"))
117 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregora2f49452010-03-16 19:09:18 +0000118 // check if we have a "parameterized" attribute
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000119 else if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000120 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner76c72282007-10-09 17:33:22 +0000122 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
124 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000125
126 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000128 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000129 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
130 ParmName, ParmLoc, 0, 0);
Chris Lattner76c72282007-10-09 17:33:22 +0000131 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 ConsumeToken();
133 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000134 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 // now parse the non-empty comma separated list of expressions
138 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000139 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000140 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 ArgExprsOk = false;
142 SkipUntil(tok::r_paren);
143 break;
144 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000145 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 break;
149 ConsumeToken(); // Eat the comma, move to the next argument
150 }
Chris Lattner76c72282007-10-09 17:33:22 +0000151 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000152 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
154 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 }
156 }
157 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000158 switch (Tok.getKind()) {
159 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000162 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
164 0, SourceLocation(), 0, 0);
Nate Begemanf2758702009-06-26 06:32:41 +0000165 break;
166 case tok::kw_char:
167 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000168 case tok::kw_char16_t:
169 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000170 case tok::kw_bool:
171 case tok::kw_short:
172 case tok::kw_int:
173 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000174 case tok::kw___int64:
Nate Begemanf2758702009-06-26 06:32:41 +0000175 case tok::kw_signed:
176 case tok::kw_unsigned:
177 case tok::kw_float:
178 case tok::kw_double:
179 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000180 case tok::kw_typeof: {
181 AttributeList *attr
John McCall084e83d2011-03-24 11:26:52 +0000182 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
183 0, SourceLocation(), 0, 0);
John McCall53fa7142010-12-24 02:08:15 +0000184 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000185 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000186 // If it's a builtin type name, eat it and expect a rparen
187 // __attribute__(( vec_type_hint(char) ))
188 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000189 if (Tok.is(tok::r_paren))
190 ConsumeParen();
191 break;
John McCall53fa7142010-12-24 02:08:15 +0000192 }
Nate Begemanf2758702009-06-26 06:32:41 +0000193 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000195 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000196 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 // now parse the list of expressions
199 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000200 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000201 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000202 ArgExprsOk = false;
203 SkipUntil(tok::r_paren);
204 break;
205 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000206 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000207 }
Chris Lattner76c72282007-10-09 17:33:22 +0000208 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 break;
210 ConsumeToken(); // Eat the comma, move to the next argument
211 }
212 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000213 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000214 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000215 attrs.addNew(AttrName, AttrNameLoc, 0,
216 AttrNameLoc, 0, SourceLocation(),
217 ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
Nate Begemanf2758702009-06-26 06:32:41 +0000219 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000220 }
221 }
222 } else {
John McCall084e83d2011-03-24 11:26:52 +0000223 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000225 }
226 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000228 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000229 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000230 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
231 SkipUntil(tok::r_paren, false);
232 }
John McCall53fa7142010-12-24 02:08:15 +0000233 if (endLoc)
234 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000235 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000236}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000237
Eli Friedman06de2b52009-06-08 07:21:15 +0000238/// ParseMicrosoftDeclSpec - Parse an __declspec construct
239///
240/// [MS] decl-specifier:
241/// __declspec ( extended-decl-modifier-seq )
242///
243/// [MS] extended-decl-modifier-seq:
244/// extended-decl-modifier[opt]
245/// extended-decl-modifier extended-decl-modifier-seq
246
John McCall53fa7142010-12-24 02:08:15 +0000247void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000248 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000249
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000250 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000251 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
252 "declspec")) {
253 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000254 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000255 }
Eli Friedman53339e02009-06-08 23:27:34 +0000256 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000257 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
258 SourceLocation AttrNameLoc = ConsumeToken();
259 if (Tok.is(tok::l_paren)) {
260 ConsumeParen();
261 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
262 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000263 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000264 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000265 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000266 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
267 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000268 }
269 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
270 SkipUntil(tok::r_paren, false);
271 } else {
John McCall084e83d2011-03-24 11:26:52 +0000272 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
273 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000274 }
275 }
276 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
277 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000278 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000279}
280
John McCall53fa7142010-12-24 02:08:15 +0000281void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000282 // Treat these like attributes
283 // FIXME: Allow Sema to distinguish between these and real attributes!
284 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000285 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
286 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000287 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
288 SourceLocation AttrNameLoc = ConsumeToken();
289 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
290 // FIXME: Support these properly!
291 continue;
John McCall084e83d2011-03-24 11:26:52 +0000292 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
293 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000294 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000295}
296
John McCall53fa7142010-12-24 02:08:15 +0000297void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000298 // Treat these like attributes
299 while (Tok.is(tok::kw___pascal)) {
300 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
301 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000302 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
303 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000304 }
John McCall53fa7142010-12-24 02:08:15 +0000305}
306
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000307void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
308 // Treat these like attributes
309 while (Tok.is(tok::kw___kernel)) {
310 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000311 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
312 AttrNameLoc, 0, AttrNameLoc, 0,
313 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000314 }
315}
316
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000317void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
318 SourceLocation Loc = Tok.getLocation();
319 switch(Tok.getKind()) {
320 // OpenCL qualifiers:
321 case tok::kw___private:
322 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000323 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000324 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000325 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000326 break;
327
328 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000329 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000330 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000331 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000332 break;
333
334 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000335 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000336 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000337 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000338 break;
339
340 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000341 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000342 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000343 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000344 break;
345
346 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000347 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000348 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000349 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000350 break;
351
352 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000353 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000354 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000355 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000356 break;
357
358 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000359 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000360 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000361 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000362 break;
363 default: break;
364 }
365}
366
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000367/// \brief Parse a version number.
368///
369/// version:
370/// simple-integer
371/// simple-integer ',' simple-integer
372/// simple-integer ',' simple-integer ',' simple-integer
373VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
374 Range = Tok.getLocation();
375
376 if (!Tok.is(tok::numeric_constant)) {
377 Diag(Tok, diag::err_expected_version);
378 SkipUntil(tok::comma, tok::r_paren, true, true, true);
379 return VersionTuple();
380 }
381
382 // Parse the major (and possibly minor and subminor) versions, which
383 // are stored in the numeric constant. We utilize a quirk of the
384 // lexer, which is that it handles something like 1.2.3 as a single
385 // numeric constant, rather than two separate tokens.
386 llvm::SmallString<512> Buffer;
387 Buffer.resize(Tok.getLength()+1);
388 const char *ThisTokBegin = &Buffer[0];
389
390 // Get the spelling of the token, which eliminates trigraphs, etc.
391 bool Invalid = false;
392 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
393 if (Invalid)
394 return VersionTuple();
395
396 // Parse the major version.
397 unsigned AfterMajor = 0;
398 unsigned Major = 0;
399 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
400 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
401 ++AfterMajor;
402 }
403
404 if (AfterMajor == 0) {
405 Diag(Tok, diag::err_expected_version);
406 SkipUntil(tok::comma, tok::r_paren, true, true, true);
407 return VersionTuple();
408 }
409
410 if (AfterMajor == ActualLength) {
411 ConsumeToken();
412
413 // We only had a single version component.
414 if (Major == 0) {
415 Diag(Tok, diag::err_zero_version);
416 return VersionTuple();
417 }
418
419 return VersionTuple(Major);
420 }
421
422 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
423 Diag(Tok, diag::err_expected_version);
424 SkipUntil(tok::comma, tok::r_paren, true, true, true);
425 return VersionTuple();
426 }
427
428 // Parse the minor version.
429 unsigned AfterMinor = AfterMajor + 1;
430 unsigned Minor = 0;
431 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
432 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
433 ++AfterMinor;
434 }
435
436 if (AfterMinor == ActualLength) {
437 ConsumeToken();
438
439 // We had major.minor.
440 if (Major == 0 && Minor == 0) {
441 Diag(Tok, diag::err_zero_version);
442 return VersionTuple();
443 }
444
445 return VersionTuple(Major, Minor);
446 }
447
448 // If what follows is not a '.', we have a problem.
449 if (ThisTokBegin[AfterMinor] != '.') {
450 Diag(Tok, diag::err_expected_version);
451 SkipUntil(tok::comma, tok::r_paren, true, true, true);
452 return VersionTuple();
453 }
454
455 // Parse the subminor version.
456 unsigned AfterSubminor = AfterMinor + 1;
457 unsigned Subminor = 0;
458 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
459 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
460 ++AfterSubminor;
461 }
462
463 if (AfterSubminor != ActualLength) {
464 Diag(Tok, diag::err_expected_version);
465 SkipUntil(tok::comma, tok::r_paren, true, true, true);
466 return VersionTuple();
467 }
468 ConsumeToken();
469 return VersionTuple(Major, Minor, Subminor);
470}
471
472/// \brief Parse the contents of the "availability" attribute.
473///
474/// availability-attribute:
475/// 'availability' '(' platform ',' version-arg-list ')'
476///
477/// platform:
478/// identifier
479///
480/// version-arg-list:
481/// version-arg
482/// version-arg ',' version-arg-list
483///
484/// version-arg:
485/// 'introduced' '=' version
486/// 'deprecated' '=' version
487/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000488/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000489void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
490 SourceLocation AvailabilityLoc,
491 ParsedAttributes &attrs,
492 SourceLocation *endLoc) {
493 SourceLocation PlatformLoc;
494 IdentifierInfo *Platform = 0;
495
496 enum { Introduced, Deprecated, Obsoleted, Unknown };
497 AvailabilityChange Changes[Unknown];
498
499 // Opening '('.
500 SourceLocation LParenLoc;
501 if (!Tok.is(tok::l_paren)) {
502 Diag(Tok, diag::err_expected_lparen);
503 return;
504 }
505 LParenLoc = ConsumeParen();
506
507 // Parse the platform name,
508 if (Tok.isNot(tok::identifier)) {
509 Diag(Tok, diag::err_availability_expected_platform);
510 SkipUntil(tok::r_paren);
511 return;
512 }
513 Platform = Tok.getIdentifierInfo();
514 PlatformLoc = ConsumeToken();
515
516 // Parse the ',' following the platform name.
517 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
518 return;
519
520 // If we haven't grabbed the pointers for the identifiers
521 // "introduced", "deprecated", and "obsoleted", do so now.
522 if (!Ident_introduced) {
523 Ident_introduced = PP.getIdentifierInfo("introduced");
524 Ident_deprecated = PP.getIdentifierInfo("deprecated");
525 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000526 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000527 }
528
529 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000530 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000531 do {
532 if (Tok.isNot(tok::identifier)) {
533 Diag(Tok, diag::err_availability_expected_change);
534 SkipUntil(tok::r_paren);
535 return;
536 }
537 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
538 SourceLocation KeywordLoc = ConsumeToken();
539
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000540 if (Keyword == Ident_unavailable) {
541 if (UnavailableLoc.isValid()) {
542 Diag(KeywordLoc, diag::err_availability_redundant)
543 << Keyword << SourceRange(UnavailableLoc);
544 }
545 UnavailableLoc = KeywordLoc;
546
547 if (Tok.isNot(tok::comma))
548 break;
549
550 ConsumeToken();
551 continue;
552 }
553
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000554 if (Tok.isNot(tok::equal)) {
555 Diag(Tok, diag::err_expected_equal_after)
556 << Keyword;
557 SkipUntil(tok::r_paren);
558 return;
559 }
560 ConsumeToken();
561
562 SourceRange VersionRange;
563 VersionTuple Version = ParseVersionTuple(VersionRange);
564
565 if (Version.empty()) {
566 SkipUntil(tok::r_paren);
567 return;
568 }
569
570 unsigned Index;
571 if (Keyword == Ident_introduced)
572 Index = Introduced;
573 else if (Keyword == Ident_deprecated)
574 Index = Deprecated;
575 else if (Keyword == Ident_obsoleted)
576 Index = Obsoleted;
577 else
578 Index = Unknown;
579
580 if (Index < Unknown) {
581 if (!Changes[Index].KeywordLoc.isInvalid()) {
582 Diag(KeywordLoc, diag::err_availability_redundant)
583 << Keyword
584 << SourceRange(Changes[Index].KeywordLoc,
585 Changes[Index].VersionRange.getEnd());
586 }
587
588 Changes[Index].KeywordLoc = KeywordLoc;
589 Changes[Index].Version = Version;
590 Changes[Index].VersionRange = VersionRange;
591 } else {
592 Diag(KeywordLoc, diag::err_availability_unknown_change)
593 << Keyword << VersionRange;
594 }
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 } while (true);
601
602 // Closing ')'.
603 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
604 if (RParenLoc.isInvalid())
605 return;
606
607 if (endLoc)
608 *endLoc = RParenLoc;
609
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000610 // The 'unavailable' availability cannot be combined with any other
611 // availability changes. Make sure that hasn't happened.
612 if (UnavailableLoc.isValid()) {
613 bool Complained = false;
614 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
615 if (Changes[Index].KeywordLoc.isValid()) {
616 if (!Complained) {
617 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
618 << SourceRange(Changes[Index].KeywordLoc,
619 Changes[Index].VersionRange.getEnd());
620 Complained = true;
621 }
622
623 // Clear out the availability.
624 Changes[Index] = AvailabilityChange();
625 }
626 }
627 }
628
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000629 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000630 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000631 0, SourceLocation(),
632 Platform, PlatformLoc,
633 Changes[Introduced],
634 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000635 Changes[Obsoleted],
636 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000637}
638
John McCall53fa7142010-12-24 02:08:15 +0000639void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
640 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
641 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000642}
643
Chris Lattner53361ac2006-08-10 05:19:57 +0000644/// ParseDeclaration - Parse a full 'declaration', which consists of
645/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000646/// 'Context' should be a Declarator::TheContext value. This returns the
647/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000648///
649/// declaration: [C99 6.7]
650/// block-declaration ->
651/// simple-declaration
652/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000653/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000654/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000655/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000656/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000657/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000658/// others... [FIXME]
659///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000660Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
661 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000662 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000663 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000664 ParenBraceBracketBalancer BalancerRAIIObj(*this);
665
John McCall48871652010-08-21 09:40:31 +0000666 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000667 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000668 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000669 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000670 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000671 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000672 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000673 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000674 // Could be the start of an inline namespace. Allowed as an ext in C++03.
675 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000676 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000677 SourceLocation InlineLoc = ConsumeToken();
678 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
679 break;
680 }
John McCall53fa7142010-12-24 02:08:15 +0000681 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000682 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000683 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000684 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000685 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000686 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000687 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000688 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000689 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000690 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000691 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000692 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000693 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000694 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000695 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000696 default:
John McCall53fa7142010-12-24 02:08:15 +0000697 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000698 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000699
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000700 // This routine returns a DeclGroup, if the thing we parsed only contains a
701 // single decl, convert it now.
702 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000703}
704
705/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
706/// declaration-specifiers init-declarator-list[opt] ';'
707///[C90/C++]init-declarator-list ';' [TODO]
708/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000709///
Richard Smith02e85f32011-04-14 22:09:26 +0000710/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
711/// attribute-specifier-seq[opt] type-specifier-seq declarator
712///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000713/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000714/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000715///
716/// If FRI is non-null, we might be parsing a for-range-declaration instead
717/// of a simple-declaration. If we find that we are, we also parse the
718/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000719Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
720 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000721 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000722 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000723 bool RequireSemi,
724 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000725 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000726 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000727 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000728
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000729 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000730 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000731 StmtResult R = Actions.ActOnVlaStmt(DS);
732 if (R.isUsable())
733 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000734
Chris Lattner0e894622006-08-13 19:58:17 +0000735 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
736 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000737 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000738 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000739 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000740 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000741 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000742 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000743 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000744
745 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000746}
Mike Stump11289f42009-09-09 15:08:12 +0000747
John McCalld5a36322009-11-03 19:26:08 +0000748/// ParseDeclGroup - Having concluded that this is either a function
749/// definition or a group of object declarations, actually parse the
750/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000751Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
752 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000753 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000754 SourceLocation *DeclEnd,
755 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000756 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000757 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000758 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000759
John McCalld5a36322009-11-03 19:26:08 +0000760 // Bail out if the first declarator didn't seem well-formed.
761 if (!D.hasName() && !D.mayOmitIdentifier()) {
762 // Skip until ; or }.
763 SkipUntil(tok::r_brace, true, true);
764 if (Tok.is(tok::semi))
765 ConsumeToken();
766 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000769 // Check to see if we have a function *definition* which must have a body.
770 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
771 // Look at the next token to make sure that this isn't a function
772 // declaration. We have to check this because __attribute__ might be the
773 // start of a function definition in GCC-extended K&R C.
774 !isDeclarationAfterDeclarator()) {
775
Chris Lattner13901342010-07-11 22:42:07 +0000776 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000777 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
778 Diag(Tok, diag::err_function_declared_typedef);
779
780 // Recover by treating the 'typedef' as spurious.
781 DS.ClearStorageClassSpecs();
782 }
783
John McCall48871652010-08-21 09:40:31 +0000784 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000785 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000786 }
787
788 if (isDeclarationSpecifier()) {
789 // If there is an invalid declaration specifier right after the function
790 // prototype, then we must be in a missing semicolon case where this isn't
791 // actually a body. Just fall through into the code that handles it as a
792 // prototype, and let the top-level code handle the erroneous declspec
793 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000794 } else {
795 Diag(Tok, diag::err_expected_fn_body);
796 SkipUntil(tok::semi);
797 return DeclGroupPtrTy();
798 }
799 }
800
Richard Smith02e85f32011-04-14 22:09:26 +0000801 if (ParseAttributesAfterDeclarator(D))
802 return DeclGroupPtrTy();
803
804 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
805 // must parse and analyze the for-range-initializer before the declaration is
806 // analyzed.
807 if (FRI && Tok.is(tok::colon)) {
808 FRI->ColonLoc = ConsumeToken();
809 // FIXME: handle braced-init-list here.
810 FRI->RangeExpr = ParseExpression();
811 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
812 Actions.ActOnCXXForRangeDecl(ThisDecl);
813 Actions.FinalizeDeclaration(ThisDecl);
814 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
815 }
816
John McCall48871652010-08-21 09:40:31 +0000817 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000818 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000819 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000820 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000821 DeclsInGroup.push_back(FirstDecl);
822
823 // If we don't have a comma, it is either the end of the list (a ';') or an
824 // error, bail out.
825 while (Tok.is(tok::comma)) {
826 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000827 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000828
829 // Parse the next declarator.
830 D.clear();
831
832 // Accept attributes in an init-declarator. In the first declarator in a
833 // declaration, these would be part of the declspec. In subsequent
834 // declarators, they become part of the declarator itself, so that they
835 // don't apply to declarators after *this* one. Examples:
836 // short __attribute__((common)) var; -> declspec
837 // short var __attribute__((common)); -> declarator
838 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000839 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000840
841 ParseDeclarator(D);
842
John McCall48871652010-08-21 09:40:31 +0000843 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000844 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000845 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000846 DeclsInGroup.push_back(ThisDecl);
847 }
848
849 if (DeclEnd)
850 *DeclEnd = Tok.getLocation();
851
852 if (Context != Declarator::ForContext &&
853 ExpectAndConsume(tok::semi,
854 Context == Declarator::FileContext
855 ? diag::err_invalid_token_after_toplevel_declarator
856 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000857 // Okay, there was no semicolon and one was expected. If we see a
858 // declaration specifier, just assume it was missing and continue parsing.
859 // Otherwise things are very confused and we skip to recover.
860 if (!isDeclarationSpecifier()) {
861 SkipUntil(tok::r_brace, true, true);
862 if (Tok.is(tok::semi))
863 ConsumeToken();
864 }
John McCalld5a36322009-11-03 19:26:08 +0000865 }
866
Douglas Gregor0be31a22010-07-02 17:43:08 +0000867 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000868 DeclsInGroup.data(),
869 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000870}
871
Richard Smith02e85f32011-04-14 22:09:26 +0000872/// Parse an optional simple-asm-expr and attributes, and attach them to a
873/// declarator. Returns true on an error.
874bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
875 // If a simple-asm-expr is present, parse it.
876 if (Tok.is(tok::kw_asm)) {
877 SourceLocation Loc;
878 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
879 if (AsmLabel.isInvalid()) {
880 SkipUntil(tok::semi, true, true);
881 return true;
882 }
883
884 D.setAsmLabel(AsmLabel.release());
885 D.SetRangeEnd(Loc);
886 }
887
888 MaybeParseGNUAttributes(D);
889 return false;
890}
891
Douglas Gregor23996282009-05-12 21:31:51 +0000892/// \brief Parse 'declaration' after parsing 'declaration-specifiers
893/// declarator'. This method parses the remainder of the declaration
894/// (including any attributes or initializer, among other things) and
895/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000896///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000897/// init-declarator: [C99 6.7]
898/// declarator
899/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000900/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
901/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000902/// [C++] declarator initializer[opt]
903///
904/// [C++] initializer:
905/// [C++] '=' initializer-clause
906/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000907/// [C++0x] '=' 'default' [TODO]
908/// [C++0x] '=' 'delete'
909///
910/// According to the standard grammar, =default and =delete are function
911/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000912///
John McCall48871652010-08-21 09:40:31 +0000913Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000914 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +0000915 if (ParseAttributesAfterDeclarator(D))
916 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000917
Richard Smith02e85f32011-04-14 22:09:26 +0000918 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
919}
Mike Stump11289f42009-09-09 15:08:12 +0000920
Richard Smith02e85f32011-04-14 22:09:26 +0000921Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
922 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000923 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000924 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000925 switch (TemplateInfo.Kind) {
926 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000927 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000928 break;
929
930 case ParsedTemplateInfo::Template:
931 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000932 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000933 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000934 TemplateInfo.TemplateParams->data(),
935 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000936 D);
937 break;
938
939 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000940 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000941 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000942 TemplateInfo.ExternLoc,
943 TemplateInfo.TemplateLoc,
944 D);
945 if (ThisRes.isInvalid()) {
946 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000947 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000948 }
949
950 ThisDecl = ThisRes.get();
951 break;
952 }
953 }
Mike Stump11289f42009-09-09 15:08:12 +0000954
Richard Smith30482bc2011-02-20 03:19:35 +0000955 bool TypeContainsAuto =
956 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
957
Douglas Gregor23996282009-05-12 21:31:51 +0000958 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000959 if (isTokenEqualOrMistypedEqualEqual(
960 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000961 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000962 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000963 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000964
965 if (!getLang().CPlusPlus0x)
966 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
967
Douglas Gregor23996282009-05-12 21:31:51 +0000968 Actions.SetDeclDeleted(ThisDecl, DelLoc);
969 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000970 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
971 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000972 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000973 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000974
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000975 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000976 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000977 ConsumeCodeCompletionToken();
978 SkipUntil(tok::comma, true, true);
979 return ThisDecl;
980 }
981
John McCalldadc5752010-08-24 06:29:42 +0000982 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000983
John McCall1f4ee7b2009-12-19 09:28:58 +0000984 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000985 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000986 ExitScope();
987 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000988
Douglas Gregor23996282009-05-12 21:31:51 +0000989 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000990 SkipUntil(tok::comma, true, true);
991 Actions.ActOnInitializerError(ThisDecl);
992 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000993 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
994 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000995 }
996 } else if (Tok.is(tok::l_paren)) {
997 // Parse C++ direct initializer: '(' expression-list ')'
998 SourceLocation LParenLoc = ConsumeParen();
999 ExprVector Exprs(Actions);
1000 CommaLocsTy CommaLocs;
1001
Douglas Gregor613bf102009-12-22 17:47:17 +00001002 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1003 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001004 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001005 }
1006
Douglas Gregor23996282009-05-12 21:31:51 +00001007 if (ParseExpressionList(Exprs, CommaLocs)) {
1008 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001009
1010 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001011 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001012 ExitScope();
1013 }
Douglas Gregor23996282009-05-12 21:31:51 +00001014 } else {
1015 // Match the ')'.
1016 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1017
1018 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1019 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001020
1021 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001022 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001023 ExitScope();
1024 }
1025
Douglas Gregor23996282009-05-12 21:31:51 +00001026 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1027 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001028 RParenLoc,
1029 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001030 }
1031 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001032 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001033 }
1034
Richard Smithb2bc2e62011-02-21 20:05:19 +00001035 Actions.FinalizeDeclaration(ThisDecl);
1036
Douglas Gregor23996282009-05-12 21:31:51 +00001037 return ThisDecl;
1038}
1039
Chris Lattner1890ac82006-08-13 01:16:23 +00001040/// ParseSpecifierQualifierList
1041/// specifier-qualifier-list:
1042/// type-specifier specifier-qualifier-list[opt]
1043/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001044/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001045///
1046void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1047 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1048 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001049 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner1890ac82006-08-13 01:16:23 +00001051 // Validate declspec for type-name.
1052 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001053 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001054 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001055 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattner1b22eed2006-11-28 05:12:07 +00001057 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001058 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001059 if (DS.getStorageClassSpecLoc().isValid())
1060 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1061 else
1062 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001063 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001064 }
Mike Stump11289f42009-09-09 15:08:12 +00001065
Chris Lattner1b22eed2006-11-28 05:12:07 +00001066 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001067 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001068 if (DS.isInlineSpecified())
1069 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1070 if (DS.isVirtualSpecified())
1071 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1072 if (DS.isExplicitSpecified())
1073 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001074 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001075 }
1076}
Chris Lattner53361ac2006-08-10 05:19:57 +00001077
Chris Lattner6cc055a2009-04-12 20:42:31 +00001078/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1079/// specified token is valid after the identifier in a declarator which
1080/// immediately follows the declspec. For example, these things are valid:
1081///
1082/// int x [ 4]; // direct-declarator
1083/// int x ( int y); // direct-declarator
1084/// int(int x ) // direct-declarator
1085/// int x ; // simple-declaration
1086/// int x = 17; // init-declarator-list
1087/// int x , y; // init-declarator-list
1088/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001089/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001090/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001091///
1092/// This is not, because 'x' does not immediately follow the declspec (though
1093/// ')' happens to be valid anyway).
1094/// int (x)
1095///
1096static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1097 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1098 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001099 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001100}
1101
Chris Lattner20a0c612009-04-14 21:34:55 +00001102
1103/// ParseImplicitInt - This method is called when we have an non-typename
1104/// identifier in a declspec (which normally terminates the decl spec) when
1105/// the declspec has no type specifier. In this case, the declspec is either
1106/// malformed or is "implicit int" (in K&R and C89).
1107///
1108/// This method handles diagnosing this prettily and returns false if the
1109/// declspec is done being processed. If it recovers and thinks there may be
1110/// other pieces of declspec after it, it returns true.
1111///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001112bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001113 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001114 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001115 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattner20a0c612009-04-14 21:34:55 +00001117 SourceLocation Loc = Tok.getLocation();
1118 // If we see an identifier that is not a type name, we normally would
1119 // parse it as the identifer being declared. However, when a typename
1120 // is typo'd or the definition is not included, this will incorrectly
1121 // parse the typename as the identifier name and fall over misparsing
1122 // later parts of the diagnostic.
1123 //
1124 // As such, we try to do some look-ahead in cases where this would
1125 // otherwise be an "implicit-int" case to see if this is invalid. For
1126 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1127 // an identifier with implicit int, we'd get a parse error because the
1128 // next token is obviously invalid for a type. Parse these as a case
1129 // with an invalid type specifier.
1130 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner20a0c612009-04-14 21:34:55 +00001132 // Since we know that this either implicit int (which is rare) or an
1133 // error, we'd do lookahead to try to do better recovery.
1134 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1135 // If this token is valid for implicit int, e.g. "static x = 4", then
1136 // we just avoid eating the identifier, so it will be parsed as the
1137 // identifier in the declarator.
1138 return false;
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Chris Lattner20a0c612009-04-14 21:34:55 +00001141 // Otherwise, if we don't consume this token, we are going to emit an
1142 // error anyway. Try to recover from various common problems. Check
1143 // to see if this was a reference to a tag name without a tag specified.
1144 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001145 //
1146 // C++ doesn't need this, and isTagName doesn't take SS.
1147 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001148 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001149 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor0be31a22010-07-02 17:43:08 +00001151 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001152 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001153 case DeclSpec::TST_enum:
1154 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1155 case DeclSpec::TST_union:
1156 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1157 case DeclSpec::TST_struct:
1158 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1159 case DeclSpec::TST_class:
1160 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001163 if (TagName) {
1164 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001165 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001166 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001168 // Parse this as a tag as if the missing tag were present.
1169 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001170 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001171 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001172 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001173 return true;
1174 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176
Douglas Gregor15e56022009-10-13 23:27:22 +00001177 // This is almost certainly an invalid type name. Let the action emit a
1178 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001179 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001180 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001182 // The action emitted a diagnostic, so we don't have to.
1183 if (T) {
1184 // The action has suggested that the type T could be used. Set that as
1185 // the type in the declaration specifiers, consume the would-be type
1186 // name token, and we're done.
1187 const char *PrevSpec;
1188 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001189 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001190 DS.SetRangeEnd(Tok.getLocation());
1191 ConsumeToken();
1192
1193 // There may be other declaration specifiers after this.
1194 return true;
1195 }
1196
1197 // Fall through; the action had no suggestion for us.
1198 } else {
1199 // The action did not emit a diagnostic, so emit one now.
1200 SourceRange R;
1201 if (SS) R = SS->getRange();
1202 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor15e56022009-10-13 23:27:22 +00001205 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001206 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001207 unsigned DiagID;
1208 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001209 DS.SetRangeEnd(Tok.getLocation());
1210 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner20a0c612009-04-14 21:34:55 +00001212 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1213 // avoid rippling error messages on subsequent uses of the same type,
1214 // could be useful if #include was forgotten.
1215 return false;
1216}
1217
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001218/// \brief Determine the declaration specifier context from the declarator
1219/// context.
1220///
1221/// \param Context the declarator context, which is one of the
1222/// Declarator::TheContext enumerator values.
1223Parser::DeclSpecContext
1224Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1225 if (Context == Declarator::MemberContext)
1226 return DSC_class;
1227 if (Context == Declarator::FileContext)
1228 return DSC_top_level;
1229 return DSC_normal;
1230}
1231
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001232/// ParseDeclarationSpecifiers
1233/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001234/// storage-class-specifier declaration-specifiers[opt]
1235/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001236/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001237/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001238///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001239/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001240/// 'typedef'
1241/// 'extern'
1242/// 'static'
1243/// 'auto'
1244/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001245/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001246/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001247/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001248/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001249/// [C++] 'virtual'
1250/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001251/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001252/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001253/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001254
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001255///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001256void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001257 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001258 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001259 DeclSpecContext DSContext) {
1260 if (DS.getSourceRange().isInvalid()) {
1261 DS.SetRangeStart(Tok.getLocation());
1262 DS.SetRangeEnd(Tok.getLocation());
1263 }
1264
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001265 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001266 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001267 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001268 unsigned DiagID = 0;
1269
Chris Lattner4d8f8732006-11-28 05:05:08 +00001270 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001271
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001272 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001273 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001274 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001275 // If this is not a declaration specifier token, we're done reading decl
1276 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001277 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001278 return;
Mike Stump11289f42009-09-09 15:08:12 +00001279
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001280 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001281 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001282 if (DS.hasTypeSpecifier()) {
1283 bool AllowNonIdentifiers
1284 = (getCurScope()->getFlags() & (Scope::ControlScope |
1285 Scope::BlockScope |
1286 Scope::TemplateParamScope |
1287 Scope::FunctionPrototypeScope |
1288 Scope::AtCatchScope)) == 0;
1289 bool AllowNestedNameSpecifiers
1290 = DSContext == DSC_top_level ||
1291 (DSContext == DSC_class && DS.isFriendSpecified());
1292
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001293 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1294 AllowNonIdentifiers,
1295 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001296 ConsumeCodeCompletionToken();
1297 return;
1298 }
1299
Douglas Gregor80039242011-02-15 20:33:25 +00001300 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1301 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1302 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001303 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1304 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001305 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001306 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001307 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001308 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001309
1310 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1311 ConsumeCodeCompletionToken();
1312 return;
1313 }
1314
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001315 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001316 // C++ scope specifier. Annotate and loop, or bail out on error.
1317 if (TryAnnotateCXXScopeToken(true)) {
1318 if (!DS.hasTypeSpecifier())
1319 DS.SetTypeSpecError();
1320 goto DoneWithDeclSpec;
1321 }
John McCall8bc2a702010-03-01 18:20:46 +00001322 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1323 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001324 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001325
1326 case tok::annot_cxxscope: {
1327 if (DS.hasTypeSpecifier())
1328 goto DoneWithDeclSpec;
1329
John McCall9dab4e62009-12-12 11:40:51 +00001330 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001331 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1332 Tok.getAnnotationRange(),
1333 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001334
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001335 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001336 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001337 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001338 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001339 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001340 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001341
1342 // C++ [class.qual]p2:
1343 // In a lookup in which the constructor is an acceptable lookup
1344 // result and the nested-name-specifier nominates a class C:
1345 //
1346 // - if the name specified after the
1347 // nested-name-specifier, when looked up in C, is the
1348 // injected-class-name of C (Clause 9), or
1349 //
1350 // - if the name specified after the nested-name-specifier
1351 // is the same as the identifier or the
1352 // simple-template-id's template-name in the last
1353 // component of the nested-name-specifier,
1354 //
1355 // the name is instead considered to name the constructor of
1356 // class C.
1357 //
1358 // Thus, if the template-name is actually the constructor
1359 // name, then the code is ill-formed; this interpretation is
1360 // reinforced by the NAD status of core issue 635.
1361 TemplateIdAnnotation *TemplateId
1362 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001363 if ((DSContext == DSC_top_level ||
1364 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1365 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001366 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001367 if (isConstructorDeclarator()) {
1368 // The user meant this to be an out-of-line constructor
1369 // definition, but template arguments are not allowed
1370 // there. Just allow this as a constructor; we'll
1371 // complain about it later.
1372 goto DoneWithDeclSpec;
1373 }
1374
1375 // The user meant this to name a type, but it actually names
1376 // a constructor with some extraneous template
1377 // arguments. Complain, then parse it as a type as the user
1378 // intended.
1379 Diag(TemplateId->TemplateNameLoc,
1380 diag::err_out_of_line_template_id_names_constructor)
1381 << TemplateId->Name;
1382 }
1383
John McCall9dab4e62009-12-12 11:40:51 +00001384 DS.getTypeSpecScope() = SS;
1385 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001386 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001387 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001388 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001389 continue;
1390 }
1391
Douglas Gregorc5790df2009-09-28 07:26:33 +00001392 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001393 DS.getTypeSpecScope() = SS;
1394 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001395 if (Tok.getAnnotationValue()) {
1396 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001397 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1398 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001399 PrevSpec, DiagID, T);
1400 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001401 else
1402 DS.SetTypeSpecError();
1403 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1404 ConsumeToken(); // The typename
1405 }
1406
Douglas Gregor167fa622009-03-25 15:40:00 +00001407 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001408 goto DoneWithDeclSpec;
1409
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001410 // If we're in a context where the identifier could be a class name,
1411 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001412 if ((DSContext == DSC_top_level ||
1413 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001414 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001415 &SS)) {
1416 if (isConstructorDeclarator())
1417 goto DoneWithDeclSpec;
1418
1419 // As noted in C++ [class.qual]p2 (cited above), when the name
1420 // of the class is qualified in a context where it could name
1421 // a constructor, its a constructor name. However, we've
1422 // looked at the declarator, and the user probably meant this
1423 // to be a type. Complain that it isn't supposed to be treated
1424 // as a type, then proceed to parse it as a type.
1425 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1426 << Next.getIdentifierInfo();
1427 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001428
John McCallba7bf592010-08-24 05:47:05 +00001429 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1430 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001431 getCurScope(), &SS,
1432 false, false, ParsedType(),
1433 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001434
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001435 // If the referenced identifier is not a type, then this declspec is
1436 // erroneous: We already checked about that it has no type specifier, and
1437 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001438 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001439 if (TypeRep == 0) {
1440 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001441 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001442 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
John McCall9dab4e62009-12-12 11:40:51 +00001445 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001446 ConsumeToken(); // The C++ scope.
1447
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001448 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001449 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001450 if (isInvalid)
1451 break;
Mike Stump11289f42009-09-09 15:08:12 +00001452
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001453 DS.SetRangeEnd(Tok.getLocation());
1454 ConsumeToken(); // The typename.
1455
1456 continue;
1457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Chris Lattnere387d9e2009-01-21 19:48:37 +00001459 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001460 if (Tok.getAnnotationValue()) {
1461 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001463 DiagID, T);
1464 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001465 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001466
1467 if (isInvalid)
1468 break;
1469
Chris Lattnere387d9e2009-01-21 19:48:37 +00001470 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1471 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001472
Chris Lattnere387d9e2009-01-21 19:48:37 +00001473 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1474 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001475 // Objective-C interface.
1476 if (Tok.is(tok::less) && getLang().ObjC1)
1477 ParseObjCProtocolQualifiers(DS);
1478
Chris Lattnere387d9e2009-01-21 19:48:37 +00001479 continue;
1480 }
Mike Stump11289f42009-09-09 15:08:12 +00001481
Douglas Gregor06873092011-04-28 15:48:45 +00001482 case tok::kw___is_signed:
1483 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1484 // typically treats it as a trait. If we see __is_signed as it appears
1485 // in libstdc++, e.g.,
1486 //
1487 // static const bool __is_signed;
1488 //
1489 // then treat __is_signed as an identifier rather than as a keyword.
1490 if (DS.getTypeSpecType() == TST_bool &&
1491 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1492 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1493 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1494 Tok.setKind(tok::identifier);
1495 }
1496
1497 // We're done with the declaration-specifiers.
1498 goto DoneWithDeclSpec;
1499
Chris Lattner16fac4f2008-07-26 01:18:38 +00001500 // typedef-name
1501 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001502 // In C++, check to see if this is a scope specifier like foo::bar::, if
1503 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001504 if (getLang().CPlusPlus) {
1505 if (TryAnnotateCXXScopeToken(true)) {
1506 if (!DS.hasTypeSpecifier())
1507 DS.SetTypeSpecError();
1508 goto DoneWithDeclSpec;
1509 }
1510 if (!Tok.is(tok::identifier))
1511 continue;
1512 }
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner16fac4f2008-07-26 01:18:38 +00001514 // This identifier can only be a typedef name if we haven't already seen
1515 // a type-specifier. Without this check we misparse:
1516 // typedef int X; struct Y { short X; }; as 'short int'.
1517 if (DS.hasTypeSpecifier())
1518 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001519
John Thompson22334602010-02-05 00:12:22 +00001520 // Check for need to substitute AltiVec keyword tokens.
1521 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1522 break;
1523
Chris Lattner16fac4f2008-07-26 01:18:38 +00001524 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001525 ParsedType TypeRep =
1526 Actions.getTypeName(*Tok.getIdentifierInfo(),
1527 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001528
Chris Lattner6cc055a2009-04-12 20:42:31 +00001529 // If this is not a typedef name, don't parse it as part of the declspec,
1530 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001531 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001532 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001533 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001534 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001535
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001536 // If we're in a context where the identifier could be a class name,
1537 // check whether this is a constructor declaration.
1538 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001539 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001540 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001541 goto DoneWithDeclSpec;
1542
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001544 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001545 if (isInvalid)
1546 break;
Mike Stump11289f42009-09-09 15:08:12 +00001547
Chris Lattner16fac4f2008-07-26 01:18:38 +00001548 DS.SetRangeEnd(Tok.getLocation());
1549 ConsumeToken(); // The identifier
1550
1551 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1552 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001553 // Objective-C interface.
1554 if (Tok.is(tok::less) && getLang().ObjC1)
1555 ParseObjCProtocolQualifiers(DS);
1556
Steve Naroffcd5e7822008-09-22 10:28:57 +00001557 // Need to support trailing type qualifiers (e.g. "id<p> const").
1558 // If a type specifier follows, it will be diagnosed elsewhere.
1559 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001560 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001561
1562 // type-name
1563 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001564 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001565 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001566 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001567 // This template-id does not refer to a type name, so we're
1568 // done with the type-specifiers.
1569 goto DoneWithDeclSpec;
1570 }
1571
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001572 // If we're in a context where the template-id could be a
1573 // constructor name or specialization, check whether this is a
1574 // constructor declaration.
1575 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001576 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001577 isConstructorDeclarator())
1578 goto DoneWithDeclSpec;
1579
Douglas Gregor7f741122009-02-25 19:37:18 +00001580 // Turn the template-id annotation token into a type annotation
1581 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001582 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001583 continue;
1584 }
1585
Chris Lattnere37e2332006-08-15 04:50:22 +00001586 // GNU attributes support.
1587 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001588 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001589 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001590
1591 // Microsoft declspec support.
1592 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001593 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001594 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Steve Naroff44ac7772008-12-25 14:16:32 +00001596 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001597 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001598 // FIXME: Add handling here!
1599 break;
1600
1601 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001602 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001603 case tok::kw___cdecl:
1604 case tok::kw___stdcall:
1605 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001606 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001607 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001608 continue;
1609
Dawn Perchik335e16b2010-09-03 01:29:35 +00001610 // Borland single token adornments.
1611 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001612 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001613 continue;
1614
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001615 // OpenCL single token adornments.
1616 case tok::kw___kernel:
1617 ParseOpenCLAttributes(DS.getAttributes());
1618 continue;
1619
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001620 // storage-class-specifier
1621 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001622 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001623 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001624 break;
1625 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001626 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001627 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001628 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001629 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001630 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001631 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001632 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001633 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001634 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001635 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001636 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001637 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001638 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001639 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001640 break;
1641 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001642 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001643 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1644 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1645 DiagID, getLang());
1646 if (!isInvalid)
1647 Diag(Tok, diag::auto_storage_class)
1648 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1649 }
1650 else
1651 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1652 DiagID);
1653 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001654 else
John McCall49bfce42009-08-03 20:12:06 +00001655 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001656 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001657 break;
1658 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001659 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001660 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001661 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001662 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001663 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001664 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001665 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001666 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001667 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001668 break;
Mike Stump11289f42009-09-09 15:08:12 +00001669
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001670 // function-specifier
1671 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001672 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001673 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001674 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001675 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001676 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001677 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001678 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001679 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001680
Anders Carlssoncd8db412009-05-06 04:46:28 +00001681 // friend
1682 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001683 if (DSContext == DSC_class)
1684 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1685 else {
1686 PrevSpec = ""; // not actually used by the diagnostic
1687 DiagID = diag::err_friend_invalid_in_context;
1688 isInvalid = true;
1689 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001690 break;
Mike Stump11289f42009-09-09 15:08:12 +00001691
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001692 // constexpr
1693 case tok::kw_constexpr:
1694 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1695 break;
1696
Chris Lattnere387d9e2009-01-21 19:48:37 +00001697 // type-specifier
1698 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001699 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1700 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001701 break;
1702 case tok::kw_long:
1703 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001704 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1705 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001706 else
John McCall49bfce42009-08-03 20:12:06 +00001707 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1708 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001709 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001710 case tok::kw___int64:
1711 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1712 DiagID);
1713 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001714 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001715 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1716 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001717 break;
1718 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001719 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1720 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001721 break;
1722 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001723 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1724 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001725 break;
1726 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001727 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1728 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001729 break;
1730 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1732 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001733 break;
1734 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1736 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001737 break;
1738 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1740 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001741 break;
1742 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1744 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001745 break;
1746 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1748 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001749 break;
1750 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1752 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001753 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001754 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1756 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001757 break;
1758 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1760 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001761 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001762 case tok::kw_bool:
1763 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001764 if (Tok.is(tok::kw_bool) &&
1765 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1766 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1767 PrevSpec = ""; // Not used by the diagnostic.
1768 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001769 // For better error recovery.
1770 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001771 isInvalid = true;
1772 } else {
1773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1774 DiagID);
1775 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001776 break;
1777 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1779 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001780 break;
1781 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1783 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001784 break;
1785 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1787 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001788 break;
John Thompson22334602010-02-05 00:12:22 +00001789 case tok::kw___vector:
1790 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1791 break;
1792 case tok::kw___pixel:
1793 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1794 break;
John McCall39439732011-04-09 22:50:59 +00001795 case tok::kw___unknown_anytype:
1796 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1797 PrevSpec, DiagID);
1798 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001799
1800 // class-specifier:
1801 case tok::kw_class:
1802 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001803 case tok::kw_union: {
1804 tok::TokenKind Kind = Tok.getKind();
1805 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001806 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001807 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001808 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001809
1810 // enum-specifier:
1811 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001812 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001813 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001814 continue;
1815
1816 // cv-qualifier:
1817 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001818 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1819 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001820 break;
1821 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001822 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1823 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001824 break;
1825 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001826 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1827 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001828 break;
1829
Douglas Gregor333489b2009-03-27 23:10:48 +00001830 // C++ typename-specifier:
1831 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001832 if (TryAnnotateTypeOrScopeToken()) {
1833 DS.SetTypeSpecError();
1834 goto DoneWithDeclSpec;
1835 }
1836 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001837 continue;
1838 break;
1839
Chris Lattnere387d9e2009-01-21 19:48:37 +00001840 // GNU typeof support.
1841 case tok::kw_typeof:
1842 ParseTypeofSpecifier(DS);
1843 continue;
1844
Anders Carlsson74948d02009-06-24 17:47:40 +00001845 case tok::kw_decltype:
1846 ParseDecltypeSpecifier(DS);
1847 continue;
1848
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001849 // OpenCL qualifiers:
1850 case tok::kw_private:
1851 if (!getLang().OpenCL)
1852 goto DoneWithDeclSpec;
1853 case tok::kw___private:
1854 case tok::kw___global:
1855 case tok::kw___local:
1856 case tok::kw___constant:
1857 case tok::kw___read_only:
1858 case tok::kw___write_only:
1859 case tok::kw___read_write:
1860 ParseOpenCLQualifiers(DS);
1861 break;
1862
Steve Naroffcfdf6162008-06-05 00:02:44 +00001863 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001864 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001865 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1866 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001867 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001868 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001869
Douglas Gregor3a001f42010-11-19 17:10:50 +00001870 if (!ParseObjCProtocolQualifiers(DS))
1871 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1872 << FixItHint::CreateInsertion(Loc, "id")
1873 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001874
1875 // Need to support trailing type qualifiers (e.g. "id<p> const").
1876 // If a type specifier follows, it will be diagnosed elsewhere.
1877 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001878 }
John McCall49bfce42009-08-03 20:12:06 +00001879 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001880 if (isInvalid) {
1881 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001882 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001883
1884 if (DiagID == diag::ext_duplicate_declspec)
1885 Diag(Tok, DiagID)
1886 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1887 else
1888 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001889 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001890
Chris Lattner2e232092008-03-13 06:29:04 +00001891 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001892 if (DiagID != diag::err_bool_redeclaration)
1893 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001894 }
1895}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001896
Chris Lattnera448d752009-01-06 06:59:53 +00001897/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001898/// primarily follow the C++ grammar with additions for C99 and GNU,
1899/// which together subsume the C grammar. Note that the C++
1900/// type-specifier also includes the C type-qualifier (for const,
1901/// volatile, and C99 restrict). Returns true if a type-specifier was
1902/// found (and parsed), false otherwise.
1903///
1904/// type-specifier: [C++ 7.1.5]
1905/// simple-type-specifier
1906/// class-specifier
1907/// enum-specifier
1908/// elaborated-type-specifier [TODO]
1909/// cv-qualifier
1910///
1911/// cv-qualifier: [C++ 7.1.5.1]
1912/// 'const'
1913/// 'volatile'
1914/// [C99] 'restrict'
1915///
1916/// simple-type-specifier: [ C++ 7.1.5.2]
1917/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1918/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1919/// 'char'
1920/// 'wchar_t'
1921/// 'bool'
1922/// 'short'
1923/// 'int'
1924/// 'long'
1925/// 'signed'
1926/// 'unsigned'
1927/// 'float'
1928/// 'double'
1929/// 'void'
1930/// [C99] '_Bool'
1931/// [C99] '_Complex'
1932/// [C99] '_Imaginary' // Removed in TC2?
1933/// [GNU] '_Decimal32'
1934/// [GNU] '_Decimal64'
1935/// [GNU] '_Decimal128'
1936/// [GNU] typeof-specifier
1937/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1938/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001939/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001940/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001941bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001942 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001943 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001944 const ParsedTemplateInfo &TemplateInfo,
1945 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001946 SourceLocation Loc = Tok.getLocation();
1947
1948 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001949 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001950 // If we already have a type specifier, this identifier is not a type.
1951 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1952 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1953 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1954 return false;
John Thompson22334602010-02-05 00:12:22 +00001955 // Check for need to substitute AltiVec keyword tokens.
1956 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1957 break;
1958 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001959 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001960 // Annotate typenames and C++ scope specifiers. If we get one, just
1961 // recurse to handle whatever we get.
1962 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001963 return true;
1964 if (Tok.is(tok::identifier))
1965 return false;
1966 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1967 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001968 case tok::coloncolon: // ::foo::bar
1969 if (NextToken().is(tok::kw_new) || // ::new
1970 NextToken().is(tok::kw_delete)) // ::delete
1971 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001972
Chris Lattner020bab92009-01-04 23:41:41 +00001973 // Annotate typenames and C++ scope specifiers. If we get one, just
1974 // recurse to handle whatever we get.
1975 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001976 return true;
1977 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1978 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Douglas Gregor450c75a2008-11-07 15:42:26 +00001980 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001981 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001982 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1984 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001985 DiagID, T);
1986 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001987 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001988 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1989 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001990
Douglas Gregor450c75a2008-11-07 15:42:26 +00001991 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1992 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1993 // Objective-C interface. If we don't have Objective-C or a '<', this is
1994 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001995 if (Tok.is(tok::less) && getLang().ObjC1)
1996 ParseObjCProtocolQualifiers(DS);
1997
Douglas Gregor450c75a2008-11-07 15:42:26 +00001998 return true;
1999 }
2000
2001 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002002 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002003 break;
2004 case tok::kw_long:
2005 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2007 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002008 else
John McCall49bfce42009-08-03 20:12:06 +00002009 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2010 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002011 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002012 case tok::kw___int64:
2013 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2014 DiagID);
2015 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002016 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002017 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002018 break;
2019 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002020 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2021 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002022 break;
2023 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002024 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2025 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002026 break;
2027 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002028 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2029 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002030 break;
2031 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002033 break;
2034 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002036 break;
2037 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002038 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002039 break;
2040 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002041 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002042 break;
2043 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002045 break;
2046 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002048 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002049 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002051 break;
2052 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002053 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002054 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002055 case tok::kw_bool:
2056 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002058 break;
2059 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2061 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002062 break;
2063 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2065 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002066 break;
2067 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2069 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002070 break;
John Thompson22334602010-02-05 00:12:22 +00002071 case tok::kw___vector:
2072 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2073 break;
2074 case tok::kw___pixel:
2075 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2076 break;
2077
Douglas Gregor450c75a2008-11-07 15:42:26 +00002078 // class-specifier:
2079 case tok::kw_class:
2080 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002081 case tok::kw_union: {
2082 tok::TokenKind Kind = Tok.getKind();
2083 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002084 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2085 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002086 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002087 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002088
2089 // enum-specifier:
2090 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002091 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002092 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002093 return true;
2094
2095 // cv-qualifier:
2096 case tok::kw_const:
2097 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002098 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002099 break;
2100 case tok::kw_volatile:
2101 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002102 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002103 break;
2104 case tok::kw_restrict:
2105 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002106 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002107 break;
2108
2109 // GNU typeof support.
2110 case tok::kw_typeof:
2111 ParseTypeofSpecifier(DS);
2112 return true;
2113
Anders Carlsson74948d02009-06-24 17:47:40 +00002114 // C++0x decltype support.
2115 case tok::kw_decltype:
2116 ParseDecltypeSpecifier(DS);
2117 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002118
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002119 // OpenCL qualifiers:
2120 case tok::kw_private:
2121 if (!getLang().OpenCL)
2122 return false;
2123 case tok::kw___private:
2124 case tok::kw___global:
2125 case tok::kw___local:
2126 case tok::kw___constant:
2127 case tok::kw___read_only:
2128 case tok::kw___write_only:
2129 case tok::kw___read_write:
2130 ParseOpenCLQualifiers(DS);
2131 break;
2132
Anders Carlssonbae27372009-06-26 23:44:14 +00002133 // C++0x auto support.
2134 case tok::kw_auto:
2135 if (!getLang().CPlusPlus0x)
2136 return false;
2137
John McCall49bfce42009-08-03 20:12:06 +00002138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002139 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002140
Eli Friedman53339e02009-06-08 23:27:34 +00002141 case tok::kw___ptr64:
2142 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002143 case tok::kw___cdecl:
2144 case tok::kw___stdcall:
2145 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002146 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002147 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002148 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002149
Dawn Perchik335e16b2010-09-03 01:29:35 +00002150 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002151 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002152 return true;
2153
Douglas Gregor450c75a2008-11-07 15:42:26 +00002154 default:
2155 // Not a type-specifier; do nothing.
2156 return false;
2157 }
2158
2159 // If the specifier combination wasn't legal, issue a diagnostic.
2160 if (isInvalid) {
2161 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002162 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002163 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002164 }
2165 DS.SetRangeEnd(Tok.getLocation());
2166 ConsumeToken(); // whatever we parsed above.
2167 return true;
2168}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002169
Chris Lattner70ae4912007-10-29 04:42:53 +00002170/// ParseStructDeclaration - Parse a struct declaration without the terminating
2171/// semicolon.
2172///
Chris Lattner90a26b02007-01-23 04:38:16 +00002173/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002174/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002175/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002176/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002177/// struct-declarator-list:
2178/// struct-declarator
2179/// struct-declarator-list ',' struct-declarator
2180/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2181/// struct-declarator:
2182/// declarator
2183/// [GNU] declarator attributes[opt]
2184/// declarator[opt] ':' constant-expression
2185/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2186///
Chris Lattnera12405b2008-04-10 06:46:29 +00002187void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002188ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002189 if (Tok.is(tok::kw___extension__)) {
2190 // __extension__ silences extension warnings in the subexpression.
2191 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002192 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002193 return ParseStructDeclaration(DS, Fields);
2194 }
Mike Stump11289f42009-09-09 15:08:12 +00002195
Steve Naroff97170802007-08-20 22:28:22 +00002196 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002197 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002198
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002199 // If there are no declarators, this is a free-standing declaration
2200 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002201 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002202 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002203 return;
2204 }
2205
2206 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002207 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002208 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002209 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002210 FieldDeclarator DeclaratorInfo(DS);
2211
2212 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002213 if (!FirstDeclarator)
2214 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002215
Steve Naroff97170802007-08-20 22:28:22 +00002216 /// struct-declarator: declarator
2217 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002218 if (Tok.isNot(tok::colon)) {
2219 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2220 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002221 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002222 }
Mike Stump11289f42009-09-09 15:08:12 +00002223
Chris Lattner76c72282007-10-09 17:33:22 +00002224 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002225 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002226 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002227 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002228 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002229 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002230 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002231 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002232
Steve Naroff97170802007-08-20 22:28:22 +00002233 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002234 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002235
John McCallcfefb6d2009-11-03 02:38:08 +00002236 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002237 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002238 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002239
Steve Naroff97170802007-08-20 22:28:22 +00002240 // If we don't have a comma, it is either the end of the list (a ';')
2241 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002242 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002243 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002244
Steve Naroff97170802007-08-20 22:28:22 +00002245 // Consume the comma.
2246 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002247
John McCallcfefb6d2009-11-03 02:38:08 +00002248 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002249 }
Steve Naroff97170802007-08-20 22:28:22 +00002250}
2251
2252/// ParseStructUnionBody
2253/// struct-contents:
2254/// struct-declaration-list
2255/// [EXT] empty
2256/// [GNU] "struct-declaration-list" without terminatoring ';'
2257/// struct-declaration-list:
2258/// struct-declaration
2259/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002260/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002261///
Chris Lattner1300fb92007-01-23 23:42:53 +00002262void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002263 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002264 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2265 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002266
Chris Lattner90a26b02007-01-23 04:38:16 +00002267 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002268
Douglas Gregor658b9552009-01-09 22:42:13 +00002269 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002270 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002271
Chris Lattner7b9ace62007-01-23 20:11:08 +00002272 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2273 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002274 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002275 Diag(Tok, diag::ext_empty_struct_union)
2276 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002277
John McCall48871652010-08-21 09:40:31 +00002278 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002279
Chris Lattner7b9ace62007-01-23 20:11:08 +00002280 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002281 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002282 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002283
Chris Lattner736ed5d2007-06-09 05:59:07 +00002284 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002285 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002286 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002287 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002288 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002289 ConsumeToken();
2290 continue;
2291 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002292
2293 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002294 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002295
John McCallcfefb6d2009-11-03 02:38:08 +00002296 if (!Tok.is(tok::at)) {
2297 struct CFieldCallback : FieldCallback {
2298 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002299 Decl *TagDecl;
2300 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002301
John McCall48871652010-08-21 09:40:31 +00002302 CFieldCallback(Parser &P, Decl *TagDecl,
2303 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002304 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2305
John McCall48871652010-08-21 09:40:31 +00002306 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002307 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002308 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002309 FD.D.getDeclSpec().getSourceRange().getBegin(),
2310 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002311 FieldDecls.push_back(Field);
2312 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002313 }
John McCallcfefb6d2009-11-03 02:38:08 +00002314 } Callback(*this, TagDecl, FieldDecls);
2315
2316 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002317 } else { // Handle @defs
2318 ConsumeToken();
2319 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2320 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002321 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002322 continue;
2323 }
2324 ConsumeToken();
2325 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2326 if (!Tok.is(tok::identifier)) {
2327 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002328 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002329 continue;
2330 }
John McCall48871652010-08-21 09:40:31 +00002331 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002332 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002333 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002334 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2335 ConsumeToken();
2336 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002337 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002338
Chris Lattner76c72282007-10-09 17:33:22 +00002339 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002340 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002341 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002342 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002343 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002344 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002345 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2346 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002347 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002348 // If we stopped at a ';', eat it.
2349 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002350 }
2351 }
Mike Stump11289f42009-09-09 15:08:12 +00002352
Steve Naroff33a1e802007-10-29 21:38:07 +00002353 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002354
John McCall084e83d2011-03-24 11:26:52 +00002355 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002356 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002357 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002358
Douglas Gregor0be31a22010-07-02 17:43:08 +00002359 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002360 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002361 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002362 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002363 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002364 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002365}
2366
Chris Lattner3b561a32006-08-13 00:12:11 +00002367/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002368/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002369/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002370///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002371/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2372/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002373/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002374/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002375///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002376/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2377/// [C++0x] enum-head '{' enumerator-list ',' '}'
2378///
2379/// enum-head: [C++0x]
2380/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2381/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2382///
2383/// enum-key: [C++0x]
2384/// 'enum'
2385/// 'enum' 'class'
2386/// 'enum' 'struct'
2387///
2388/// enum-base: [C++0x]
2389/// ':' type-specifier-seq
2390///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002391/// [C++] elaborated-type-specifier:
2392/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2393///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002394void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002395 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002396 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002397 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002398 if (Tok.is(tok::code_completion)) {
2399 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002400 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002401 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002402 }
2403
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002404 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002405 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002406 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002407
Abramo Bagnarad7548482010-05-19 21:37:53 +00002408 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002409 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002410 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002411 return;
2412
2413 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002414 Diag(Tok, diag::err_expected_ident);
2415 if (Tok.isNot(tok::l_brace)) {
2416 // Has no name and is not a definition.
2417 // Skip the rest of this declarator, up until the comma or semicolon.
2418 SkipUntil(tok::comma, true);
2419 return;
2420 }
2421 }
2422 }
Mike Stump11289f42009-09-09 15:08:12 +00002423
Douglas Gregora1aec292011-02-22 20:32:04 +00002424 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002425 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002426 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002427
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002428 if (getLang().CPlusPlus0x &&
2429 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002430 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002431 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2432 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002433 }
2434
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002435 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002436 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2437 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002438 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002439
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002440 // Skip the rest of this declarator, up until the comma or semicolon.
2441 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002442 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002443 }
Mike Stump11289f42009-09-09 15:08:12 +00002444
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002445 // If an identifier is present, consume and remember it.
2446 IdentifierInfo *Name = 0;
2447 SourceLocation NameLoc;
2448 if (Tok.is(tok::identifier)) {
2449 Name = Tok.getIdentifierInfo();
2450 NameLoc = ConsumeToken();
2451 }
Mike Stump11289f42009-09-09 15:08:12 +00002452
Douglas Gregor0bf31402010-10-08 23:50:27 +00002453 if (!Name && IsScopedEnum) {
2454 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2455 // declaration of a scoped enumeration.
2456 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2457 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002458 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002459 }
2460
2461 TypeResult BaseType;
2462
Douglas Gregord1f69f62010-12-01 17:42:47 +00002463 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002464 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002465 bool PossibleBitfield = false;
2466 if (getCurScope()->getFlags() & Scope::ClassScope) {
2467 // If we're in class scope, this can either be an enum declaration with
2468 // an underlying type, or a declaration of a bitfield member. We try to
2469 // use a simple disambiguation scheme first to catch the common cases
2470 // (integer literal, sizeof); if it's still ambiguous, we then consider
2471 // anything that's a simple-type-specifier followed by '(' as an
2472 // expression. This suffices because function types are not valid
2473 // underlying types anyway.
2474 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2475 // If the next token starts an expression, we know we're parsing a
2476 // bit-field. This is the common case.
2477 if (TPR == TPResult::True())
2478 PossibleBitfield = true;
2479 // If the next token starts a type-specifier-seq, it may be either a
2480 // a fixed underlying type or the start of a function-style cast in C++;
2481 // lookahead one more token to see if it's obvious that we have a
2482 // fixed underlying type.
2483 else if (TPR == TPResult::False() &&
2484 GetLookAheadToken(2).getKind() == tok::semi) {
2485 // Consume the ':'.
2486 ConsumeToken();
2487 } else {
2488 // We have the start of a type-specifier-seq, so we have to perform
2489 // tentative parsing to determine whether we have an expression or a
2490 // type.
2491 TentativeParsingAction TPA(*this);
2492
2493 // Consume the ':'.
2494 ConsumeToken();
2495
Douglas Gregora1aec292011-02-22 20:32:04 +00002496 if ((getLang().CPlusPlus &&
2497 isCXXDeclarationSpecifier() != TPResult::True()) ||
2498 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002499 // We'll parse this as a bitfield later.
2500 PossibleBitfield = true;
2501 TPA.Revert();
2502 } else {
2503 // We have a type-specifier-seq.
2504 TPA.Commit();
2505 }
2506 }
2507 } else {
2508 // Consume the ':'.
2509 ConsumeToken();
2510 }
2511
2512 if (!PossibleBitfield) {
2513 SourceRange Range;
2514 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002515
2516 if (!getLang().CPlusPlus0x)
2517 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2518 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002519 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002520 }
2521
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002522 // There are three options here. If we have 'enum foo;', then this is a
2523 // forward declaration. If we have 'enum foo {...' then this is a
2524 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2525 //
2526 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2527 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2528 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2529 //
John McCallfaf5fb42010-08-26 23:41:50 +00002530 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002531 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002532 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002533 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002534 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002535 else
John McCallfaf5fb42010-08-26 23:41:50 +00002536 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002537
2538 // enums cannot be templates, although they can be referenced from a
2539 // template.
2540 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002541 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002542 Diag(Tok, diag::err_enum_template);
2543
2544 // Skip the rest of this declarator, up until the comma or semicolon.
2545 SkipUntil(tok::comma, true);
2546 return;
2547 }
2548
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002549 if (!Name && TUK != Sema::TUK_Definition) {
2550 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2551
2552 // Skip the rest of this declarator, up until the comma or semicolon.
2553 SkipUntil(tok::comma, true);
2554 return;
2555 }
2556
Douglas Gregord6ab8742009-05-28 23:31:59 +00002557 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002558 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002559 const char *PrevSpec = 0;
2560 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002561 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002562 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002563 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002564 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002565 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002566 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002567
Douglas Gregorba41d012010-04-24 16:38:41 +00002568 if (IsDependent) {
2569 // This enum has a dependent nested-name-specifier. Handle it as a
2570 // dependent tag.
2571 if (!Name) {
2572 DS.SetTypeSpecError();
2573 Diag(Tok, diag::err_expected_type_name_after_typename);
2574 return;
2575 }
2576
Douglas Gregor0be31a22010-07-02 17:43:08 +00002577 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002578 TUK, SS, Name, StartLoc,
2579 NameLoc);
2580 if (Type.isInvalid()) {
2581 DS.SetTypeSpecError();
2582 return;
2583 }
2584
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002585 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2586 NameLoc.isValid() ? NameLoc : StartLoc,
2587 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002588 Diag(StartLoc, DiagID) << PrevSpec;
2589
2590 return;
2591 }
Mike Stump11289f42009-09-09 15:08:12 +00002592
John McCall48871652010-08-21 09:40:31 +00002593 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002594 // The action failed to produce an enumeration tag. If this is a
2595 // definition, consume the entire definition.
2596 if (Tok.is(tok::l_brace)) {
2597 ConsumeBrace();
2598 SkipUntil(tok::r_brace);
2599 }
2600
2601 DS.SetTypeSpecError();
2602 return;
2603 }
2604
Chris Lattner76c72282007-10-09 17:33:22 +00002605 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002606 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002607
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002608 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2609 NameLoc.isValid() ? NameLoc : StartLoc,
2610 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002611 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002612}
2613
Chris Lattnerc1915e22007-01-25 07:29:02 +00002614/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2615/// enumerator-list:
2616/// enumerator
2617/// enumerator-list ',' enumerator
2618/// enumerator:
2619/// enumeration-constant
2620/// enumeration-constant '=' constant-expression
2621/// enumeration-constant:
2622/// identifier
2623///
John McCall48871652010-08-21 09:40:31 +00002624void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002625 // Enter the scope of the enum body and start the definition.
2626 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002627 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002628
Chris Lattnerc1915e22007-01-25 07:29:02 +00002629 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002630
Chris Lattner37256fb2007-08-27 17:24:30 +00002631 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002632 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002633 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002634
John McCall48871652010-08-21 09:40:31 +00002635 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002636
John McCall48871652010-08-21 09:40:31 +00002637 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002638
Chris Lattnerc1915e22007-01-25 07:29:02 +00002639 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002640 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002641 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2642 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002643
John McCall811a0f52010-10-22 23:36:17 +00002644 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002645 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002646 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002647
Chris Lattnerc1915e22007-01-25 07:29:02 +00002648 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002649 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002650 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002651 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002652 AssignedVal = ParseConstantExpression();
2653 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002654 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002655 }
Mike Stump11289f42009-09-09 15:08:12 +00002656
Chris Lattnerc1915e22007-01-25 07:29:02 +00002657 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002658 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2659 LastEnumConstDecl,
2660 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002661 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002662 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002663 EnumConstantDecls.push_back(EnumConstDecl);
2664 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002665
Douglas Gregorce66d022010-09-07 14:51:08 +00002666 if (Tok.is(tok::identifier)) {
2667 // We're missing a comma between enumerators.
2668 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2669 Diag(Loc, diag::err_enumerator_list_missing_comma)
2670 << FixItHint::CreateInsertion(Loc, ", ");
2671 continue;
2672 }
2673
Chris Lattner76c72282007-10-09 17:33:22 +00002674 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002675 break;
2676 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002677
2678 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002679 !(getLang().C99 || getLang().CPlusPlus0x))
2680 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2681 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002682 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002683 }
Mike Stump11289f42009-09-09 15:08:12 +00002684
Chris Lattnerc1915e22007-01-25 07:29:02 +00002685 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002686 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002687
Chris Lattnerc1915e22007-01-25 07:29:02 +00002688 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002689 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002690 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002691
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002692 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2693 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002694 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002695
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002696 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002697 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002698}
Chris Lattner3b561a32006-08-13 00:12:11 +00002699
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002700/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002701/// start of a type-qualifier-list.
2702bool Parser::isTypeQualifier() const {
2703 switch (Tok.getKind()) {
2704 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002705
2706 // type-qualifier only in OpenCL
2707 case tok::kw_private:
2708 return getLang().OpenCL;
2709
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002710 // type-qualifier
2711 case tok::kw_const:
2712 case tok::kw_volatile:
2713 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002714 case tok::kw___private:
2715 case tok::kw___local:
2716 case tok::kw___global:
2717 case tok::kw___constant:
2718 case tok::kw___read_only:
2719 case tok::kw___read_write:
2720 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002721 return true;
2722 }
2723}
2724
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002725/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2726/// is definitely a type-specifier. Return false if it isn't part of a type
2727/// specifier or if we're not sure.
2728bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2729 switch (Tok.getKind()) {
2730 default: return false;
2731 // type-specifiers
2732 case tok::kw_short:
2733 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002734 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002735 case tok::kw_signed:
2736 case tok::kw_unsigned:
2737 case tok::kw__Complex:
2738 case tok::kw__Imaginary:
2739 case tok::kw_void:
2740 case tok::kw_char:
2741 case tok::kw_wchar_t:
2742 case tok::kw_char16_t:
2743 case tok::kw_char32_t:
2744 case tok::kw_int:
2745 case tok::kw_float:
2746 case tok::kw_double:
2747 case tok::kw_bool:
2748 case tok::kw__Bool:
2749 case tok::kw__Decimal32:
2750 case tok::kw__Decimal64:
2751 case tok::kw__Decimal128:
2752 case tok::kw___vector:
2753
2754 // struct-or-union-specifier (C99) or class-specifier (C++)
2755 case tok::kw_class:
2756 case tok::kw_struct:
2757 case tok::kw_union:
2758 // enum-specifier
2759 case tok::kw_enum:
2760
2761 // typedef-name
2762 case tok::annot_typename:
2763 return true;
2764 }
2765}
2766
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002767/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002768/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002769bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002770 switch (Tok.getKind()) {
2771 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner020bab92009-01-04 23:41:41 +00002773 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002774 if (TryAltiVecVectorToken())
2775 return true;
2776 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002777 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002778 // Annotate typenames and C++ scope specifiers. If we get one, just
2779 // recurse to handle whatever we get.
2780 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002781 return true;
2782 if (Tok.is(tok::identifier))
2783 return false;
2784 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002785
Chris Lattner020bab92009-01-04 23:41:41 +00002786 case tok::coloncolon: // ::foo::bar
2787 if (NextToken().is(tok::kw_new) || // ::new
2788 NextToken().is(tok::kw_delete)) // ::delete
2789 return false;
2790
Chris Lattner020bab92009-01-04 23:41:41 +00002791 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002792 return true;
2793 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnere37e2332006-08-15 04:50:22 +00002795 // GNU attributes support.
2796 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002797 // GNU typeof support.
2798 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002799
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002800 // type-specifiers
2801 case tok::kw_short:
2802 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002803 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002804 case tok::kw_signed:
2805 case tok::kw_unsigned:
2806 case tok::kw__Complex:
2807 case tok::kw__Imaginary:
2808 case tok::kw_void:
2809 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002810 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002811 case tok::kw_char16_t:
2812 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002813 case tok::kw_int:
2814 case tok::kw_float:
2815 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002816 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002817 case tok::kw__Bool:
2818 case tok::kw__Decimal32:
2819 case tok::kw__Decimal64:
2820 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002821 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002822
Chris Lattner861a2262008-04-13 18:59:07 +00002823 // struct-or-union-specifier (C99) or class-specifier (C++)
2824 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002825 case tok::kw_struct:
2826 case tok::kw_union:
2827 // enum-specifier
2828 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002829
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002830 // type-qualifier
2831 case tok::kw_const:
2832 case tok::kw_volatile:
2833 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002834
2835 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002836 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002837 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002838
Chris Lattner409bf7d2008-10-20 00:25:30 +00002839 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2840 case tok::less:
2841 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002842
Steve Naroff44ac7772008-12-25 14:16:32 +00002843 case tok::kw___cdecl:
2844 case tok::kw___stdcall:
2845 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002846 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002847 case tok::kw___w64:
2848 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002849 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002850
2851 case tok::kw___private:
2852 case tok::kw___local:
2853 case tok::kw___global:
2854 case tok::kw___constant:
2855 case tok::kw___read_only:
2856 case tok::kw___read_write:
2857 case tok::kw___write_only:
2858
Eli Friedman53339e02009-06-08 23:27:34 +00002859 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002860
2861 case tok::kw_private:
2862 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002863 }
2864}
2865
Chris Lattneracd58a32006-08-06 17:24:14 +00002866/// isDeclarationSpecifier() - Return true if the current token is part of a
2867/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002868///
2869/// \param DisambiguatingWithExpression True to indicate that the purpose of
2870/// this check is to disambiguate between an expression and a declaration.
2871bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002872 switch (Tok.getKind()) {
2873 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002874
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002875 case tok::kw_private:
2876 return getLang().OpenCL;
2877
Chris Lattner020bab92009-01-04 23:41:41 +00002878 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002879 // Unfortunate hack to support "Class.factoryMethod" notation.
2880 if (getLang().ObjC1 && NextToken().is(tok::period))
2881 return false;
John Thompson22334602010-02-05 00:12:22 +00002882 if (TryAltiVecVectorToken())
2883 return true;
2884 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002885 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002886 // Annotate typenames and C++ scope specifiers. If we get one, just
2887 // recurse to handle whatever we get.
2888 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002889 return true;
2890 if (Tok.is(tok::identifier))
2891 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002892
2893 // If we're in Objective-C and we have an Objective-C class type followed
2894 // by an identifier and then either ':' or ']', in a place where an
2895 // expression is permitted, then this is probably a class message send
2896 // missing the initial '['. In this case, we won't consider this to be
2897 // the start of a declaration.
2898 if (DisambiguatingWithExpression &&
2899 isStartOfObjCClassMessageMissingOpenBracket())
2900 return false;
2901
John McCall1f476a12010-02-26 08:45:28 +00002902 return isDeclarationSpecifier();
2903
Chris Lattner020bab92009-01-04 23:41:41 +00002904 case tok::coloncolon: // ::foo::bar
2905 if (NextToken().is(tok::kw_new) || // ::new
2906 NextToken().is(tok::kw_delete)) // ::delete
2907 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002908
Chris Lattner020bab92009-01-04 23:41:41 +00002909 // Annotate typenames and C++ scope specifiers. If we get one, just
2910 // recurse to handle whatever we get.
2911 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002912 return true;
2913 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002914
Chris Lattneracd58a32006-08-06 17:24:14 +00002915 // storage-class-specifier
2916 case tok::kw_typedef:
2917 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002918 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002919 case tok::kw_static:
2920 case tok::kw_auto:
2921 case tok::kw_register:
2922 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002923
Chris Lattneracd58a32006-08-06 17:24:14 +00002924 // type-specifiers
2925 case tok::kw_short:
2926 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002927 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00002928 case tok::kw_signed:
2929 case tok::kw_unsigned:
2930 case tok::kw__Complex:
2931 case tok::kw__Imaginary:
2932 case tok::kw_void:
2933 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002934 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002935 case tok::kw_char16_t:
2936 case tok::kw_char32_t:
2937
Chris Lattneracd58a32006-08-06 17:24:14 +00002938 case tok::kw_int:
2939 case tok::kw_float:
2940 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002941 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002942 case tok::kw__Bool:
2943 case tok::kw__Decimal32:
2944 case tok::kw__Decimal64:
2945 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002946 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002947
Chris Lattner861a2262008-04-13 18:59:07 +00002948 // struct-or-union-specifier (C99) or class-specifier (C++)
2949 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002950 case tok::kw_struct:
2951 case tok::kw_union:
2952 // enum-specifier
2953 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002954
Chris Lattneracd58a32006-08-06 17:24:14 +00002955 // type-qualifier
2956 case tok::kw_const:
2957 case tok::kw_volatile:
2958 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002959
Chris Lattneracd58a32006-08-06 17:24:14 +00002960 // function-specifier
2961 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002962 case tok::kw_virtual:
2963 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002964
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002965 // static_assert-declaration
2966 case tok::kw__Static_assert:
2967
Chris Lattner599e47e2007-08-09 17:01:07 +00002968 // GNU typeof support.
2969 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002970
Chris Lattner599e47e2007-08-09 17:01:07 +00002971 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002972 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002973 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002974
Chris Lattner8b2ec162008-07-26 03:38:44 +00002975 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2976 case tok::less:
2977 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002978
Douglas Gregor19b7acf2011-04-27 05:41:15 +00002979 // typedef-name
2980 case tok::annot_typename:
2981 return !DisambiguatingWithExpression ||
2982 !isStartOfObjCClassMessageMissingOpenBracket();
2983
Steve Narofff192fab2009-01-06 19:34:12 +00002984 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002985 case tok::kw___cdecl:
2986 case tok::kw___stdcall:
2987 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002988 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002989 case tok::kw___w64:
2990 case tok::kw___ptr64:
2991 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002992 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002993
2994 case tok::kw___private:
2995 case tok::kw___local:
2996 case tok::kw___global:
2997 case tok::kw___constant:
2998 case tok::kw___read_only:
2999 case tok::kw___read_write:
3000 case tok::kw___write_only:
3001
Eli Friedman53339e02009-06-08 23:27:34 +00003002 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003003 }
3004}
3005
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003006bool Parser::isConstructorDeclarator() {
3007 TentativeParsingAction TPA(*this);
3008
3009 // Parse the C++ scope specifier.
3010 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003011 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003012 TPA.Revert();
3013 return false;
3014 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003015
3016 // Parse the constructor name.
3017 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3018 // We already know that we have a constructor name; just consume
3019 // the token.
3020 ConsumeToken();
3021 } else {
3022 TPA.Revert();
3023 return false;
3024 }
3025
3026 // Current class name must be followed by a left parentheses.
3027 if (Tok.isNot(tok::l_paren)) {
3028 TPA.Revert();
3029 return false;
3030 }
3031 ConsumeParen();
3032
3033 // A right parentheses or ellipsis signals that we have a constructor.
3034 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3035 TPA.Revert();
3036 return true;
3037 }
3038
3039 // If we need to, enter the specified scope.
3040 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003041 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003042 DeclScopeObj.EnterDeclaratorScope();
3043
Francois Pichet79f3a872011-01-31 04:54:32 +00003044 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003045 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003046 MaybeParseMicrosoftAttributes(Attrs);
3047
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003048 // Check whether the next token(s) are part of a declaration
3049 // specifier, in which case we have the start of a parameter and,
3050 // therefore, we know that this is a constructor.
3051 bool IsConstructor = isDeclarationSpecifier();
3052 TPA.Revert();
3053 return IsConstructor;
3054}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003055
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003056/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003057/// type-qualifier-list: [C99 6.7.5]
3058/// type-qualifier
3059/// [vendor] attributes
3060/// [ only if VendorAttributesAllowed=true ]
3061/// type-qualifier-list type-qualifier
3062/// [vendor] type-qualifier-list attributes
3063/// [ only if VendorAttributesAllowed=true ]
3064/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3065/// [ only if CXX0XAttributesAllowed=true ]
3066/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003067///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003068void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3069 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003070 bool CXX0XAttributesAllowed) {
3071 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3072 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003073 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003074 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003075 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003076 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003077 else
3078 Diag(Loc, diag::err_attributes_not_allowed);
3079 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003080
3081 SourceLocation EndLoc;
3082
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003083 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003084 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003085 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003086 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003087 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003088
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003089 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003090 case tok::code_completion:
3091 Actions.CodeCompleteTypeQualifiers(DS);
3092 ConsumeCodeCompletionToken();
3093 break;
3094
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003095 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003096 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3097 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003098 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003099 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003100 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3101 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003102 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003103 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003104 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3105 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003106 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003107
3108 // OpenCL qualifiers:
3109 case tok::kw_private:
3110 if (!getLang().OpenCL)
3111 goto DoneWithTypeQuals;
3112 case tok::kw___private:
3113 case tok::kw___global:
3114 case tok::kw___local:
3115 case tok::kw___constant:
3116 case tok::kw___read_only:
3117 case tok::kw___write_only:
3118 case tok::kw___read_write:
3119 ParseOpenCLQualifiers(DS);
3120 break;
3121
Eli Friedman53339e02009-06-08 23:27:34 +00003122 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003123 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003124 case tok::kw___cdecl:
3125 case tok::kw___stdcall:
3126 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003127 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003128 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003129 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003130 continue;
3131 }
3132 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003133 case tok::kw___pascal:
3134 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003135 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003136 continue;
3137 }
3138 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003139 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003140 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003141 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003142 continue; // do *not* consume the next token!
3143 }
3144 // otherwise, FALL THROUGH!
3145 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003146 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003147 // If this is not a type-qualifier token, we're done reading type
3148 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003149 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003150 if (EndLoc.isValid())
3151 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003152 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003153 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003154
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003155 // If the specifier combination wasn't legal, issue a diagnostic.
3156 if (isInvalid) {
3157 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003158 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003159 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003160 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003161 }
3162}
3163
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003164
3165/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3166///
3167void Parser::ParseDeclarator(Declarator &D) {
3168 /// This implements the 'declarator' production in the C grammar, then checks
3169 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003170 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003171}
3172
Sebastian Redlbd150f42008-11-21 19:14:01 +00003173/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3174/// is parsed by the function passed to it. Pass null, and the direct-declarator
3175/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003176/// ptr-operator production.
3177///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003178/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3179/// [C] pointer[opt] direct-declarator
3180/// [C++] direct-declarator
3181/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003182///
3183/// pointer: [C99 6.7.5]
3184/// '*' type-qualifier-list[opt]
3185/// '*' type-qualifier-list[opt] pointer
3186///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003187/// ptr-operator:
3188/// '*' cv-qualifier-seq[opt]
3189/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003190/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003191/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003192/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003193/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003194void Parser::ParseDeclaratorInternal(Declarator &D,
3195 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003196 if (Diags.hasAllExtensionsSilenced())
3197 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003198
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003199 // C++ member pointers start with a '::' or a nested-name.
3200 // Member pointers get special handling, since there's no place for the
3201 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003202 if (getLang().CPlusPlus &&
3203 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3204 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003205 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003206 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003207
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003208 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003209 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003210 // The scope spec really belongs to the direct-declarator.
3211 D.getCXXScopeSpec() = SS;
3212 if (DirectDeclParser)
3213 (this->*DirectDeclParser)(D);
3214 return;
3215 }
3216
3217 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003218 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003219 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003220 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003221 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003222
3223 // Recurse to parse whatever is left.
3224 ParseDeclaratorInternal(D, DirectDeclParser);
3225
3226 // Sema will have to catch (syntactically invalid) pointers into global
3227 // scope. It has to catch pointers into namespace scope anyway.
3228 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003229 Loc),
3230 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003231 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003232 return;
3233 }
3234 }
3235
3236 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003237 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003238 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003239 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003240 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003241 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003242 if (DirectDeclParser)
3243 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003244 return;
3245 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003246
Sebastian Redled0f3b02009-03-15 22:02:01 +00003247 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3248 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003249 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003250 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003251
Chris Lattner9eac9312009-03-27 04:18:06 +00003252 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003253 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003254 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003255
Bill Wendling3708c182007-05-27 10:15:43 +00003256 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003257 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003258
Bill Wendling3708c182007-05-27 10:15:43 +00003259 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003260 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003261 if (Kind == tok::star)
3262 // Remember that we parsed a pointer type, and remember the type-quals.
3263 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003264 DS.getConstSpecLoc(),
3265 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003266 DS.getRestrictSpecLoc()),
3267 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003268 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003269 else
3270 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003271 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003272 Loc),
3273 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003274 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003275 } else {
3276 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003277 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003278
Sebastian Redl3b27be62009-03-23 00:00:23 +00003279 // Complain about rvalue references in C++03, but then go on and build
3280 // the declarator.
3281 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003282 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003283
Bill Wendling93efb222007-06-02 23:28:54 +00003284 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3285 // cv-qualifiers are introduced through the use of a typedef or of a
3286 // template type argument, in which case the cv-qualifiers are ignored.
3287 //
3288 // [GNU] Retricted references are allowed.
3289 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003290 // [C++0x] Attributes on references are not allowed.
3291 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003292 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003293
3294 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3295 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3296 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003297 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003298 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3299 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003300 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003301 }
Bill Wendling3708c182007-05-27 10:15:43 +00003302
3303 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003304 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003305
Douglas Gregor66583c52008-11-03 15:51:28 +00003306 if (D.getNumTypeObjects() > 0) {
3307 // C++ [dcl.ref]p4: There shall be no references to references.
3308 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3309 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003310 if (const IdentifierInfo *II = D.getIdentifier())
3311 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3312 << II;
3313 else
3314 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3315 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003316
Sebastian Redlbd150f42008-11-21 19:14:01 +00003317 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003318 // can go ahead and build the (technically ill-formed)
3319 // declarator: reference collapsing will take care of it.
3320 }
3321 }
3322
Bill Wendling3708c182007-05-27 10:15:43 +00003323 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003324 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003325 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003326 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003327 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003328 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003329}
3330
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003331/// ParseDirectDeclarator
3332/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003333/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003334/// '(' declarator ')'
3335/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003336/// [C90] direct-declarator '[' constant-expression[opt] ']'
3337/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3338/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3339/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3340/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003341/// direct-declarator '(' parameter-type-list ')'
3342/// direct-declarator '(' identifier-list[opt] ')'
3343/// [GNU] direct-declarator '(' parameter-forward-declarations
3344/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003345/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3346/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003347/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003348///
3349/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003350/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003351/// '::'[opt] nested-name-specifier[opt] type-name
3352///
3353/// id-expression: [C++ 5.1]
3354/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003355/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003356///
3357/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003358/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003359/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003360/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003361/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003362/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003363///
Chris Lattneracd58a32006-08-06 17:24:14 +00003364void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003365 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003366
Douglas Gregor7861a802009-11-03 01:35:08 +00003367 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3368 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003369 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003370 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003371 }
3372
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003373 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003374 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003375 // Change the declaration context for name lookup, until this function
3376 // is exited (and the declarator has been parsed).
3377 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003378 }
3379
Douglas Gregor27b4c162010-12-23 22:44:42 +00003380 // C++0x [dcl.fct]p14:
3381 // There is a syntactic ambiguity when an ellipsis occurs at the end
3382 // of a parameter-declaration-clause without a preceding comma. In
3383 // this case, the ellipsis is parsed as part of the
3384 // abstract-declarator if the type of the parameter names a template
3385 // parameter pack that has not been expanded; otherwise, it is parsed
3386 // as part of the parameter-declaration-clause.
3387 if (Tok.is(tok::ellipsis) &&
3388 !((D.getContext() == Declarator::PrototypeContext ||
3389 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003390 NextToken().is(tok::r_paren) &&
3391 !Actions.containsUnexpandedParameterPacks(D)))
3392 D.setEllipsisLoc(ConsumeToken());
3393
Douglas Gregor7861a802009-11-03 01:35:08 +00003394 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3395 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3396 // We found something that indicates the start of an unqualified-id.
3397 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003398 bool AllowConstructorName;
3399 if (D.getDeclSpec().hasTypeSpecifier())
3400 AllowConstructorName = false;
3401 else if (D.getCXXScopeSpec().isSet())
3402 AllowConstructorName =
3403 (D.getContext() == Declarator::FileContext ||
3404 (D.getContext() == Declarator::MemberContext &&
3405 D.getDeclSpec().isFriendSpecified()));
3406 else
3407 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3408
Douglas Gregor7861a802009-11-03 01:35:08 +00003409 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3410 /*EnteringContext=*/true,
3411 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003412 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003413 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003414 D.getName()) ||
3415 // Once we're past the identifier, if the scope was bad, mark the
3416 // whole declarator bad.
3417 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003418 D.SetIdentifier(0, Tok.getLocation());
3419 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003420 } else {
3421 // Parsed the unqualified-id; update range information and move along.
3422 if (D.getSourceRange().getBegin().isInvalid())
3423 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3424 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003425 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003426 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003427 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003428 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003429 assert(!getLang().CPlusPlus &&
3430 "There's a C++-specific check for tok::identifier above");
3431 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3432 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3433 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003434 goto PastIdentifier;
3435 }
3436
3437 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003438 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003439 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003440 // Example: 'char (*X)' or 'int (*XX)(void)'
3441 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003442
3443 // If the declarator was parenthesized, we entered the declarator
3444 // scope when parsing the parenthesized declarator, then exited
3445 // the scope already. Re-enter the scope, if we need to.
3446 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003447 // If there was an error parsing parenthesized declarator, declarator
3448 // scope may have been enterred before. Don't do it again.
3449 if (!D.isInvalidType() &&
3450 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003451 // Change the declaration context for name lookup, until this function
3452 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003453 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003454 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003455 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003456 // This could be something simple like "int" (in which case the declarator
3457 // portion is empty), if an abstract-declarator is allowed.
3458 D.SetIdentifier(0, Tok.getLocation());
3459 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003460 if (D.getContext() == Declarator::MemberContext)
3461 Diag(Tok, diag::err_expected_member_name_or_semi)
3462 << D.getDeclSpec().getSourceRange();
3463 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003464 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003465 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003466 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003467 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003468 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003469 }
Mike Stump11289f42009-09-09 15:08:12 +00003470
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003471 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003472 assert(D.isPastIdentifier() &&
3473 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003474
Alexis Hunt96d5c762009-11-21 08:43:09 +00003475 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003476 if (D.getIdentifier())
3477 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003478
Chris Lattneracd58a32006-08-06 17:24:14 +00003479 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003480 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003481 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3482 // In such a case, check if we actually have a function declarator; if it
3483 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003484 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3485 // When not in file scope, warn for ambiguous function declarators, just
3486 // in case the author intended it as a variable definition.
3487 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3488 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3489 break;
3490 }
John McCall084e83d2011-03-24 11:26:52 +00003491 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003492 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003493 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003494 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003495 } else {
3496 break;
3497 }
3498 }
3499}
3500
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003501/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3502/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003503/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003504/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3505///
3506/// direct-declarator:
3507/// '(' declarator ')'
3508/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003509/// direct-declarator '(' parameter-type-list ')'
3510/// direct-declarator '(' identifier-list[opt] ')'
3511/// [GNU] direct-declarator '(' parameter-forward-declarations
3512/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003513///
3514void Parser::ParseParenDeclarator(Declarator &D) {
3515 SourceLocation StartLoc = ConsumeParen();
3516 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003517
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003518 // Eat any attributes before we look at whether this is a grouping or function
3519 // declarator paren. If this is a grouping paren, the attribute applies to
3520 // the type being built up, for example:
3521 // int (__attribute__(()) *x)(long y)
3522 // If this ends up not being a grouping paren, the attribute applies to the
3523 // first argument, for example:
3524 // int (__attribute__(()) int x)
3525 // In either case, we need to eat any attributes to be able to determine what
3526 // sort of paren this is.
3527 //
John McCall084e83d2011-03-24 11:26:52 +00003528 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003529 bool RequiresArg = false;
3530 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003531 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003532
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003533 // We require that the argument list (if this is a non-grouping paren) be
3534 // present even if the attribute list was empty.
3535 RequiresArg = true;
3536 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003537 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003538 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003539 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3540 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003541 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003542 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003543 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003544 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003545 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003546
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003547 // If we haven't past the identifier yet (or where the identifier would be
3548 // stored, if this is an abstract declarator), then this is probably just
3549 // grouping parens. However, if this could be an abstract-declarator, then
3550 // this could also be the start of function arguments (consider 'void()').
3551 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003552
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003553 if (!D.mayOmitIdentifier()) {
3554 // If this can't be an abstract-declarator, this *must* be a grouping
3555 // paren, because we haven't seen the identifier yet.
3556 isGrouping = true;
3557 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003558 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003559 isDeclarationSpecifier()) { // 'int(int)' is a function.
3560 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3561 // considered to be a type, not a K&R identifier-list.
3562 isGrouping = false;
3563 } else {
3564 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3565 isGrouping = true;
3566 }
Mike Stump11289f42009-09-09 15:08:12 +00003567
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003568 // If this is a grouping paren, handle:
3569 // direct-declarator: '(' declarator ')'
3570 // direct-declarator: '(' attributes declarator ')'
3571 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003572 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003573 D.setGroupingParens(true);
3574
Sebastian Redlbd150f42008-11-21 19:14:01 +00003575 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003576 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003577 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003578 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3579 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003580
3581 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003582 return;
3583 }
Mike Stump11289f42009-09-09 15:08:12 +00003584
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003585 // Okay, if this wasn't a grouping paren, it must be the start of a function
3586 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003587 // identifier (and remember where it would have been), then call into
3588 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003589 D.SetIdentifier(0, Tok.getLocation());
3590
John McCall53fa7142010-12-24 02:08:15 +00003591 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003592}
3593
3594/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3595/// declarator D up to a paren, which indicates that we are parsing function
3596/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003597///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003598/// If AttrList is non-null, then the caller parsed those arguments immediately
3599/// after the open paren - they should be considered to be the first argument of
3600/// a parameter. If RequiresArg is true, then the first argument of the
3601/// function is required to be present and required to not be an identifier
3602/// list.
3603///
Chris Lattneracd58a32006-08-06 17:24:14 +00003604/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003605/// parameter-type-list: [C99 6.7.5]
3606/// parameter-list
3607/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003608/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003609///
3610/// parameter-list: [C99 6.7.5]
3611/// parameter-declaration
3612/// parameter-list ',' parameter-declaration
3613///
3614/// parameter-declaration: [C99 6.7.5]
3615/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003616/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003617/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003618/// declaration-specifiers abstract-declarator[opt]
3619/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003620/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003621/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003622///
Douglas Gregor54992352011-01-26 03:43:54 +00003623/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3624/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003625///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003626/// [C++0x] exception-specification:
3627/// dynamic-exception-specification
3628/// noexcept-specification
3629///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003630void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003631 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003632 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003633 // lparen is already consumed!
3634 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003635
Douglas Gregor7fb25412010-10-01 18:44:50 +00003636 ParsedType TrailingReturnType;
3637
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003638 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003639 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003640 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003641 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003642
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003643 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003644
3645 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003646 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003647 SourceLocation RefQualifierLoc;
3648 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003649 ExceptionSpecificationType ESpecType = EST_None;
3650 SourceRange ESpecRange;
3651 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3652 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3653 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003654 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003655 MaybeParseCXX0XAttributes(attrs);
3656
Chris Lattnercf0bab22008-12-18 07:02:59 +00003657 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003658 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003659 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003660
Douglas Gregor54992352011-01-26 03:43:54 +00003661 // Parse ref-qualifier[opt]
3662 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3663 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003664 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003665
Douglas Gregor54992352011-01-26 03:43:54 +00003666 RefQualifierIsLValueRef = Tok.is(tok::amp);
3667 RefQualifierLoc = ConsumeToken();
3668 EndLoc = RefQualifierLoc;
3669 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003670
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003671 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003672 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3673 DynamicExceptions,
3674 DynamicExceptionRanges,
3675 NoexceptExpr);
3676 if (ESpecType != EST_None)
3677 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003678
3679 // Parse trailing-return-type.
3680 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3681 TrailingReturnType = ParseTrailingReturnType().get();
3682 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003683 }
3684
Chris Lattner371ed4e2008-04-06 06:57:35 +00003685 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003686 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003687 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003688 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003689 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003690 /*arglist*/ 0, 0,
3691 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003692 RefQualifierIsLValueRef,
3693 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003694 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003695 DynamicExceptions.data(),
3696 DynamicExceptionRanges.data(),
3697 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003698 NoexceptExpr.isUsable() ?
3699 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003700 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003701 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003702 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003703 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003704 }
3705
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003706 // Alternatively, this parameter list may be an identifier list form for a
3707 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003708 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3709 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003710 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003711 // K&R identifier lists can't have typedefs as identifiers, per
3712 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003713 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003714 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003715
Steve Naroffb0486722009-01-28 19:16:40 +00003716 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003717 // normal declarators, not for abstract-declarators. Get the first
3718 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003719 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003720 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003721
3722 // Identifier lists follow a really simple grammar: the identifiers can
3723 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3724 // identifier lists are really rare in the brave new modern world, and it
3725 // is very common for someone to typo a type in a non-k&r style list. If
3726 // we are presented with something like: "void foo(intptr x, float y)",
3727 // we don't want to start parsing the function declarator as though it is
3728 // a K&R style declarator just because intptr is an invalid type.
3729 //
3730 // To handle this, we check to see if the token after the first identifier
3731 // is a "," or ")". Only if so, do we parse it as an identifier list.
3732 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3733 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3734 FirstTok.getIdentifierInfo(),
3735 FirstTok.getLocation(), D);
3736
3737 // If we get here, the code is invalid. Push the first identifier back
3738 // into the token stream and parse the first argument as an (invalid)
3739 // normal argument declarator.
3740 PP.EnterToken(Tok);
3741 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003742 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003743 }
Mike Stump11289f42009-09-09 15:08:12 +00003744
Chris Lattner371ed4e2008-04-06 06:57:35 +00003745 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003746
Chris Lattner371ed4e2008-04-06 06:57:35 +00003747 // Build up an array of information about the parsed arguments.
3748 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003749
3750 // Enter function-declaration scope, limiting any declarators to the
3751 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003752 ParseScope PrototypeScope(this,
3753 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003754
Chris Lattner371ed4e2008-04-06 06:57:35 +00003755 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003756 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003757 while (1) {
3758 if (Tok.is(tok::ellipsis)) {
3759 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003760 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003761 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003762 }
Mike Stump11289f42009-09-09 15:08:12 +00003763
Chris Lattner371ed4e2008-04-06 06:57:35 +00003764 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003765 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003766 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003767
3768 // Skip any Microsoft attributes before a param.
3769 if (getLang().Microsoft && Tok.is(tok::l_square))
3770 ParseMicrosoftAttributes(DS.getAttributes());
3771
3772 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003773
3774 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003775 // Take them so that we only apply the attributes to the first parameter.
3776 DS.takeAttributesFrom(attrs);
3777
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003778 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003779
Chris Lattner371ed4e2008-04-06 06:57:35 +00003780 // Parse the declarator. This is "PrototypeContext", because we must
3781 // accept either 'declarator' or 'abstract-declarator' here.
3782 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3783 ParseDeclarator(ParmDecl);
3784
3785 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003786 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003787
Chris Lattner371ed4e2008-04-06 06:57:35 +00003788 // Remember this parsed parameter in ParamInfo.
3789 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003790
Douglas Gregor4d87df52008-12-16 21:30:33 +00003791 // DefArgToks is used when the parsing of default arguments needs
3792 // to be delayed.
3793 CachedTokens *DefArgToks = 0;
3794
Chris Lattner371ed4e2008-04-06 06:57:35 +00003795 // If no parameter was specified, verify that *something* was specified,
3796 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003797 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3798 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003799 // Completely missing, emit error.
3800 Diag(DSStart, diag::err_missing_param);
3801 } else {
3802 // Otherwise, we have something. Add it and let semantic analysis try
3803 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003804
Chris Lattner371ed4e2008-04-06 06:57:35 +00003805 // Inform the actions module about the parameter declarator, so it gets
3806 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003807 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003808
3809 // Parse the default argument, if any. We parse the default
3810 // arguments in all dialects; the semantic analysis in
3811 // ActOnParamDefaultArgument will reject the default argument in
3812 // C.
3813 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003814 SourceLocation EqualLoc = Tok.getLocation();
3815
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003816 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003817 if (D.getContext() == Declarator::MemberContext) {
3818 // If we're inside a class definition, cache the tokens
3819 // corresponding to the default argument. We'll actually parse
3820 // them when we see the end of the class definition.
3821 // FIXME: Templates will require something similar.
3822 // FIXME: Can we use a smart pointer for Toks?
3823 DefArgToks = new CachedTokens;
3824
Mike Stump11289f42009-09-09 15:08:12 +00003825 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003826 /*StopAtSemi=*/true,
3827 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003828 delete DefArgToks;
3829 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003830 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003831 } else {
3832 // Mark the end of the default argument so that we know when to
3833 // stop when we parse it later on.
3834 Token DefArgEnd;
3835 DefArgEnd.startToken();
3836 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3837 DefArgEnd.setLocation(Tok.getLocation());
3838 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003839 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003840 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003841 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003842 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003843 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003844 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003845
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003846 // The argument isn't actually potentially evaluated unless it is
3847 // used.
3848 EnterExpressionEvaluationContext Eval(Actions,
3849 Sema::PotentiallyEvaluatedIfUsed);
3850
John McCalldadc5752010-08-24 06:29:42 +00003851 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003852 if (DefArgResult.isInvalid()) {
3853 Actions.ActOnParamDefaultArgumentError(Param);
3854 SkipUntil(tok::comma, tok::r_paren, true, true);
3855 } else {
3856 // Inform the actions module about the default argument
3857 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003858 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003859 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003860 }
3861 }
Mike Stump11289f42009-09-09 15:08:12 +00003862
3863 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3864 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003865 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003866 }
3867
3868 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003869 if (Tok.isNot(tok::comma)) {
3870 if (Tok.is(tok::ellipsis)) {
3871 IsVariadic = true;
3872 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3873
3874 if (!getLang().CPlusPlus) {
3875 // We have ellipsis without a preceding ',', which is ill-formed
3876 // in C. Complain and provide the fix.
3877 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003878 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003879 }
3880 }
3881
3882 break;
3883 }
Mike Stump11289f42009-09-09 15:08:12 +00003884
Chris Lattner371ed4e2008-04-06 06:57:35 +00003885 // Consume the comma.
3886 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003887 }
Mike Stump11289f42009-09-09 15:08:12 +00003888
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003889 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003890 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003891
John McCall084e83d2011-03-24 11:26:52 +00003892 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003893 SourceLocation RefQualifierLoc;
3894 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003895 ExceptionSpecificationType ESpecType = EST_None;
3896 SourceRange ESpecRange;
3897 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3898 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3899 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003900
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003901 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003902 MaybeParseCXX0XAttributes(attrs);
3903
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003904 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003905 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003906 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003907 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003908
Douglas Gregor54992352011-01-26 03:43:54 +00003909 // Parse ref-qualifier[opt]
3910 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3911 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003912 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003913
3914 RefQualifierIsLValueRef = Tok.is(tok::amp);
3915 RefQualifierLoc = ConsumeToken();
3916 EndLoc = RefQualifierLoc;
3917 }
3918
Sebastian Redl965b0e32011-03-05 14:45:16 +00003919 // FIXME: We should leave the prototype scope before parsing the exception
3920 // specification, and then reenter it when parsing the trailing return type.
3921 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3922
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003923 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003924 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3925 DynamicExceptions,
3926 DynamicExceptionRanges,
3927 NoexceptExpr);
3928 if (ESpecType != EST_None)
3929 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003930
3931 // Parse trailing-return-type.
3932 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3933 TrailingReturnType = ParseTrailingReturnType().get();
3934 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003935 }
3936
Douglas Gregor7fb25412010-10-01 18:44:50 +00003937 // Leave prototype scope.
3938 PrototypeScope.Exit();
3939
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003940 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003941 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003942 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003943 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003944 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003945 RefQualifierIsLValueRef,
3946 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003947 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003948 DynamicExceptions.data(),
3949 DynamicExceptionRanges.data(),
3950 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003951 NoexceptExpr.isUsable() ?
3952 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003953 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003954 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003955 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003956}
Chris Lattneracd58a32006-08-06 17:24:14 +00003957
Chris Lattner6c940e62008-04-06 06:34:08 +00003958/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3959/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003960/// first identifier has already been consumed, and the current token is the
3961/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003962///
3963/// identifier-list: [C99 6.7.5]
3964/// identifier
3965/// identifier-list ',' identifier
3966///
3967void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003968 IdentifierInfo *FirstIdent,
3969 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003970 Declarator &D) {
3971 // Build up an array of information about the parsed arguments.
3972 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3973 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003974
Chris Lattner6c940e62008-04-06 06:34:08 +00003975 // If there was no identifier specified for the declarator, either we are in
3976 // an abstract-declarator, or we are in a parameter declarator which was found
3977 // to be abstract. In abstract-declarators, identifier lists are not valid:
3978 // diagnose this.
3979 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003980 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003981
Chris Lattner9453ab82010-05-14 17:23:36 +00003982 // The first identifier was already read, and is known to be the first
3983 // identifier in the list. Remember this identifier in ParamInfo.
3984 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003985 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003986
Chris Lattner6c940e62008-04-06 06:34:08 +00003987 while (Tok.is(tok::comma)) {
3988 // Eat the comma.
3989 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003990
Chris Lattner9186f552008-04-06 06:39:19 +00003991 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003992 if (Tok.isNot(tok::identifier)) {
3993 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003994 SkipUntil(tok::r_paren);
3995 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003996 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003997
Chris Lattner6c940e62008-04-06 06:34:08 +00003998 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003999
4000 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004001 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00004002 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00004003
Chris Lattner6c940e62008-04-06 06:34:08 +00004004 // Verify that the argument identifier has not already been mentioned.
4005 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004006 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00004007 } else {
4008 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00004009 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00004010 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00004011 0));
Chris Lattner9186f552008-04-06 06:39:19 +00004012 }
Mike Stump11289f42009-09-09 15:08:12 +00004013
Chris Lattner6c940e62008-04-06 06:34:08 +00004014 // Eat the identifier.
4015 ConsumeToken();
4016 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004017
4018 // If we have the closing ')', eat it and we're done.
4019 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4020
Chris Lattner9186f552008-04-06 06:39:19 +00004021 // Remember that we parsed a function type, and remember the attributes. This
4022 // function type is always a K&R style function type, which is not varargs and
4023 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00004024 ParsedAttributes attrs(AttrFactory);
4025 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00004026 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00004027 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00004028 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00004029 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00004030 EST_None, SourceLocation(), 0, 0,
4031 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00004032 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00004033}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004034
Chris Lattnere8074e62006-08-06 18:30:15 +00004035/// [C90] direct-declarator '[' constant-expression[opt] ']'
4036/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4037/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4038/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4039/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4040void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004041 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004042
Chris Lattner84a11622008-12-18 07:27:21 +00004043 // C array syntax has many features, but by-far the most common is [] and [4].
4044 // This code does a fast path to handle some of the most obvious cases.
4045 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004046 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004047 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004048 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004049
Chris Lattner84a11622008-12-18 07:27:21 +00004050 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004051 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004052 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004053 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004054 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004055 return;
4056 } else if (Tok.getKind() == tok::numeric_constant &&
4057 GetLookAheadToken(1).is(tok::r_square)) {
4058 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004059 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004060 ConsumeToken();
4061
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004062 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004063 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004064 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004065
Chris Lattner84a11622008-12-18 07:27:21 +00004066 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004067 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004068 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004069 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004070 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004071 return;
4072 }
Mike Stump11289f42009-09-09 15:08:12 +00004073
Chris Lattnere8074e62006-08-06 18:30:15 +00004074 // If valid, this location is the position where we read the 'static' keyword.
4075 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004076 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004077 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004078
Chris Lattnere8074e62006-08-06 18:30:15 +00004079 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004080 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004081 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004082 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004083
Chris Lattnere8074e62006-08-06 18:30:15 +00004084 // If we haven't already read 'static', check to see if there is one after the
4085 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004086 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004087 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004088
Chris Lattnere8074e62006-08-06 18:30:15 +00004089 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004090 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004091 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004092
Chris Lattner521ff2b2008-04-06 05:26:30 +00004093 // Handle the case where we have '[*]' as the array size. However, a leading
4094 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4095 // the the token after the star is a ']'. Since stars in arrays are
4096 // infrequent, use of lookahead is not costly here.
4097 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004098 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004099
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004100 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004101 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004102 StaticLoc = SourceLocation(); // Drop the static.
4103 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004104 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004105 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004106 // Note, in C89, this production uses the constant-expr production instead
4107 // of assignment-expr. The only difference is that assignment-expr allows
4108 // things like '=' and '*='. Sema rejects these in C89 mode because they
4109 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004110
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004111 // Parse the constant-expression or assignment-expression now (depending
4112 // on dialect).
4113 if (getLang().CPlusPlus)
4114 NumElements = ParseConstantExpression();
4115 else
4116 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004117 }
Mike Stump11289f42009-09-09 15:08:12 +00004118
Chris Lattner62591722006-08-12 18:40:58 +00004119 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004120 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004121 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004122 // If the expression was invalid, skip it.
4123 SkipUntil(tok::r_square);
4124 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004125 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004126
4127 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4128
John McCall084e83d2011-03-24 11:26:52 +00004129 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004130 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004131
Chris Lattner84a11622008-12-18 07:27:21 +00004132 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004133 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004134 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004135 NumElements.release(),
4136 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004137 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004138}
4139
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004140/// [GNU] typeof-specifier:
4141/// typeof ( expressions )
4142/// typeof ( type-name )
4143/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004144///
4145void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004146 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004147 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004148 SourceLocation StartLoc = ConsumeToken();
4149
John McCalle8595032010-01-13 20:03:27 +00004150 const bool hasParens = Tok.is(tok::l_paren);
4151
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004152 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004153 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004154 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004155 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4156 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004157 if (hasParens)
4158 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004159
4160 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004161 // FIXME: Not accurate, the range gets one token more than it should.
4162 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004163 else
4164 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004165
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004166 if (isCastExpr) {
4167 if (!CastTy) {
4168 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004169 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004170 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004171
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004172 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004173 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004174 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4175 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004176 DiagID, CastTy))
4177 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004178 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004179 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004180
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004181 // If we get here, the operand to the typeof was an expresion.
4182 if (Operand.isInvalid()) {
4183 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004184 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004185 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004186
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004187 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004188 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004189 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4190 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004191 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004192 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004193}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004194
4195
4196/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4197/// from TryAltiVecVectorToken.
4198bool Parser::TryAltiVecVectorTokenOutOfLine() {
4199 Token Next = NextToken();
4200 switch (Next.getKind()) {
4201 default: return false;
4202 case tok::kw_short:
4203 case tok::kw_long:
4204 case tok::kw_signed:
4205 case tok::kw_unsigned:
4206 case tok::kw_void:
4207 case tok::kw_char:
4208 case tok::kw_int:
4209 case tok::kw_float:
4210 case tok::kw_double:
4211 case tok::kw_bool:
4212 case tok::kw___pixel:
4213 Tok.setKind(tok::kw___vector);
4214 return true;
4215 case tok::identifier:
4216 if (Next.getIdentifierInfo() == Ident_pixel) {
4217 Tok.setKind(tok::kw___vector);
4218 return true;
4219 }
4220 return false;
4221 }
4222}
4223
4224bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4225 const char *&PrevSpec, unsigned &DiagID,
4226 bool &isInvalid) {
4227 if (Tok.getIdentifierInfo() == Ident_vector) {
4228 Token Next = NextToken();
4229 switch (Next.getKind()) {
4230 case tok::kw_short:
4231 case tok::kw_long:
4232 case tok::kw_signed:
4233 case tok::kw_unsigned:
4234 case tok::kw_void:
4235 case tok::kw_char:
4236 case tok::kw_int:
4237 case tok::kw_float:
4238 case tok::kw_double:
4239 case tok::kw_bool:
4240 case tok::kw___pixel:
4241 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4242 return true;
4243 case tok::identifier:
4244 if (Next.getIdentifierInfo() == Ident_pixel) {
4245 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4246 return true;
4247 }
4248 break;
4249 default:
4250 break;
4251 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004252 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004253 DS.isTypeAltiVecVector()) {
4254 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4255 return true;
4256 }
4257 return false;
4258}