blob: d7b90f129c4494310c45f1aec007d5ee54b1b2cc [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 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000256
Eli Friedman53339e02009-06-08 23:27:34 +0000257 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000258 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
259 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-05-07 19:04:49 +0000260
261 // FIXME: Remove this when we have proper __declspec(property()) support.
262 // Just skip everything inside property().
263 if (AttrName->getName() == "property") {
264 ConsumeParen();
265 SkipUntil(tok::r_paren);
266 }
Eli Friedman06de2b52009-06-08 07:21:15 +0000267 if (Tok.is(tok::l_paren)) {
268 ConsumeParen();
269 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
270 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000271 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000272 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000273 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000274 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
275 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000276 }
277 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
278 SkipUntil(tok::r_paren, false);
279 } else {
John McCall084e83d2011-03-24 11:26:52 +0000280 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
281 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000282 }
283 }
284 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
285 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000286 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000287}
288
John McCall53fa7142010-12-24 02:08:15 +0000289void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000290 // Treat these like attributes
291 // FIXME: Allow Sema to distinguish between these and real attributes!
292 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000293 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
294 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000295 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
296 SourceLocation AttrNameLoc = ConsumeToken();
297 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
298 // FIXME: Support these properly!
299 continue;
John McCall084e83d2011-03-24 11:26:52 +0000300 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
301 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000302 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000303}
304
John McCall53fa7142010-12-24 02:08:15 +0000305void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000306 // Treat these like attributes
307 while (Tok.is(tok::kw___pascal)) {
308 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
309 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000310 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
311 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000312 }
John McCall53fa7142010-12-24 02:08:15 +0000313}
314
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000315void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
316 // Treat these like attributes
317 while (Tok.is(tok::kw___kernel)) {
318 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000319 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
320 AttrNameLoc, 0, AttrNameLoc, 0,
321 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000322 }
323}
324
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000325void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
326 SourceLocation Loc = Tok.getLocation();
327 switch(Tok.getKind()) {
328 // OpenCL qualifiers:
329 case tok::kw___private:
330 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000331 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000332 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000333 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000334 break;
335
336 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000337 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000338 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000339 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000340 break;
341
342 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000343 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000344 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000345 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000346 break;
347
348 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000349 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000350 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000351 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000352 break;
353
354 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000355 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000356 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000357 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000358 break;
359
360 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000361 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000362 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000363 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000364 break;
365
366 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000367 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000368 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000369 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000370 break;
371 default: break;
372 }
373}
374
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000375/// \brief Parse a version number.
376///
377/// version:
378/// simple-integer
379/// simple-integer ',' simple-integer
380/// simple-integer ',' simple-integer ',' simple-integer
381VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
382 Range = Tok.getLocation();
383
384 if (!Tok.is(tok::numeric_constant)) {
385 Diag(Tok, diag::err_expected_version);
386 SkipUntil(tok::comma, tok::r_paren, true, true, true);
387 return VersionTuple();
388 }
389
390 // Parse the major (and possibly minor and subminor) versions, which
391 // are stored in the numeric constant. We utilize a quirk of the
392 // lexer, which is that it handles something like 1.2.3 as a single
393 // numeric constant, rather than two separate tokens.
394 llvm::SmallString<512> Buffer;
395 Buffer.resize(Tok.getLength()+1);
396 const char *ThisTokBegin = &Buffer[0];
397
398 // Get the spelling of the token, which eliminates trigraphs, etc.
399 bool Invalid = false;
400 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
401 if (Invalid)
402 return VersionTuple();
403
404 // Parse the major version.
405 unsigned AfterMajor = 0;
406 unsigned Major = 0;
407 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
408 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
409 ++AfterMajor;
410 }
411
412 if (AfterMajor == 0) {
413 Diag(Tok, diag::err_expected_version);
414 SkipUntil(tok::comma, tok::r_paren, true, true, true);
415 return VersionTuple();
416 }
417
418 if (AfterMajor == ActualLength) {
419 ConsumeToken();
420
421 // We only had a single version component.
422 if (Major == 0) {
423 Diag(Tok, diag::err_zero_version);
424 return VersionTuple();
425 }
426
427 return VersionTuple(Major);
428 }
429
430 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
431 Diag(Tok, diag::err_expected_version);
432 SkipUntil(tok::comma, tok::r_paren, true, true, true);
433 return VersionTuple();
434 }
435
436 // Parse the minor version.
437 unsigned AfterMinor = AfterMajor + 1;
438 unsigned Minor = 0;
439 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
440 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
441 ++AfterMinor;
442 }
443
444 if (AfterMinor == ActualLength) {
445 ConsumeToken();
446
447 // We had major.minor.
448 if (Major == 0 && Minor == 0) {
449 Diag(Tok, diag::err_zero_version);
450 return VersionTuple();
451 }
452
453 return VersionTuple(Major, Minor);
454 }
455
456 // If what follows is not a '.', we have a problem.
457 if (ThisTokBegin[AfterMinor] != '.') {
458 Diag(Tok, diag::err_expected_version);
459 SkipUntil(tok::comma, tok::r_paren, true, true, true);
460 return VersionTuple();
461 }
462
463 // Parse the subminor version.
464 unsigned AfterSubminor = AfterMinor + 1;
465 unsigned Subminor = 0;
466 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
467 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
468 ++AfterSubminor;
469 }
470
471 if (AfterSubminor != ActualLength) {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476 ConsumeToken();
477 return VersionTuple(Major, Minor, Subminor);
478}
479
480/// \brief Parse the contents of the "availability" attribute.
481///
482/// availability-attribute:
483/// 'availability' '(' platform ',' version-arg-list ')'
484///
485/// platform:
486/// identifier
487///
488/// version-arg-list:
489/// version-arg
490/// version-arg ',' version-arg-list
491///
492/// version-arg:
493/// 'introduced' '=' version
494/// 'deprecated' '=' version
495/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000496/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000497void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
498 SourceLocation AvailabilityLoc,
499 ParsedAttributes &attrs,
500 SourceLocation *endLoc) {
501 SourceLocation PlatformLoc;
502 IdentifierInfo *Platform = 0;
503
504 enum { Introduced, Deprecated, Obsoleted, Unknown };
505 AvailabilityChange Changes[Unknown];
506
507 // Opening '('.
508 SourceLocation LParenLoc;
509 if (!Tok.is(tok::l_paren)) {
510 Diag(Tok, diag::err_expected_lparen);
511 return;
512 }
513 LParenLoc = ConsumeParen();
514
515 // Parse the platform name,
516 if (Tok.isNot(tok::identifier)) {
517 Diag(Tok, diag::err_availability_expected_platform);
518 SkipUntil(tok::r_paren);
519 return;
520 }
521 Platform = Tok.getIdentifierInfo();
522 PlatformLoc = ConsumeToken();
523
524 // Parse the ',' following the platform name.
525 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
526 return;
527
528 // If we haven't grabbed the pointers for the identifiers
529 // "introduced", "deprecated", and "obsoleted", do so now.
530 if (!Ident_introduced) {
531 Ident_introduced = PP.getIdentifierInfo("introduced");
532 Ident_deprecated = PP.getIdentifierInfo("deprecated");
533 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000534 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000535 }
536
537 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000538 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000539 do {
540 if (Tok.isNot(tok::identifier)) {
541 Diag(Tok, diag::err_availability_expected_change);
542 SkipUntil(tok::r_paren);
543 return;
544 }
545 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
546 SourceLocation KeywordLoc = ConsumeToken();
547
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000548 if (Keyword == Ident_unavailable) {
549 if (UnavailableLoc.isValid()) {
550 Diag(KeywordLoc, diag::err_availability_redundant)
551 << Keyword << SourceRange(UnavailableLoc);
552 }
553 UnavailableLoc = KeywordLoc;
554
555 if (Tok.isNot(tok::comma))
556 break;
557
558 ConsumeToken();
559 continue;
560 }
561
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000562 if (Tok.isNot(tok::equal)) {
563 Diag(Tok, diag::err_expected_equal_after)
564 << Keyword;
565 SkipUntil(tok::r_paren);
566 return;
567 }
568 ConsumeToken();
569
570 SourceRange VersionRange;
571 VersionTuple Version = ParseVersionTuple(VersionRange);
572
573 if (Version.empty()) {
574 SkipUntil(tok::r_paren);
575 return;
576 }
577
578 unsigned Index;
579 if (Keyword == Ident_introduced)
580 Index = Introduced;
581 else if (Keyword == Ident_deprecated)
582 Index = Deprecated;
583 else if (Keyword == Ident_obsoleted)
584 Index = Obsoleted;
585 else
586 Index = Unknown;
587
588 if (Index < Unknown) {
589 if (!Changes[Index].KeywordLoc.isInvalid()) {
590 Diag(KeywordLoc, diag::err_availability_redundant)
591 << Keyword
592 << SourceRange(Changes[Index].KeywordLoc,
593 Changes[Index].VersionRange.getEnd());
594 }
595
596 Changes[Index].KeywordLoc = KeywordLoc;
597 Changes[Index].Version = Version;
598 Changes[Index].VersionRange = VersionRange;
599 } else {
600 Diag(KeywordLoc, diag::err_availability_unknown_change)
601 << Keyword << VersionRange;
602 }
603
604 if (Tok.isNot(tok::comma))
605 break;
606
607 ConsumeToken();
608 } while (true);
609
610 // Closing ')'.
611 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
612 if (RParenLoc.isInvalid())
613 return;
614
615 if (endLoc)
616 *endLoc = RParenLoc;
617
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000618 // The 'unavailable' availability cannot be combined with any other
619 // availability changes. Make sure that hasn't happened.
620 if (UnavailableLoc.isValid()) {
621 bool Complained = false;
622 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
623 if (Changes[Index].KeywordLoc.isValid()) {
624 if (!Complained) {
625 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
626 << SourceRange(Changes[Index].KeywordLoc,
627 Changes[Index].VersionRange.getEnd());
628 Complained = true;
629 }
630
631 // Clear out the availability.
632 Changes[Index] = AvailabilityChange();
633 }
634 }
635 }
636
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000637 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000638 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000639 0, SourceLocation(),
640 Platform, PlatformLoc,
641 Changes[Introduced],
642 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000643 Changes[Obsoleted],
644 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000645}
646
John McCall53fa7142010-12-24 02:08:15 +0000647void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
648 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
649 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000650}
651
Chris Lattner53361ac2006-08-10 05:19:57 +0000652/// ParseDeclaration - Parse a full 'declaration', which consists of
653/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000654/// 'Context' should be a Declarator::TheContext value. This returns the
655/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000656///
657/// declaration: [C99 6.7]
658/// block-declaration ->
659/// simple-declaration
660/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000661/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000662/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000663/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000664/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000665/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000666/// others... [FIXME]
667///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000668Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
669 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000670 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000671 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000672 ParenBraceBracketBalancer BalancerRAIIObj(*this);
673
John McCall48871652010-08-21 09:40:31 +0000674 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000675 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000676 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000677 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000678 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000679 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000680 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000681 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000682 // Could be the start of an inline namespace. Allowed as an ext in C++03.
683 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000684 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000685 SourceLocation InlineLoc = ConsumeToken();
686 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
687 break;
688 }
John McCall53fa7142010-12-24 02:08:15 +0000689 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000690 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000691 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000692 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000693 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000694 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000695 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000696 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000697 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000698 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000699 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000700 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000701 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000702 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000703 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000704 default:
John McCall53fa7142010-12-24 02:08:15 +0000705 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000706 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000707
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000708 // This routine returns a DeclGroup, if the thing we parsed only contains a
709 // single decl, convert it now.
710 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000711}
712
713/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
714/// declaration-specifiers init-declarator-list[opt] ';'
715///[C90/C++]init-declarator-list ';' [TODO]
716/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000717///
Richard Smith02e85f32011-04-14 22:09:26 +0000718/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
719/// attribute-specifier-seq[opt] type-specifier-seq declarator
720///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000721/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000722/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000723///
724/// If FRI is non-null, we might be parsing a for-range-declaration instead
725/// of a simple-declaration. If we find that we are, we also parse the
726/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000727Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
728 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000729 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000730 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000731 bool RequireSemi,
732 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000733 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000734 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000735 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000736
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000737 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000738 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000739 StmtResult R = Actions.ActOnVlaStmt(DS);
740 if (R.isUsable())
741 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000742
Chris Lattner0e894622006-08-13 19:58:17 +0000743 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
744 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000745 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000746 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000747 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000748 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000749 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000750 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000751 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000752
753 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000754}
Mike Stump11289f42009-09-09 15:08:12 +0000755
John McCalld5a36322009-11-03 19:26:08 +0000756/// ParseDeclGroup - Having concluded that this is either a function
757/// definition or a group of object declarations, actually parse the
758/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000759Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
760 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000761 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000762 SourceLocation *DeclEnd,
763 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000764 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000765 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000766 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000767
John McCalld5a36322009-11-03 19:26:08 +0000768 // Bail out if the first declarator didn't seem well-formed.
769 if (!D.hasName() && !D.mayOmitIdentifier()) {
770 // Skip until ; or }.
771 SkipUntil(tok::r_brace, true, true);
772 if (Tok.is(tok::semi))
773 ConsumeToken();
774 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000777 // Check to see if we have a function *definition* which must have a body.
778 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
779 // Look at the next token to make sure that this isn't a function
780 // declaration. We have to check this because __attribute__ might be the
781 // start of a function definition in GCC-extended K&R C.
782 !isDeclarationAfterDeclarator()) {
783
Chris Lattner13901342010-07-11 22:42:07 +0000784 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000785 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
786 Diag(Tok, diag::err_function_declared_typedef);
787
788 // Recover by treating the 'typedef' as spurious.
789 DS.ClearStorageClassSpecs();
790 }
791
John McCall48871652010-08-21 09:40:31 +0000792 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000793 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000794 }
795
796 if (isDeclarationSpecifier()) {
797 // If there is an invalid declaration specifier right after the function
798 // prototype, then we must be in a missing semicolon case where this isn't
799 // actually a body. Just fall through into the code that handles it as a
800 // prototype, and let the top-level code handle the erroneous declspec
801 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000802 } else {
803 Diag(Tok, diag::err_expected_fn_body);
804 SkipUntil(tok::semi);
805 return DeclGroupPtrTy();
806 }
807 }
808
Richard Smith02e85f32011-04-14 22:09:26 +0000809 if (ParseAttributesAfterDeclarator(D))
810 return DeclGroupPtrTy();
811
812 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
813 // must parse and analyze the for-range-initializer before the declaration is
814 // analyzed.
815 if (FRI && Tok.is(tok::colon)) {
816 FRI->ColonLoc = ConsumeToken();
817 // FIXME: handle braced-init-list here.
818 FRI->RangeExpr = ParseExpression();
819 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
820 Actions.ActOnCXXForRangeDecl(ThisDecl);
821 Actions.FinalizeDeclaration(ThisDecl);
822 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
823 }
824
John McCall48871652010-08-21 09:40:31 +0000825 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000826 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000827 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000828 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000829 DeclsInGroup.push_back(FirstDecl);
830
831 // If we don't have a comma, it is either the end of the list (a ';') or an
832 // error, bail out.
833 while (Tok.is(tok::comma)) {
834 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000835 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000836
837 // Parse the next declarator.
838 D.clear();
839
840 // Accept attributes in an init-declarator. In the first declarator in a
841 // declaration, these would be part of the declspec. In subsequent
842 // declarators, they become part of the declarator itself, so that they
843 // don't apply to declarators after *this* one. Examples:
844 // short __attribute__((common)) var; -> declspec
845 // short var __attribute__((common)); -> declarator
846 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000847 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000848
849 ParseDeclarator(D);
850
John McCall48871652010-08-21 09:40:31 +0000851 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000852 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000853 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000854 DeclsInGroup.push_back(ThisDecl);
855 }
856
857 if (DeclEnd)
858 *DeclEnd = Tok.getLocation();
859
860 if (Context != Declarator::ForContext &&
861 ExpectAndConsume(tok::semi,
862 Context == Declarator::FileContext
863 ? diag::err_invalid_token_after_toplevel_declarator
864 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000865 // Okay, there was no semicolon and one was expected. If we see a
866 // declaration specifier, just assume it was missing and continue parsing.
867 // Otherwise things are very confused and we skip to recover.
868 if (!isDeclarationSpecifier()) {
869 SkipUntil(tok::r_brace, true, true);
870 if (Tok.is(tok::semi))
871 ConsumeToken();
872 }
John McCalld5a36322009-11-03 19:26:08 +0000873 }
874
Douglas Gregor0be31a22010-07-02 17:43:08 +0000875 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000876 DeclsInGroup.data(),
877 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000878}
879
Richard Smith02e85f32011-04-14 22:09:26 +0000880/// Parse an optional simple-asm-expr and attributes, and attach them to a
881/// declarator. Returns true on an error.
882bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
883 // If a simple-asm-expr is present, parse it.
884 if (Tok.is(tok::kw_asm)) {
885 SourceLocation Loc;
886 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
887 if (AsmLabel.isInvalid()) {
888 SkipUntil(tok::semi, true, true);
889 return true;
890 }
891
892 D.setAsmLabel(AsmLabel.release());
893 D.SetRangeEnd(Loc);
894 }
895
896 MaybeParseGNUAttributes(D);
897 return false;
898}
899
Douglas Gregor23996282009-05-12 21:31:51 +0000900/// \brief Parse 'declaration' after parsing 'declaration-specifiers
901/// declarator'. This method parses the remainder of the declaration
902/// (including any attributes or initializer, among other things) and
903/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000904///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000905/// init-declarator: [C99 6.7]
906/// declarator
907/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000908/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
909/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000910/// [C++] declarator initializer[opt]
911///
912/// [C++] initializer:
913/// [C++] '=' initializer-clause
914/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000915/// [C++0x] '=' 'default' [TODO]
916/// [C++0x] '=' 'delete'
917///
918/// According to the standard grammar, =default and =delete are function
919/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000920///
John McCall48871652010-08-21 09:40:31 +0000921Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000922 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +0000923 if (ParseAttributesAfterDeclarator(D))
924 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000925
Richard Smith02e85f32011-04-14 22:09:26 +0000926 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
927}
Mike Stump11289f42009-09-09 15:08:12 +0000928
Richard Smith02e85f32011-04-14 22:09:26 +0000929Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
930 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000931 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000932 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000933 switch (TemplateInfo.Kind) {
934 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000935 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000936 break;
937
938 case ParsedTemplateInfo::Template:
939 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000940 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000941 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000942 TemplateInfo.TemplateParams->data(),
943 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000944 D);
945 break;
946
947 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000948 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000949 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000950 TemplateInfo.ExternLoc,
951 TemplateInfo.TemplateLoc,
952 D);
953 if (ThisRes.isInvalid()) {
954 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000955 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000956 }
957
958 ThisDecl = ThisRes.get();
959 break;
960 }
961 }
Mike Stump11289f42009-09-09 15:08:12 +0000962
Richard Smith30482bc2011-02-20 03:19:35 +0000963 bool TypeContainsAuto =
964 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
965
Douglas Gregor23996282009-05-12 21:31:51 +0000966 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000967 if (isTokenEqualOrMistypedEqualEqual(
968 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000969 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000970 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000971 if (D.isFunctionDeclarator())
972 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
973 << 1 /* delete */;
974 else
975 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +0000976 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000977 if (D.isFunctionDeclarator())
978 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
979 << 1 /* delete */;
980 else
981 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +0000982 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000983 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
984 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000985 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000986 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000987
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000988 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000989 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000990 ConsumeCodeCompletionToken();
991 SkipUntil(tok::comma, true, true);
992 return ThisDecl;
993 }
994
John McCalldadc5752010-08-24 06:29:42 +0000995 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000996
John McCall1f4ee7b2009-12-19 09:28:58 +0000997 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000998 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000999 ExitScope();
1000 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001001
Douglas Gregor23996282009-05-12 21:31:51 +00001002 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001003 SkipUntil(tok::comma, true, true);
1004 Actions.ActOnInitializerError(ThisDecl);
1005 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001006 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1007 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001008 }
1009 } else if (Tok.is(tok::l_paren)) {
1010 // Parse C++ direct initializer: '(' expression-list ')'
1011 SourceLocation LParenLoc = ConsumeParen();
1012 ExprVector Exprs(Actions);
1013 CommaLocsTy CommaLocs;
1014
Douglas Gregor613bf102009-12-22 17:47:17 +00001015 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1016 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001017 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001018 }
1019
Douglas Gregor23996282009-05-12 21:31:51 +00001020 if (ParseExpressionList(Exprs, CommaLocs)) {
1021 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001022
1023 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001024 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001025 ExitScope();
1026 }
Douglas Gregor23996282009-05-12 21:31:51 +00001027 } else {
1028 // Match the ')'.
1029 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1030
1031 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1032 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001033
1034 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001035 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001036 ExitScope();
1037 }
1038
Douglas Gregor23996282009-05-12 21:31:51 +00001039 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1040 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001041 RParenLoc,
1042 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001043 }
1044 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001045 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001046 }
1047
Richard Smithb2bc2e62011-02-21 20:05:19 +00001048 Actions.FinalizeDeclaration(ThisDecl);
1049
Douglas Gregor23996282009-05-12 21:31:51 +00001050 return ThisDecl;
1051}
1052
Chris Lattner1890ac82006-08-13 01:16:23 +00001053/// ParseSpecifierQualifierList
1054/// specifier-qualifier-list:
1055/// type-specifier specifier-qualifier-list[opt]
1056/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001057/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001058///
1059void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1060 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1061 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001062 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattner1890ac82006-08-13 01:16:23 +00001064 // Validate declspec for type-name.
1065 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001066 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001067 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001068 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001069
Chris Lattner1b22eed2006-11-28 05:12:07 +00001070 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001071 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001072 if (DS.getStorageClassSpecLoc().isValid())
1073 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1074 else
1075 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001076 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001077 }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner1b22eed2006-11-28 05:12:07 +00001079 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001080 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001081 if (DS.isInlineSpecified())
1082 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1083 if (DS.isVirtualSpecified())
1084 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1085 if (DS.isExplicitSpecified())
1086 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001087 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001088 }
1089}
Chris Lattner53361ac2006-08-10 05:19:57 +00001090
Chris Lattner6cc055a2009-04-12 20:42:31 +00001091/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1092/// specified token is valid after the identifier in a declarator which
1093/// immediately follows the declspec. For example, these things are valid:
1094///
1095/// int x [ 4]; // direct-declarator
1096/// int x ( int y); // direct-declarator
1097/// int(int x ) // direct-declarator
1098/// int x ; // simple-declaration
1099/// int x = 17; // init-declarator-list
1100/// int x , y; // init-declarator-list
1101/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001102/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001103/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001104///
1105/// This is not, because 'x' does not immediately follow the declspec (though
1106/// ')' happens to be valid anyway).
1107/// int (x)
1108///
1109static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1110 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1111 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001112 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001113}
1114
Chris Lattner20a0c612009-04-14 21:34:55 +00001115
1116/// ParseImplicitInt - This method is called when we have an non-typename
1117/// identifier in a declspec (which normally terminates the decl spec) when
1118/// the declspec has no type specifier. In this case, the declspec is either
1119/// malformed or is "implicit int" (in K&R and C89).
1120///
1121/// This method handles diagnosing this prettily and returns false if the
1122/// declspec is done being processed. If it recovers and thinks there may be
1123/// other pieces of declspec after it, it returns true.
1124///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001125bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001126 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001127 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001128 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner20a0c612009-04-14 21:34:55 +00001130 SourceLocation Loc = Tok.getLocation();
1131 // If we see an identifier that is not a type name, we normally would
1132 // parse it as the identifer being declared. However, when a typename
1133 // is typo'd or the definition is not included, this will incorrectly
1134 // parse the typename as the identifier name and fall over misparsing
1135 // later parts of the diagnostic.
1136 //
1137 // As such, we try to do some look-ahead in cases where this would
1138 // otherwise be an "implicit-int" case to see if this is invalid. For
1139 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1140 // an identifier with implicit int, we'd get a parse error because the
1141 // next token is obviously invalid for a type. Parse these as a case
1142 // with an invalid type specifier.
1143 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001144
Chris Lattner20a0c612009-04-14 21:34:55 +00001145 // Since we know that this either implicit int (which is rare) or an
1146 // error, we'd do lookahead to try to do better recovery.
1147 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1148 // If this token is valid for implicit int, e.g. "static x = 4", then
1149 // we just avoid eating the identifier, so it will be parsed as the
1150 // identifier in the declarator.
1151 return false;
1152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner20a0c612009-04-14 21:34:55 +00001154 // Otherwise, if we don't consume this token, we are going to emit an
1155 // error anyway. Try to recover from various common problems. Check
1156 // to see if this was a reference to a tag name without a tag specified.
1157 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001158 //
1159 // C++ doesn't need this, and isTagName doesn't take SS.
1160 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001161 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001162 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001163
Douglas Gregor0be31a22010-07-02 17:43:08 +00001164 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001165 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001166 case DeclSpec::TST_enum:
1167 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1168 case DeclSpec::TST_union:
1169 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1170 case DeclSpec::TST_struct:
1171 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1172 case DeclSpec::TST_class:
1173 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001174 }
Mike Stump11289f42009-09-09 15:08:12 +00001175
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001176 if (TagName) {
1177 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001178 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001179 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001181 // Parse this as a tag as if the missing tag were present.
1182 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001183 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001184 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001185 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001186 return true;
1187 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001188 }
Mike Stump11289f42009-09-09 15:08:12 +00001189
Douglas Gregor15e56022009-10-13 23:27:22 +00001190 // This is almost certainly an invalid type name. Let the action emit a
1191 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001192 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001193 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001194 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001195 // The action emitted a diagnostic, so we don't have to.
1196 if (T) {
1197 // The action has suggested that the type T could be used. Set that as
1198 // the type in the declaration specifiers, consume the would-be type
1199 // name token, and we're done.
1200 const char *PrevSpec;
1201 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001202 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001203 DS.SetRangeEnd(Tok.getLocation());
1204 ConsumeToken();
1205
1206 // There may be other declaration specifiers after this.
1207 return true;
1208 }
1209
1210 // Fall through; the action had no suggestion for us.
1211 } else {
1212 // The action did not emit a diagnostic, so emit one now.
1213 SourceRange R;
1214 if (SS) R = SS->getRange();
1215 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1216 }
Mike Stump11289f42009-09-09 15:08:12 +00001217
Douglas Gregor15e56022009-10-13 23:27:22 +00001218 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001219 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001220 unsigned DiagID;
1221 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001222 DS.SetRangeEnd(Tok.getLocation());
1223 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattner20a0c612009-04-14 21:34:55 +00001225 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1226 // avoid rippling error messages on subsequent uses of the same type,
1227 // could be useful if #include was forgotten.
1228 return false;
1229}
1230
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001231/// \brief Determine the declaration specifier context from the declarator
1232/// context.
1233///
1234/// \param Context the declarator context, which is one of the
1235/// Declarator::TheContext enumerator values.
1236Parser::DeclSpecContext
1237Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1238 if (Context == Declarator::MemberContext)
1239 return DSC_class;
1240 if (Context == Declarator::FileContext)
1241 return DSC_top_level;
1242 return DSC_normal;
1243}
1244
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001245/// ParseDeclarationSpecifiers
1246/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001247/// storage-class-specifier declaration-specifiers[opt]
1248/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001249/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001250/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001251///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001252/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001253/// 'typedef'
1254/// 'extern'
1255/// 'static'
1256/// 'auto'
1257/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001258/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001259/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001260/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001261/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001262/// [C++] 'virtual'
1263/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001264/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001265/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001266/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001267
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001268///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001269void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001270 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001271 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001272 DeclSpecContext DSContext) {
1273 if (DS.getSourceRange().isInvalid()) {
1274 DS.SetRangeStart(Tok.getLocation());
1275 DS.SetRangeEnd(Tok.getLocation());
1276 }
1277
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001278 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001279 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001280 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001281 unsigned DiagID = 0;
1282
Chris Lattner4d8f8732006-11-28 05:05:08 +00001283 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001284
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001285 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001286 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001287 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001288 // If this is not a declaration specifier token, we're done reading decl
1289 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001290 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001291 return;
Mike Stump11289f42009-09-09 15:08:12 +00001292
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001293 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001294 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001295 if (DS.hasTypeSpecifier()) {
1296 bool AllowNonIdentifiers
1297 = (getCurScope()->getFlags() & (Scope::ControlScope |
1298 Scope::BlockScope |
1299 Scope::TemplateParamScope |
1300 Scope::FunctionPrototypeScope |
1301 Scope::AtCatchScope)) == 0;
1302 bool AllowNestedNameSpecifiers
1303 = DSContext == DSC_top_level ||
1304 (DSContext == DSC_class && DS.isFriendSpecified());
1305
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001306 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1307 AllowNonIdentifiers,
1308 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001309 ConsumeCodeCompletionToken();
1310 return;
1311 }
1312
Douglas Gregor80039242011-02-15 20:33:25 +00001313 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1314 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1315 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001316 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1317 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001318 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001319 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001320 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001321 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001322
1323 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1324 ConsumeCodeCompletionToken();
1325 return;
1326 }
1327
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001328 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001329 // C++ scope specifier. Annotate and loop, or bail out on error.
1330 if (TryAnnotateCXXScopeToken(true)) {
1331 if (!DS.hasTypeSpecifier())
1332 DS.SetTypeSpecError();
1333 goto DoneWithDeclSpec;
1334 }
John McCall8bc2a702010-03-01 18:20:46 +00001335 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1336 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001337 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001338
1339 case tok::annot_cxxscope: {
1340 if (DS.hasTypeSpecifier())
1341 goto DoneWithDeclSpec;
1342
John McCall9dab4e62009-12-12 11:40:51 +00001343 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001344 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1345 Tok.getAnnotationRange(),
1346 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001347
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001348 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001349 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001350 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001351 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001352 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001353 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001354
1355 // C++ [class.qual]p2:
1356 // In a lookup in which the constructor is an acceptable lookup
1357 // result and the nested-name-specifier nominates a class C:
1358 //
1359 // - if the name specified after the
1360 // nested-name-specifier, when looked up in C, is the
1361 // injected-class-name of C (Clause 9), or
1362 //
1363 // - if the name specified after the nested-name-specifier
1364 // is the same as the identifier or the
1365 // simple-template-id's template-name in the last
1366 // component of the nested-name-specifier,
1367 //
1368 // the name is instead considered to name the constructor of
1369 // class C.
1370 //
1371 // Thus, if the template-name is actually the constructor
1372 // name, then the code is ill-formed; this interpretation is
1373 // reinforced by the NAD status of core issue 635.
1374 TemplateIdAnnotation *TemplateId
1375 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001376 if ((DSContext == DSC_top_level ||
1377 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1378 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001379 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001380 if (isConstructorDeclarator()) {
1381 // The user meant this to be an out-of-line constructor
1382 // definition, but template arguments are not allowed
1383 // there. Just allow this as a constructor; we'll
1384 // complain about it later.
1385 goto DoneWithDeclSpec;
1386 }
1387
1388 // The user meant this to name a type, but it actually names
1389 // a constructor with some extraneous template
1390 // arguments. Complain, then parse it as a type as the user
1391 // intended.
1392 Diag(TemplateId->TemplateNameLoc,
1393 diag::err_out_of_line_template_id_names_constructor)
1394 << TemplateId->Name;
1395 }
1396
John McCall9dab4e62009-12-12 11:40:51 +00001397 DS.getTypeSpecScope() = SS;
1398 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001399 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001400 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001401 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001402 continue;
1403 }
1404
Douglas Gregorc5790df2009-09-28 07:26:33 +00001405 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001406 DS.getTypeSpecScope() = SS;
1407 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001408 if (Tok.getAnnotationValue()) {
1409 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1411 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001412 PrevSpec, DiagID, T);
1413 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001414 else
1415 DS.SetTypeSpecError();
1416 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1417 ConsumeToken(); // The typename
1418 }
1419
Douglas Gregor167fa622009-03-25 15:40:00 +00001420 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001421 goto DoneWithDeclSpec;
1422
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001423 // If we're in a context where the identifier could be a class name,
1424 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001425 if ((DSContext == DSC_top_level ||
1426 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001427 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001428 &SS)) {
1429 if (isConstructorDeclarator())
1430 goto DoneWithDeclSpec;
1431
1432 // As noted in C++ [class.qual]p2 (cited above), when the name
1433 // of the class is qualified in a context where it could name
1434 // a constructor, its a constructor name. However, we've
1435 // looked at the declarator, and the user probably meant this
1436 // to be a type. Complain that it isn't supposed to be treated
1437 // as a type, then proceed to parse it as a type.
1438 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1439 << Next.getIdentifierInfo();
1440 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001441
John McCallba7bf592010-08-24 05:47:05 +00001442 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1443 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001444 getCurScope(), &SS,
1445 false, false, ParsedType(),
1446 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001447
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001448 // If the referenced identifier is not a type, then this declspec is
1449 // erroneous: We already checked about that it has no type specifier, and
1450 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001451 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001452 if (TypeRep == 0) {
1453 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001454 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001455 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
John McCall9dab4e62009-12-12 11:40:51 +00001458 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001459 ConsumeToken(); // The C++ scope.
1460
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001462 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001463 if (isInvalid)
1464 break;
Mike Stump11289f42009-09-09 15:08:12 +00001465
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001466 DS.SetRangeEnd(Tok.getLocation());
1467 ConsumeToken(); // The typename.
1468
1469 continue;
1470 }
Mike Stump11289f42009-09-09 15:08:12 +00001471
Chris Lattnere387d9e2009-01-21 19:48:37 +00001472 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001473 if (Tok.getAnnotationValue()) {
1474 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001476 DiagID, T);
1477 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001478 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001479
1480 if (isInvalid)
1481 break;
1482
Chris Lattnere387d9e2009-01-21 19:48:37 +00001483 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1484 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001485
Chris Lattnere387d9e2009-01-21 19:48:37 +00001486 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1487 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001488 // Objective-C interface.
1489 if (Tok.is(tok::less) && getLang().ObjC1)
1490 ParseObjCProtocolQualifiers(DS);
1491
Chris Lattnere387d9e2009-01-21 19:48:37 +00001492 continue;
1493 }
Mike Stump11289f42009-09-09 15:08:12 +00001494
Douglas Gregor06873092011-04-28 15:48:45 +00001495 case tok::kw___is_signed:
1496 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1497 // typically treats it as a trait. If we see __is_signed as it appears
1498 // in libstdc++, e.g.,
1499 //
1500 // static const bool __is_signed;
1501 //
1502 // then treat __is_signed as an identifier rather than as a keyword.
1503 if (DS.getTypeSpecType() == TST_bool &&
1504 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1505 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1506 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1507 Tok.setKind(tok::identifier);
1508 }
1509
1510 // We're done with the declaration-specifiers.
1511 goto DoneWithDeclSpec;
1512
Chris Lattner16fac4f2008-07-26 01:18:38 +00001513 // typedef-name
1514 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001515 // In C++, check to see if this is a scope specifier like foo::bar::, if
1516 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001517 if (getLang().CPlusPlus) {
1518 if (TryAnnotateCXXScopeToken(true)) {
1519 if (!DS.hasTypeSpecifier())
1520 DS.SetTypeSpecError();
1521 goto DoneWithDeclSpec;
1522 }
1523 if (!Tok.is(tok::identifier))
1524 continue;
1525 }
Mike Stump11289f42009-09-09 15:08:12 +00001526
Chris Lattner16fac4f2008-07-26 01:18:38 +00001527 // This identifier can only be a typedef name if we haven't already seen
1528 // a type-specifier. Without this check we misparse:
1529 // typedef int X; struct Y { short X; }; as 'short int'.
1530 if (DS.hasTypeSpecifier())
1531 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001532
John Thompson22334602010-02-05 00:12:22 +00001533 // Check for need to substitute AltiVec keyword tokens.
1534 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1535 break;
1536
Chris Lattner16fac4f2008-07-26 01:18:38 +00001537 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001538 ParsedType TypeRep =
1539 Actions.getTypeName(*Tok.getIdentifierInfo(),
1540 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001541
Chris Lattner6cc055a2009-04-12 20:42:31 +00001542 // If this is not a typedef name, don't parse it as part of the declspec,
1543 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001544 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001545 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001546 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001547 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001548
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001549 // If we're in a context where the identifier could be a class name,
1550 // check whether this is a constructor declaration.
1551 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001552 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001553 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001554 goto DoneWithDeclSpec;
1555
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001557 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001558 if (isInvalid)
1559 break;
Mike Stump11289f42009-09-09 15:08:12 +00001560
Chris Lattner16fac4f2008-07-26 01:18:38 +00001561 DS.SetRangeEnd(Tok.getLocation());
1562 ConsumeToken(); // The identifier
1563
1564 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1565 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001566 // Objective-C interface.
1567 if (Tok.is(tok::less) && getLang().ObjC1)
1568 ParseObjCProtocolQualifiers(DS);
1569
Steve Naroffcd5e7822008-09-22 10:28:57 +00001570 // Need to support trailing type qualifiers (e.g. "id<p> const").
1571 // If a type specifier follows, it will be diagnosed elsewhere.
1572 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001573 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001574
1575 // type-name
1576 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001577 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001578 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001579 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001580 // This template-id does not refer to a type name, so we're
1581 // done with the type-specifiers.
1582 goto DoneWithDeclSpec;
1583 }
1584
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001585 // If we're in a context where the template-id could be a
1586 // constructor name or specialization, check whether this is a
1587 // constructor declaration.
1588 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001589 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001590 isConstructorDeclarator())
1591 goto DoneWithDeclSpec;
1592
Douglas Gregor7f741122009-02-25 19:37:18 +00001593 // Turn the template-id annotation token into a type annotation
1594 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001595 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001596 continue;
1597 }
1598
Chris Lattnere37e2332006-08-15 04:50:22 +00001599 // GNU attributes support.
1600 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001601 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001602 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001603
1604 // Microsoft declspec support.
1605 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001606 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001607 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Steve Naroff44ac7772008-12-25 14:16:32 +00001609 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001610 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001611 // FIXME: Add handling here!
1612 break;
1613
1614 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001615 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001616 case tok::kw___cdecl:
1617 case tok::kw___stdcall:
1618 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001619 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001620 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001621 continue;
1622
Dawn Perchik335e16b2010-09-03 01:29:35 +00001623 // Borland single token adornments.
1624 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001625 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001626 continue;
1627
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001628 // OpenCL single token adornments.
1629 case tok::kw___kernel:
1630 ParseOpenCLAttributes(DS.getAttributes());
1631 continue;
1632
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001633 // storage-class-specifier
1634 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001635 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001636 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001637 break;
1638 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001639 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001640 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001641 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001642 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001643 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001644 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001645 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001646 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001647 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001648 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001649 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001650 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001651 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001652 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001653 break;
1654 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001655 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001656 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1657 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1658 DiagID, getLang());
1659 if (!isInvalid)
1660 Diag(Tok, diag::auto_storage_class)
1661 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1662 }
1663 else
1664 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1665 DiagID);
1666 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001667 else
John McCall49bfce42009-08-03 20:12:06 +00001668 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001669 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001670 break;
1671 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001672 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001673 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001674 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001675 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001676 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001677 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001678 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001679 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001680 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001681 break;
Mike Stump11289f42009-09-09 15:08:12 +00001682
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001683 // function-specifier
1684 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001685 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001686 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001687 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001688 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001689 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001690 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001691 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001692 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001693
Anders Carlssoncd8db412009-05-06 04:46:28 +00001694 // friend
1695 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001696 if (DSContext == DSC_class)
1697 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1698 else {
1699 PrevSpec = ""; // not actually used by the diagnostic
1700 DiagID = diag::err_friend_invalid_in_context;
1701 isInvalid = true;
1702 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001703 break;
Mike Stump11289f42009-09-09 15:08:12 +00001704
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001705 // constexpr
1706 case tok::kw_constexpr:
1707 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1708 break;
1709
Chris Lattnere387d9e2009-01-21 19:48:37 +00001710 // type-specifier
1711 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001712 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1713 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001714 break;
1715 case tok::kw_long:
1716 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001717 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1718 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001719 else
John McCall49bfce42009-08-03 20:12:06 +00001720 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1721 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001722 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001723 case tok::kw___int64:
1724 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1725 DiagID);
1726 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001727 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001728 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1729 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001730 break;
1731 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001732 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1733 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001734 break;
1735 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001736 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1737 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001738 break;
1739 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001740 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1741 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001742 break;
1743 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1745 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001746 break;
1747 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1749 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001750 break;
1751 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1753 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001754 break;
1755 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1757 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001758 break;
1759 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1761 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001762 break;
1763 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1765 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001766 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001767 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1769 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001770 break;
1771 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1773 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001774 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001775 case tok::kw_bool:
1776 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001777 if (Tok.is(tok::kw_bool) &&
1778 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1779 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1780 PrevSpec = ""; // Not used by the diagnostic.
1781 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001782 // For better error recovery.
1783 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001784 isInvalid = true;
1785 } else {
1786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1787 DiagID);
1788 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001789 break;
1790 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1792 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001793 break;
1794 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1796 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001797 break;
1798 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1800 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001801 break;
John Thompson22334602010-02-05 00:12:22 +00001802 case tok::kw___vector:
1803 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1804 break;
1805 case tok::kw___pixel:
1806 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1807 break;
John McCall39439732011-04-09 22:50:59 +00001808 case tok::kw___unknown_anytype:
1809 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1810 PrevSpec, DiagID);
1811 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001812
1813 // class-specifier:
1814 case tok::kw_class:
1815 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001816 case tok::kw_union: {
1817 tok::TokenKind Kind = Tok.getKind();
1818 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001819 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001820 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001821 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001822
1823 // enum-specifier:
1824 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001825 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001826 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001827 continue;
1828
1829 // cv-qualifier:
1830 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001831 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1832 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001833 break;
1834 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001835 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1836 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001837 break;
1838 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001839 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1840 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001841 break;
1842
Douglas Gregor333489b2009-03-27 23:10:48 +00001843 // C++ typename-specifier:
1844 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001845 if (TryAnnotateTypeOrScopeToken()) {
1846 DS.SetTypeSpecError();
1847 goto DoneWithDeclSpec;
1848 }
1849 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001850 continue;
1851 break;
1852
Chris Lattnere387d9e2009-01-21 19:48:37 +00001853 // GNU typeof support.
1854 case tok::kw_typeof:
1855 ParseTypeofSpecifier(DS);
1856 continue;
1857
Anders Carlsson74948d02009-06-24 17:47:40 +00001858 case tok::kw_decltype:
1859 ParseDecltypeSpecifier(DS);
1860 continue;
1861
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001862 // OpenCL qualifiers:
1863 case tok::kw_private:
1864 if (!getLang().OpenCL)
1865 goto DoneWithDeclSpec;
1866 case tok::kw___private:
1867 case tok::kw___global:
1868 case tok::kw___local:
1869 case tok::kw___constant:
1870 case tok::kw___read_only:
1871 case tok::kw___write_only:
1872 case tok::kw___read_write:
1873 ParseOpenCLQualifiers(DS);
1874 break;
1875
Steve Naroffcfdf6162008-06-05 00:02:44 +00001876 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001877 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001878 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1879 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001880 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001881 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregor3a001f42010-11-19 17:10:50 +00001883 if (!ParseObjCProtocolQualifiers(DS))
1884 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1885 << FixItHint::CreateInsertion(Loc, "id")
1886 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001887
1888 // Need to support trailing type qualifiers (e.g. "id<p> const").
1889 // If a type specifier follows, it will be diagnosed elsewhere.
1890 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001891 }
John McCall49bfce42009-08-03 20:12:06 +00001892 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001893 if (isInvalid) {
1894 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001895 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001896
1897 if (DiagID == diag::ext_duplicate_declspec)
1898 Diag(Tok, DiagID)
1899 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1900 else
1901 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001902 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001903
Chris Lattner2e232092008-03-13 06:29:04 +00001904 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001905 if (DiagID != diag::err_bool_redeclaration)
1906 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001907 }
1908}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001909
Chris Lattnera448d752009-01-06 06:59:53 +00001910/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001911/// primarily follow the C++ grammar with additions for C99 and GNU,
1912/// which together subsume the C grammar. Note that the C++
1913/// type-specifier also includes the C type-qualifier (for const,
1914/// volatile, and C99 restrict). Returns true if a type-specifier was
1915/// found (and parsed), false otherwise.
1916///
1917/// type-specifier: [C++ 7.1.5]
1918/// simple-type-specifier
1919/// class-specifier
1920/// enum-specifier
1921/// elaborated-type-specifier [TODO]
1922/// cv-qualifier
1923///
1924/// cv-qualifier: [C++ 7.1.5.1]
1925/// 'const'
1926/// 'volatile'
1927/// [C99] 'restrict'
1928///
1929/// simple-type-specifier: [ C++ 7.1.5.2]
1930/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1931/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1932/// 'char'
1933/// 'wchar_t'
1934/// 'bool'
1935/// 'short'
1936/// 'int'
1937/// 'long'
1938/// 'signed'
1939/// 'unsigned'
1940/// 'float'
1941/// 'double'
1942/// 'void'
1943/// [C99] '_Bool'
1944/// [C99] '_Complex'
1945/// [C99] '_Imaginary' // Removed in TC2?
1946/// [GNU] '_Decimal32'
1947/// [GNU] '_Decimal64'
1948/// [GNU] '_Decimal128'
1949/// [GNU] typeof-specifier
1950/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1951/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001952/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001953/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001954bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001955 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001956 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001957 const ParsedTemplateInfo &TemplateInfo,
1958 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001959 SourceLocation Loc = Tok.getLocation();
1960
1961 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001962 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001963 // If we already have a type specifier, this identifier is not a type.
1964 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1965 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1966 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1967 return false;
John Thompson22334602010-02-05 00:12:22 +00001968 // Check for need to substitute AltiVec keyword tokens.
1969 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1970 break;
1971 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001972 case tok::kw_typename: // typename foo::bar
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 if (Tok.is(tok::identifier))
1978 return false;
1979 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1980 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001981 case tok::coloncolon: // ::foo::bar
1982 if (NextToken().is(tok::kw_new) || // ::new
1983 NextToken().is(tok::kw_delete)) // ::delete
1984 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattner020bab92009-01-04 23:41:41 +00001986 // Annotate typenames and C++ scope specifiers. If we get one, just
1987 // recurse to handle whatever we get.
1988 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001989 return true;
1990 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1991 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001992
Douglas Gregor450c75a2008-11-07 15:42:26 +00001993 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001994 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001995 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001996 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1997 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001998 DiagID, T);
1999 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002000 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002001 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2002 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002003
Douglas Gregor450c75a2008-11-07 15:42:26 +00002004 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2005 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2006 // Objective-C interface. If we don't have Objective-C or a '<', this is
2007 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002008 if (Tok.is(tok::less) && getLang().ObjC1)
2009 ParseObjCProtocolQualifiers(DS);
2010
Douglas Gregor450c75a2008-11-07 15:42:26 +00002011 return true;
2012 }
2013
2014 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002015 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002016 break;
2017 case tok::kw_long:
2018 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2020 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002021 else
John McCall49bfce42009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2023 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002024 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002025 case tok::kw___int64:
2026 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2027 DiagID);
2028 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002029 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002030 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002031 break;
2032 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002033 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2034 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002035 break;
2036 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002037 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2038 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002039 break;
2040 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002041 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2042 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002043 break;
2044 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002046 break;
2047 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002049 break;
2050 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002052 break;
2053 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002055 break;
2056 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002058 break;
2059 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002061 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002062 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002064 break;
2065 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002067 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002068 case tok::kw_bool:
2069 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002071 break;
2072 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2074 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002075 break;
2076 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2078 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002079 break;
2080 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2082 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002083 break;
John Thompson22334602010-02-05 00:12:22 +00002084 case tok::kw___vector:
2085 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2086 break;
2087 case tok::kw___pixel:
2088 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2089 break;
2090
Douglas Gregor450c75a2008-11-07 15:42:26 +00002091 // class-specifier:
2092 case tok::kw_class:
2093 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002094 case tok::kw_union: {
2095 tok::TokenKind Kind = Tok.getKind();
2096 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002097 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2098 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002099 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002100 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002101
2102 // enum-specifier:
2103 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002104 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002105 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002106 return true;
2107
2108 // cv-qualifier:
2109 case tok::kw_const:
2110 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002111 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002112 break;
2113 case tok::kw_volatile:
2114 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002115 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002116 break;
2117 case tok::kw_restrict:
2118 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002119 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002120 break;
2121
2122 // GNU typeof support.
2123 case tok::kw_typeof:
2124 ParseTypeofSpecifier(DS);
2125 return true;
2126
Anders Carlsson74948d02009-06-24 17:47:40 +00002127 // C++0x decltype support.
2128 case tok::kw_decltype:
2129 ParseDecltypeSpecifier(DS);
2130 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002131
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002132 // OpenCL qualifiers:
2133 case tok::kw_private:
2134 if (!getLang().OpenCL)
2135 return false;
2136 case tok::kw___private:
2137 case tok::kw___global:
2138 case tok::kw___local:
2139 case tok::kw___constant:
2140 case tok::kw___read_only:
2141 case tok::kw___write_only:
2142 case tok::kw___read_write:
2143 ParseOpenCLQualifiers(DS);
2144 break;
2145
Anders Carlssonbae27372009-06-26 23:44:14 +00002146 // C++0x auto support.
2147 case tok::kw_auto:
2148 if (!getLang().CPlusPlus0x)
2149 return false;
2150
John McCall49bfce42009-08-03 20:12:06 +00002151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002152 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002153
Eli Friedman53339e02009-06-08 23:27:34 +00002154 case tok::kw___ptr64:
2155 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002156 case tok::kw___cdecl:
2157 case tok::kw___stdcall:
2158 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002159 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002160 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002161 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002162
Dawn Perchik335e16b2010-09-03 01:29:35 +00002163 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002164 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002165 return true;
2166
Douglas Gregor450c75a2008-11-07 15:42:26 +00002167 default:
2168 // Not a type-specifier; do nothing.
2169 return false;
2170 }
2171
2172 // If the specifier combination wasn't legal, issue a diagnostic.
2173 if (isInvalid) {
2174 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002175 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002176 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002177 }
2178 DS.SetRangeEnd(Tok.getLocation());
2179 ConsumeToken(); // whatever we parsed above.
2180 return true;
2181}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002182
Chris Lattner70ae4912007-10-29 04:42:53 +00002183/// ParseStructDeclaration - Parse a struct declaration without the terminating
2184/// semicolon.
2185///
Chris Lattner90a26b02007-01-23 04:38:16 +00002186/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002187/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002188/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002189/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002190/// struct-declarator-list:
2191/// struct-declarator
2192/// struct-declarator-list ',' struct-declarator
2193/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2194/// struct-declarator:
2195/// declarator
2196/// [GNU] declarator attributes[opt]
2197/// declarator[opt] ':' constant-expression
2198/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2199///
Chris Lattnera12405b2008-04-10 06:46:29 +00002200void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002201ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002202 if (Tok.is(tok::kw___extension__)) {
2203 // __extension__ silences extension warnings in the subexpression.
2204 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002205 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002206 return ParseStructDeclaration(DS, Fields);
2207 }
Mike Stump11289f42009-09-09 15:08:12 +00002208
Steve Naroff97170802007-08-20 22:28:22 +00002209 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002210 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002211
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002212 // If there are no declarators, this is a free-standing declaration
2213 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002214 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002215 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002216 return;
2217 }
2218
2219 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002220 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002221 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002222 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002223 FieldDeclarator DeclaratorInfo(DS);
2224
2225 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002226 if (!FirstDeclarator)
2227 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002228
Steve Naroff97170802007-08-20 22:28:22 +00002229 /// struct-declarator: declarator
2230 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002231 if (Tok.isNot(tok::colon)) {
2232 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2233 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002234 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002235 }
Mike Stump11289f42009-09-09 15:08:12 +00002236
Chris Lattner76c72282007-10-09 17:33:22 +00002237 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002238 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002239 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002240 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002241 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002242 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002243 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002244 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002245
Steve Naroff97170802007-08-20 22:28:22 +00002246 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002247 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002248
John McCallcfefb6d2009-11-03 02:38:08 +00002249 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002250 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002251 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002252
Steve Naroff97170802007-08-20 22:28:22 +00002253 // If we don't have a comma, it is either the end of the list (a ';')
2254 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002255 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002256 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002257
Steve Naroff97170802007-08-20 22:28:22 +00002258 // Consume the comma.
2259 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002260
John McCallcfefb6d2009-11-03 02:38:08 +00002261 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002262 }
Steve Naroff97170802007-08-20 22:28:22 +00002263}
2264
2265/// ParseStructUnionBody
2266/// struct-contents:
2267/// struct-declaration-list
2268/// [EXT] empty
2269/// [GNU] "struct-declaration-list" without terminatoring ';'
2270/// struct-declaration-list:
2271/// struct-declaration
2272/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002273/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002274///
Chris Lattner1300fb92007-01-23 23:42:53 +00002275void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002276 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002277 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2278 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002279
Chris Lattner90a26b02007-01-23 04:38:16 +00002280 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002281
Douglas Gregor658b9552009-01-09 22:42:13 +00002282 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002283 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002284
Chris Lattner7b9ace62007-01-23 20:11:08 +00002285 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2286 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002287 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002288 Diag(Tok, diag::ext_empty_struct_union)
2289 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002290
John McCall48871652010-08-21 09:40:31 +00002291 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002292
Chris Lattner7b9ace62007-01-23 20:11:08 +00002293 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002294 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002295 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattner736ed5d2007-06-09 05:59:07 +00002297 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002298 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002299 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002300 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002301 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002302 ConsumeToken();
2303 continue;
2304 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002305
2306 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002307 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002308
John McCallcfefb6d2009-11-03 02:38:08 +00002309 if (!Tok.is(tok::at)) {
2310 struct CFieldCallback : FieldCallback {
2311 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002312 Decl *TagDecl;
2313 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002314
John McCall48871652010-08-21 09:40:31 +00002315 CFieldCallback(Parser &P, Decl *TagDecl,
2316 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002317 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2318
John McCall48871652010-08-21 09:40:31 +00002319 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002320 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002321 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002322 FD.D.getDeclSpec().getSourceRange().getBegin(),
2323 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002324 FieldDecls.push_back(Field);
2325 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002326 }
John McCallcfefb6d2009-11-03 02:38:08 +00002327 } Callback(*this, TagDecl, FieldDecls);
2328
2329 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002330 } else { // Handle @defs
2331 ConsumeToken();
2332 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2333 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002334 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002335 continue;
2336 }
2337 ConsumeToken();
2338 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2339 if (!Tok.is(tok::identifier)) {
2340 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002341 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002342 continue;
2343 }
John McCall48871652010-08-21 09:40:31 +00002344 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002345 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002346 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002347 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2348 ConsumeToken();
2349 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002350 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002351
Chris Lattner76c72282007-10-09 17:33:22 +00002352 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002353 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002354 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002355 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002356 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002357 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002358 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2359 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002360 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002361 // If we stopped at a ';', eat it.
2362 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002363 }
2364 }
Mike Stump11289f42009-09-09 15:08:12 +00002365
Steve Naroff33a1e802007-10-29 21:38:07 +00002366 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002367
John McCall084e83d2011-03-24 11:26:52 +00002368 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002369 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002370 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002371
Douglas Gregor0be31a22010-07-02 17:43:08 +00002372 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002373 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002374 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002375 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002376 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002377 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002378}
2379
Chris Lattner3b561a32006-08-13 00:12:11 +00002380/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002381/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002382/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002383///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002384/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2385/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002386/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002387/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002388///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002389/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2390/// [C++0x] enum-head '{' enumerator-list ',' '}'
2391///
2392/// enum-head: [C++0x]
2393/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2394/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2395///
2396/// enum-key: [C++0x]
2397/// 'enum'
2398/// 'enum' 'class'
2399/// 'enum' 'struct'
2400///
2401/// enum-base: [C++0x]
2402/// ':' type-specifier-seq
2403///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002404/// [C++] elaborated-type-specifier:
2405/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2406///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002407void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002408 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002409 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002410 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002411 if (Tok.is(tok::code_completion)) {
2412 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002413 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002414 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002415 }
2416
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002417 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002418 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002419 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002420
Abramo Bagnarad7548482010-05-19 21:37:53 +00002421 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002422 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002423 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002424 return;
2425
2426 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002427 Diag(Tok, diag::err_expected_ident);
2428 if (Tok.isNot(tok::l_brace)) {
2429 // Has no name and is not a definition.
2430 // Skip the rest of this declarator, up until the comma or semicolon.
2431 SkipUntil(tok::comma, true);
2432 return;
2433 }
2434 }
2435 }
Mike Stump11289f42009-09-09 15:08:12 +00002436
Douglas Gregora1aec292011-02-22 20:32:04 +00002437 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002438 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002439 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002440
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002441 if (getLang().CPlusPlus0x &&
2442 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002443 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002444 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2445 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002446 }
2447
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002448 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002449 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2450 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002451 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002452
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002453 // Skip the rest of this declarator, up until the comma or semicolon.
2454 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002455 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002456 }
Mike Stump11289f42009-09-09 15:08:12 +00002457
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002458 // If an identifier is present, consume and remember it.
2459 IdentifierInfo *Name = 0;
2460 SourceLocation NameLoc;
2461 if (Tok.is(tok::identifier)) {
2462 Name = Tok.getIdentifierInfo();
2463 NameLoc = ConsumeToken();
2464 }
Mike Stump11289f42009-09-09 15:08:12 +00002465
Douglas Gregor0bf31402010-10-08 23:50:27 +00002466 if (!Name && IsScopedEnum) {
2467 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2468 // declaration of a scoped enumeration.
2469 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2470 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002471 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002472 }
2473
2474 TypeResult BaseType;
2475
Douglas Gregord1f69f62010-12-01 17:42:47 +00002476 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002477 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002478 bool PossibleBitfield = false;
2479 if (getCurScope()->getFlags() & Scope::ClassScope) {
2480 // If we're in class scope, this can either be an enum declaration with
2481 // an underlying type, or a declaration of a bitfield member. We try to
2482 // use a simple disambiguation scheme first to catch the common cases
2483 // (integer literal, sizeof); if it's still ambiguous, we then consider
2484 // anything that's a simple-type-specifier followed by '(' as an
2485 // expression. This suffices because function types are not valid
2486 // underlying types anyway.
2487 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2488 // If the next token starts an expression, we know we're parsing a
2489 // bit-field. This is the common case.
2490 if (TPR == TPResult::True())
2491 PossibleBitfield = true;
2492 // If the next token starts a type-specifier-seq, it may be either a
2493 // a fixed underlying type or the start of a function-style cast in C++;
2494 // lookahead one more token to see if it's obvious that we have a
2495 // fixed underlying type.
2496 else if (TPR == TPResult::False() &&
2497 GetLookAheadToken(2).getKind() == tok::semi) {
2498 // Consume the ':'.
2499 ConsumeToken();
2500 } else {
2501 // We have the start of a type-specifier-seq, so we have to perform
2502 // tentative parsing to determine whether we have an expression or a
2503 // type.
2504 TentativeParsingAction TPA(*this);
2505
2506 // Consume the ':'.
2507 ConsumeToken();
2508
Douglas Gregora1aec292011-02-22 20:32:04 +00002509 if ((getLang().CPlusPlus &&
2510 isCXXDeclarationSpecifier() != TPResult::True()) ||
2511 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002512 // We'll parse this as a bitfield later.
2513 PossibleBitfield = true;
2514 TPA.Revert();
2515 } else {
2516 // We have a type-specifier-seq.
2517 TPA.Commit();
2518 }
2519 }
2520 } else {
2521 // Consume the ':'.
2522 ConsumeToken();
2523 }
2524
2525 if (!PossibleBitfield) {
2526 SourceRange Range;
2527 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002528
2529 if (!getLang().CPlusPlus0x)
2530 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2531 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002532 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002533 }
2534
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002535 // There are three options here. If we have 'enum foo;', then this is a
2536 // forward declaration. If we have 'enum foo {...' then this is a
2537 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2538 //
2539 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2540 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2541 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2542 //
John McCallfaf5fb42010-08-26 23:41:50 +00002543 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002544 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002545 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002546 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002547 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002548 else
John McCallfaf5fb42010-08-26 23:41:50 +00002549 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002550
2551 // enums cannot be templates, although they can be referenced from a
2552 // template.
2553 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002554 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002555 Diag(Tok, diag::err_enum_template);
2556
2557 // Skip the rest of this declarator, up until the comma or semicolon.
2558 SkipUntil(tok::comma, true);
2559 return;
2560 }
2561
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002562 if (!Name && TUK != Sema::TUK_Definition) {
2563 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2564
2565 // Skip the rest of this declarator, up until the comma or semicolon.
2566 SkipUntil(tok::comma, true);
2567 return;
2568 }
2569
Douglas Gregord6ab8742009-05-28 23:31:59 +00002570 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002571 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002572 const char *PrevSpec = 0;
2573 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002574 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002575 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002576 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002577 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002578 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002579 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002580
Douglas Gregorba41d012010-04-24 16:38:41 +00002581 if (IsDependent) {
2582 // This enum has a dependent nested-name-specifier. Handle it as a
2583 // dependent tag.
2584 if (!Name) {
2585 DS.SetTypeSpecError();
2586 Diag(Tok, diag::err_expected_type_name_after_typename);
2587 return;
2588 }
2589
Douglas Gregor0be31a22010-07-02 17:43:08 +00002590 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002591 TUK, SS, Name, StartLoc,
2592 NameLoc);
2593 if (Type.isInvalid()) {
2594 DS.SetTypeSpecError();
2595 return;
2596 }
2597
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002598 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2599 NameLoc.isValid() ? NameLoc : StartLoc,
2600 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002601 Diag(StartLoc, DiagID) << PrevSpec;
2602
2603 return;
2604 }
Mike Stump11289f42009-09-09 15:08:12 +00002605
John McCall48871652010-08-21 09:40:31 +00002606 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002607 // The action failed to produce an enumeration tag. If this is a
2608 // definition, consume the entire definition.
2609 if (Tok.is(tok::l_brace)) {
2610 ConsumeBrace();
2611 SkipUntil(tok::r_brace);
2612 }
2613
2614 DS.SetTypeSpecError();
2615 return;
2616 }
2617
Chris Lattner76c72282007-10-09 17:33:22 +00002618 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002619 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002620
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002621 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2622 NameLoc.isValid() ? NameLoc : StartLoc,
2623 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002624 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002625}
2626
Chris Lattnerc1915e22007-01-25 07:29:02 +00002627/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2628/// enumerator-list:
2629/// enumerator
2630/// enumerator-list ',' enumerator
2631/// enumerator:
2632/// enumeration-constant
2633/// enumeration-constant '=' constant-expression
2634/// enumeration-constant:
2635/// identifier
2636///
John McCall48871652010-08-21 09:40:31 +00002637void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002638 // Enter the scope of the enum body and start the definition.
2639 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002640 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002641
Chris Lattnerc1915e22007-01-25 07:29:02 +00002642 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002643
Chris Lattner37256fb2007-08-27 17:24:30 +00002644 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002645 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002646 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002647
John McCall48871652010-08-21 09:40:31 +00002648 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002649
John McCall48871652010-08-21 09:40:31 +00002650 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002651
Chris Lattnerc1915e22007-01-25 07:29:02 +00002652 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002653 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002654 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2655 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002656
John McCall811a0f52010-10-22 23:36:17 +00002657 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002658 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002659 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002660
Chris Lattnerc1915e22007-01-25 07:29:02 +00002661 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002662 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002663 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002664 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002665 AssignedVal = ParseConstantExpression();
2666 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002667 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002668 }
Mike Stump11289f42009-09-09 15:08:12 +00002669
Chris Lattnerc1915e22007-01-25 07:29:02 +00002670 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002671 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2672 LastEnumConstDecl,
2673 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002674 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002675 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002676 EnumConstantDecls.push_back(EnumConstDecl);
2677 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002678
Douglas Gregorce66d022010-09-07 14:51:08 +00002679 if (Tok.is(tok::identifier)) {
2680 // We're missing a comma between enumerators.
2681 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2682 Diag(Loc, diag::err_enumerator_list_missing_comma)
2683 << FixItHint::CreateInsertion(Loc, ", ");
2684 continue;
2685 }
2686
Chris Lattner76c72282007-10-09 17:33:22 +00002687 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002688 break;
2689 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002690
2691 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002692 !(getLang().C99 || getLang().CPlusPlus0x))
2693 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2694 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002695 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002696 }
Mike Stump11289f42009-09-09 15:08:12 +00002697
Chris Lattnerc1915e22007-01-25 07:29:02 +00002698 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002699 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002700
Chris Lattnerc1915e22007-01-25 07:29:02 +00002701 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002702 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002703 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002704
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002705 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2706 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002707 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002708
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002709 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002710 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002711}
Chris Lattner3b561a32006-08-13 00:12:11 +00002712
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002713/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002714/// start of a type-qualifier-list.
2715bool Parser::isTypeQualifier() const {
2716 switch (Tok.getKind()) {
2717 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002718
2719 // type-qualifier only in OpenCL
2720 case tok::kw_private:
2721 return getLang().OpenCL;
2722
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002723 // type-qualifier
2724 case tok::kw_const:
2725 case tok::kw_volatile:
2726 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002727 case tok::kw___private:
2728 case tok::kw___local:
2729 case tok::kw___global:
2730 case tok::kw___constant:
2731 case tok::kw___read_only:
2732 case tok::kw___read_write:
2733 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002734 return true;
2735 }
2736}
2737
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002738/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2739/// is definitely a type-specifier. Return false if it isn't part of a type
2740/// specifier or if we're not sure.
2741bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2742 switch (Tok.getKind()) {
2743 default: return false;
2744 // type-specifiers
2745 case tok::kw_short:
2746 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002747 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002748 case tok::kw_signed:
2749 case tok::kw_unsigned:
2750 case tok::kw__Complex:
2751 case tok::kw__Imaginary:
2752 case tok::kw_void:
2753 case tok::kw_char:
2754 case tok::kw_wchar_t:
2755 case tok::kw_char16_t:
2756 case tok::kw_char32_t:
2757 case tok::kw_int:
2758 case tok::kw_float:
2759 case tok::kw_double:
2760 case tok::kw_bool:
2761 case tok::kw__Bool:
2762 case tok::kw__Decimal32:
2763 case tok::kw__Decimal64:
2764 case tok::kw__Decimal128:
2765 case tok::kw___vector:
2766
2767 // struct-or-union-specifier (C99) or class-specifier (C++)
2768 case tok::kw_class:
2769 case tok::kw_struct:
2770 case tok::kw_union:
2771 // enum-specifier
2772 case tok::kw_enum:
2773
2774 // typedef-name
2775 case tok::annot_typename:
2776 return true;
2777 }
2778}
2779
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002780/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002781/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002782bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002783 switch (Tok.getKind()) {
2784 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002785
Chris Lattner020bab92009-01-04 23:41:41 +00002786 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002787 if (TryAltiVecVectorToken())
2788 return true;
2789 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002790 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002791 // Annotate typenames and C++ scope specifiers. If we get one, just
2792 // recurse to handle whatever we get.
2793 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002794 return true;
2795 if (Tok.is(tok::identifier))
2796 return false;
2797 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002798
Chris Lattner020bab92009-01-04 23:41:41 +00002799 case tok::coloncolon: // ::foo::bar
2800 if (NextToken().is(tok::kw_new) || // ::new
2801 NextToken().is(tok::kw_delete)) // ::delete
2802 return false;
2803
Chris Lattner020bab92009-01-04 23:41:41 +00002804 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002805 return true;
2806 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002807
Chris Lattnere37e2332006-08-15 04:50:22 +00002808 // GNU attributes support.
2809 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002810 // GNU typeof support.
2811 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002813 // type-specifiers
2814 case tok::kw_short:
2815 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002816 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002817 case tok::kw_signed:
2818 case tok::kw_unsigned:
2819 case tok::kw__Complex:
2820 case tok::kw__Imaginary:
2821 case tok::kw_void:
2822 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002823 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002824 case tok::kw_char16_t:
2825 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002826 case tok::kw_int:
2827 case tok::kw_float:
2828 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002829 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002830 case tok::kw__Bool:
2831 case tok::kw__Decimal32:
2832 case tok::kw__Decimal64:
2833 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002834 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002835
Chris Lattner861a2262008-04-13 18:59:07 +00002836 // struct-or-union-specifier (C99) or class-specifier (C++)
2837 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002838 case tok::kw_struct:
2839 case tok::kw_union:
2840 // enum-specifier
2841 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002842
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002843 // type-qualifier
2844 case tok::kw_const:
2845 case tok::kw_volatile:
2846 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002847
2848 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002849 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002850 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002851
Chris Lattner409bf7d2008-10-20 00:25:30 +00002852 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2853 case tok::less:
2854 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002855
Steve Naroff44ac7772008-12-25 14:16:32 +00002856 case tok::kw___cdecl:
2857 case tok::kw___stdcall:
2858 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002859 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002860 case tok::kw___w64:
2861 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002862 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002863
2864 case tok::kw___private:
2865 case tok::kw___local:
2866 case tok::kw___global:
2867 case tok::kw___constant:
2868 case tok::kw___read_only:
2869 case tok::kw___read_write:
2870 case tok::kw___write_only:
2871
Eli Friedman53339e02009-06-08 23:27:34 +00002872 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002873
2874 case tok::kw_private:
2875 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002876 }
2877}
2878
Chris Lattneracd58a32006-08-06 17:24:14 +00002879/// isDeclarationSpecifier() - Return true if the current token is part of a
2880/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002881///
2882/// \param DisambiguatingWithExpression True to indicate that the purpose of
2883/// this check is to disambiguate between an expression and a declaration.
2884bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002885 switch (Tok.getKind()) {
2886 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002887
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002888 case tok::kw_private:
2889 return getLang().OpenCL;
2890
Chris Lattner020bab92009-01-04 23:41:41 +00002891 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002892 // Unfortunate hack to support "Class.factoryMethod" notation.
2893 if (getLang().ObjC1 && NextToken().is(tok::period))
2894 return false;
John Thompson22334602010-02-05 00:12:22 +00002895 if (TryAltiVecVectorToken())
2896 return true;
2897 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002898 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002899 // Annotate typenames and C++ scope specifiers. If we get one, just
2900 // recurse to handle whatever we get.
2901 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002902 return true;
2903 if (Tok.is(tok::identifier))
2904 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002905
2906 // If we're in Objective-C and we have an Objective-C class type followed
2907 // by an identifier and then either ':' or ']', in a place where an
2908 // expression is permitted, then this is probably a class message send
2909 // missing the initial '['. In this case, we won't consider this to be
2910 // the start of a declaration.
2911 if (DisambiguatingWithExpression &&
2912 isStartOfObjCClassMessageMissingOpenBracket())
2913 return false;
2914
John McCall1f476a12010-02-26 08:45:28 +00002915 return isDeclarationSpecifier();
2916
Chris Lattner020bab92009-01-04 23:41:41 +00002917 case tok::coloncolon: // ::foo::bar
2918 if (NextToken().is(tok::kw_new) || // ::new
2919 NextToken().is(tok::kw_delete)) // ::delete
2920 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002921
Chris Lattner020bab92009-01-04 23:41:41 +00002922 // Annotate typenames and C++ scope specifiers. If we get one, just
2923 // recurse to handle whatever we get.
2924 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002925 return true;
2926 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002927
Chris Lattneracd58a32006-08-06 17:24:14 +00002928 // storage-class-specifier
2929 case tok::kw_typedef:
2930 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002931 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002932 case tok::kw_static:
2933 case tok::kw_auto:
2934 case tok::kw_register:
2935 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002936
Chris Lattneracd58a32006-08-06 17:24:14 +00002937 // type-specifiers
2938 case tok::kw_short:
2939 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002940 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00002941 case tok::kw_signed:
2942 case tok::kw_unsigned:
2943 case tok::kw__Complex:
2944 case tok::kw__Imaginary:
2945 case tok::kw_void:
2946 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002947 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002948 case tok::kw_char16_t:
2949 case tok::kw_char32_t:
2950
Chris Lattneracd58a32006-08-06 17:24:14 +00002951 case tok::kw_int:
2952 case tok::kw_float:
2953 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002954 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002955 case tok::kw__Bool:
2956 case tok::kw__Decimal32:
2957 case tok::kw__Decimal64:
2958 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002959 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002960
Chris Lattner861a2262008-04-13 18:59:07 +00002961 // struct-or-union-specifier (C99) or class-specifier (C++)
2962 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002963 case tok::kw_struct:
2964 case tok::kw_union:
2965 // enum-specifier
2966 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002967
Chris Lattneracd58a32006-08-06 17:24:14 +00002968 // type-qualifier
2969 case tok::kw_const:
2970 case tok::kw_volatile:
2971 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002972
Chris Lattneracd58a32006-08-06 17:24:14 +00002973 // function-specifier
2974 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002975 case tok::kw_virtual:
2976 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002977
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002978 // static_assert-declaration
2979 case tok::kw__Static_assert:
2980
Chris Lattner599e47e2007-08-09 17:01:07 +00002981 // GNU typeof support.
2982 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002983
Chris Lattner599e47e2007-08-09 17:01:07 +00002984 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002985 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002986 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002987
Chris Lattner8b2ec162008-07-26 03:38:44 +00002988 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2989 case tok::less:
2990 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002991
Douglas Gregor19b7acf2011-04-27 05:41:15 +00002992 // typedef-name
2993 case tok::annot_typename:
2994 return !DisambiguatingWithExpression ||
2995 !isStartOfObjCClassMessageMissingOpenBracket();
2996
Steve Narofff192fab2009-01-06 19:34:12 +00002997 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002998 case tok::kw___cdecl:
2999 case tok::kw___stdcall:
3000 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003001 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003002 case tok::kw___w64:
3003 case tok::kw___ptr64:
3004 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003005 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003006
3007 case tok::kw___private:
3008 case tok::kw___local:
3009 case tok::kw___global:
3010 case tok::kw___constant:
3011 case tok::kw___read_only:
3012 case tok::kw___read_write:
3013 case tok::kw___write_only:
3014
Eli Friedman53339e02009-06-08 23:27:34 +00003015 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003016 }
3017}
3018
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003019bool Parser::isConstructorDeclarator() {
3020 TentativeParsingAction TPA(*this);
3021
3022 // Parse the C++ scope specifier.
3023 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003024 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003025 TPA.Revert();
3026 return false;
3027 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003028
3029 // Parse the constructor name.
3030 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3031 // We already know that we have a constructor name; just consume
3032 // the token.
3033 ConsumeToken();
3034 } else {
3035 TPA.Revert();
3036 return false;
3037 }
3038
3039 // Current class name must be followed by a left parentheses.
3040 if (Tok.isNot(tok::l_paren)) {
3041 TPA.Revert();
3042 return false;
3043 }
3044 ConsumeParen();
3045
3046 // A right parentheses or ellipsis signals that we have a constructor.
3047 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3048 TPA.Revert();
3049 return true;
3050 }
3051
3052 // If we need to, enter the specified scope.
3053 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003054 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003055 DeclScopeObj.EnterDeclaratorScope();
3056
Francois Pichet79f3a872011-01-31 04:54:32 +00003057 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003058 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003059 MaybeParseMicrosoftAttributes(Attrs);
3060
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003061 // Check whether the next token(s) are part of a declaration
3062 // specifier, in which case we have the start of a parameter and,
3063 // therefore, we know that this is a constructor.
3064 bool IsConstructor = isDeclarationSpecifier();
3065 TPA.Revert();
3066 return IsConstructor;
3067}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003068
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003069/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003070/// type-qualifier-list: [C99 6.7.5]
3071/// type-qualifier
3072/// [vendor] attributes
3073/// [ only if VendorAttributesAllowed=true ]
3074/// type-qualifier-list type-qualifier
3075/// [vendor] type-qualifier-list attributes
3076/// [ only if VendorAttributesAllowed=true ]
3077/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3078/// [ only if CXX0XAttributesAllowed=true ]
3079/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003080///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003081void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3082 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003083 bool CXX0XAttributesAllowed) {
3084 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3085 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003086 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003087 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003088 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003089 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003090 else
3091 Diag(Loc, diag::err_attributes_not_allowed);
3092 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003093
3094 SourceLocation EndLoc;
3095
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003096 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003097 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003098 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003099 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003100 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003101
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003102 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003103 case tok::code_completion:
3104 Actions.CodeCompleteTypeQualifiers(DS);
3105 ConsumeCodeCompletionToken();
3106 break;
3107
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003108 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003109 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3110 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003111 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003112 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003113 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3114 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003115 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003116 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003117 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3118 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003119 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003120
3121 // OpenCL qualifiers:
3122 case tok::kw_private:
3123 if (!getLang().OpenCL)
3124 goto DoneWithTypeQuals;
3125 case tok::kw___private:
3126 case tok::kw___global:
3127 case tok::kw___local:
3128 case tok::kw___constant:
3129 case tok::kw___read_only:
3130 case tok::kw___write_only:
3131 case tok::kw___read_write:
3132 ParseOpenCLQualifiers(DS);
3133 break;
3134
Eli Friedman53339e02009-06-08 23:27:34 +00003135 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003136 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003137 case tok::kw___cdecl:
3138 case tok::kw___stdcall:
3139 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003140 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003141 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003142 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003143 continue;
3144 }
3145 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003146 case tok::kw___pascal:
3147 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003148 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003149 continue;
3150 }
3151 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003152 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003153 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003154 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003155 continue; // do *not* consume the next token!
3156 }
3157 // otherwise, FALL THROUGH!
3158 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003159 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003160 // If this is not a type-qualifier token, we're done reading type
3161 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003162 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003163 if (EndLoc.isValid())
3164 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003165 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003166 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003167
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003168 // If the specifier combination wasn't legal, issue a diagnostic.
3169 if (isInvalid) {
3170 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003171 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003172 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003173 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003174 }
3175}
3176
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003177
3178/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3179///
3180void Parser::ParseDeclarator(Declarator &D) {
3181 /// This implements the 'declarator' production in the C grammar, then checks
3182 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003183 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003184}
3185
Sebastian Redlbd150f42008-11-21 19:14:01 +00003186/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3187/// is parsed by the function passed to it. Pass null, and the direct-declarator
3188/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003189/// ptr-operator production.
3190///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003191/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3192/// [C] pointer[opt] direct-declarator
3193/// [C++] direct-declarator
3194/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003195///
3196/// pointer: [C99 6.7.5]
3197/// '*' type-qualifier-list[opt]
3198/// '*' type-qualifier-list[opt] pointer
3199///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003200/// ptr-operator:
3201/// '*' cv-qualifier-seq[opt]
3202/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003203/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003204/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003205/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003206/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003207void Parser::ParseDeclaratorInternal(Declarator &D,
3208 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003209 if (Diags.hasAllExtensionsSilenced())
3210 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003211
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003212 // C++ member pointers start with a '::' or a nested-name.
3213 // Member pointers get special handling, since there's no place for the
3214 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003215 if (getLang().CPlusPlus &&
3216 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3217 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003218 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003219 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003220
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003221 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003222 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003223 // The scope spec really belongs to the direct-declarator.
3224 D.getCXXScopeSpec() = SS;
3225 if (DirectDeclParser)
3226 (this->*DirectDeclParser)(D);
3227 return;
3228 }
3229
3230 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003231 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003232 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003233 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003234 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003235
3236 // Recurse to parse whatever is left.
3237 ParseDeclaratorInternal(D, DirectDeclParser);
3238
3239 // Sema will have to catch (syntactically invalid) pointers into global
3240 // scope. It has to catch pointers into namespace scope anyway.
3241 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003242 Loc),
3243 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003244 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003245 return;
3246 }
3247 }
3248
3249 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003250 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003251 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003252 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003253 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003254 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003255 if (DirectDeclParser)
3256 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003257 return;
3258 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003259
Sebastian Redled0f3b02009-03-15 22:02:01 +00003260 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3261 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003262 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003263 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003264
Chris Lattner9eac9312009-03-27 04:18:06 +00003265 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003266 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003267 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003268
Bill Wendling3708c182007-05-27 10:15:43 +00003269 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003270 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003271
Bill Wendling3708c182007-05-27 10:15:43 +00003272 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003273 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003274 if (Kind == tok::star)
3275 // Remember that we parsed a pointer type, and remember the type-quals.
3276 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003277 DS.getConstSpecLoc(),
3278 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003279 DS.getRestrictSpecLoc()),
3280 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003281 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003282 else
3283 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003284 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003285 Loc),
3286 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003287 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003288 } else {
3289 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003290 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003291
Sebastian Redl3b27be62009-03-23 00:00:23 +00003292 // Complain about rvalue references in C++03, but then go on and build
3293 // the declarator.
3294 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003295 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003296
Bill Wendling93efb222007-06-02 23:28:54 +00003297 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3298 // cv-qualifiers are introduced through the use of a typedef or of a
3299 // template type argument, in which case the cv-qualifiers are ignored.
3300 //
3301 // [GNU] Retricted references are allowed.
3302 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003303 // [C++0x] Attributes on references are not allowed.
3304 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003305 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003306
3307 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3308 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3309 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003310 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003311 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3312 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003313 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003314 }
Bill Wendling3708c182007-05-27 10:15:43 +00003315
3316 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003317 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003318
Douglas Gregor66583c52008-11-03 15:51:28 +00003319 if (D.getNumTypeObjects() > 0) {
3320 // C++ [dcl.ref]p4: There shall be no references to references.
3321 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3322 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003323 if (const IdentifierInfo *II = D.getIdentifier())
3324 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3325 << II;
3326 else
3327 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3328 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003329
Sebastian Redlbd150f42008-11-21 19:14:01 +00003330 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003331 // can go ahead and build the (technically ill-formed)
3332 // declarator: reference collapsing will take care of it.
3333 }
3334 }
3335
Bill Wendling3708c182007-05-27 10:15:43 +00003336 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003337 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003338 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003339 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003340 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003341 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003342}
3343
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003344/// ParseDirectDeclarator
3345/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003346/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003347/// '(' declarator ')'
3348/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003349/// [C90] direct-declarator '[' constant-expression[opt] ']'
3350/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3351/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3352/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3353/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003354/// direct-declarator '(' parameter-type-list ')'
3355/// direct-declarator '(' identifier-list[opt] ')'
3356/// [GNU] direct-declarator '(' parameter-forward-declarations
3357/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003358/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3359/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003360/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003361///
3362/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003363/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003364/// '::'[opt] nested-name-specifier[opt] type-name
3365///
3366/// id-expression: [C++ 5.1]
3367/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003368/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003369///
3370/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003371/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003372/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003373/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003374/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003375/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003376///
Chris Lattneracd58a32006-08-06 17:24:14 +00003377void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003378 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003379
Douglas Gregor7861a802009-11-03 01:35:08 +00003380 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3381 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003382 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003383 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003384 }
3385
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003386 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003387 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003388 // Change the declaration context for name lookup, until this function
3389 // is exited (and the declarator has been parsed).
3390 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003391 }
3392
Douglas Gregor27b4c162010-12-23 22:44:42 +00003393 // C++0x [dcl.fct]p14:
3394 // There is a syntactic ambiguity when an ellipsis occurs at the end
3395 // of a parameter-declaration-clause without a preceding comma. In
3396 // this case, the ellipsis is parsed as part of the
3397 // abstract-declarator if the type of the parameter names a template
3398 // parameter pack that has not been expanded; otherwise, it is parsed
3399 // as part of the parameter-declaration-clause.
3400 if (Tok.is(tok::ellipsis) &&
3401 !((D.getContext() == Declarator::PrototypeContext ||
3402 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003403 NextToken().is(tok::r_paren) &&
3404 !Actions.containsUnexpandedParameterPacks(D)))
3405 D.setEllipsisLoc(ConsumeToken());
3406
Douglas Gregor7861a802009-11-03 01:35:08 +00003407 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3408 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3409 // We found something that indicates the start of an unqualified-id.
3410 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003411 bool AllowConstructorName;
3412 if (D.getDeclSpec().hasTypeSpecifier())
3413 AllowConstructorName = false;
3414 else if (D.getCXXScopeSpec().isSet())
3415 AllowConstructorName =
3416 (D.getContext() == Declarator::FileContext ||
3417 (D.getContext() == Declarator::MemberContext &&
3418 D.getDeclSpec().isFriendSpecified()));
3419 else
3420 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3421
Douglas Gregor7861a802009-11-03 01:35:08 +00003422 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3423 /*EnteringContext=*/true,
3424 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003425 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003426 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003427 D.getName()) ||
3428 // Once we're past the identifier, if the scope was bad, mark the
3429 // whole declarator bad.
3430 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003431 D.SetIdentifier(0, Tok.getLocation());
3432 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003433 } else {
3434 // Parsed the unqualified-id; update range information and move along.
3435 if (D.getSourceRange().getBegin().isInvalid())
3436 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3437 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003438 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003439 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003440 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003441 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003442 assert(!getLang().CPlusPlus &&
3443 "There's a C++-specific check for tok::identifier above");
3444 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3445 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3446 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003447 goto PastIdentifier;
3448 }
3449
3450 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003451 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003452 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003453 // Example: 'char (*X)' or 'int (*XX)(void)'
3454 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003455
3456 // If the declarator was parenthesized, we entered the declarator
3457 // scope when parsing the parenthesized declarator, then exited
3458 // the scope already. Re-enter the scope, if we need to.
3459 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003460 // If there was an error parsing parenthesized declarator, declarator
3461 // scope may have been enterred before. Don't do it again.
3462 if (!D.isInvalidType() &&
3463 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003464 // Change the declaration context for name lookup, until this function
3465 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003466 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003467 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003468 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003469 // This could be something simple like "int" (in which case the declarator
3470 // portion is empty), if an abstract-declarator is allowed.
3471 D.SetIdentifier(0, Tok.getLocation());
3472 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003473 if (D.getContext() == Declarator::MemberContext)
3474 Diag(Tok, diag::err_expected_member_name_or_semi)
3475 << D.getDeclSpec().getSourceRange();
3476 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003477 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003478 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003479 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003480 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003481 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003482 }
Mike Stump11289f42009-09-09 15:08:12 +00003483
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003484 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003485 assert(D.isPastIdentifier() &&
3486 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003487
Alexis Hunt96d5c762009-11-21 08:43:09 +00003488 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003489 if (D.getIdentifier())
3490 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003491
Chris Lattneracd58a32006-08-06 17:24:14 +00003492 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003493 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003494 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3495 // In such a case, check if we actually have a function declarator; if it
3496 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003497 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3498 // When not in file scope, warn for ambiguous function declarators, just
3499 // in case the author intended it as a variable definition.
3500 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3501 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3502 break;
3503 }
John McCall084e83d2011-03-24 11:26:52 +00003504 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003505 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003506 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003507 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003508 } else {
3509 break;
3510 }
3511 }
3512}
3513
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003514/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3515/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003516/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003517/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3518///
3519/// direct-declarator:
3520/// '(' declarator ')'
3521/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003522/// direct-declarator '(' parameter-type-list ')'
3523/// direct-declarator '(' identifier-list[opt] ')'
3524/// [GNU] direct-declarator '(' parameter-forward-declarations
3525/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003526///
3527void Parser::ParseParenDeclarator(Declarator &D) {
3528 SourceLocation StartLoc = ConsumeParen();
3529 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003530
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003531 // Eat any attributes before we look at whether this is a grouping or function
3532 // declarator paren. If this is a grouping paren, the attribute applies to
3533 // the type being built up, for example:
3534 // int (__attribute__(()) *x)(long y)
3535 // If this ends up not being a grouping paren, the attribute applies to the
3536 // first argument, for example:
3537 // int (__attribute__(()) int x)
3538 // In either case, we need to eat any attributes to be able to determine what
3539 // sort of paren this is.
3540 //
John McCall084e83d2011-03-24 11:26:52 +00003541 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003542 bool RequiresArg = false;
3543 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003544 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003545
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003546 // We require that the argument list (if this is a non-grouping paren) be
3547 // present even if the attribute list was empty.
3548 RequiresArg = true;
3549 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003550 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003551 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003552 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3553 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003554 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003555 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003556 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003557 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003558 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003559
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003560 // If we haven't past the identifier yet (or where the identifier would be
3561 // stored, if this is an abstract declarator), then this is probably just
3562 // grouping parens. However, if this could be an abstract-declarator, then
3563 // this could also be the start of function arguments (consider 'void()').
3564 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003565
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003566 if (!D.mayOmitIdentifier()) {
3567 // If this can't be an abstract-declarator, this *must* be a grouping
3568 // paren, because we haven't seen the identifier yet.
3569 isGrouping = true;
3570 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003571 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003572 isDeclarationSpecifier()) { // 'int(int)' is a function.
3573 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3574 // considered to be a type, not a K&R identifier-list.
3575 isGrouping = false;
3576 } else {
3577 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3578 isGrouping = true;
3579 }
Mike Stump11289f42009-09-09 15:08:12 +00003580
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003581 // If this is a grouping paren, handle:
3582 // direct-declarator: '(' declarator ')'
3583 // direct-declarator: '(' attributes declarator ')'
3584 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003585 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003586 D.setGroupingParens(true);
3587
Sebastian Redlbd150f42008-11-21 19:14:01 +00003588 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003589 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003590 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003591 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3592 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003593
3594 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003595 return;
3596 }
Mike Stump11289f42009-09-09 15:08:12 +00003597
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003598 // Okay, if this wasn't a grouping paren, it must be the start of a function
3599 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003600 // identifier (and remember where it would have been), then call into
3601 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003602 D.SetIdentifier(0, Tok.getLocation());
3603
John McCall53fa7142010-12-24 02:08:15 +00003604 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003605}
3606
3607/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3608/// declarator D up to a paren, which indicates that we are parsing function
3609/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003610///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003611/// If AttrList is non-null, then the caller parsed those arguments immediately
3612/// after the open paren - they should be considered to be the first argument of
3613/// a parameter. If RequiresArg is true, then the first argument of the
3614/// function is required to be present and required to not be an identifier
3615/// list.
3616///
Chris Lattneracd58a32006-08-06 17:24:14 +00003617/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003618/// parameter-type-list: [C99 6.7.5]
3619/// parameter-list
3620/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003621/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003622///
3623/// parameter-list: [C99 6.7.5]
3624/// parameter-declaration
3625/// parameter-list ',' parameter-declaration
3626///
3627/// parameter-declaration: [C99 6.7.5]
3628/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003629/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003630/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003631/// declaration-specifiers abstract-declarator[opt]
3632/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003633/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003634/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003635///
Douglas Gregor54992352011-01-26 03:43:54 +00003636/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3637/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003638///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003639/// [C++0x] exception-specification:
3640/// dynamic-exception-specification
3641/// noexcept-specification
3642///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003643void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003644 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003645 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003646 // lparen is already consumed!
3647 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003648
Douglas Gregor7fb25412010-10-01 18:44:50 +00003649 ParsedType TrailingReturnType;
3650
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003651 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003652 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003653 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003654 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003655
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003656 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003657
3658 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003659 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003660 SourceLocation RefQualifierLoc;
3661 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003662 ExceptionSpecificationType ESpecType = EST_None;
3663 SourceRange ESpecRange;
3664 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3665 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3666 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003667 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003668 MaybeParseCXX0XAttributes(attrs);
3669
Chris Lattnercf0bab22008-12-18 07:02:59 +00003670 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003671 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003672 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003673
Douglas Gregor54992352011-01-26 03:43:54 +00003674 // Parse ref-qualifier[opt]
3675 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3676 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003677 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003678
Douglas Gregor54992352011-01-26 03:43:54 +00003679 RefQualifierIsLValueRef = Tok.is(tok::amp);
3680 RefQualifierLoc = ConsumeToken();
3681 EndLoc = RefQualifierLoc;
3682 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003683
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003684 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003685 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3686 DynamicExceptions,
3687 DynamicExceptionRanges,
3688 NoexceptExpr);
3689 if (ESpecType != EST_None)
3690 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003691
3692 // Parse trailing-return-type.
3693 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3694 TrailingReturnType = ParseTrailingReturnType().get();
3695 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003696 }
3697
Chris Lattner371ed4e2008-04-06 06:57:35 +00003698 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003699 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003700 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003701 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003702 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003703 /*arglist*/ 0, 0,
3704 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003705 RefQualifierIsLValueRef,
3706 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003707 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003708 DynamicExceptions.data(),
3709 DynamicExceptionRanges.data(),
3710 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003711 NoexceptExpr.isUsable() ?
3712 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003713 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003714 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003715 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003716 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003717 }
3718
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003719 // Alternatively, this parameter list may be an identifier list form for a
3720 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003721 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3722 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003723 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003724 // K&R identifier lists can't have typedefs as identifiers, per
3725 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003726 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003727 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003728
Steve Naroffb0486722009-01-28 19:16:40 +00003729 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003730 // normal declarators, not for abstract-declarators. Get the first
3731 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003732 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003733 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003734
3735 // Identifier lists follow a really simple grammar: the identifiers can
3736 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3737 // identifier lists are really rare in the brave new modern world, and it
3738 // is very common for someone to typo a type in a non-k&r style list. If
3739 // we are presented with something like: "void foo(intptr x, float y)",
3740 // we don't want to start parsing the function declarator as though it is
3741 // a K&R style declarator just because intptr is an invalid type.
3742 //
3743 // To handle this, we check to see if the token after the first identifier
3744 // is a "," or ")". Only if so, do we parse it as an identifier list.
3745 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3746 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3747 FirstTok.getIdentifierInfo(),
3748 FirstTok.getLocation(), D);
3749
3750 // If we get here, the code is invalid. Push the first identifier back
3751 // into the token stream and parse the first argument as an (invalid)
3752 // normal argument declarator.
3753 PP.EnterToken(Tok);
3754 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003755 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003756 }
Mike Stump11289f42009-09-09 15:08:12 +00003757
Chris Lattner371ed4e2008-04-06 06:57:35 +00003758 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003759
Chris Lattner371ed4e2008-04-06 06:57:35 +00003760 // Build up an array of information about the parsed arguments.
3761 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003762
3763 // Enter function-declaration scope, limiting any declarators to the
3764 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003765 ParseScope PrototypeScope(this,
3766 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003767
Chris Lattner371ed4e2008-04-06 06:57:35 +00003768 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003769 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003770 while (1) {
3771 if (Tok.is(tok::ellipsis)) {
3772 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003773 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003774 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003775 }
Mike Stump11289f42009-09-09 15:08:12 +00003776
Chris Lattner371ed4e2008-04-06 06:57:35 +00003777 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003778 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003779 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003780
3781 // Skip any Microsoft attributes before a param.
3782 if (getLang().Microsoft && Tok.is(tok::l_square))
3783 ParseMicrosoftAttributes(DS.getAttributes());
3784
3785 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003786
3787 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003788 // Take them so that we only apply the attributes to the first parameter.
3789 DS.takeAttributesFrom(attrs);
3790
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003791 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003792
Chris Lattner371ed4e2008-04-06 06:57:35 +00003793 // Parse the declarator. This is "PrototypeContext", because we must
3794 // accept either 'declarator' or 'abstract-declarator' here.
3795 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3796 ParseDeclarator(ParmDecl);
3797
3798 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003799 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003800
Chris Lattner371ed4e2008-04-06 06:57:35 +00003801 // Remember this parsed parameter in ParamInfo.
3802 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003803
Douglas Gregor4d87df52008-12-16 21:30:33 +00003804 // DefArgToks is used when the parsing of default arguments needs
3805 // to be delayed.
3806 CachedTokens *DefArgToks = 0;
3807
Chris Lattner371ed4e2008-04-06 06:57:35 +00003808 // If no parameter was specified, verify that *something* was specified,
3809 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003810 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3811 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003812 // Completely missing, emit error.
3813 Diag(DSStart, diag::err_missing_param);
3814 } else {
3815 // Otherwise, we have something. Add it and let semantic analysis try
3816 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003817
Chris Lattner371ed4e2008-04-06 06:57:35 +00003818 // Inform the actions module about the parameter declarator, so it gets
3819 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003820 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003821
3822 // Parse the default argument, if any. We parse the default
3823 // arguments in all dialects; the semantic analysis in
3824 // ActOnParamDefaultArgument will reject the default argument in
3825 // C.
3826 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003827 SourceLocation EqualLoc = Tok.getLocation();
3828
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003829 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003830 if (D.getContext() == Declarator::MemberContext) {
3831 // If we're inside a class definition, cache the tokens
3832 // corresponding to the default argument. We'll actually parse
3833 // them when we see the end of the class definition.
3834 // FIXME: Templates will require something similar.
3835 // FIXME: Can we use a smart pointer for Toks?
3836 DefArgToks = new CachedTokens;
3837
Mike Stump11289f42009-09-09 15:08:12 +00003838 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003839 /*StopAtSemi=*/true,
3840 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003841 delete DefArgToks;
3842 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003843 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003844 } else {
3845 // Mark the end of the default argument so that we know when to
3846 // stop when we parse it later on.
3847 Token DefArgEnd;
3848 DefArgEnd.startToken();
3849 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3850 DefArgEnd.setLocation(Tok.getLocation());
3851 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003852 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003853 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003854 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003855 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003856 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003857 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003858
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003859 // The argument isn't actually potentially evaluated unless it is
3860 // used.
3861 EnterExpressionEvaluationContext Eval(Actions,
3862 Sema::PotentiallyEvaluatedIfUsed);
3863
John McCalldadc5752010-08-24 06:29:42 +00003864 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003865 if (DefArgResult.isInvalid()) {
3866 Actions.ActOnParamDefaultArgumentError(Param);
3867 SkipUntil(tok::comma, tok::r_paren, true, true);
3868 } else {
3869 // Inform the actions module about the default argument
3870 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003871 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003872 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003873 }
3874 }
Mike Stump11289f42009-09-09 15:08:12 +00003875
3876 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3877 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003878 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003879 }
3880
3881 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003882 if (Tok.isNot(tok::comma)) {
3883 if (Tok.is(tok::ellipsis)) {
3884 IsVariadic = true;
3885 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3886
3887 if (!getLang().CPlusPlus) {
3888 // We have ellipsis without a preceding ',', which is ill-formed
3889 // in C. Complain and provide the fix.
3890 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003891 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003892 }
3893 }
3894
3895 break;
3896 }
Mike Stump11289f42009-09-09 15:08:12 +00003897
Chris Lattner371ed4e2008-04-06 06:57:35 +00003898 // Consume the comma.
3899 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003900 }
Mike Stump11289f42009-09-09 15:08:12 +00003901
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003902 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003903 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003904
John McCall084e83d2011-03-24 11:26:52 +00003905 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003906 SourceLocation RefQualifierLoc;
3907 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003908 ExceptionSpecificationType ESpecType = EST_None;
3909 SourceRange ESpecRange;
3910 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3911 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3912 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003913
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003914 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003915 MaybeParseCXX0XAttributes(attrs);
3916
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003917 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003918 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003919 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003920 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003921
Douglas Gregor54992352011-01-26 03:43:54 +00003922 // Parse ref-qualifier[opt]
3923 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3924 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003925 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003926
3927 RefQualifierIsLValueRef = Tok.is(tok::amp);
3928 RefQualifierLoc = ConsumeToken();
3929 EndLoc = RefQualifierLoc;
3930 }
3931
Sebastian Redl965b0e32011-03-05 14:45:16 +00003932 // FIXME: We should leave the prototype scope before parsing the exception
3933 // specification, and then reenter it when parsing the trailing return type.
3934 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3935
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003936 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003937 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3938 DynamicExceptions,
3939 DynamicExceptionRanges,
3940 NoexceptExpr);
3941 if (ESpecType != EST_None)
3942 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003943
3944 // Parse trailing-return-type.
3945 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3946 TrailingReturnType = ParseTrailingReturnType().get();
3947 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003948 }
3949
Douglas Gregor7fb25412010-10-01 18:44:50 +00003950 // Leave prototype scope.
3951 PrototypeScope.Exit();
3952
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003953 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003954 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003955 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003956 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003957 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003958 RefQualifierIsLValueRef,
3959 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003960 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003961 DynamicExceptions.data(),
3962 DynamicExceptionRanges.data(),
3963 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003964 NoexceptExpr.isUsable() ?
3965 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003966 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003967 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003968 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003969}
Chris Lattneracd58a32006-08-06 17:24:14 +00003970
Chris Lattner6c940e62008-04-06 06:34:08 +00003971/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3972/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003973/// first identifier has already been consumed, and the current token is the
3974/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003975///
3976/// identifier-list: [C99 6.7.5]
3977/// identifier
3978/// identifier-list ',' identifier
3979///
3980void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003981 IdentifierInfo *FirstIdent,
3982 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003983 Declarator &D) {
3984 // Build up an array of information about the parsed arguments.
3985 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3986 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003987
Chris Lattner6c940e62008-04-06 06:34:08 +00003988 // If there was no identifier specified for the declarator, either we are in
3989 // an abstract-declarator, or we are in a parameter declarator which was found
3990 // to be abstract. In abstract-declarators, identifier lists are not valid:
3991 // diagnose this.
3992 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003993 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003994
Chris Lattner9453ab82010-05-14 17:23:36 +00003995 // The first identifier was already read, and is known to be the first
3996 // identifier in the list. Remember this identifier in ParamInfo.
3997 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003998 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003999
Chris Lattner6c940e62008-04-06 06:34:08 +00004000 while (Tok.is(tok::comma)) {
4001 // Eat the comma.
4002 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004003
Chris Lattner9186f552008-04-06 06:39:19 +00004004 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00004005 if (Tok.isNot(tok::identifier)) {
4006 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00004007 SkipUntil(tok::r_paren);
4008 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00004009 }
Chris Lattner67b450c2008-04-06 06:47:48 +00004010
Chris Lattner6c940e62008-04-06 06:34:08 +00004011 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00004012
4013 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004014 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00004015 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00004016
Chris Lattner6c940e62008-04-06 06:34:08 +00004017 // Verify that the argument identifier has not already been mentioned.
4018 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004019 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00004020 } else {
4021 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00004022 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00004023 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00004024 0));
Chris Lattner9186f552008-04-06 06:39:19 +00004025 }
Mike Stump11289f42009-09-09 15:08:12 +00004026
Chris Lattner6c940e62008-04-06 06:34:08 +00004027 // Eat the identifier.
4028 ConsumeToken();
4029 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004030
4031 // If we have the closing ')', eat it and we're done.
4032 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4033
Chris Lattner9186f552008-04-06 06:39:19 +00004034 // Remember that we parsed a function type, and remember the attributes. This
4035 // function type is always a K&R style function type, which is not varargs and
4036 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00004037 ParsedAttributes attrs(AttrFactory);
4038 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00004039 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00004040 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00004041 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00004042 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00004043 EST_None, SourceLocation(), 0, 0,
4044 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00004045 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00004046}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004047
Chris Lattnere8074e62006-08-06 18:30:15 +00004048/// [C90] direct-declarator '[' constant-expression[opt] ']'
4049/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4050/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4051/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4052/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4053void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004054 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004055
Chris Lattner84a11622008-12-18 07:27:21 +00004056 // C array syntax has many features, but by-far the most common is [] and [4].
4057 // This code does a fast path to handle some of the most obvious cases.
4058 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004059 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004060 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004061 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004062
Chris Lattner84a11622008-12-18 07:27:21 +00004063 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004064 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004065 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004066 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004067 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004068 return;
4069 } else if (Tok.getKind() == tok::numeric_constant &&
4070 GetLookAheadToken(1).is(tok::r_square)) {
4071 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004072 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004073 ConsumeToken();
4074
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004075 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004076 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004077 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004078
Chris Lattner84a11622008-12-18 07:27:21 +00004079 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004080 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004081 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004082 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004083 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004084 return;
4085 }
Mike Stump11289f42009-09-09 15:08:12 +00004086
Chris Lattnere8074e62006-08-06 18:30:15 +00004087 // If valid, this location is the position where we read the 'static' keyword.
4088 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004089 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004090 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004091
Chris Lattnere8074e62006-08-06 18:30:15 +00004092 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004093 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004094 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004095 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004096
Chris Lattnere8074e62006-08-06 18:30:15 +00004097 // If we haven't already read 'static', check to see if there is one after the
4098 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004099 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004100 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004101
Chris Lattnere8074e62006-08-06 18:30:15 +00004102 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004103 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004104 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004105
Chris Lattner521ff2b2008-04-06 05:26:30 +00004106 // Handle the case where we have '[*]' as the array size. However, a leading
4107 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4108 // the the token after the star is a ']'. Since stars in arrays are
4109 // infrequent, use of lookahead is not costly here.
4110 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004111 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004112
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004113 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004114 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004115 StaticLoc = SourceLocation(); // Drop the static.
4116 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004117 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004118 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004119 // Note, in C89, this production uses the constant-expr production instead
4120 // of assignment-expr. The only difference is that assignment-expr allows
4121 // things like '=' and '*='. Sema rejects these in C89 mode because they
4122 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004123
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004124 // Parse the constant-expression or assignment-expression now (depending
4125 // on dialect).
4126 if (getLang().CPlusPlus)
4127 NumElements = ParseConstantExpression();
4128 else
4129 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004130 }
Mike Stump11289f42009-09-09 15:08:12 +00004131
Chris Lattner62591722006-08-12 18:40:58 +00004132 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004133 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004134 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004135 // If the expression was invalid, skip it.
4136 SkipUntil(tok::r_square);
4137 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004138 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004139
4140 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4141
John McCall084e83d2011-03-24 11:26:52 +00004142 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004143 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004144
Chris Lattner84a11622008-12-18 07:27:21 +00004145 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004146 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004147 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004148 NumElements.release(),
4149 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004150 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004151}
4152
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004153/// [GNU] typeof-specifier:
4154/// typeof ( expressions )
4155/// typeof ( type-name )
4156/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004157///
4158void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004159 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004160 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004161 SourceLocation StartLoc = ConsumeToken();
4162
John McCalle8595032010-01-13 20:03:27 +00004163 const bool hasParens = Tok.is(tok::l_paren);
4164
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004165 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004166 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004167 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004168 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4169 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004170 if (hasParens)
4171 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004172
4173 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004174 // FIXME: Not accurate, the range gets one token more than it should.
4175 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004176 else
4177 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004178
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004179 if (isCastExpr) {
4180 if (!CastTy) {
4181 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004182 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004183 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004184
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004185 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004186 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004187 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4188 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004189 DiagID, CastTy))
4190 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004191 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004192 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004193
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004194 // If we get here, the operand to the typeof was an expresion.
4195 if (Operand.isInvalid()) {
4196 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004197 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004198 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004199
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004200 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004201 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004202 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4203 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004204 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004205 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004206}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004207
4208
4209/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4210/// from TryAltiVecVectorToken.
4211bool Parser::TryAltiVecVectorTokenOutOfLine() {
4212 Token Next = NextToken();
4213 switch (Next.getKind()) {
4214 default: return false;
4215 case tok::kw_short:
4216 case tok::kw_long:
4217 case tok::kw_signed:
4218 case tok::kw_unsigned:
4219 case tok::kw_void:
4220 case tok::kw_char:
4221 case tok::kw_int:
4222 case tok::kw_float:
4223 case tok::kw_double:
4224 case tok::kw_bool:
4225 case tok::kw___pixel:
4226 Tok.setKind(tok::kw___vector);
4227 return true;
4228 case tok::identifier:
4229 if (Next.getIdentifierInfo() == Ident_pixel) {
4230 Tok.setKind(tok::kw___vector);
4231 return true;
4232 }
4233 return false;
4234 }
4235}
4236
4237bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4238 const char *&PrevSpec, unsigned &DiagID,
4239 bool &isInvalid) {
4240 if (Tok.getIdentifierInfo() == Ident_vector) {
4241 Token Next = NextToken();
4242 switch (Next.getKind()) {
4243 case tok::kw_short:
4244 case tok::kw_long:
4245 case tok::kw_signed:
4246 case tok::kw_unsigned:
4247 case tok::kw_void:
4248 case tok::kw_char:
4249 case tok::kw_int:
4250 case tok::kw_float:
4251 case tok::kw_double:
4252 case tok::kw_bool:
4253 case tok::kw___pixel:
4254 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4255 return true;
4256 case tok::identifier:
4257 if (Next.getIdentifierInfo() == Ident_pixel) {
4258 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4259 return true;
4260 }
4261 break;
4262 default:
4263 break;
4264 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004265 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004266 DS.isTypeAltiVecVector()) {
4267 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4268 return true;
4269 }
4270 return false;
4271}